0% found this document useful (0 votes)
6 views

Algo 4

Uploaded by

csindirareddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Algo 4

Uploaded by

csindirareddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter 4

Greedy Approach

LEARNING OBJECTIVES

 Greedy approach  Graph traversal


 Knapsack problem  Breadth first traversal
 Fractional knapsack problem  Depth first search
 Spanning trees  Huffman codes
 Prim's algorithm  Task-scheduling problem
 Kruskal's algorithm  Sorting and order statistics
 Tree and graph traversals  Simultaneous minimum and maximum
 Back tracking  Graph algorithms

greedY approaCh Solved Examples


In a greedy method, we attempt to construct an optimal solution Example 1: (Making change)
in stages.
Problem: Accept n dollars, to return a collection of coins
• At each stage we make a decision that appears to be the best with a total value of n dollars.
(under some criterion) at the time. Configuration: A collection of coins with a total value of n.
Objective function: Minimize number of coins returned.
• A decision made at one stage is not changed in a later stage, so
Greedy solution: Always return the largest coin you can.
each decision should assure feasibility.
• Some problems that use greedy approach are: • Coins are valued $.30, $.020, $0.05, $0.01 use a greedy choice
property and make $.40 by using 3 coins.
1. Knapsack problem
Solution: $0.30 + $0.05 + $0.05 = $0.40
2. Minimum spanning tree
3. Prims algorithm Fractional Knapsack Problem
4. Kruskals algorithm Given: A set S of n items, with each item i having
• bi - a positive benefit
• wi - a positive weight
KnapSaCK problem Goal: Choose items with maximum total benefit but with weight
The knapsack problem is a problem in combinatorial optimization: atmost W.
given a set of items, each with a weight and a value, determine If we are allowed to take fractional amounts, then this is the frac-
the number of each item to include in a collection so that the total tional knapsack problem.
weight is less than or equal to a given limit and the total value is
• In this case, let xi denote the amount we take of item i.
as large as possible. We have n kinds of items, 1 through n. Each
kind of item i has a value Vi and a weight Wi, usually assume that • Objective: Maximize ∑ bi ( xi /wi )
i∈S
all values and weights are non-negative. The maximum weight that
we can carry in the bag is W. • Constraint: ∑x
i∈S
i
≤W
Chapter 4 • Greedy Approach | 3.117

Example 2: Prim’s Algorithm


Items Prim’s algorithm is a greedy algorithm that finds a minimum
1 2 3 4 5 spanning tree for a connected weighted undirected graph.
This means it finds a subset of the edges that forms a tree
Weight 4 ml 8 ml 2 ml 6 ml 1 ml that includes every vertex, where the total weight of all the
Benefit $12 $32 $40 $30 $50
edges in the tree is minimized. The algorithm continuously
increases the size, of a tree, one edge at a time starting with
Value ($ per ml) 3 4 20 5 50
a tree consisting of a single vertex, until it spans all vertices.
“Knapsack” •• Using a simple binary heap data structure and an adja-
cency list representation, prim’s algorithm can be shown
to run in time O(E log V) where E is the number of edges
and V is the number of vertices.
10 ml
Example:
1
Solution: 1 ml of 5, 2 ml of 3, 6 ml of 4, 1 ml of 2
6 5
•• Greedy choice: Keep taking item with highest value (ben-
efit to weight ratio). 1
•• Correctness: suppose there is a better solution, there is 2 5 4
an item i with higher value than a chosen item j. (i.e., vj 3 3 5
< vi). If we replace some j with i, we get a better solution. 2
6
4
Thus there is no better solution than the greedy one. 5 6
N = 3, m = 20 6
(P1, P2, P3) = (25, 24, 15)
(W1, W2, W3) = (18, 15, 10) Start:
Example 3: 1
1
4
1 4 2 1
X1 X2 X3 2
S WiXi S Pi Xi

1. 1/2 1/3 1/4 9 + 5 + 2.5 = 16.5 12.5 + 8 + 3.75 = 24.25 3


3
2. 1 2/15 0 18 + 2 + 0 = 20 25 + 3.2 + 0 = 28.2 4
5 6
5 6
3. 0 2/3 1 0 + 10 + 10 = 20 0 + 16 + 15 = 31
Iteration 1: U = {1, 3} Iteration 2: U = {1, 3, 6}
4. 0 1 1/2 0 + 15 + 5 = 20 0 + 24 + 7.5 = 31.5
1 1
4 4
(1), (2), (3), (4) are feasible ones but (4) is the optimum 2 1 2 1
solution. 2 2
5
3 3
4 4
5
Spanning Trees 6 5 6

A spanning tree of a graph is just a sub graph that contains


   Iteration 3: U = {1, 3, 6, 4}   Iteration 4: U = {1, 3, 6, 4, 2}
