GATE 2004
199 questions
MCQ 1991-mark 199
Questions 116–120 of 199
Let A[1,..., n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is (m). Consider the following program fragment written in a C like language:
counter = 0;
for (i = 1; i <= n; i++)
{
if (A[i] == 1)
counter++;
else {
f(counter);
counter = 0;
}
} The complexity of this program fragment isAnswer: (C)
The loop makes passes, and the branch costs constant time, so the loop alone costs . The only extra cost is the calls , each costing .
When , is the length of the run of 1s just before position , and then it is reset to 0. So the runs passed to never overlap, and their lengths add up to at most the number of 1s, which is at most . All calls to together cost .
So the total is . The loop always makes passes, so the cost is for every input. Hence the complexity is .
(A) and (B) claim more than linear growth (, ), which is false since the total is . (D) needs strictly slower than linear, but the cost is at least .
The reset is what keeps it linear. Without it, an array like 1,0,1,0,... would call with counter values up to , giving .
Answer: (C)
When , is the length of the run of 1s just before position , and then it is reset to 0. So the runs passed to never overlap, and their lengths add up to at most the number of 1s, which is at most . All calls to together cost .
So the total is . The loop always makes passes, so the cost is for every input. Hence the complexity is .
(A) and (B) claim more than linear growth (, ), which is false since the total is . (D) needs strictly slower than linear, but the cost is at least .
The reset is what keeps it linear. Without it, an array like 1,0,1,0,... would call with counter values up to , giving .
Answer: (C)
Consider a list of recursive algorithms and a list of recurrence relations as shown below. Each recurrence relation corresponds to exactly one algorithm and is used to derive the time complexity of the algorithm.
Which of the following is the correct match between the algorithms and their recurrence relations?
Which of the following is the correct match between the algorithms and their recurrence relations?
Answer: (B)
Write the recurrence of each algorithm.
- Binary search (P): one comparison, , then we keep one half of elements and discard the other half. So , which is IV.
- Merge sort (Q): divide the array into two parts of elements, sort each recursively, then merge the two sorted halves in time. So , which is III.
- Quick sort (R): time to partition around the pivot, then two recursive calls on the two parts. The two parts have about and elements. So , which is I.
- Tower of Hanoi (S) with disks: move the top disks to the spare peg, move the largest disk (one move), then move the disks onto it. The problem of size is solved twice, so , which is II.
The match is P-IV, Q-III, R-I, S-II.
Answer: (B)
- Binary search (P): one comparison, , then we keep one half of elements and discard the other half. So , which is IV.
- Merge sort (Q): divide the array into two parts of elements, sort each recursively, then merge the two sorted halves in time. So , which is III.
- Quick sort (R): time to partition around the pivot, then two recursive calls on the two parts. The two parts have about and elements. So , which is I.
- Tower of Hanoi (S) with disks: move the top disks to the spare peg, move the largest disk (one move), then move the disks onto it. The problem of size is solved twice, so , which is II.
The match is P-IV, Q-III, R-I, S-II.
Answer: (B)
The recurrence equation
T(1) = 1
T(n) = 2T(n - 1) + n, n 2
evaluates to
T(1) = 1
T(n) = 2T(n - 1) + n, n 2
evaluates to
Answer: (A)
Unroll the recurrence :
Continue down to . This gives
Group the terms:
, where .
The first sum is a GP: .
To find , write and subtract from it. The coefficient of becomes for , and for :
.
Put in the sums:
Check: and . Option (C) would give 2 and option (D) would give 6.
Answer: (A)
Continue down to . This gives
Group the terms:
, where .
The first sum is a GP: .
To find , write and subtract from it. The coefficient of becomes for , and for :
.
Put in the sums:
Check: and . Option (C) would give 2 and option (D) would give 6.
Answer: (A)
Consider the undirected graph below:
Using Prim's algorithm to construct a minimum spanning tree starting with node A, which one of the following sequences of edges represents a possible order in which the edges would be added to construct the minimum spanning tree?
Using Prim's algorithm to construct a minimum spanning tree starting with node A, which one of the following sequences of edges represents a possible order in which the edges would be added to construct the minimum spanning tree?
Answer: (D)
Edge weights from the figure: AD 7, AB 10, AC 22, BC 30, BE 49, CE 10, CD 26, CF 4, DF 22, FG 5, EG 2.
Prim's algorithm grows one tree from A. At each step it adds the lightest edge with exactly one end in the tree.
1. Tree {A}: edges AB 10, AC 22, AD 7. Add (A,D).
2. Tree {A,D}: AB 10, AC 22, DC 26, DF 22. Add (A,B).
3. Tree {A,D,B}: AC 22, DF 22, DC 26, BC 30, BE 49. AC and DF tie at 22, so either is allowed. Add (D,F).
4. Tree {A,B,D,F}: CF 4, FG 5, AC 22. Add (F,C).
5. FG 5, CE 10, AC 22. Add (F,G).
6. GE 2, CE 10. Add (G,E).
This gives (A,D), (A,B), (D,F), (F,C), (F,G), (G,E), which is (D).
(A) starts with (E,G), which does not touch A.
(B) has (G,E) before the tree has reached G or E, so the edge is not connected to the tree.
(C) starts with (A,B) of weight 10, but (A,D) of weight 7 is lighter and must come first.
Answer: (D)
Prim's algorithm grows one tree from A. At each step it adds the lightest edge with exactly one end in the tree.
1. Tree {A}: edges AB 10, AC 22, AD 7. Add (A,D).
2. Tree {A,D}: AB 10, AC 22, DC 26, DF 22. Add (A,B).
3. Tree {A,D,B}: AC 22, DF 22, DC 26, BC 30, BE 49. AC and DF tie at 22, so either is allowed. Add (D,F).
4. Tree {A,B,D,F}: CF 4, FG 5, AC 22. Add (F,C).
5. FG 5, CE 10, AC 22. Add (F,G).
6. GE 2, CE 10. Add (G,E).
This gives (A,D), (A,B), (D,F), (F,C), (F,G), (G,E), which is (D).
(A) starts with (E,G), which does not touch A.
(B) has (G,E) before the tree has reached G or E, so the edge is not connected to the tree.
(C) starts with (A,B) of weight 10, but (A,D) of weight 7 is lighter and must come first.
Answer: (D)
Suppose we run Dijkstra's single source shortest-path algorithm on the following edge-weighted directed graph with vertex P as the source.
In what order do the nodes get included into the set of vertices for which the shortest path distances are finalized?
In what order do the nodes get included into the set of vertices for which the shortest path distances are finalized?
Answer: (B)
Dijkstra's algorithm finalises the unfinished vertex with the smallest tentative distance from the source , then relaxes its outgoing edges.
Start: . Edges from give .
1. Finalise .
2. Finalise (1): , .
3. Finalise (2): , .
4. Finalise (3): it has no outgoing edges.
5. Finalise (4): , and stays 3.
6. Finalise (7).
Order: .
Answer: (B)
Start: . Edges from give .
1. Finalise .
2. Finalise (1): , .
3. Finalise (2): , .
4. Finalise (3): it has no outgoing edges.
5. Finalise (4): , and stays 3.
6. Finalise (7).
Order: .
Answer: (B)