You are on page 1of 10

# Course 02110 Paul Fischer

Algorithms and Data Structures 2 Fall 2011 Assignment TEST EXAM - SOLUTIONS
Date: 01. 12. 2011, Due date:

## The test exam is approximately 60% of the real exam.

Exercise TEST EXAM - SOLUTIONS.1: Consider the graph in Figure 1. It shows a network with ow at top. Find a maximal ow and a minimum cut. Indicate both in the middle respectively lower part of Figure 1. Solution: A maximum ow.
0/4 6/7
3/3

8/8

s
5/8

3/3

6/8

2/3

3/3

A minimum cut.
4 7

s
8

3
3 8 3

End of Solution. End of Exercise 1 Exercise TEST EXAM - SOLUTIONS.2: Answer the multiple-choice questions in Figure 3. A wrong answers cancels a correct one. Overall you cannot get a negative score. 1

Solution: a. NO, Kirchhos is not obeyed. b. NO. One can easily nd a ow with value 8 (or more) and use min-cut = maxow. c. NO. d. YES. e. NO. CDDAB is a common subsequence of length 5. f. NO. g. MST

End of Solution. End of Exercise 2 Exercise TEST EXAM - SOLUTIONS.3: Consider the following program: i 0; while (rand(1, 10) = 1) do i i + 1; od print(i); What is the probability that the value 3 is printed? What is the expected value of i that is printed? Solution: To print a 3, the while-loop has to be executed exactly 3 times. This means that the random number generator has to return a value from {2, . . . , 10} in the rst 3 calls and returns 1 in the fourth call. The probability for a non-1 is 9/10 that for a 1 is 1/10. The calls are independent, thus the probability for all four outcomes is the product of the four individual probabilities. (9) (9) (9) (1) (729) = = 0.0729. (10) (10) (10) (10) (10000) The possible values are 0, 1, 2, . . .. The probability pn to print n is the probability to execute the while loop exactly n times, which is in turn the probability that the random number generator rst returns n non-1, followed by a 1. Hence (9) n (1) pn = (10) (10) 2

(n p n ) =
n=0 n=0

## (9) n (1) n (10) (10)

=9

Note that you do NOT have to evlauate the formula to a number. Thus the following is a correct answer:
n (9) (1) n (10) (10) n=0

End of Solution. End of Exercise 3 Exercise TEST EXAM - SOLUTIONS.4: In the bipartite graph shown in Figure 4 indicate the edges of a maximum matching. Solution: Take edges {1, e}; {2, a}; {3, b}; {4, d}; {6, c}. Which gives size 5. A larger matching is not possible because this graph has 5 nodes at right. End of Solution. End of Exercise 4 Exercise TEST EXAM - SOLUTIONS.5: Consider the graph G = (V, E ) in Figure 5. The thick edges form a non-minimum spanning tree T = (V, F ). Find a tree-edge e F and an non-tree-edge e E \ F such that exchanging them gives a minimum spanning tree. That is, the tree T = (v, F ), where F = (F \ {e}) {e } is a minimum spanning tree. Argue why the tree is minimum. Solution: Use e = {b, c} and e = {b, g }. The tree is minimum because when looking at Kruskals algo it considers edges in increasing order of weight: All edges with weight 1 can be chosen; they do not give a cycle. Of the edges with weight 2 only {f, e} can be chosen. {b, e} would close a cycle. This is independent of the order in which the edges of weight 2 are considered. Both edges with weight 3 can be chosen. This gives a tree (7 edges).

## End of Solution. End of Exercise 5 3

Exercise TEST EXAM - SOLUTIONS.6: Given a sequence x1 , . . . , xn of integers. Find indices 1 i j n such that the sum
j

xk
k=i

is maximum. That is nd a sub-interval with maximum sum. Example for n = 8 and 5,-2,-6,4,-1,6,-3,2. The maximum sum is 9 and dened by i = 4, j = 6. Describe a Dynamic Programming algorithm for solving this problem and analyse its running time. Hint: Traverse the sequence from left to right. At each position i, there are at most two candidates for the maximum sub-interval in x1 , . . . , xi , (which?). Determine the update rule when moving one position to the right. IF YOU CANNOT FIND A DP-SOLUTION then describe the best (fastest) solution you can think of and analyse its running time. Solution: For the solution the indexing starts with 0 and ends with n 1. It is much more detailed than what we expect in the exam. The algorithm runs through the sequence x0 , . . . , xn1 only once. If the current position is k then the algorithm knows the optimal solution (maximum sum) ak1 on the subproblem consisting of x0 , . . . , xk1 . Let [i, j ], 0 i j k 1, be the interval of the maximum sum ak1 , i.e., ak = j t=i xt . The optimal solution for x0 , . . . , xk is either ak1 or it involves xx . Then it is some interval [ , k ] which ends at the current position k . Let bk be the maximum sum of this form, i.e., bk = k t= xk . We start with the subproblem consisting just of the rst integer x0 . Then we have b0 = x0 and we set a0 = x0 . If we are at a position k > 0 then there are two possibilities for bk : Either bk is realized by an interval [ , k ] where < k or by the interval [k, k ] consisting of xk only. In the rst case the maximum sum bk1 is extended by xk . Hence we can compute bk as that possibility which maximizes value: bk = max {bk1 + xk , xk } Similarly the value ak is the larger one of ak1 and bk : ak = max {ak1 , bk } Here is the pseudo-code of the algorithm DynamicProgrammingAlgorithm. We only compute the maximum sum and leave it to the reader to extend the algorithm such that also the corresponding interval is computed. Also we can immediately forget bk1 if bk is computed and similarly for ak1 and ak . Hence it suces to have just two variables a and b. The running time of the DynamicProgrammingAlgorithm in input x0 , . . . , xn1 is O (n) in the uniform cost measure. The FOR-loop in line 1 is executed n 1 times. In each execution one addition and two maxima are computed in lines 1 and 1 which is constant time. Hence the loop takes time 3n = O (n). Outside the loop are only two assignments which are constant time. Hence we end up with a total running time of O (n). 4

## a x0 ; b x0 ; for k = 1, . . . , n 1 do b max {b + xk , xk } a max {a, b} od

There is a divide-and-conquer solution with time O(n log(n)) and straight forward solutions with times O(n2 ) and O(n3 ).

## Show a maximum ow.

Indicate a minimum cut. The labels at the edges are the capacities.

0/3

## Figure 2: Image for multiple-choice problem a) and b).

a. b. c. d.

Figure 2 shows a ow network and a ow. The ow network in Figure 2 has a minimum cut with value 6. The running time of the Edmonds-Karp algorithm depends on the value of the maximum ow. The residual capacities of a residual network are always less than or equal to the capacities of the corresponding ow network. The longest common subsequence of the strings ACBDDDAB and BBCDADAAB has length 4 The longest common substring of the strings ACBDDDAB and BBCDADAAB has length 3 The union-nd data structure is used in an algorithm for minimum spanning tree (MST) or closest pair (CP)

No No No No

e. f. g.

No No CP

## Figure 3: Questions for the multiple-choice problem.

1 2 3 4 5 6 a b c d e

a 5 d 1

1 2

b 3 e

4 c 1 6

2 5

4 3 g h

10