all the vertices and is a tree. A graph may have many span-
ning trees. 1
•• A sub graph that spans (reaches out to) all vertices of a 1 4
2
graph is called a spanning sub graph. 5
2
•• A sub graph that is a tree and that spans all vertices of the
3
original graph is called a spanning tree. 3
4
•• Among all the spanning trees of a weighted and con-
nected graph, the one (possibly more) with the least 5 6
total weight is called a Minimum Spanning Tree
(MST). Iteration 5: U = {1, 3, 6, 4, 2, 5}

Figure 1 An example graph for illustrating prim’s algorithm.


3.118 | Unit 3 • Algorithms

Kruskal’s Algorithm Backtracking is also known as depth first search (or)


branch and bound. Backtracking is an important tool for
Like prim’s algorithm, Kruskal’s algorithm also constructs
solving constraint satisfaction problems, such as cross-
the minimum spanning tree of a graph by adding edges to the
words, verbal arithmetic, sudoku and many other puzzles.
spanning tree one-by-one. At all points, during its execution
It is often the more convenient technique for parsing, for
the set of edges selected by prim’s algorithm forms exactly one
the knapsack problem and other combinational optimiza-
tree. On the other hand the set of edges selected by Kruskal’s
tion problems.
algorithm forms a forest of trees. Kruskals algorithm is con-
ceptually simple. The edges are selected and added to the •• The advantage of backtracking algorithm is that they are
spanning tree in increasing order of their weights. An edge is complete, that is they are guaranteed to find every solu-
added to the tree only if it does not create a cycle. tion to every possible puzzle.
Example:
Graph Traversal
1
6 5
To traverse a graph is to process every node in the graph
exactly once, because there are many paths leading from
5 1
2 4
one node to another, the hardest part about traversing a
5
3 graph is making sure that you do not process some node
3 2
6 4 twice. There are general solutions to this difficulty.
5 6
6 1. When you first encounter a node, mark it as
REACHED. When you visit a node, check if it is
Start: marked REACHED, if it is, just ignore it. This is the
1 1 method our algorithms will use.
4 4
2 2 1 2. When you process a node, delete it from the graph.
1 Deleting the node causes the deletion of all the arcs
3 3 that lead to the node, so it will be impossible to reach
it more than once.
5 6 5 6

Initial configuration    Setp 1: choose (1, 3)


General traversal strategy
1. Mark all nodes in the graph as NOT REACHED,
1 1
4 4 2. Pick a starting node, mark it as REACHED, and place
2
1
2
1
it on the READY list.
3. Pick a node on the READY list. Process it remove it
2 2
3 3 3 from READY. Find all its neighbors, those that are
NOT REACHED should marked as REACHED and
5 6 5 6 added to READY.
4. Repeat 3 until READY is empty.
Setep 2: choose (4, 6)     Setep 3: choose (2, 5)
Example:
1 1
4 4 A B
2 1 2 1
5
2 2
3 3 3 3
4 4
C D
5 6 5 6

Step I: A = B = C = D = NOT REACHED


Setep 4: choose (3, 6)      Setep 6: choose (4, 3)
Step II: READY = {A} . A = REACHED
Step III: Process A. READY = {B, C}.
Tree and Graph Traversals B = C = REACHED
Back Tracking Step IV: Process C. READY = {B, D}.
Backtracking is a general algorithm technique that consid- D = REACHED
ers searching every possible combination in order to solve Step V: Process B. READY = {D}
an optimization problem. Step VI: Process D. READY = { }
Chapter 4 • Greedy Approach | 3.119

The two most common traversal patterns are Unexplored edge


Discovery edge
•• Breadth first traversal
Cross edge
•• Depth first traversal
A A
Breadth First Traversal
B C D B C D
In breadth first traversal, READY is a QUEUE, not an arbi-
trary list. Nodes are processed in the order they are reached
(FIFO), this has the effect of processing nodes according to E F E F
their distance from the initial node. First, the initial node is
processed. Then all its neighbors are processed. Then all of
A A
the neighbors etc.
•• Since a graph has no root, we must specify the vertex at B C D B C D
which to start the traversal.
•• Breadth first tree traversal first visits all the nodes at depth
E F E F
zero (i.e., the root) then all the nodes at depth 1, and so on.

Procedure A A

First, the starting vertex is enqueued. Then, the following B C D B C D


steps are repeated until the queue is empty.
1. Remove the vertex at the head of the queue and call it E F E F
vertex.
2. Visit vertex A A
3. Follow each edge emanating from vertex to find the
adjacent vertex and call it ‘to’. If ‘to’ has not already B C D B C D
been put into the queue, enqueued it.
E F E F
Notice, that a vertex can be put into the queue at most once.
Therefore, the algorithm must some how keep track of the
vertices that have been enqueued. A

Procedure for BFS for undirected graph G(V, E) B C D


