Algo 4
Algo 4
Greedy Approach
LEARNING OBJECTIVES
Procedure A A
Procedure for DFS(V) It is at this point that two traversal strategies differ.
Steps Breadth first adds B’s neighbors to the back of READY,
1. push the start vertex ‘V ’ into the stack S depth first adds them to the front.
2. while (S is not empty)
Breadth first
(i) pop a vertex V
(ii) if ‘V ’ is not visited •• READY = [E, C, G]
(a) visit the vertex •• Process E. READY = [C, G, F]
(b) Store ‘V ’ in visited •• Process C. READY = [G, F, D]
(c) push all the adjacent vertices of ‘V ’ in to visited •• Process G .READY = [F, D, H]
(iii) End if •• Process F. READY = [D, H]
3. End while •• Process D. READY = [H]
4. Stop. •• Process H. READY = [ ]
Example: Depth First
A A
•• READY = [C, G, E]
•• Process C. READY = [D, G, E]
•• Process D. READY = [G, E]
B D E B D E
•• Process G. READY = [H, F, E]
•• Process H. READY = [F, E]
•• Process F. READY = [E]
C C •• Process E. READY = [ ]
A A Connected Components
A graph is said to be connected if every pair of vertices
in the graph are connected. A connected component is a
B D E B D E maximal connected sub graph of ‘G’. Each vertex belongs
to exactly one connected component as does each edge.
C C •• A graph that is not connected is naturally and obvi-
ously decomposed into several connected components
(Figure 4). Depth first search does this handily. Each restart
A A of the algorithm marks a new connected component.
•• The directed graph in (Figure 5) is “Connected” Part of
it can be “Pulled apart” (so to speak, without “breaking”
B D E B D E any edges).
•• Meaningful way to define connectivity in directed graph is:
C C ‘Two nodes U and V of a directed graph G = (V, E) con-
nected if there is a path from U to V ’, and one from V to U. This
relation between nodes is reflective, symmetric and transitive.
A As such, it partitions V into disjoint sets, called the strongly
connected components of the graph. In the directed graph of
figure 2 there are four strongly connected components.
B D E
1 2
C
3 5
4
Figure 3 Depth first search
6 8
•• Let us compare two traversal orders on the following graph: 7
A B C D 12 13
11
9 10
E F G H
14
Initial steps:
READY = [A]. process A. READY = [B, E ]. process B. Figure 4 Undirected graph.
Chapter 4 • Greedy Approach | 3.121
9 10 a : 47 b : 12 c : 11 d : 14 e : 10 f:6
11
Step III:
100
12
0 1
f:6 e : 10 c : 11 b : 12 d : 14 a : 47
Solution: Two methods are used for compression of data are:
3.122 | Unit 3 • Algorithms
Let x and y be 2 characters in C having the lowest frequen- •• A unit – time task is a job, such as a program to be run
cies. Then there exists an optimal prefix code for C in which on a computer, that requires exactly one unit of time to
the code words for x and y have the same length and differ complete.
only in the last bit •• Given a finite set S of unit – time tasks, a schedule for S
16
is a permutation of S specifying the order in which these
tasks are to be performed.
0 1
•• The first task in the schedule begins at time ‘0’ and fin-
ishes at time 1, the second task begins at time 1 and fin-
f:6 e : 10
ishes at time 2, and so on
•• The problem of scheduling unit – time tasks with dead-
53
lines and penalties for a single processor has the follow-
0 1 ing inputs:
1. A set S = {a1, a2, … an} of n unit – time tasks:
23 30 2. A set of n integer deadlines d1, d2, … dn such that each
di satisfies 1 ≤ di ≤ n and task ai is supposed to finish
0 1 0 1
by time di.
c : 11 b : 12 d : 14 16 3. A set of n non-negative weights or penalties w1,
0 1
w2, … wn, such that we incur a penalty of wi if task ai
is not finished by time di and we incur no penalty if a
f:6 e : 10
task finishes by its deadline.
Example: Consider the following 7 tasks, T1, T2, T3, T4, T5
100 T6, T7. Every task is associated with profit and deadline.
0 1 Tasks T1 T2 T3 T4 T5 T6 T7
Deadline 4 2 4 3 1 4 5
a : 47 53 Profit 75 65 55 45 40 35 30
0 1
45 65 55 75 30
T4 T2 T3 T1 T7
0 1 2 3 4 5 6 7
23 30
0 T1 has highest profit, so it will be executed first and the
1 0 1
deadline of T1, is ‘4’ so T1 has to be executed within 4
c : 11 b : 12 d : 14
slots of time, same procedure is applied to other tasks
16
also.
0 1 The tasks which are not executed by CPU are T5 and T6.
j
Graph Algorithms
Single Source Shortest Path Solution:
In a shortest-path problem, we are given a weighted •• Shortest path from S to a is d(S, a) = W(S, a) = 4 (because
directed graph G = (V, E) with weight function W : E →R there is only one path from ‘S’ to ‘a’)
mapping edges to real-valued weights. The weight of path •• Similarly, there is only one path from ‘s’ to ‘b’
P = < VO, V1 … VK > is the sum of the weights of its constitu- d(S, a) = W(S, a) + W(a, b) = 4 + (-5) = -1
ent edges. Shortest-path weight from U to V is defined by •• Shortest-path from ‘s’ to ‘c’
3.124 | Unit 3 • Algorithms
There are infinitely many paths from ‘S’ to ‘c’ The algorithm maintains the invariant that Q = V - S at the
1. <S, c> start of each iteration of the while loop. Initially the min -
2. <S, c, d, c> priority queue Q contains all the vertices in V. ( S = ∅).
\
3. <S, c, d, c, d, c > and so on Each time through the while loop, a vertex ‘u’ is extracted
d <S, c> = 6 from Q = V - S and added to set S.
d (S, d, d, c) = 6 + 7- 2 = 11
•• Each vertex is extracted from Q and added to S exactly
d (S, c, d, c, d, c) = 6 + 7 – 2 + 7 - 2 = 16
once, so the contents of while loop will be executed
d (S, c, d, c, d, c, d, c)
exactly |v| times.
= 6 + 7 - 2 + 7 - 2 + 7 - 2 = 21
•• Dijkstra’s algorithm always chooses the ‘closest’ or
The cycle <c, d, c> has weight = 7 + (-2) = 5 > 0
‘lightest’ vertex in (V - S) to add to set S, we say that it
The shortest path from ‘S’ to ‘c’ is <s, c> with weight
uses a greedy strategy.
d (S, c) = 6 similarly, the shortest-path from ‘S’ to ‘d’ is
<s, c, d>, with weight d (S, d) = w (S, c) + W(c, d) = 13
Example: Consider the following graph, what is the
May there are infinitely paths from ‘S’ to ‘e’ shortest path?
1. <s, e>
a b
2. <s, e f, e> 2
3. <s, e, f, e, f , e> and so on 9 10
Since the cycle <e, f, e> has weight 4 + (-5) = -1 < 0. S 3 4 5 6
However, there is no shortest path from ‘S’ to ‘e’ by traversing
8
the negative-weight cycle <e, f, e> arbitrarily many times, we 6
2 d
can find paths from ‘s’ to ‘e’ with arbitrarily large negative c
weights,
So d(S, e) = -∞ Solution:
Similarly, d(S, f ) = - ∞
S V-S
•• The shortest path from ‘S’ to ‘g’:
‘g’ can be reachable from ‘f ; we can also find paths with arbi- S abcd
trarily large negative weights from ‘s’ to ‘g’ and d(s, g) = -∞ Sc abd
•• Vertices ‘h’, ‘i’ and ‘j’ also form a negative - weight
Scd ab
cycle. They are not reachable from ‘S’ so, d(S, h ) = d(S,
i) = d(S, j) = ∞ Scda b
Scdab ∅
Dijkstra’s Algorithm
Dijkstra’s algotithm solves the single-source shortest-path Distance from S to all vertices of (V - S)
problem on a weighted, directed graph G = (V, E), for the d[a] = 9
case in which all edge weights are non-negative. d[b] = ∞
d [c] = 6
•• The running time of Dijkstra’s algorithm is lower than
d[d] = ∞
that of the Bellman–Ford algorithm.
9, ∞, 6, ∞ values are given to MIN-PRIORITY Queue ‘Q’,
•• Dijkstra’s algorithm maintains a set ‘s’ of vertices whose
‘6’ is returned.
final shortest-path weights from the source ‘S’ have
already been determined.
•• The algorithm repeatedly selects the vertex u ∈ (V - S)
with the minimum shortest-path estimate, adds ‘u’ to ‘S’
S 0
DIJKSTRA (G, W, S)
INITIALIZE - SINGLE - SOURCE (G, S) 6
6
S←∅ c
S ← V[G]
While Q ≠ 0 Distance from [Sc] to all vertices of (V - S)
do u ← EXTRACT - MIN(Q) d[b] = (S - c - b) = 6 + 10 = 16
S ← S U{u}
d[a] = min{(S - a) = 9, (s - c - a) = 10} = 9
For each vertex v ∈ Adj[u]
do RELAX (u, v, w) d[d] = min{∞, (S - c - d) = 6 + 2 = 8 } = 8
Chapter 4 • Greedy Approach | 3.125
Exercises
Practice Problems 1 C 1 2 3 4
1 0 15 20 25
Directions for questions 1 to 14: Select the correct alterna-
2 10 0 14 15
tive from the given choices. 3 11 18 0 17
1. A thief enters a store and sees the following: 4 13 13 14 0
C
A B 1 2
His knapsack can hold 4 pounds, what should he steal (A) 1 - 2 - 4 - 3 - 1 (B) 2 - 3 - 4 - 1 - 2
to maximize profit? (Use 0-1 Knapsack). (C) 1 - 4 - 2 - 3 - 1 (D) 2 - 4 - 3 - 1 - 2
(A) A and B (B) A and C
(C) B and C (D) A, B and C 7. Calculate the maximum profit using greedy strategy,
knapsack capacity is 50. The data is given below:
2. By using fractional Knapsack, calculate the maximum
profit, for the data given in the above question? n=3
(A) 180 (B) 170 (w1, w2, w3) = (10, 20, 30)
(C) 160 (D) 150 (p1, p2, p3) = (60, 100, 120) (dollars)? (0/1 knapsack)
3. Consider the below figure: (A) 180 (B) 220
8 7 (C) 240 (D) 260
b c d
4 7
2 Common data for questions 8 and 9: Given that
4
a 11 i e a b c d e f
14
7 6
10 Frequency 45 13 12 16 9 5
8
g Fixed length code word 000 001 010 011 100 101
h f
1 2
8. Using Huffman code, find the path length of internal
What is the weight of the minimum spanning tree using nodes.
Kruskals algorithm? (A) 8 (B) 100
(A) 34 (B) 35 (C) 100 × 8 (D) 100/8
(C) 36 (D) 38 9. Using above answer, external path length will be
4. Construct a minimum spanning tree for the figure given (A) 18 (B) 108
in the above question, using prim’s algorithm. What are (C) 8 (D) None of these
the first three nodes, added to the solution set respec-
tively (consider ‘a’ as starting node). Common data for questions 10 and 11:
(A) b, c, i (B) h, b, c 10. Item 3
(C) c, i, b (D) h, c, b
Item 2
5. Consider the below graph, calculate the shortest dis- Item 1
50 kg
4
A D
18
1 11 9
5 60/kg 100/kg 20/kg knapsack
2 13
S B E T
Using 0-1 knapsack select a subset of the three items
5 shown, whose weight must not exceed 50 kg. What is
16
2
the value?
C F
2 (A) 2220 (B) 2100
(C) 2600 (D) 2180
(A) 23 (B) 9
(C) 20 (D) 22 11. Which of the following gives maximum profit, using
6. Solve the travelling salesman problem, with the given fractional knapsack?
distances in the form of matrix of graph, which of the (A) x1 = 1, x2 = 1, x3 = 0 (B) x1 = 1, x2 = 1, x3 = 2/3
following gives optimal solution? (C) x1 = 1, x2 = 0, x3 = 1 (D) x1 = 1, x2 = 1, x3 = 1/3
Chapter 4 • Greedy Approach | 3.127
a
f d
8
e
Which one of the following cannot be the sequence of
edges added, in that order, to a minimum spanning tree (D) b c
using Kruskal’s algorithm?
a f
(A) (a - b), (d - f ), (b - f ), (d - c), (d - e) d
(B) (a - b), (d - f ), (d - c), (b - f ), (d - e)
e
(C) (d - f ), (a - b), (d - c), (b - f ), (d - e)
(D) (d - f ), (a - b), (b - f ), (d - e), (d - c) 4. Consider the following graph:
2. The worst case height analysis of B-tree is b
4
c
(A) O(n)
(B) O(n2) 3 2 5 6
(C) O(log n)
(D) O(n log n) a d e
7 4
3. Consider the given graph:
1 Find the shortest path using Dijkstra’s algorithm.
b c (A) a - b - d - e (B) a - b - c - d
3 6
4 4
(C) a - c - d - e (D) a - b - c - e
a 5 5 d
f 5. Which statement is true about Kruskal’s algorithm?
(A) It is a greedy algorithm for the minimum spanning
2
6 8 tree problem.
(B) It constructs spanning tree by selecting edges in
e
increasing order of their weights.
Which of the following is the minimum spanning tree. (C) It does not accept creation of cycles in spanning tree.
(If we apply Kruskal algorithm). (D) All the above
3.128 | Unit 3 • Algorithms
6. Dijkstra’s algorithm bears similarity to which of the (A) I, II, III, IV (B) IV, III, I, II
following for computing minimum spanning trees? (C) IV, II, I, III (D) III, IV, II I
(A) Breadth first search (B) Prim’s algorithm 11. Let V stands for vertex, E stands for edges.
(C) Both (A) and (B) (D) None of these
For both directed and undirected graphs, the adjacency
7. Which of the following algorithm always yields a cor- list representation has the desirable property that the
rect solution for a graph with non-negative weights to amount of memory required is
compute shortest paths? (A) q(V ) (B) q(E)
(A) Prim’s algorithm (B) Kruskal’s algorithm (C) q(V + E ) (D) q(V - E )
(C) Dijkstra’s algorithm (D) Huffman tree
12. Which of the following is false?
8. Let the load factor of the hash table is number of keys (A) Adjacency-matrix representation of a graph per-
is n, cells of the hash table is m then mits faster edge look up.
(B) The adjacency matrix of a graph requires q(v2)
(A) ∝ = n/m (B) ∝ = m/n memory, independent of the number of edges in
m +1 n +1
(C) ∝ (D) ∝ the graph.
n m (C) Adjacency-matrix representation can be used for
9. To implement Dijkstra’s shortest path algorithm on weighted graphs.
unweighted graphs so that it runs in linear time, the (D) All the above
data structure to be used is: 13. Dynamic programming is a technique for solving prob-
(A) Queue lems with
(B) Stack (A) Overlapped sub problems
(C) Heap (B) Huge size sub problems
(D) B-tree (C) Small size sub problems
10. The development of a dynamic-programming algo- (D) None of these
rithm can be broken into a sequence of four steps, 14. The way a card game player arranges his cards, as he
which are given below randomly. picks them up one by one is an example of _____.
I. Construct an optimal solution from computed in- (A) Bubble sort (B) Selection sort
formation. (C) Insertion sort (D) None of the above
II. Compute the value of an optimal solution in a bot- 15. You want to check whether a given set of items is
tom-up fashion. sorted. Which method will be the most efficient if it is
III. Characterize the structure of an optimal solution. already in sorted order?
IV. Recursively defines the value of an optimal solution. (A) Heap sort (B) Bubble sort
The correct sequence of the above steps is (C) Merge sort (D) Insertion sort
(A) (a - b), (d - f), (b - f), (d - c), (d - e) int GetValue (struct CellNode *ptr) {
(B) (a - b), (d - f), (d - c), (b - f), (d - e) int value = 0;
(C) (d - f), (a - b), (d - c), (b - f), (d - e) if (ptr != NULL) {
(D) (d - f), (a - b), (b - f), (d - e), (d - c) if ((ptr->leftChild == NULL) &&
Common data for questions 5 and 6: A 3-ary max-heap (ptr->rightChild == NULL))
is like a binary max-heap, but instead of 2 children, value = 1;
nodes have 3 children. A 3-ary heap can be repre- else
sented by an array as follows: The root is stored in the value = value + GetValue(ptr->leftChild)
first location, a[0], nodes in the next level, from left + GetValue(ptr->rightChild);
to right, is stored from a[1] to a[3]. The nodes from }
the second level of the tree from left to right are stored return(value);
from a[4] location onward. An item x can be inserted
into a 3-ary heap containing n items by placing x in he value returned by GetValue when a pointer to the
T
the location a[n] and pushing it up the tree to satisfy root of a binary tree is passed as its argument is:
the heap property. (A) The number of nodes in the tree
(B) The number of internal nodes in the tree
5. Which one of the following is a valid sequence of
(C) The number of leaf nodes in the tree
elements in an array representing 3-ary max-heap?
(D) The height of the tree
[2006]
(A) 1, 3, 5, 6, 8, 9 (B) 9, 6, 3, 1, 8, 5 10. Let w be the minimum weight among all edge weights
(C) 9, 3, 6, 8, 5, 1 (D) 9, 5, 6, 8, 3, 1 in an undirected connected graph. Let e be a specific
6. Suppose the elements 7, 2, 10 and 4 are inserted, in edge of weight w. Which of the following is FALSE?
that order, into the valid 3-ary max-heap found in the [2007]
above question, Q-76. Which one of the following is (A) There is a minimum spanning tree containing e.
the sequence of items in the array representing the (B) If e is not in a minimum spanning tree T, then in
resultant heap? [2006] the cycle formed by adding e to T, all edges have
(A) 10, 7, 9, 8, 3, 1, 5, 2, 6, 4 the same weight.
(B) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 (C) Every minimum spanning tree has an edge of
(C) 10, 9, 4, 5, 7, 6, 8, 2, 1, 3 weight w.
(D) 10, 8, 6, 9, 7, 2, 3, 4, 1, 5 (D) e is present in every minimum spanning tree.
7. In an unweighted, undirected connected graph, the 11. The Breadth first search algorithm has been imple-
shortest path from a node S to every other node is mented using the queue data structure. One possible
computed most efficiently, in terms of time complex- order of visiting the nodes of the following graph is
ity, by [2007] [2008]
(A) Dijkstra’s algorithm starting from S. M N O
(B) Warshall’s algorithm
(C) Performing a DFS starting from S.
(D) Performing a BFS starting from S.
8. A complete n-ary tree is a tree in which each node has R
Q P
n children or no children. Let I be the number of inter-
nal nodes and L be the number of leaves in a complete (A) MNOPQR (B) NQMPOR
n-ary tree. If L = 41, and I = 10, what is the value of (C) QMNPRO (D) QMNPOR
n? [2007] 12. G is a graph on n vertices and 2n - 2 edges. The edges
(A) 3 (B) 4 of G can be partitioned into two edge-disjoint span-
(C) 5 (D) 6 ning trees. Which of the following is NOT true for G?
9. Consider the following C program segment where [2008]
CellNode represents a node in a binary tree: [2007] (A) For every subset of k vertices, the induced sub-
struct CellNode { graph has atmost 2k - 2 edges
struct CellNode *leftChild; (B) The minimum cut in G has atleast two edges
int element; (C) There are two edge-disjoint paths between every
struct CellNode *rightChild; pair of vertices
};
(D) There are two vertex-disjoint paths between eve-
ry pair of vertices
3.130 | Unit 3 • Algorithms
22. How many different insertion sequences of the key 24. What will be the cost of the minimum spanning tree
values using the same hash function and linear prob- (MST) of such a graph with n nodes? [2011]
ing will result in the hash table shown above?[2010] 1
(A) 10 (B) 20 (A) (11n 2 − 5n) (B) n2 - n + 1
12
(C) 30 (D) 40
(C) 6n - 11 (D) 2n + 1
23. A max-heap is a heap where the value of each parent
is greater than or equal to the value of its children. 25. The length of the path from V5 to V6 in the MST of
Which of the following is a max-heap? [2011] previous question with n = 10 is [2011]
(A) 11 (B) 25
(A) (C) 31 (D) 41
10
P 0.22 1 3
Q 0.34 4 5
R 0.17
4
S 0.19
Answer Keys
Exercises
Practice Problems 1
1. A 2. A 3. B 4. A 5. B 6. A 7. B 8. A 9. A 10. C
11. B 12. A 13. A 14. B
Practice Problems 2
1. D 2. C 3. A 4. A 5. D 6. C 7. C 8. A 9. A 10. D
11. C 12. C 13. A 14. C 15. D