Daa All

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Name:

Advanced Programming, II Semester, 2014–2015


Quiz 7, 15 April 2015

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)

2. How would we evaluate this recursive definition using dynamic programming?

(a) A one dimensional table t of size n, to be filled from t[1] to t[n]


(b) A one dimensional table t of size n, to be filled from t[n] to t[1]
(c) A two dimensional table t of size n × n, to be filled row-wise from t[1][1] to t[n][n].
(d) A two dimensional table t of size n × n, to be filled row-wise from t[n][n] to t[1][1].

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:

Advanced Programming, II Semester, 2014–2015


Quiz 6, 6 April 2015

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.

• Replace each edge with weight w by w edges of weight 1 connected by new


intermediate nodes.
• Run BFS(s) on the modified graph to find the shortest path to each of the
original vertices in the graph.

Which of the following statements is correct?

(a) This strategy will not solve the problem correctly.


(b) This strategy will solve the problem correctly and is as efficient as Dijkstra’s
algorithm.
(c) This strategy will solve the problem correctly but is not as efficient as Dijkstra’s
algorithm.
(d) This strategy will only work if the graph is connected.

(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?

(a) Array+list representation: O(n), Tree representation: O(n)


(b) Array+list representation: O(size(c)), Tree representation: O(n)
(c) Array+list representation: O(n), Tree representation: O(size(c))
(d) Array+list representation: O(size(c)), Tree representation: O(size(c))

(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:

Advanced Programming, II Semester, 2014–2015


Quiz 5, 30 March 2015

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.

Let G = (V, E) with |V | = n and |E| = m. What is the complexity of Dijkstra’s


algorithm in each of the following implementations, in terms of n and m?

Representation Data structure for


Worst-case Complexity
of G expected burning time
Adjacency Matrix Array O(n2 )
Adjacency List Array O(n2 )
Adjacency Matrix Heap O(n + m log n) or O(n2 log n)
2

Adjacency List Heap O((m + n) log n)

(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.

(a) What does the algorithm achieve?

Constructs a minimum-cost spanning tree.

(b) How many edges will the algorithm delete?

The final tree has n − 1 edges, so m − (n − 1) edges will be deleted.

(5 marks)
Name:

Advanced Programming, II Semester, 2014–2015


Quiz 4, 18 March 2015
Answer all questions in the space provided. Use the reverse for rough work, if any.
Don’t forget to fill your name!
1. How many topological orderings does the following directed acyclic graph have?

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:

Advanced Programming, II Semester, 2014–2015


Quiz 3, 11 March 2015
Answer all questions in the space provided. Use the reverse for rough work, if any.
Don’t forget to fill your name!

1. Let G = (V, E) with |V | = n and |E| = m. What is the worst-case complexity of:

(a) BFS using an adjacency matrix representation of G. O(n2)

(b) BFS using an adjacency list representation of G. O(n + m)

(c) DFS using an adjacency matrix representation of G. O(n2 )

(d) DFS using an adjacency list representation of G. O(n + m)


(2 marks)
2. Given the following data about DFS on a directed graph, reconstruct the DFS tree.

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.

(a) (c, b) — backward edge


(b) (f, e) — cross edge
(c) (d, g) — forward edge
(3 marks)
Name:

Advanced Programming, II Semester, 2014–2015


Quiz 2, 2 March 2015

Answer all questions in the space provided. Use the designated space for rough work, if any.
Don’t forget to fill your name!

1. Complete the following function definition so that it behaves as described below—that


is, fill in the parameters for f() in the correct order, with default values, as appropriate.

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):

• The first line gives us the default values for c and d.


• The second line tells us the positions for a and b.
• The third line tells us that neither a nor b has default values.

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.

For example, minout([1,3,2,4,17]) is 0 and minout([1,3,0,2,4]) is 5.

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

You might also like