To perform BFS over a graph, the data structures required are
queue (Q) and the visited set (Visited), ‘V ’ is the starting vertex.
E F
Procedure for BFS(V)
Figure 2 Breadth–first search
Steps
1. Visit the vertex ‘V’ Depth First Search
2. Enqueue the vertex V A depth first traversal of a tree always starts at the root of
3. while (Q is not Empty) the tree. Since a graph has no root, when we do a depth first
(i) V = dequeue (); traversal, we must specify the vertex at which to begin. A
(ii) for all vertices J adjacent to V depth first traversal of a tree visits a node and then recur-
(a) if not visited (J) sively visits the sub trees of that node similarly, depth first
•• Enqueue (J) traversal of a graph visits a vertex and then recursively vis-
•• Visit the vertex ‘J’ its all the vertices adjacent to that node. A graph may con-
•• end if. tain cycles, but the traversal must visit every vertex at most
•• end for once.
•• end while The solution to the problem is to keep track of the nodes
•• Stop that have been visited.
Example:
Unexplored vertex
Procedure for DFS for undirected graph G(V, E)
A
To perform DFS over a graph, the data structures
A required are stack (S) and the list (visited), ‘V ’ is the
Visited vertex
start vertex.
3.120 | Unit 3 • Algorithms

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

1 2 3 Fixed Length Coding


•• Arrange all the characters in sequence (no particular
order is followed)
•• a = 47, b = 12, c = 11, d = 14, e = 10, f = 6
Step I:
6
a : 47 b : 12 c : 11 d : 14 e : 10 f:6
4
5
Step II:
7 59 25 16
8

9 10 a : 47 b : 12 c : 11 d : 14 e : 10 f:6
11

Step III:
100
12
0 1

Figure 5 A directed graph and its strongly connected components


84 16
If we shrink each of these strongly connected compo-
nents down to a single node and draw an edge between two 0
1 0
of them if there is an edge from some node in the first to
some node in the second, the resulting directed graph has to 59 25 16
be a directed acyclic graph (DAG) – it has no cycles (figure 0 1 0 1 0 1
6). The reason is simple.
A cycle containing several strongly connected compo- a : 47 b : 12 c : 11 d : 14 e : 10 f:6
nents would merge them all to a single strongly connected
component. We interpret the binary code word for a character as
the path from the root to that character where ‘0’ means
1 2-4-5 3-6
‘go to the left child’, and 1 means ‘go to the right child’.
The above tree is not binary search tree, since the leaves
need not appear in sorted order.

Constructing Huffman Code


7-8-9-10-11-12
This algorithm builds the tree T corresponding to the opti-
mal code in a bottom-up manner. It begins with set of |C|
Every directed graph is a DAG of its strongly connected leaves and performs a sequence of |C|-1 ‘merging’ opera-
components. tions to create the final tree.
•• A min-priority queue Q, keyed on f, is used to identify the
Huffman Codes 2 least – frequent objects to merge together. The result of
For compressing data, a very effective and widely used the merger of 2 objects is a new object whose frequency
technique is Huffman coding. We consider the data to be a is the sum of the frequencies of the 2 objects that were
sequence of characters. Huffmans’s greedy algorithm uses merged.
a table of the frequencies of occurrence of the characters to •• In the given example, there are 6 alphabets the initial
build up an optimal way of representing each character as a queue size is n = 6, and 5 merge steps are required to
binary string. build the tree. The final tree represents the optimal prefix
Example: Suppose we have a 1,00,000 – character data file, code. The code word for a letter is the sequence of edge
that we wish to store compactly. The characters in the file labels on the path from the root to the letter.
occur with the frequencies given below: a = 47, b = 12, c = 11, d = 14, e = 10, f = 6
Character a b c d e f
Step I: Arrange the characters in non-decreasing order
Frequency 47 12 11 14 10 6 according to their frequencies

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.

f:6 e : 10 Profit: sum up the profits made by executing the tasks.


Profit = 45 + 65 + 55 + 75 + 30 = 270
Analysis: The analysis of the running time of Huffman’s
algorithm assumes that Q is implemented as a binary min- Analysis: We can use a greedy algorithm to find a maxi-
heap for a set C of ‘n’ characters, the initialization of Q can mum weight independent set of tasks. We can then create
be performed in O(n) time using the BUILD – MIN HEAP an optimal schedule having the tasks in A as its early tasks.
procedure. This method is an efficient algorithm for scheduling unit –
Each heap operation requires O(log n) time, and this time tasks with deadlines and penalties for a single processor.
will be performed exactly (n - 1) times, it contributes to The running time is O(n2) using GREEDY METHOD, since
O(n logn ) running time. Thus the total running time of each of the O(n) independent checks made by that algorithm
HUFFMAN on a set of ‘n’ characters is O(n log n). takes time O(n).

Task-scheduling problem Sorting and Order Statistics


This is the problem of optimally scheduling unit – time
Minimum and Maximum
tasks on a single processor, where each task has a deadline,
along with a penalty that must be paid if the deadline is This algorithm determines, how many comparisons are nec-
missed. The problem looks complicated, but it can be solved essary to find minimum or maximum of a set of ‘n’ elements.
in simple manner using a greedy algorithm. Usually we can obtain maximum or minimum, by performing
Chapter 4 • Greedy Approach | 3.123

