Daa All
Daa All
Daa All
Answer all questions in the space provided. Use the reverse for rough work, if any.
At the end of its fifth successful season, the Siruseri Premier League is planning to give an
award to the Most Improved Batsman over the five years. For this, an Improvement Index
will be computed for each batsman. This is defined as the longest subsequence of strictly
increasing scores by the batsman among all his scores over the five seasons. For example,
if the scores for a batsman over the five seasons are [20, 23, 6, 34, 22, 52, 42, 67, 89, 5, 100],
his improvement index is 7 based on the subsequence [20, 23, 34, 52, 67, 89, 100].
1. let S[1..n] denote a sequence of n scores for which the improvement index is to be
calculated. For 1 ≤ j ≤ n, let I(j) denote the improvement index for the prefix of
scores S[1..j] ending at S[j].
Which of the following is a correct recursive formulation of I(j)?
(a) I(1) = 1
For j ∈ 2, 3, . . . , n, I(j) = 1 + max{I(k) | 1 ≤ k < j, S[k] > S[j]}
(b) I(1) = 1
For j ∈ 2, 3, . . . , n, I(j) = 1 + max{I(k) | 1 ≤ k < j, S[k] < S[j]}
(c) I(1) = 1 (
1 + S[j − 1], if S[j − 1] < S[j]
For j ∈ 2, 3, . . . , n, I(j) =
1, otherwise
(d) I(1) = 1 (
1 + S[j − 1], if S[j − 1] > S[j]
For j ∈ 2, 3, . . . , n, I(j) =
1, otherwise
Answer: (b) Look for the longest sequence that can be extended by S[j].
(4 marks)
Answer: (a) The recursive function has a single argument, so we need only a one
dimensional table. The base case is I(1), so start with T [1] and work up to T [n].
(3 marks)
3. How much time will it take to evaluate this recursive definition using dynamic pro-
gramming?
(a) O(n)
(b) O(n log n)
(c) O(n2 )
(d) O(n3 )
Answer: (c) Each entry I(j) requires scanning I(1) to I(j − 1), so the time taken is
1 + 2 + · · · + n − 1 which is O(n2 ).
(3 marks)
Name:
Answer all questions in the space provided. Use the reverse for rough work, if any.
1. Consider the following strategy to solve the single source shortest path problem
with positive integer edge weights from source s.
(5 marks)
Answer: (c) The size of the graph blows up according to the edge weights, but the
strategy is otherwise correct.
2. Suppose we want to extend the Union-Find data structure to support the operation
Reset(c), which takes as input the name of a component c and then breaks up c into
singleton components. For instance if c = 3 and c currently consists of {1, 3, 7},
then Reset(c) will produce three components called 1, 3 and 7 consisting of {1},
{3} and {7}, respectively.
Which of the following is correct about the cost of adding Reset(c) to the array+list
and tree implementations of Union-Find?
(5 marks)
Answer: (b) In the array+list representation we have the list of members of c which
allows us to update the contents of c in time O(size(c)). In the tree representation
there is no easy way to identify all elements that belong to component c without
scanning the entire set, so it takes time O(n).
Name:
Answer all questions in the space provided. Use the reverse for rough work, if any.
1. Recall that Dijkstra’s algorithm to compute the shortest path from a source vertex
s to every other vertex keeps track of two quantities: the time at which each already
visited vertex was “burnt” and the expected burning time for all as yet unburnt
vertices. The main loop of the algorithm repeats the following steps till all the
vertices are burnt:
• Burn the unburnt vertex with minimum expected burning time.
• Update the expected burning time of all neighbours of the vertext just burnt.
(5 marks)
2. We have a connected graph with edge weights with n vertices and m edges. We
execute the following algorithm. Start with the original graph. Consider each edge
ej in decreasing order of cost. If this edge is part of a cycle delete it.
(5 marks)
Name:
2 3 7 8
1 6 10
4 5 9
In any valid linearization, the vertices {2,3,4,5} must come between 1 and 6 and the
vertices {7,8,9} must come between 6 and 10. If we fix the positions of {2,3}, the
positions of {4,5} are decided. Likewise if we fix the positions of {7,8}, the position of
9 is decided. This gives us the following calculation for the total number of orderings.
4 3
2
× 2
= 6 × 3 = 18
(5 marks)
2. Orient the edges of the following undirected graph so that the resulting directed graph
has four strongly connected components. Draw the corresponding scc dag—label each
vertex of the dag with the corresponding scc.
2 4
1 6
3 5
The smallest nontrivial scc one can generate is a triangle. This means that the other
three scc’s must be singletons. You can orient any triangle as an scc and set up the
other edges appropriately. For instance:
2 4 {4}
1 6 {1,2,3} {6}
3 5 {5}
(5 marks)
Name:
1. Let G = (V, E) with |V | = n and |E| = m. What is the worst-case complexity of:
Vertex a b c d e f g
Entry(v) 2 1 10 3 4 6 7
Exit(v) 13 14 11 12 5 9 8
(5 marks)
b
e f c
3. Here are three non-tree edges in the graph of the previous question. Classify them as
forward/backward/cross.
Answer all questions in the space provided. Use the designated space for rough work, if any.
Don’t forget to fill your name!
def f(.........):
print("a",a,"b",b,"c",c,"d",d)
Expected behaviour:
>>> f(b=4,a=3)
a 3 b 4 c 10 d 15
>>> f(3,5,7)
a 3 b 5 c 7 d 15
>>> f(3,c=7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() takes at least 2 arguments (2 given)
(4 marks)
Solution:
def f(a,b,c=10,d=15):
Rough work
2. The function minout(inl) takes a list inl of distinct natural numbers (i.e., integers
from the set {0,1,2,. . . }) and returns the smallest natural number not present in inl.
In other words, if 0 is not in inl, then minout(inl) returns 0, else if 0 is in inl but
1 is not in inl, them minout(inl) returns 1, etc.
Here is a recursive algorithm to compute minout. (Note that inl is not assumed to
be sorted, nor do we make any assumptions about the range of values in inl.)
• Suppose the length of inl is n. Construct two lists lower and upper, where
lower contains elements smaller than b n2 c and upper contains the rest.
• If the size of lower is strictly smaller than n2 , the missing number is smaller than
n
2
, so recursively invoke minout on lower.
• Otherwise, invoke minout on upper. (All numbers in upper are bigger than n2 , so
some offset is required.)
Analyze the running time of this algorithm. (Write a recurrence and solve it.) (6 marks)
Solution:
The recurrence is:
• T (1) = 1
n
• T (n) = T +n
2
n n
Unwinding this we get n + + + . . . 1 = O(n).
2 4
Rough work