(n - 1) comparisons; examine each element of the set in turn


and keep track of the smallest element seen so far.
δ (U ,V ) = {
{min{W (P ):u → v}if there is a path from
∞ ‘U ’ to ‘V ’ otherwise
Consider the following procedure.
Edge weights can be interpreted as metrics other than dis-
Assume that the set of elements reside in an array A
tances. They are often used to represent time, cost, penal-
where length [A] = n
ties, loss, or any other quantity.
MINIMUM (A)
•• The breadth first search algorithm is a shortest-path algo-
Min ← A[1]
rithm that works on un weighted graphs, that is, graphs in
For i ←2 to length [A]
which each edge can be considered to have unit weight.
Do if min > A [i]
Then min ← A [i]
Return min. Negative-weight edges
Some of the instances of the single-source-shortest-paths
Simultaneous minimum and maximum problem, there may be edges whose weights are negative.
In some applications, we must find both the minimum and •• If the graph G = (V, E) contains no negative weight cycles
the maximum of a set of ‘n’ elements. reachable from source S, then for all v ∈ V, the shortest –
We can find the minimum and maximum independently path weight d(S, V ) remains well defined, even if it has a
using (n - 1) comparisons for each, for a total of (2n – 2) negative value.
comparisons. •• If there is a negative-weight cycle reachable from S,
shortest-path weights are not well defined.
•• In fact, atmost 3  n/ 2  comparisons are sufficient to find
•• No path from ‘S’ to a vertex on the cycle can be a shortest
both the minimum and the maximum.
path - a lesser weight path can always be found that fol-
•• The strategy is to maintain the minimum and maximum
lows the proposed ‘shortest’ path and then traverses the
elements seen so far.
negative-weight cycle.
•• Rather than processing each element of the input by compar-
•• If there is a negative-weight cycle on some path form ‘S’
ing it against the current minimum and maximum at a cost
to ‘V’, we define d(S,V) = -∞.
of 2 comparisons per element, we process elements in pairs.
•• Compare pairs of elements from the input with each Example: Consider the following graph, calculate the
other, and then we compare smaller to the current mini- shortest distance to all vertices from sources ‘S’.
mum and the larger to the current maximum, at a cost of a b
−5
3 comparisons for every 2 elements.
•• Setting up initial values for the current minimum and
4 5
maximum depends on whether ‘n’ is odd or even. If ‘n’ is
7
odd, we set both the minimum and maximum to the value c
d
of the first element, and then we process the rest of the 6 9
S 0 g
elements in pairs.
−2
•• If ‘n’ is even, we perform 1 comparison on the first 2 ele-
ments to determine the initial values of the minimum and 3 4
e 8
maximum and then process the rest of the elements in pairs. f

Analysis: If ‘n’ is odd the total number of comparisons


would be 3  n/ 2  . −5
If ‘n’ is even, we need 1 initial comparison followed by h i
3
3( n − 2) 3n
comparisons, for a total of − 2.
2 2
−7 4
\ The total number of comparisons is atmost 3  n/ 2 

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

a b necessary to ensure that shortest-paths consist of a finite


number of edges.
•• When there are no cycles of negative length, there is a
S 0 shortest-path between any two vertices of an n-vertex
graph that has atmost (n - 1) edges on it.
6
•• A path that has more than (n - 1) edges must repeat atleast
2
c d one vertex and hence must contain a cycle
•• Let distx[u] be the length of a shortest-path from the
Distance from [s c d] to [ab] source vertex ‘v’ to vertex ‘u’ under the constraint that
d[a] = min{9, (S - c - d - S - a) = 25} = 9 the shortest-path contains atmost ‘x’ edges. Then dist′[u]
d[b] = min{16, (S - c - d - b) =14} = 14 = cost [v, u] 1 ≤ u ≤ n when there are no cycles of negative
a b
length we can limit our search for shortest-paths to paths
with at most (n - 1) edges. Hence, distn-1[u] is the length
9 of an unrestricted shortest-path from ‘v’ to ‘u’.
S 0 The Recurrence Relation for dist is:
6 Distk[u] = min{distk-1[u], min{distk-1[i] + cost[i, u]}}
2 This recurrence can be used to compute distk from dist ,
k-1
c d
for k = 2, 3, … , n - 1.
d[a] = min{14, (s - a - b) = 9 + 2 = 11} = 11 Example: Consider the given directed graph
−1
a b 2 5
2
9 5 1
−1 1
S 0 4
1 3 7
6
2 −1
4 2
c
4 6
−1
Analysis: It maintains the min-priority queue ‘Q’ by calling
three priority-queue operations: INSERT, EXTRACT-MIN, Find the shortest path from vertex ‘1’ to all other vertices
and DECREASE-KEY. We maintain the min-priority queue using Bellman–Ford algorithm?
by taking the vertices being numbered 1 to |v|. We store d[v]
Solution: Source vertex is ‘1’ the distance from ‘1’ to ‘1’ in
in the vth entry of an array. Each INSERT and DECREASE-
all ‘6’ iterations will be zero. Since the graph has ‘7’ verti-
KEY operation takes O(1) time, and each EXTRACT- MIN
ces, the shortest-path can have atmost ‘6’ edges. The follow-
operation takes O(v) time (\ we have to search through the
ing figure illustrates the implementation of Bellman–Ford
entire array) for a total time of O(v2 + E) = O(v2).
algorithm:
•• If we implement the min - priority queue with a binary
1 2 3 4 5 6 7
min-heap. Each EXTRACT-MIN operation takes time
O(log V ), there are |V | such operations. 1 0 5 4 4 ∞ ∞ ∞
•• The time to build the binary min-heap is O(v). Each 2 0 3 3 4 4 3 ∞
DECREASE-KEY operation takes time O(log V), and 3 0 2 3 4 2 3 5
there are still atmost |E | such operations. The total run- 4 0 2 3 4 1 3 3
ning time is O((V + E ) log V ), which is O(E log V ) if all
5 0 2 3 4 1 3 2
vertices are reachable form the source.
•• We can achieve a running time of O(V log V + E) by imple- 6 0 2 3 4 1 3 2
menting the min-priority queue with a Fibonacci heap. 7 0 2 3 4 1 3 2

Bellman–Ford Algorithm Analysis


•• Each iteration takes O(n2) time if adjacency matrices are
Bellman–Ford algorithm solves the single-source shortest-path
used and O(e) time if adjacency lists are used. Here ‘e’ is
problems in the case in which edge weights may be negative.
the number of edges in the graph.
•• When negative edge lengths are permitted, we require •• The time complexity is O(n3) when adjacency matrices
that the graph have no cycles of negative length. This is are used and O(N * E) when adjacency lists are used.
3.126 | Unit 3 • Algorithms

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

$100 $80 $120

2 pds 2 pds 3 pds


4 3

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

tance from ‘S’ to ‘T ’?


30 kg
20 kg
10 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

12. Using dynamic programming find the longest common C2 = 10 paisa


subsequence (LCS) in the given 2 sub sequences: C3 = 5 paisa
x [1, … , m] C4 = 1 paisa
y [1, … , n] To represents 48 paisa, what is the minimum number of
x:ABCBDAB coins used, using greedy approach?
Y:BDCABA (A) 6 (B) 7
Find longest sequence sets common to both. (C) 8 (D) 9
(A) (BDAB, BCAB, BCBA) 14. Worst-case analysis of hashing occurs when
(B) (BADB, BCAB, BCBA) (A) All the keys are distributed
(C) (BDAB, BACB, BCBA) (B) Every key hash to the same slot
(D) (BDAB, BCAB, BBCA) (C) Key values with even number, hashes to slots with
even number
13. Let C1, C2, C3, C4 represent coins.
(D) Key values with odd number hashes to slots with
C1 = 25 paisa odd number.

Practice Problems 2 (A) b c

Directions for questions 1 to 15: Select the correct alterna-


tive from the given choices. a f d
1. Consider the given graph:
3 e
(B) b c
b d f
2 5 d
a
2
a 4 3 4 f e
6
7
c
5
e (C) b c

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

Previous Years’ Questions


Data for question 1: We are given 9 tasks T1, T2 … T9. 3. To implement Dijkstra’s shortest path algorithm on
The execution of each task requires one unit of time. We unweighted graphs so that it runs in linear time, the
can execute one task at a time. Each task Ti has a profit Pi data structure to be used is:  [2006]
and a deadline Di. Profit Pi is earned if the task is com- (A) Queue
pleted before the end of the Dith unit of time. (B) Stack
(C) Heap
Task T1 T2 T3 T4 T5 T6 T7 T8 T9
(D) B-Tree
Profit 15 20 30 18 18 10 23 16 25
Deadline 7 2 5 3 4 5 2 7 3 4. Consider the following graph:[2006]
2
1. What is the maximum profit earned?[2005]
(A) 147 (B) 165
b d
(C) 167 (D) 175 1 4
2 1
2. Consider a weighted complete graph G on the ver- a 3 3 f
5
tex set {v1, v2,…, vn} such that the weight of the edge 6 4
c e
( vi , v j ) is 2 i − j . The weight of the minimum span-
7
ning tree is:[2006]
(A) n - 1 (B) 2n - 2 Which one of the following cannot be the sequence
 n of edges added, in that order, to a minimum spanning
(C)   (D) n2 tree using Kruskal’s algorithm?
 2
Chapter 4 • Greedy Approach | 3.129

(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

13. (A) {25, 12, 16, 13, 10, 8, 14}


−3 (B) {25, 14, 13, 16, 10, 8, 12}
b e
(C) {25, 14, 16, 13, 10, 8, 12}
2 2
1 1 1 (D) {25, 14, 12, 13, 10, 8, 16}
−5
a c h f 18. What is the content of the array after two delete oper-
2 3 2 3
ations on the correct answer to the previous question?
2
d g [2009]
(A) {14, 13, 12, 10, 8} (B) {14, 12, 13, 8, 10}
Dijkstra’s single source shortest path algorithm when
(C) {14, 13, 8, 12, 10} (D) {14, 13, 12, 8, 10}
run from vertex a in the above graph, computes the
correct shortest path distance to [2008] Common data for questions 19 and 20: Consider a com-
(A) Only vertex a plete undirected graph with vertex set {0, 1, 2, 3, 4}. Entry
(B) Only vertices a, e, f, g, h Wij in the matrix W below is the weight of the edge {i, j}.
(C) Only vertices a, b, c, d  0 1 8 1 4
(D) all the vertices  
 1 0 12 4 9 
14. You are given the post-order traversal, P, of a binary W =  8 12 0 7 3 
search tree on the n elements 1, 2,…, n. You have to  
determine the unique binary search tree that has P as 1 4 7 0 2
4 9 3 2 0
its post-order traversal. What is the time complexity  
of the most efficient algorithm for doing this?[2008]
(A) Q(log n) 19. What is the minimum possible weight of a spanning
(B) Q(n) tree T in this graph such that vertex 0 is a leaf node in
(C) Q(n log n) the tree T ?[2010]
(D) None of the above, as the tree cannot be uniquely (A) 7 (B) 8
determined (C) 9 (D) 10
15. Which of the following statement(s) is/are correct 20. What is the minimum possible weight of a path P
regarding Bellman–Ford shortest path algorithm? from vertex 1 to vertex 2 in this graph such that P
[2009] contains at most 3 edges? [2010]
P. Always finds a negative weighted cycle, if one (A) 7 (B) 8
exists. (C) 9 (D) 10
Q. Finds whether any negative weighted cycle is
reachable from the source. Common data for questions 21 and 22: A hash table of
(A) P only (B) Q only length 10 uses open addressing with hash function h(k) =
(C) Both P and Q (D) Neither P nor Q k mod 10, and linear probing. After inserting 6 values into
an empty hash table, the table is as shown below:
16. Consider the following graph:[2009]
0
b e
5 2 5 1
6 6 2 42
a 4 d 3 g 3 23
6
3 4 4 34
5
c f 5 52
6 6 46
Which one of the following is NOT the sequence 7 33
of edges added to the minimum spanning tree using 8
Kruskal’s algorithm?[2009] 9
(A) (b, e) (e, f) (a, c) (b, c) (f, g) (c, d)
21. Which one of the following choices gives a possi-
(B) (b, e) (e, f) (a, c) (f, g) (b, c) (c, d)
ble order in which the key values could have been
(C) (b, e) (a, c) (e, f) (b, c) (f, g) (c, d)
inserted in the table?
(D) (b, e) (e, f) (b, c) (a, c) (f, g) (c, d)
[2010]
Common data for questions 17 and 18: Consider a
(A) 46, 42, 34, 52, 23, 33
binary max-heap implemented using an array.
(B) 34, 42, 23, 52, 33, 46
17. Which one of the following array represents a binary (C) 46, 34, 42, 23, 52, 33
max-heap?[2009] (D) 42, 46, 33, 23, 34, 52
Chapter 4 • Greedy Approach | 3.131

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

26. Consider the directed graph shown in the figure


8 6 below. There are multiple shortest paths between ver-
tices S and T. Which one will be reported by Dijkstra’s
shortest path algorithm? Assume that, in any iteration,
4 5 2 the shortest path to a vertex v is updated only when a
strictly shorter path to v is discovered.  [2012]
1 2
(B) C 1 E 2
10 G
1
A 1
3 4
8 6 4
7 3
S
D 3
4 5 1 2 3 4
5
T
(C) 10
3 5
B
F

5 6 (A) SDT (B) SBDT


(C) SACDT (D) SACET
4 8 2 1 27. Let G be a weighted graph with edge weights greater
than one and G1 be the graph constructed by squaring
(D) the weights of edges in G. Let T and T1 be the mini-
5
mum spanning trees of G and G1, respectively, with
total weights t and t1. Which of the following state-
2 8 ments is TRUE? [2012]
(A) T 1 = T with total weight t1 = t2
(B) T 1 = T with total weight t1 < t2
1 4 6 1 (C) T 1 ≠ T but total weight t1 = t2
(D) None of the above
Common data for questions 24 and 25: An undirected
graph G (V, E) contains n(n > 2) nodes named V1, V2, …, 28. What is the time complexity of Bellman–Ford single-
Vn. Two nodes Vi, Vj are connected if and only if 0 < |i - j| source shortest path algorithm on a complete graph of
≤ 2. Each edge (Vi, Vj) is assigned a weight i + j. A sample n vertices?[2013]
graph with n = 4 is shown below. (A) Θ(n2) (B) Θ(n2 log n)
(C) Θ(n )3
(D) Θ(n3 log n)
V3 7
V4 29. Consider the following operation along with Enqueue
4 and Dequeue operations on queues, where k is a
5
6 global parameter.
MultiDequeue(Q) {
V1 V2
3 m = k
3.132 | Unit 3 • Algorithms

while (Q is not empty) and (m > 0) { (A) 10, 8, 7, 3, 2, 1, 5 (B) 10, 8, 7, 2, 3, 1, 5


Dequeue (Q) (C) 10, 8, 7, 1, 2, 3, 5 (D) 10, 8, 7, 5, 3, 2, 1
m = m - 1 35. Consider the tree arcs of a BFS traversal from a
} source node W in an unweighted, connected, undi-
} rected graph. The tree T formed by the tree acrs is a
What is the worst case time complexity of a sequence data structure for computing  [2014]
of n queue operations on an initially empty queue? (A) The shortest path between every pair of vertices
[2013] (B) The shortest path from W to every vertex in the
(A) Θ(n) (B) Θ(n + k) graph
(C) Θ(nk) (D) Θ(n2) (C) The shortest paths from W to only those nodes
that are leaves of T.
30. The preorder traversal sequence of a binary search (D) The longest path in the graph
tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of
36. The number of distinct minimum spanning trees for
the following is the post order traversal sequence of
the weighted graph below is _______.
the same tree? [2013]
(A) 10, 20, 15, 23, 25, 35, 42, 39, 30 [2014]
(B) 15, 10, 25, 23, 20, 42, 35, 39, 30 2
(C) 15, 20, 10, 23, 25, 42, 35, 39, 30 2 1 2 1
(D) 15, 10, 23, 25, 20, 35, 42, 39, 30
31. Let G be a graph with n vertices and m edges. What is 2 2
the tightest upper bound on the running time of depth 2 1
1 1
first search on G, when G is represented as an adja-
cency matrix?  [2014] 2 2
(A) q(n) (B) q(n + m)
(C) q(n2) (D) q(m2) 37. Suppose depth first search is executed on the graph
below starting at some unknown vertex. Assume that
32. Consider the directed graph given below. [2014]
a recursive call to visit a vertex is made only after
P Q first checking that the vertex has not been visited
earlier. Then the maximum possible recursion depth
(Including the initial call) is _______. [2014]
R S

Which one of the following is TRUE?


(A) The graph does not have any topological order-
ing.
(B) Both PQRS and SRQP are topological orderings.
(C) Both PSRQ and SPRQ are topological orderings. 38. Suppose we have a balanced binary search tree T
(D) PSRQ is the only topological ordering. holding n numbers. We are given two numbers L and
H and wish to sum up all the numbers in T that lie
33. There are 5 bags labeled 1 to 5. All the coins in a given
between L and H. suppose there are m such numbers
bag have the same weight. Some bags have coins of
in T. If the tightest upper bound on the time to com-
weight 10 gm. Others have coins of weight 11 gm. I
pute the sum is O(na logb n + mc logd n), the value of a
pick 1, 2, 4, 8, 16 coins respectively from bags 1 to
+ 10b + 100c + 1000d is -----.
5. Their total weight comes out to 323 gm. Then the
product of the labels of the bags having 11 gm coins  [2014]
is ___[2014] 39. The graph shown below has 8 edges with distinct inte-
ger edge weights. The minimum spanning tree (MST)
34. A priority queue is implemented as a max-heap.
is of weight 36 and contains the edges: {(A, C), (B, C),
Initially it has 5 elements. The level-order traversal
(B, E), (E, F), (D, F)}. The edge weights of only those
of the heap is : 10, 8, 5, 3, 2. Two new elements 1 and
edges which are in the MST are given in the figure
7 are inserted into the heap in that order. The level-
shown below. The minimum possible sum of weights
order traversal of the heap after the insertion of the
of all 8 edges of this graph is _______ [2015]
elements is  [2014]
Chapter 4 • Greedy Approach | 3.133

15 numbered 0 to 9 for i ranging from 0 to 2020?


B E
4  [2015]
(A) h(i) = i 2 mod 10
A F
2 (B) h(i) = i 3 mod 10
9 6 (C) h(i) = (11 * i2 ) mod 10
C D (D) h(i) = (12 * i) mod 10
44. Let G be a weighted connected undirected graph with
40. Consider two decision problems Q1, Q2 such that distinct positive edge weights. If every edge weight
Q1 reduces in polynomial time to 3-SAT and 3-SAT is increased by the same value, then which of the fol-
reduces in polynomial time to Q2. Then which one of lowing statements is/are TRUE? [2016]
the following is consistent with the above statement? P : Minimum spanning tree of G does not change.
[2015]
(A) Q­1 is in NP, Q2 is NP hard. Q : Shortest path between any pair of vertices does
(B) Q2 is in NP, Q1 is NP hard. not change.
(C) Both Q1 and Q2 are in NP. (A) P only (B) Q only
(D) Both Q1 and Q2 are NP hard. (C) Neither P nor Q (D) Both P and Q
41. Given below are some algorithms, and some algo- 45. Let G be a complete undirected graph on 4 vertices,
rithm design paradigms. having 6 edges with weights being 1,2,3,4,5, and
6. The maximum possible weight that a minimum
1. Dijkstra’s Shortest Path i. Divide and Conquer weight spanning tree of G can have is _____ . [2016]
2. Floyd-Warshall algo- ii. Dynamic 46. G = (V,E) is an undirected simple graph in which each
rithm to compute all Programming edge has a distinct weight, and e is a particular edge
pairs shortest path of G. Which of the following statements about the
3. Binary search on a iii. Greedy design minimum spanning trees (MSTs) of G is/ are TRUE?
sorted array [2016]
4. Backtracking search on iv. Depth-first search I. If e is the lightest edge of some cycle in G, then
a graph every MST of G includes e
v. Breadth-first search II. If e is the heaviest edge of some cycle in G, then
Match the above algorithms on the left to the corre- every MST of G excludes e
sponding design paradigm they follow. (A) I only (B) II only
(A) 1–i, 2–iii, 3–i, 4–v (B) 1–iii, 2–iii, 3–i, 4–v (C) both I and II (D) neither I nor II
(C) 1–iii, 2–ii, 3–i, 4–iv (D) 1–iii, 2–ii, 3–i, 4–v 47. Breadth First Search (BFS) is started on a binary tree
42. A Young tableau is a 2D array of integers increasing beginning from the root vertex. There is a vertex t at
from left to right and from top to bottom. Any unfilled a distance four from the root. If t is the n-th vertex in
entries are marked with ∞, and hence there cannot be this BFS traversal, then the maximum possible value
any entry to the right of, or below a ∞. The following of n is _____. [2016]
Young tableau consists of unique entries. 48. Let G = (V,E) be any connected undirected edge-
weighted graph. The weights of the edges in E are pos-
1 2 5 14 itive and distinct. Consider the following statements:
(I) Minimum spanning Tree of G is always unique.
3 4 6 23
(II) Shortest path between any two vertices of G is
10 12 18 25 always unique.
31 ∞ ∞ ∞ Which of the above statements is/are necessarily true?
 [2017]
When an element is removed from a Young tableau, (A) (I) only
other elements should be moved into its place so that (B) (II) only
the resulting table is still a Young tableau (unfilled (C) both (I) and (II)
entries maybe filled in with a ∞). The minimum num- (D) neither (I) nor (II)
ber of entries (other than 1) to be shifted, to remove 1
49. The Breadth First Search (BFS) algorithm has been
from the given Young tableau is _______ [2015]
implemented using the queue data structure. Which
43. Which one of the following hash functions on integers one of the following is a possible order of visiting the
will distribute keys most uniformly over 10 buckets nodes in the graph below? [2017]
3.134 | Unit 3 • Algorithms

51. Let G be a simple undirected graph. Let TD be a depth


M N O
first search tree of G. Let TB be a breadth first search
tree of G.
Consider the following statements.
(I) No edge of G is a cross edge with respect to TD.
(A cross edge in G is between two nodes neither
R Q P of which is an ancestor of the other in TD.)
(II) For every edge (u, v) of G, if u is at depth i and v
is at depth j in TB, then |i – j| = 1.
(A) MNOPQR
(B) NQMPOR Which of the statements above must necessarily be
(C) QMNROP true? [2018]
(D) POQNMR (A) I only (B) II only
(C) Both I and II (D) Neither I nor II
50. A message is made up entirely of characters from the
set X = {P, Q, R, S,T}. The table of probabilities for 52. Consider the following undirected graph G:
each of the characters is shown below:
4 x
Character Probability

P 0.22 1 3

Q 0.34 4 5

R 0.17
4
S 0.19

T 0.08 Choose a value for x that will maximize the number


Total 1.00 of minimum weight spanning trees (MWSTs) of G.
The number of MWSTs of G for this value of x is
If a message of 100 characters over X is encoded ______. [2018]
using Huffman coding, then the expected length of
the encoded message in bits is _________. [2017]

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

Previous Years’ Questions


1. A 2. B 3. C 4. D 5. D 6. A 7. D 8. C 9. C 10. B
11. C 12. D 13. D 14. B 15. B 16. D 17. C 18. D 19. D 20. B
21. C 22. C 23. B 24. B 25. C 26. D 27.  28. C 29. A 30. D
31. C 32. C 33. 12 to 12 34. A 35. B 36. 6 to 6 37. 19 38. 110 39. 69
40. A 41. C 42. 5 43. B 44. A 45. 7 46. B 47. 31 48. A 49. D
50. 225 51. A 52. 4

You might also like