Final Crib

10
Last Name: ............................. First Name: .............................. Email: .............................. CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 1/8 Problem 1: Analysis of Recursive Algorithm (10 points). Consider the function Mystery defined below. Mystery(n) if n> 1 then begin print(”x”); Mystery(n 1); Mystery(n 1); Mystery(n 1); Mystery(n 1); end If we call Mystery(n), where n is an integer n> 1, how many x’s (as an exact function of n) does call Mystery(n) print? Justify your answer/show your work (solve recurrences using the substitution method, do not use O() notation.) Answer: Let f(n) be the number of x’s Mystery(n) prints, when n is an integer n> 1. For the base case, we see that f(2) = 1. For n> 2 we have f(n)=4f(n 1) + 1. We thus have to solve: f(n)=4f(n 1) + 1 , with f(2) = 1. We solve by substitutioon. The general form is f(x)=4f(x 1) + 1. f(n) = 4f(n 1) + 1 for x = n 1 the general form becomes f(n 1) = 4f(n 2) + 1 = 4 (4f(n 2) + 1) + 1 = 4 2 f(n 2) + 4 + 1 for x = n 2 the general form becomes f(n 2) = 4f(n 3) + 1 = 4 2 (4f(n 3) + 1) + 4 + 1 = 4 3 f(n 3) + 4 2 +4+1 = ...... = 4 k f(n k)+4 k1 + ... +4 2 +4+1 guessing general term = 4 k f(n k)+ 4 k 1 4 1 using x N + ... + x 2 + x +1= x N+1 1 x1 for x = 4 and N = k 1 = 4 k f(n k)+ 4 k 1 3 = 4 n2 f(2) + 4 n2 1 3 substitute k = n 2 because we know f(2) = 1 = 4 n2 + 4 n2 1 3 = 3 × 4 n2 +4 n2 1 3 = 4 × 4 n2 1 3 = 4 n1 1 3 . Thus f(n)= ( 4 n1 1 ) /3. (You can verify that this formula gives f(2) = 1, f(3) = 5, etc, as it should.) Last Name: ............................. First Name: .............................. Email: .............................. CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 2/8 Problem 2: Analysis of Recursive Algorithm (10 points). Consider the function Mystery defined below, where T is a global variable initialized to T := 0. Mystery(x) begin T := T + 1; if x> 1 then begin Mystery(x 1); Mystery(x 1); Mystery(x 1); end end If we call Mystery(n), where n is a positive integer, what is the value of T (as an exact function of n), at the end of the execution of Mystery(n). Justify your answer (solve recurrences using the substitution method do not use O() notation.) Answer: If we call Mystery(1), then T will become 1. In general, whatever the value of T is, once Mystery(1) is called, the value of T will increase by 1. If we call Mystery(x) for x> 1, then this will cause the increase of T by 1, plus the increase caused by three recursive calls to Mystery(x1). If f(n) is the final value of T as a function of n after a call to Mystery(n), or, in other words, f(n) is the total increase of T the above imply: f(n)=3f(n 1) + 1 , with f(1) = 1 . Let’s solve f(n) by substitution. The general form is f(x)=3f(x 1) + 1. f(n) = 3f(n 1) + 1 = 3(3f(n 2) + 1) + 1 by substituting f(n 1) = 3f(n 2) + 1 = 3 2 f(n 2) + 3 + 1 = 3 2 (3f(n 3) + 1) + 3 + 1 by substituting f(n 2) = 3f(n 3) + 1 = 3 3 f(n 3) + 3 2 +3+1 = ...... = 3 k f(n k)+3 k1 + ... +3 2 +3+1 guessing general term = 3 k f(n k)+ 3 k 1 31 using x N + ... + x 2 + x +1= x N+1 1 x1 for x = 3 and N = k 1 = 3 k f(n k)+ 3 k 1 2 = 3 n1 f(1) + 3 n1 1 2 by substituting k = n 1 because we know f(1) = 1 = 3 n1 + 3 n1 1 2 = 2×3 n1 +3 n1 1 2 = 3×3 n1 1 2 = (3 n 1) /2 Thus f(n) = (3 n 1) /2. (You can verify that this formula gives f(1) = 1, f(2) = 4, etc, as it should.) Last Name: ............................. First Name: .............................. Email: .............................. CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 3/8 Problem 3: Analysis of Recursive Algorithm (10 points). Consider the function Mystery defined below. Mystery(n) if n> 1 then begin print(”xx”); Mystery(n/3); Mystery(n/3); Mystery(n/3); end If we call Mystery(n), where n> 1 and n is a power of 3, how many x’s (as a function of n) does call Mystery(n) print? Justify your answer/show your work (solve recurrences using the substitution method.) Answer: Let f(n) be the number of x’s Mystery(n) prints, when n is an integer n> 1. Since n is a power of 3, this means n 3. For the base case, we see that f(3) = 2. For n> 3 we have f(n)=3f( n 3 ) + 2. We thus have to solve: f(n)=3f( n 3 )+2 , with f(3) = 2. We solve by substitutioon. The general form is f(x)=3f( n 3 ) + 2. f(n) = 3f( n 3 )+2 for x = n/3 the general form becomes f( n 3 )=3f( n 3 2 )+2 = 3 3f( n 3 2 )+2 +2 = 3 2 f( n 3 2 )+3 × 2+2 for x = n/3 2 the general form becomes f( n 3 2 )=3f( n 3 3 )+2 = 3 2 3f( n 3 3 )+2 +3 × 2+2 = 3 3 f( n 3 3 )+3 2 × 2+3 × 2+2 = 3 3 f( n 3 3 )+2 × (3 2 + 3 + 1) = ...... = 3 k f( n 3 k )+2 × (3 k1 + ... 3 2 + 3 + 1) guessing general term = 3 k f( n 3 k )+2 3 k 1 3 1 using x N + ... + x 2 + x +1= x N+1 1 x1 for x = 3 and N = k 1 = 3 k f( n 3 k )+2 3 k 1 2 = 3 k f( n 3 k )+3 k 1 substitute n 3 k =3 because we know f(3) = 2 implying n 3 k =3 ⇐⇒ 3 k+1 = n ⇐⇒ ⇐⇒ k + 1 = log 3 n ⇐⇒ k = log 3 n 1 = 3 log 3 n1 f(3) + 3 log 3 n1 1 = 2 × 3 log 3 n1 +3 log 3 n1 1=3 × 3 log 3 n1 1=3 log 3 n 1= n 1 . Thus f(n)= n 1, where n is a power of 3. (You can verify that this formula gives f(3) = 2, f(9) = 8, etc, as it should.) Last Name: ............................. First Name: .............................. Email: .............................. CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 4/8 Problem 4: Analysis of Recursive Algorithm (10 points). Consider the function Mystery defined below. Mystery(n) if n> 1 then begin for i = 1 to n print(”x”); Mystery(n/2); Mystery(n/2); end If we call Mystery(n), where n> 1 and n is a power of 2, how many x’s (as a function of n) does call Mystery(n) print? Justify your answer/show your work (solve recurrences using the substitution method.) Answer: Let f(n) be the number of x’s Mystery(n) prints, when n is a power of 2 with n> 1. Thus the smallest number that Mystery is called with is Mystery(2). For the base case, we see that f(2) = 2. For n> 2 we have f(n)=2f( n 2 )+ n. We thus have to solve: f(n)=2f( n 2 )+ n , with f(2) = 2. We solve by substitutioon. The general form is f(x)=2f( n 2 )+ x. f(n) = 2f( n 2 )+ n for x = n/2 the general form becomes f( n 2 )=2f( n 2 2 )+ n 2 = 2 2f( n 2 2 )+ n 2 + n = 2 2 f( n 2 2 )+2 × n 2 + n = 2 2 f( n 2 2 )+2n for x = n/2 2 the general form becomes f( n 2 2 )=2f( n 2 3 )+ n 2 2 = 2 2 2f( n 2 3 )+ n 2 2 +2n = 2 3 f( n 2 3 )+3n = ...... = 2 k f( n 2 k )+(k 1)n guessing general term = 2 k f( n 2 k )+(k 1)n substitute n 2 k =2 because we know f(2) = 2 implying n 2 k =2 ⇐⇒ 2 k+1 = n ⇐⇒ ⇐⇒ k + 1 = log 2 n ⇐⇒ k = log 2 n 1 = 2 log 2 n1 f(2) + (log 2 n 1)n = 2 × 2 log 2 n1 + log 2 n n = 2 log 2 n + n log 2 n n = n + n log 2 n n = n log 2 n . Thus f(n)= n log 2 n, where n is a power of 2. (You can verify that this formula gives f(2) = 2, f(4) = 8, etc, as it should.) Last Name: ............................. First Name: .............................. Email: .............................. CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 5/8 Problem 5: Mergesort Application: Counting Inversions (15 points). Let A be an array of n distinct numbers. If for some i<j we have A(i) >A(j) then the pair (i, j) is called an inversion of A. a. List the five inversions of the array 2,3,8,6,1. b. What array with elements from the set 1, 2,...,n has the most inversions? How many does it have? c. Show how to determine the number of inversions in any array of n distinct elements in time O(n log n). Hint: Modify mergesort. Note: If you give pseudocode, it should be very short and very clear. Answer: a. 2 >1, 3 >1, 8 >6, 8 >1 and 6 >1, thus the inversions are indices (1,5) for 2 >1, indices (2,5) for 3>1, indices (3,4) for 8>6, indices (3,5) for 8>1, and indices (4,5) for 6>1. b. We first argue that the maximum number of inversions that an array with n elements can have is n(n1)/2. This is because, by definition, an inversion involves two indices i and j with i<j, thus the first element with index 1 can result to at most (n1) inversions with each one of the indices 2,...,n, and in general, the i-th element with index i can result to at most (ni) inversions with each one of the indices (i+1),...,n. The above gives a max total of n1 i=1 (ni)= n(n1)/2. It is now easy to see that the array n, (n1),..., 2, 1 indeed has n1 i=1 (ni)= n(n1)/2 inversions, thus the above array indeed has the maximum possible number of inversions. c. Let A be an array of n distinct numbers. We want to count the inversions of A, ie the pairs of indices i and j with i<j and A(i) >A(j). The difficulty in counting the number of inversions in time O(n log n) is that there could be O(n 2 ) inversions. Therefore, our algorithm should be accounting for many sets of inversions in large groups (without expllicitly enumerating the members of the group.) Let us assume that n is a power of 2, and let us partition the pairs to three classes. The first class consists of indices i<j n/2 with A(i) >A(j). These inversions will be computed recursively. The second class consists of indices ( n 2 + 1) i<j with A(i) >A(j). These inversions will be also computed recursively. The third class consists of indices i<j, with 1 i n/2, ( n 2 + 1) j n, and A(i) >A(j). It is now important to notice that the number of inversions of the third class will not change if we permute the elements in the first half of A arbitrarily, and if we also permute the elements in the second half of A arbitrarily. In particular, it is convenient to have the subarrays A(1),...,A( n 2 ) and A( n 2 + 1),...,A(n) sorted (as in mergesort.) This is because we can now compute the number of inversions of the third class while merging (as in mergesort) the first half of A with the second half of A as follows: Suppose that we have found the first k elements of the sorted merged array, and all the inversions involved with these elements. Suppose also that, of these k elements, k 1 come from the first part of A and k 2 come from the second part of A. Thus, we are now comparing the i =(k 1 + 1)-st element of the first part of A with the j =(k 2 + 1)-st element of A. If A(i) <A(j) then the (k + 1)-st element of the merged sorted array is A(i) and there are no more inversions involved with i or A(i). This is because the only possible inversions would have to involve a j > qj with A(i) >A(j ), which is impossible, since the second half of A is sorted, thus A(i) <A(j) <A(j ) for all j >j. On the other hand, if A(i) >A(j) then the (k + 1)-st element of the merged sorted array is A(j) and there are ( n 2 i +1 ) inversions involved with j or A(j). This is because all the pairs (i, j), (i +1,j), ...,(n/2,j) involve inversions, since j n 2 + 1, while the second part of A is sorted thus A(n/2) >...>A(i + 1) >A(i) >A(j). The above algorithm is a straightforward modification of mergesort and runs in time O(n log n). Last Name: ............................. First Name: .............................. Email: .............................. CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 6/8 Problem 6: Divide and Conquer (15 points). Given a sorted array of distinct integers A(1, ..., n), you want to find out whether there is an index i for whichA(i)= i. Give a divide-and-conquer algorithm that runs in time O(log n). You have to justify correctness and running time. Note: If you give pseudocode, it should be very short and very clear. Answer: Assume that n is a power of 2. Main Fact: If A(i) <i then A(j) <j, for all j<i. (Thus there is no fixed point j : A(j)= j in the range 1 j i, and any possible fixed point must be in the range (i + 1) j n.) Proof: Since the A(j)’s are distinct integers and the array A is sorted, we have A(j1) A(j)1, for all j’s. It now follows immediately that for j = i k we have A(j)= A(i k) A(i)k<i k = j. Similarly to the above Main Fact, we can argue that if A(i) >i, then A(j) >j, for all j>i. (Thus there is no fixed point j : A(j)= j in the range i j n, and any possible fixed point must be in the range 1 j (i 1). Algorithm: The above leads to the following natural algorithm: Check if A ( n 2 ) = n 2 . If yes, then you have found a fixed point and you are done. If no, then if A ( n 2 ) < n 2 recurse looking for a possible fixed point in A ( n 2 +1 ) ,...,A(n). If however A ( n 2 ) > n 2 , then recurse looking for a possible fixed point in A(1),...,A ( n 2 ) . Analysis: T(n) T n 2 + c T n 2 2 +2c T n 2 3 +3c ..... T n 2 k + kc = T(1) + c log 2 n , for k = log 2 n = O(log n)

description

3510 gatech

Transcript of Final Crib

Page 1: Final Crib

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 1/8

Problem 1: Analysis of Recursive Algorithm (10 points).Consider the function Mystery defined below.

Mystery(n)if n > 1 then begin

print(”x”);Mystery(n− 1);Mystery(n− 1);Mystery(n− 1);Mystery(n− 1);end

If we call Mystery(n), where n is an integer n > 1, how many x’s (as an exact function of n) does callMystery(n) print? Justify your answer/show your work (solve recurrences using the substitutionmethod, do not use O() notation.)

Answer: Let f(n) be the number of x’s Mystery(n) prints, when n is an integer n > 1. For thebase case, we see that f(2) = 1. For n > 2 we have f(n) = 4f(n− 1) + 1. We thus have to solve:

f(n) = 4f(n− 1) + 1 , with f(2) = 1.

We solve by substitutioon. The general form is f(x) = 4f(x− 1) + 1.

f(n) = 4f(n− 1) + 1for x = n− 1 the general form becomes

f(n− 1) = 4f(n− 2) + 1

= 4 (4f(n− 2) + 1) + 1

= 42f(n− 2) + 4 + 1for x = n− 2 the general form becomes

f(n− 2) = 4f(n− 3) + 1

= 42 (4f(n− 3) + 1) + 4 + 1

= 43f(n− 3) + 42 + 4 + 1

= . . . . . .

= 4kf(n− k) + 4k−1 + . . .+ 42 + 4 + 1 guessing general term

= 4kf(n− k) +4k − 1

4− 1

using xN + . . .+ x2 + x+ 1 = xN+1−1x−1

for x = 4 and N = k − 1

= 4kf(n− k) +4k − 1

3

= 4n−2f(2) +4n−2 − 1

3

substitute k = n− 2because we know f(2) = 1

= 4n−2 +4n−2 − 1

3

=3× 4n−2 + 4n−2 − 1

3

=4× 4n−2 − 1

3

=4n−1 − 1

3.

Thus f(n) =(4n−1 − 1

)/3.

(You can verify that this formula gives f(2) = 1, f(3) = 5, etc, as it should.)

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 2/8

Problem 2: Analysis of Recursive Algorithm (10 points).Consider the function Mystery defined below, where T is a global variable initialized to T := 0.Mystery(x)beginT := T + 1;if x > 1 then begin

Mystery(x− 1); Mystery(x− 1); Mystery(x− 1);end

endIf we call Mystery(n), where n is a positive integer, what is the value of T (as an exact functionof n), at the end of the execution of Mystery(n). Justify your answer (solve recurrences using thesubstitution method do not use O() notation.)

Answer: If we call Mystery(1), then T will become 1. In general, whatever the value of T is, onceMystery(1) is called, the value of T will increase by 1. If we call Mystery(x) for x > 1, then thiswill cause the increase of T by 1, plus the increase caused by three recursive calls to Mystery(x−1).If f(n) is the final value of T as a function of n after a call to Mystery(n), or, in other words, f(n)is the total increase of T the above imply:

f(n) = 3f(n− 1) + 1 ,with f(1) = 1 .

Let’s solve f(n) by substitution. The general form is f(x) = 3f(x− 1) + 1.

f(n) = 3f(n− 1) + 1= 3(3f(n− 2) + 1) + 1 by substituting f(n− 1) = 3f(n− 2) + 1= 32f(n− 2) + 3 + 1= 32(3f(n− 3) + 1) + 3 + 1 by substituting f(n− 2) = 3f(n− 3) + 1= 33f(n− 3) + 32 + 3 + 1= ......= 3kf(n− k) + 3k−1 + . . .+ 32 + 3 + 1 guessing general term

= 3kf(n− k) + 3k−13−1

using xN + . . .+ x2 + x+ 1 = xN+1−1x−1

for x = 3 and N = k − 1

= 3kf(n− k) + 3k−12

= 3n−1f(1) + 3n−1−12

by substituting k = n− 1because we know f(1) = 1

= 3n−1 + 3n−1−12

= 2×3n−1+3n−1−12

= 3×3n−1−12

= (3n − 1) /2

Thus f(n) = (3n − 1) /2.(You can verify that this formula gives f(1) = 1, f(2) = 4, etc, as it should.)

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 3/8

Problem 3: Analysis of Recursive Algorithm (10 points).Consider the function Mystery defined below.

Mystery(n)if n > 1 then begin

print(”xx”);Mystery(n/3);Mystery(n/3);Mystery(n/3);end

If we call Mystery(n), where n > 1 and n is a power of 3, how many x’s (as a function of n) does callMystery(n) print? Justify your answer/show your work (solve recurrences using the substitutionmethod.)

Answer: Let f(n) be the number of x’s Mystery(n) prints, when n is an integer n > 1. Since nis a power of 3, this means n ≥ 3. For the base case, we see that f(3) = 2. For n > 3 we havef(n) = 3f(n3 ) + 2. We thus have to solve:

f(n) = 3f(n

3) + 2 , with f(3) = 2.

We solve by substitutioon. The general form is f(x) = 3f(n3 ) + 2.

f(n) = 3f(n

3) + 2

for x = n/3 the general form becomesf(n3 ) = 3f( n

32) + 2

= 3

(3f(

n

32) + 2

)+ 2

= 32f(n

32) + 3× 2 + 2

for x = n/32 the general form becomesf( n

32) = 3f( n

33) + 2

= 32(3f(

n

33) + 2

)+ 3× 2 + 2

= 33f(n

33) + 32 × 2 + 3× 2 + 2

= 33f(n

33) + 2× (32 + 3 + 1)

= . . . . . .

= 3kf(n

3k) + 2× (3k−1 + . . . 32 + 3 + 1) guessing general term

= 3kf(n

3k) + 2

3k − 1

3− 1

using xN + . . .+ x2 + x+ 1 = xN+1−1x−1

for x = 3 and N = k − 1

= 3kf(n

3k) + 2

3k − 1

2

= 3kf(n

3k) + 3k − 1

substitute n3k

= 3

because we know f(3) = 2implying n

3k= 3 ⇐⇒ 3k+1 = n ⇐⇒

⇐⇒ k + 1 = log3 n ⇐⇒ k = log3 n− 1

= 3log3 n−1f(3) + 3log3 n−1 − 1

= 2× 3log3 n−1 + 3log3 n−1 − 1 = 3× 3log3 n−1 − 1 = 3log3 n − 1 = n− 1 .

Thus f(n) = n− 1, where n is a power of 3.(You can verify that this formula gives f(3) = 2, f(9) = 8, etc, as it should.)

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 4/8

Problem 4: Analysis of Recursive Algorithm (10 points).Consider the function Mystery defined below.

Mystery(n)if n > 1 then begin

for i = 1 to n print(”x”);Mystery(n/2);Mystery(n/2);end

If we call Mystery(n), where n > 1 and n is a power of 2, how many x’s (as a function of n) does callMystery(n) print? Justify your answer/show your work (solve recurrences using the substitutionmethod.)

Answer: Let f(n) be the number of x’s Mystery(n) prints, when n is a power of 2 with n > 1.Thus the smallest number that Mystery is called with is Mystery(2). For the base case, we see thatf(2) = 2. For n > 2 we have f(n) = 2f(n2 ) + n. We thus have to solve:

f(n) = 2f(n

2) + n , with f(2) = 2.

We solve by substitutioon. The general form is f(x) = 2f(n2 ) + x.

f(n) = 2f(n

2) + n

for x = n/2 the general form becomesf(n2 ) = 2f( n

22) + n

2

= 2

(2f(

n

22) +

n

2

)+ n

= 22f(n

22) + 2× n

2+ n

= 22f(n

22) + 2n

for x = n/22 the general form becomesf( n

22) = 2f( n

23) + n

22

= 22(2f(

n

23) +

n

22

)+ 2n

= 23f(n

23) + 3n

= . . . . . .

= 2kf(n

2k) + (k − 1)n guessing general term

= 2kf(n

2k) + (k − 1)n

substitute n2k

= 2

because we know f(2) = 2implying n

2k= 2 ⇐⇒ 2k+1 = n ⇐⇒

⇐⇒ k + 1 = log2 n ⇐⇒ k = log2 n− 1

= 2log2 n−1f(2) + (log2 n− 1)n

= 2× 2log2 n−1 + log2 n− n

= 2log2 n + n log2 n− n

= n+ n log2 n− n

= n log2 n .

Thus f(n) = n log2 n, where n is a power of 2.(You can verify that this formula gives f(2) = 2, f(4) = 8, etc, as it should.)

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 5/8

Problem 5: Mergesort Application: Counting Inversions (15 points).Let A be an array of n distinct numbers. If for some i < j we have A(i) > A(j) then the pair (i, j)is called an inversion of A.a. List the five inversions of the array 2,3,8,6,1.b. What array with elements from the set 1, 2, . . . , n has the most inversions? How many does ithave?c. Show how to determine the number of inversions in any array of n distinct elements in timeO(n log n). Hint: Modify mergesort. Note: If you give pseudocode, it should be very short andvery clear.

Answer:a. 2>1, 3>1, 8>6, 8>1 and 6>1, thus the inversions are indices (1,5) for 2>1, indices (2,5) for3>1, indices (3,4) for 8>6, indices (3,5) for 8>1, and indices (4,5) for 6>1.b. We first argue that the maximum number of inversions that an array with n elements can haveis n(n−1)/2. This is because, by definition, an inversion involves two indices i and j with i < j,thus the first element with index 1 can result to at most (n−1) inversions with each one of theindices 2, . . . , n, and in general, the i-th element with index i can result to at most (n−i) inversionswith each one of the indices (i+1), . . . , n. The above gives a max total of

∑n−1i=1 (n−i) = n(n−1)/2.

It is now easy to see that the array n, (n−1), . . . , 2, 1 indeed has∑n−1

i=1 (n−i) = n(n−1)/2 inversions,thus the above array indeed has the maximum possible number of inversions.c. Let A be an array of n distinct numbers. We want to count the inversions of A, ie the pairs ofindices i and j with i < j and A(i) > A(j). The difficulty in counting the number of inversionsin time O(n log n) is that there could be O(n2) inversions. Therefore, our algorithm should beaccounting for many sets of inversions in large groups (without expllicitly enumerating the membersof the group.)

Let us assume that n is a power of 2, and let us partition the pairs to three classes.The first class consists of indices i < j ≤ n/2 with A(i) > A(j). These inversions will be computedrecursively. The second class consists of indices (n2 +1) ≤ i < j with A(i) > A(j). These inversionswill be also computed recursively. The third class consists of indices i < j, with 1 ≤ i ≤ n/2,(n2 + 1) ≤ j ≤ n, and A(i) > A(j). It is now important to notice that the number of inversions ofthe third class will not change if we permute the elements in the first half of A arbitrarily, and if wealso permute the elements in the second half of A arbitrarily. In particular, it is convenient to havethe subarrays A(1), . . . , A(n2 ) and A(n2 + 1), . . . , A(n) sorted (as in mergesort.) This is because wecan now compute the number of inversions of the third class while merging (as in mergesort) thefirst half of A with the second half of A as follows: Suppose that we have found the first k elementsof the sorted merged array, and all the inversions involved with these elements. Suppose also that,of these k elements, k1 come from the first part of A and k2 come from the second part of A. Thus,we are now comparing the i = (k1 + 1)-st element of the first part of A with the j = (k2 + 1)-stelement of A. If A(i) < A(j) then the (k + 1)-st element of the merged sorted array is A(i) andthere are no more inversions involved with i or A(i). This is because the only possible inversionswould have to involve a j′ > qj with A(i) > A(j′), which is impossible, since the second half ofA is sorted, thus A(i) < A(j) < A(j′) for all j′ > j. On the other hand, if A(i) > A(j) then the(k + 1)-st element of the merged sorted array is A(j) and there are

(n2 − i+ 1

)inversions involved

with j or A(j). This is because all the pairs (i, j), (i + 1, j), . . ., (n/2, j) involve inversions, sincej ≥ n

2 + 1, while the second part of A is sorted thus A(n/2) > . . . > A(i+ 1) > A(i) > A(j).The above algorithm is a straightforward modification of mergesort and runs in time O(n log n).

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 6/8

Problem 6: Divide and Conquer (15 points).Given a sorted array of distinct integers A(1, ..., n), you want to find out whether there is an indexi for whichA(i) = i. Give a divide-and-conquer algorithm that runs in time O(log n). You have tojustify correctness and running time. Note: If you give pseudocode, it should be very short andvery clear.

Answer: Assume that n is a power of 2.Main Fact: If A(i) < i then A(j) < j, for all j < i. (Thus there is no fixed point j : A(j) = j inthe range 1 ≤ j ≤ i, and any possible fixed point must be in the range (i+ 1) ≤ j ≤ n.)Proof: Since the A(j)’s are distinct integers and the array A is sorted, we have A(j−1) ≤ A(j)−1, forall j’s. It now follows immediately that for j = i−k we have A(j) = A(i−k) ≤ A(i)−k < i−k = j.

Similarly to the above Main Fact, we can argue that if A (i) > i, then A(j) > j, for all j > i.(Thus there is no fixed point j : A(j) = j in the range i ≤ j ≤ n, and any possible fixed pointmust be in the range 1 ≤ j ≤ (i− 1).Algorithm: The above leads to the following natural algorithm: Check if A

(n2

)= n

2 . If yes, then youhave found a fixed point and you are done. If no, then if A

(n2

)< n

2 recurse looking for a possiblefixed point in A

(n2 + 1

), . . . , A(n). If however A

(n2

)> n

2 , then recurse looking for a possible fixedpoint in A(1), . . . , A

(n2

).

Analysis:

T (n) ≤ T

(n

2

)+ c

≤ T

(n

22

)+ 2c

≤ T

(n

23

)+ 3c

.....

≤ T

(n

2k

)+ kc

= T (1) + c log2 n , for k = log2 n

= O(log n)

Page 2: Final Crib

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 7a/8

Problem 7: Mix and Max with very few comparisons: Efficient Design 15 points).Let A be an array of n distinct numbers. It is clear that one can find the maximum of these numberswith n−1 comparisons. Similarly, it is clear that one can find the minimum of these numbers withn−1 comparisons. Thus, one can find both the maximum and the minimum of these numbers with2n−2 comparisons. But one can do better! Show how to find both the maximum and the minimumof n distinct numbers with 3n

2 − 2 comparisons (you may assume that n is even.) Note: If you givepseudocode, it should be very short and very clear.

Answer No 1: In STEP 1 below, using n/2 comparisons, we partition the n elements of A intotwo sets B and C, such that the maximum of A belongs to the set B, the minimum of A belongsto the set C, and each one of B and C has exactly n/2 elements.

In STEP 2 below, we use(n2 − 1

)comparisons to find the maximum of B, which is also the

maximum of A, and another(n2 − 1

)comparisons to find the minimum of C, which is also the

minimum of A.Thus, the total number of comparisons is n/2 from STEP 1 and 2

(n2 − 1

)from STEP 2, for a

total ofn

2+ 2

(n

2− 1

)= 3

n

2− 2 .

STEP 1: For each successive pair of integers in A, we put the maximum of the pair in B and theminimum of the pair in C. In particular:

for i = 1 to n/2if A(2i− 1) > A(2i) then begin

B(i) := A(2i− 1);C(i) := A(2i);end

else beginB(i) := A(2i);C(i) := A(2i− 1);end

We now argue that B indeed contains the maximum of A, and C indeed contains the minimumof A. To argue that B indeed contains the maximum of A, it suffices to argue that C does notcontain the maximum of A. This is clear, since for each element of C there is one element of Awhich is larger than C (if C(i) = A(2i) then A(2i − 1) > A(2i) while if C(i) = A(2i − 1) thenA(2i) > A(2i− 1).) Similarly, we can argue that C indeed contains the minimum of A.STEP 2: The standard method of using

(n2 − 1

)comparisons to find the maximum of B, and an-

other(n2 − 1

)comparisons to find the minimum of C:

max := B(1); for i := 2 to n/2 if B(i) > max then max := B(i);min := C(1); for i := 2 to n/2 if C(i) < min then min := C(i);

Turn page to see a different answer.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 7b/8

Answer No 2 for Problem 7:M and μ will eventually hold the maximum and minimum of the array respectively.If n = 2 we can find M and μ by comparing A(1) and A(2).If n = 4, how can we compute the maximum of A(1), . . . , A(4)? Initially, with one comparisons,

M and μ are equal to the maximum and minimum of {A(1), A(2)} respectively. With one morecomparisons, we let X and x be the maximum and minimum of {A(3), A(4)} respectively. Finally,with two more comparisons, we update M = max{M,X} and μ = min{μ, x} respectively. ThusM and μ are eventually equal to the maximum and minimum of {A(1), . . . , A(4)} respectively.

Suppose that for some i < n/2, M and μ hold the maximum and minimum of A(1), . . . , A(2i)respectively. How can we compute the maximum of A(1), . . . , A(2(i+1))? Suppose thatM and μ areequal to the maximum and minimum of {A(1), . . . , A(2i)} respectively. With one more comparisons,we let X and x be the maximum and minimum of {A(2i+1), A(2i+2)} respectively. Next, with twomore comparisons, we update M = max{M,X} and μ = min{μ, x} respectively. Thus M and μ areeventually equal to the maximum and minimum of {A(1), . . . , A(2i+2)} = {A(1), . . . , A(2(i+1))}respectively.

This leads us to the following algorithm:if A(1) < A(2) then begin μ := A(1); M := A(2); end

else begin μ := A(2); M := A(1); endfor i = 1 to

(n2 − 1

)beginif A(2i+ 1) < A(2i+ 2) then begin x := A(2i+ 1); X := A(2i+ 2); end

else begin x := A(2i+ 2); X := A(2i+ 1); endif x < μ then μ := x;if X > M then M := X;end

The total number of comparisons is T (n) = T (n− 2) + 3 with T (2) = 1.We solve the above reccurence by substitution:

T (n) = T (n− 2) + 3

= T (n− 4) + 3× 2

= T (n− 6) + 3× 3

= . . . . . .

= T (n− 2k) + 3× k guessing general term

= T (n− 2k) + 3× ksince T (2) = 1 substitute n− 2k = 2

thus k =(n2 − 1

)= T (2) + 3×

(n

2− 1

)

= 1 + 3× n

2− 3

= 3× n

2− 2 .

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 1, 1/7/15 Due 1/14/15 in class Page 8/8

Problem 8: Exact Sum: Sorting Application (15 points).Given a sorted array of distinct integers A(1, ..., n) and a target integer S, you want to find if Scan be expressed as the sum of two distinct entries of A. That is, you want to find 1 ≤ i < j ≤ nsuch that A(i) + A(j) = S, if such a pair exists. If such a pair does not exist, you want to answerNO. Give an algorithm that runs in time O(n). You have to justify correctness and running time.Note: If you give pseudocode, it should be very short and very clear.

Answer:Main Fact: If A(1) + A(n) > S then A(i) + A(n) > S for all i. (Thus we may discard A(n) as acandidate in a pair A(i) +A(j) = S.)Proof: The integers are distinct and sortd, thus A(i) > A(1) for all i > 1, thus A(i) + A(n) >A(1) +A(n) > S.Similarly, if A(1) + A(n) < S then A(1) + A(i) < S for all i. Thus we may discard A(1) as acandidate in a pair A(i) +A(j) = S.Algorithm: We have two pointers, initially placed on A(1) and A(n) respectivey. We will proceedin at most (n−1) rounds until either the left pointer is on some i and the right pointer is on some jwith i < j and a(i)+ a(j) = S (in which case we have found a desired pair), or the left pointer andthe right pointer coincide (in which case there is no pair with the desired property.) In each step, ifthe left pointer is on i and the right pointer is on j, we compare a(i)+ a(j) to S. If a(i)+ a(j) > Swe move the right pointer to (j − 1). If a(i) + a(j) < S we move the left pointer to (i + 1). Ifa(i) + a(j) = S we have found a pair with the desired property.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 2, 1/16/15 Due 1/23/15 in class Page 1/5

Problem 1: Analysis of Recursive Algorithm (20 points).Consider the function Mystery defined below.

Mystery(n)for i = 1 to n print(”xx”);if n > 16 then for i = 1 to 3 Mystery

(n4

);

If we call Mystery(n), where n > 4 and n is a power of 4, how many x’s (as a function of n) does callMystery(n) print? Justify your answer/show your work (solve recurrences using the substitutionmethod.)Answer: The first line of Mystery(n) prints 2n ”x”s, and the second line makes 3 calls toMystery

(n4

), for n > 16 = 42. We thus have: T (n) = 3T

(n4

)+ 2n, with T (16) = 32.

We solve by substitution. The general form is T (x) = 3T(x4

)+ 2x.

T (n) = 3T

(n

4

)+ 2n for x = n

4 the general form gives T (n4 ) = 3T(

x42

)+ 2× n

4

= 3

(3T

(n

42

)+ 2× n

4

)+ 2n

= 32T

(x

42

)+ 3× 2× n

4+ 2n

for x = n42

the general form gives

T ( n42) = 3T

(n43

)+ 2× n

42

= 32(3T

(n

43

)+ 2× n

42

)+ 3× 2× n

4+ 2n

= 33T

(n

43

)+ 32 × 2× n

42+ 3× 2× n

4+ 2n

= 33T

(n

43

)+ 2n

((3

4

)2

+3

4+ 1

)

= 3kT

(n

4k

)+ 2n

((3

4

)k−1

+ . . .+3

4+ 1

)guessing general term

= 3kT

(n

4k

)+ 2n

⎛⎜⎝1−

(34

)k1− 3

4

⎞⎟⎠ using xk−1 + . . .+ x+ 1 = 1−xk

1−x , for x < 1

stop whenn

4k= 16 ⇔ n

16= 4k ⇔ log4

n

16= k ⇔ k = log4 n− log4 16 ⇔ k = log4 n− 2

= 3log4 n−2T

(n

4log4 n−2

)+ 2n

⎛⎜⎝1−

(34

)log4 n−2

1− 34

⎞⎟⎠

=3log4 n

32T

(42

n

4log4 n

)+ 2n

((4log4 n−2 − 3log4 n−2)/4log4 n−2

1/4

)

=3log4 n

32T(42

)+ 8n

4log4 n

16 − 3log4 n

94log4 n

16

=T (16)

93log4 n + 8n− 8× 16× 3log4 n

9

=32

93log4 n + 8n− 8× 16× 3log4 n

9= 8n− 96

93log4 n = O(n)

Hence T (n) = 8n − 969 3

log4 n (verify also that formula gives T (16) = 8 × 16 − 969 3

log4 16 =8× 16− 96

9 32 = 8× 16− 96 = 32 as it should.)

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 2, 1/16/15 Due 1/23/15 in class Page 2/5

Problem 2: Analysis of Recursive Algorithm (20 points).Consider the function Mystery defined below.

Mystery(n)if n > 1 then print(”xxxxxxx”);if n > 1 then for i = 1 to 5 Mystery

(n3

);

If we call Mystery(n), where n > 1 and n is a power of 3, how many x’s (as a function of n) does callMystery(n) print? Justify your answer/show your work (solve recurrences using the substitutionmethod.)Answer: The first line of Mystery(n) prints 7 ”x”s, and the second line makes 5 calls toMystery

(n3

), for n ≥ 3. We thus have: T (n) = 5T

(n3

)+ 7, with T (3) = 7.

We solve by substitution. The general form is T (x) = 5T(x3

)+ 7.

T (n) = 5T

(n

3

)+ 7 for x = n

3 the general form gives T (n3 ) = 5T(x3

)+ 7

= 5

(5T

(n

32

)+ 7

)+ 7

= 52T

(n

32

)+ 5× 7 + 7

for x = n32

the general form gives

T ( n32) = 5T

(n33

)+ 7

= 52(5T

(n

33

)+ 7

)+ 5× 7 + 7

= 53T

(n

33

)+ 52 × 7 + 5× 7 + 7

= 5kT

(n

3k

)+ 7× (5k−1 + . . .+ 5 + 1) guessing general term

= 5kT

(n

3k

)+ 7

(5k − 1

5− 1

)using xk−1 + . . .+ x+ 1 = xk−1

x−1 , for x > 1

stop whenn

3k= 3 ⇔ n

3= 3k ⇔ log3

n

3= k ⇔ k = log3 n− log3 3 ⇔ k = log3 n− 1

= 5log3 n−1T (3) + 7

(5log3 n−1 − 1

4

)

= 5log3 n−1 × 7 + 7

(5log3 n−1 − 1

4

)

= 7

(5log3 n

5+

5log3 n

20− 1

4

)

= 7

(4× 5log3 n

20+

5log3 n

20− 1

4

)

= 7

(5× 5log3 n

20− 1

4

)

=7

4(5log3 n − 1)

=7

4(3log3 5 log3 n − 1) =

7

4(nlog3 5 − 1) = O(nlog3 5)

Hence T (n) = 74(5

log3 n − 1) (verify also formula gives T (3) = 74(5

log3 3 − 1) = 74(5− 1) = 7.)

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 2, 1/16/15 Due 1/23/15 in class Page 3/5

Problem 3: Correctness and Analysis of a Recursive Algorithm (20 points).A startup has proposed the following ”elegant” sorting algorithm:STOOGE-SORT(A, i, j)if A(i) > A(j) then exchange A(i) ←→ A(j);if (i+ 1) < j then begin

k := (j − i+ 1)/3;STOOGE-SORT(A, i, j − k); Remank: First two thirds.STOOGE-SORT(A, i+ k, j); Remank: Last two thirds.STOOGE-SORT(A, i, j − k); Remank: First two thirds again.end

a. Argue that STOOGE-SORT(A, 1, legth(A)) correctly sorts the input array A(1, . . . , n), wheren = legth(A).b. Give a recurrence for the worst-case running time of STOOGE-SORT and a bound (in O()notation) for the worst-case running time.c. Compare the worse-case running time of STOOGE-SORT with that of bubblesort and mergesort.Does the startup deserve funding?Answer:(a) Assume wlog (wlog=without loss of generality) that n is a multiple of 3, so n = 3k for some positive integer k.

Assume also wlog that all elements of A are distinct. Let S be the array A correctly sorted, that is s1 < s2 < . . . < sn

and ∪ni=1{ai} = ∪n

i=1{si}. Let SL = ∪ki=1{si} be the n/3 smallest elements of A. Let SM = ∪2k

i=k+1{si} be the n/3

middle elements elements of A. Let SR = ∪ni=2k+1{si} be the n/3 largest elements of A. Notice that every element

z ∈ SR has less that n/3 elements that are strictly greater than z. We call this the fundamental property of SR.

After we sort the first 2/3 of the elements of A, let X1 be the set of elements occupying the first 1/3 positions, let

X2 be the set of elements occupying the middle 1/3 positions, and let X3 be the set of elements occupying the last

1/3 positions. It is now easy to see that X1 ∩ SR = ∅, that is, the elements in X1 do not occupy any of the last 1/3

positions of the sorted array s1 < s2 < . . . < sn. This is because every element in x ∈ X1 has at least n/3 elements

that are strictly greater than x (namely all the elements in X2), thus it fails to satisfy the fundamental property of

SR. Thus, the elements of SR ⊂ X2 ∪X3.

Thus, after we sort the last 2/3 of the elements, ie we sort X2 ∪ X3, we are guaranteed that all elements of SR

are correctly sorted and placed in the last 1/3 positions s2k+1, . . . , sn. Consequently, the elements of SL ∪ SM are

correctly placed in the range of positions 1, . . . , 2k, however they are not necessarily sorted.

The final sorting of elements in positions 1, . . . , 2k guarantess that the elements of SL ∪ SM are correctly sorted and

placed first 2/3 positions. Thus A is sorted and coincides with S.

(b)

T (n) = 3T (2n/3) + c

= 3(3T (22n/32) + c) + c = 32T (22n/32) + 3c+ c

= 32(3T (23n/33) + c) + 3c+ c = 33T (23n/33) + c(32 + 3 + 1)

= 3kT (2kn/3k) + c(3k−1 + ...+ 3 + 1)

= 3kT (2kn/3k) + c(3k − 1)/2

= stop when(23

)kn = 1 ⇔ n =

(32

)k⇔ k = log1.5 n

= 3log1.5 nT (1) + c3log1.5 n − c/2

= O(3log1.5 n) = O(1.5log1.5 3 log1.5n) = O(nlog1.5 3)

(c) Mergesort is O(n log n), bubblesort is O(n2), Stoogesort is O(nlog1.5 3) � O(n2.725), no funding!

Page 3: Final Crib

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 2, 1/16/15 Due 1/23/15 in class Page 4/5

Problem 4: Median finding Under Partial Sorting (20 points).Let X(1, . . . , n) and Y (1, . . . , n) be two arrays, each containing n numbers already in sorted order.Describe an O(log n)-comparison algorithm to find the median of all 2n elements in arrays x andY . Justify your answer in correctness and running time.Answer: Suppose S = s1 < s2 < . . . < s2n is the set of elements of X ∪ Y in sorted order. We arelooking for the two medians, sn and sn+1.Case 1 n is odd, thus n = 2k + 1. Thus there are altogether 2(2k + 1) = 4k + 2 elements, and weare looking for the two medians (2k + 1)st and (2k + 2)nd elements.Compare xk+1 and yk+1 and suppose that xk+1 < yk+1. We then know that, for each element xiwhere i = {1, . . . , k} there are at least 2(k + 1) = 2k + 2 elements that are strictly larger than xi(namely, the elements xk+1 to x2k+1 and yk+1 to y2k+1.) Thus, in the sorted array S every elementxi where i = {1, . . . , k} has rank at most 2n− (2k + 2) = 4k + 2− 2k − 2 = 2k, it is smaller thanthe medians, and it is belongs to the first 2k positions of S. Similarly, for each element elementyi where i = {k + 2, . . . , 2k + 1} there are at least 2(k + 1) = 2k + 2 elements that are strictlysmaller than yi. Thus, in the sorted array S every element yi where i = {k+2, . . . , 2k+1} is largerthan the medians and belongs to the last 2k positions of S. We may therefore look recursively forthe medians of X(k + 1, . . . , 2k + 1) and Y (1, . . . k + 1), and with one comparison the problem isreduced to one that is half the size. (Of course the case where xk+1 > yk+1 follows identical logic.)Case 2 n is even, thus n = 2k. Thus there are altogether 4k elements, and we are looking for thetwo medians 2kth and (2k + 1)st elements of S.Compare xk and yk and suppose that xk < yk. We can similarly identify the (k − 1) elementsx1, . . . , xk−1 as being smaller than both medians and the (k − 1) elements yk+2, . . . , yn as beinglarger than both medians, and recurse on X(k, . . . , n) and Y (1, . . . , (k + 1)). Again, with onecomparison we have reduced the problem to one that is half the size.Complexity T (n) = T (n/2) + c which solves to O(log n).

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 2, 1/16/15 Due 1/23/15 in class Page 5/5

Problem 5: Numbers Closest to Median (20 points).Describe an O(n)-comparison algorithm that, given a set S of n distinct integers and a positiveinteger k ≤ n, determines the k numbers in S that are closest to the median of S. For example,if S = {11, 14, 2, 1, 30, 20, 5, 4, 14, 10, 9} and k = 4 the median of S is 10 and the 4 numbers thatare closest to 10 are 9, 11, 12 and 14 since 10-9=1, 11-10=1, 12-10=2 and 14-10=4, while 10-5=5.Justify your answer in correctness and running time.Answer:Using an O(n) median-finding algorithm we find the median m of S.We compute the array A, where a(i) = |s(i)−m|, for all s(i) ∈ S. That’s another O(n) operations.Using an O(n) KSelect algorithm, we find the kth element of A, say its value is δ.We scan S one more time, and for every element with |s(i)−m| ≤ δ, we include s(i) as one of theelements that are among the k elements closest to the median.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 3, 1/23/15 Due 1/27/15 in class Page 1/2

Problem 1: Weighted Median (50 points).For n distinct elements x1, x2, . . . , xn with positive weights w1, w2, . . . , wn such that

∑ni=1wi = 1,

the weighted median is the element xk satisfying

∑xi<xk

wi ≤1

2and

∑xi>xk

wi ≤1

2.

a. Show how to compute the weighted median of n elements in O(n log n) time using sorting.b. Show how to compute the weighted median of n elements in O(n) time using a linear-timemedian finding algorithm similar to KSELECT.

Answer:(a) STEP 1: Use an O(n log n) sorting algorithm to sort the xi’s. If the position of xi in sortedorder is j, then define p(xi) := j.STEP 2: Scan the xi’s in sorted oder, until you find xk satisfying weighted median property:m := 0; index := 0;while m ≤ 1

2index := index+ 1;m := m+ wp(xindex);

return( index );(b) We solve a more general problem: For n distinct elements x1, x2, . . . , xn with positive weightsw1, w2, . . . , wn and a number ε ≤ ∑n

i=1wi, we want the element xk satisfying

∑xi<xk

wi ≤ ε and∑

xi>xk

wi ≤ (n∑

i=1

wi − ε) .

EpsilonSplitter(x1, x2, . . . , xn, w1, w2, . . . , wn, ε)STEP 1: Use KSELECT to find the median s of x1, . . . , xn.STEP 2: Let A = {xi : xi ≤ s} and B := {xi : xi > s}.STEP 3: compute w(A) :=

∑xi∈Awi;

STEP 4: if w(A) > ε then EpsilonSplitter(xi ∈ A,wi : xi ∈ A, ε)else EpsilonSplitter(xi ∈ B,wi : xi ∈ B, ε− w(A));

Complexity: Using O(n) operations in STEPS 1 through 3, we reduce the problem to one of halfthe size. Thus T (n) = T

(n2

)+ cn which solve to O(n).

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 2, 1/23/15 Due 1/27/15 in class Page 2/2

Problem 2: Recursive Design/Divide and Conquer (50 points).Diogenes Technologies have n supposedly identical integrated-circuit chips that in principle arecapable of testing each other. Their testjig accomodates two chips at a time. When the jig isloaded, each chip tests the other and reports whether it is good or bad. A good chip always reportsaccurately whether the other chip is good or bad. But the answer of a bad chip cannot be trusted.Thus, the four possible outcomes of a test are as follows:

Chip A says Chip B says ConclusionB is good A is good both are good, or both are badB is good A is bad at least one is badB is bad A is good at least one is badB is bad A is bad at least one is bad

(a) Consider the problem of finding a single good chip from among n chips, assuming that morethan n/2 of the chips are good. Show that n/2 pairwise tests are sufficient to reduce the probelmto one of nearly half the size.(b) Show that the good chips can be identified with O(n) pairwise tests, assuming that more thann/2 of the chips are good, as in part (a). Give and solve the recurrence that describes the numberof tests. (Avoiding code or pseudocode recommended.)Abswer:(a) Case 1: n is even, so n = 2k and there are at least (k + 1) good chips.Suppose that we test the chips in k pairs.If a pair answers {good, bad} or {bad, bad} then we know that the pair contains at least one badchip. Suppose there are k′ such pairs, and we discard all the chips in these pairs. By discardingthese pairs, we remove exactly 2k′ chips of which at least k′ are bad and at most k′ are good.We are thus left with 2(k − k′) chips, at least (k + 1)− k′ of them are good, and when compairedin pairs the answer of each pair is {good, good}. This means that, in each pair, either both chipsare good, or both chips are bad, and more than half of the pairs correspond to two good chips. Wemay thus keep one chip from each pair, and be left with (k− k′) chips such that more than half ofthese chips are good. This reduces the problem of finding a good chip from 2k to at most k (noteals0 that (k − k′) > 0 since there is at least one pair that outputs {good, good} among the first kcomparisons, ie k′ < k.)Case 2: n is odd, so n = 2k + 1 and there are at least (k + 1) good chips.Suppose that we test the chips in k pairs, and one last odd chip is not tested.For the k pairs, follow the method of Case 1, and suppose that we are left with (k − k′) chips. If(k− k′) is even (it could also be 0, which is an even number) then for the next round we use these(k − k′) chips AND the last odd chip. If (k − k′) is then for the next roud we use these (k − k′)chips AND DO NOT use the last odd chip.(b) The above method reduces the problem of finding a good chip to one of half the size, using n/2comparisons. In the end we will be left with a single chip which is a good chip. The total numberof comparisons is T (n) = T (n/2) + n/2 which solves to T (n) ≤ n. Once a good chip is found, wecan use it in (n− 1) pairwise tests with the remaining (n− 1) chips, and determine the good andthe bad chips in the entire set.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 4, 1/30/15 Due 2/4/15 in class Page 1/4

Problem 1: Recursive Design, Divide and Conquer, 25 points.You have n = 3k coins (k ≥ 1) and a pan balance. One of the coins is counterfeit and it is lighter(in weight) than the rest. All the other (n − 1) coins have the same weight. Design a divide andconquer algorithm to find the counterfeit coin in k rounds. In each round, you may put any numberof coins in each pan of the balance, and, it tells you either that bothe sides have the same weight,or which side is heavier. Explain why your algorithm is correct, and justify the running time.Answer:If n = 3 then we find the counterfeit as follows. We take 2 of the 3 coins and put them on thebalance. If the balance is not horizontal, then the higher side reveals the counterfeit coin. If thebalance is horzintal, then the 2 coins on the balance have equal weight, and the lighter counterfeitis the 3rd one.If n = 3k for some k > 1 then we reduce the problem to a problem on size 3k−1 = n/3 as follows.We partition the coins into 3 sets, each set containing 3k−1 = n/3 coins. We take 2 of these 3 setsand put them on the balance. If the balance is not horizontal, then the higher sode reveals the setof 3k−1 = n/3 coins that contain the counterfeit coin. If the balance is horizontal, then the 2 setson the balance have equal weight, and the lighter counterfeit coin is in the 3rd set. We recurse onthe set of size 3k−1 = n/3 that contains the lighter counterfeit coin.We thus have T (n) = T (n/3) + 1 with T (3) = 1. Let’s solve it by substitution:

T (n) = T (n/3) + 1

= T (n/32) + 1 + 1 = T (n/32) + 2

= T (n/33) + 1 + 2 = T (n/33) + 3

= T (n/3k) + k

= T (n/3log3 n−1) + log3 n− 1

= T (3) + log3 n− 1

= 1 + log3 n− 1

= log3 n

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 4, 1/30/15 Due 2/4/15 in class Page 2/4

Problem 2: Recursive Design, Divide and Conquer, 25 points.Suppose that you are consulting for a bank that is concerned about fraud detection, and theycome to you with the following problem. They have a collectioon of n bank cards that they haveconfiscated, suspecting them of being used in fraud. Each bank card is a small plastic object,containing a magnetic stripe woth some encrypted data, and it corresponds to a unique account inthe bank. Each account can have many bank cards corresponding to it, and we will say that twobank cards are equivalent if and only if they correspond to the same account.

It is very difficult to read the account number off the card directly, but the bank has a hightech ”equivalence tester” that takes two bank cards and, after performing some computations,determines whether they are equivalent.

Their question is the following: among the collection of n cards, is there a set of more than n/2of them that are all equivalent to one another? Assume that the only feasible operations that youcan do with the cards are to pick two of them and plug them in to the equivalence tester. Show howto decide the answer to their question with only O(n log n) invocations of the equivalence tester.Answer: Assume that n is a power of 2. Let c1, . . . , cn be the cards. Say that a bank account xis a majority element for c1, . . . , cn if and only if the number of cards ci corresponding to x is atleast

(n2 + k

), for some k > 0. Clearly, if there exists a majority element x, then x is unique.

Main Lemma: If there is a majority element x for c1, . . . , cn theneither Case 1: x is a marority element for c1, . . . , cn

2,

or Case 2: x is a marority element for cn2+1, . . . , cn,

or Case 3: x is a majority element for both c1, . . . , cn2and cn

2+1, . . . , cn. Proof: By contradition.

Suppose that x is not a majority element for neither of c1, . . . , cn2nor cn

2+1, . . . , cn. That means

that the number of cards in c1, . . . , cn2corresponding to x is at most n/4 and the number of cards in

cn2+1, . . . , cn corresponding to x is also at most n/4. Thus, the total number of cards corresponding

to x in x for c1, . . . , cn is at most n4 + n

4 = n2 . This, however. contradicts the fact that the total

number of cards corresponding to x is at least(n2 + k

), for some k > 0, since x is a majority element

for c1, . . . , cn.

Algorithm: If n = 2 then check the two cards. If they are equivalent, then output one of them. Ifthey are not equivalent, then output nil. If n > 2 thenRecurse on c1, . . . , cn

2. The output should be, either nil, or a card ci with 1 ≤ i ≤ n

2 .Recurse on cn

2+1, . . . , cn. The output should be, either nil, or a card cj with (n2 + 1) ≤ cj ≤ n.

If the output of both the above recursive calls is nil then output nil and halt.If the output of one of the recursive calls is nil and of the other recursive call is a card ca, thencompare ca with all the cards in c1, . . . , cn and if the number of cards found equivalent to ca isgreater than n/2 then output ca and halt.If the output of both recursive calls is not nil, ie the first recursive call output a card ca and thesecond recursive call output a card cb then

Compare ca with all the cards in c1, . . . , cn andif the number of cards equivalent to ca is greater than n/2 then output ca and haltelse compare cb with all the cards in c1, . . . , cn and

if the number of cards equivalent to cb is greater than n/2 then output cb and haltelse output nil.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 4, 1/30/15 Due 2/4/15 in class Page 3/4

Problem 3: Application of KSelect, Median, Order Statistic, 25 points.Let a(1), . . . , a(n) be an unsorted input array of n distinct positive integers, where n is odd. Awigly arrangement of a is a permutation π of the input such that

a(π(1)) > a(π(2)) < a(π(3)) > a(π(4)) < a(π(5)) . . . a(π(n− 2)) > a(π(n− 1)) < a(π(n)).

For example, on input 100, 20, 2, 5, 200, 50, 40, 201, 300a wigly arrangement is 100, 20, 200, 2, 50, 5, 201, 40, 300since 100 > 20 < 200 > 2 < 50 > 5 < 201 > 40 < 300.

Give an O(n) comparison algorithm that outputs a wigly arrangement of an unsorted input arraya(1), . . . , a(n) of n distimct integers, where n is odd. You may give a simple description of thealgorithm (no pseudocode.) You should include a short argument of correctness and running time.Answer:Using O(n) comparisons, find the median m of a(1), . . . , a(n).Scanning a(1), . . . , a(n) and using n comparisons, find the set S1 of (n − 1)/2 elements that aresmaller than m, and the set S2 of (n+ 1)/2 elements that are greater than or equal to m. Clearly,every element of S1 is smaller than every element of S2.Place the elements of S1 on the (n− 1)/2 even numbered indices a(2), a(4), . . . , a(n− 1).Place the elements of S2 on the (n+ 1)/2 odd numbered indices a(1), a(3), . . . , a(n).Since every element of S1 is smaller than every element of S2, the final arrangement is wigly.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 4, 1/30/15 Due 2/4/15 in class Page 4/4

Problem 4: Recursive Design, Divide and Conquer, 25 points.Consider an n-node complete binary tree T , where n = 2d − 1 for some d. Each node in v of T islabelled with a distinct number xv. A node v of T is a local minimum if and only if the label xv isless than the label xw for all nodes w that are joined to v by an edge.

You are given such a complete binary tree T , but the labeling is only specified in the followingimplicit way: for each node v, you can determine the vale xv by probing the node v. Show how tofind a local minimum of T using onlt O(log n) probes to nodes of T .Answer: Algorithm and Reasoning:If d = 2 then the tree has 3 nodes: the root u, the root’s left child uL and the root’s right childuR. If xu < xuL and xu < xuR then u is a local minimim. Otherwise, if xu > xuL then uL is a localminimum, or if xu < xuR then uR is a local minimum.If d > 2 then let u be the root, uL is the root’s left child and uR is the root’s right child. If xu < xuL

and xu < xuR then u is a local minimim.Otherwise, if xu > xuL we recursively look for a local minimum in the tree rooted in uL. If uLis the local minimum of the tree rooted in uL then uL is also a local minimum of the entire tree.This is because, by being a local minimum of the tree rooted in uL, xuL is smaller than both itschildren. And since it is also smaller than its parent xu > xuL , u’s value is smaller than the valueof all its neighbors, hence local minimum. If uL is not the local minimum of the tree rooted in uL,then the local minimum of this subtree is automatically a local minimum of the entire tree.Otherwise, if xu < xuL and xu > xuR we recursively look for a local minimum in the tree rooted inuR.Complexity Analysis: In all cases, the probes consist of the root u and the nodes along a sigle pathfrom u to a some descendant of u. So let u, u1, . . . , uk be that path. The longest possibility forthis path is if uk is a leaf, in which case k = d and the number of nodes probed along this path is(d+1). In addition, for u1, . . . , ud, the sigling of these nodes was also probed, which is an additionald probes. Thus the total number of probes is at most (2d + 1). Since d = log2(n + 1), the totalnumber of probes interms of n is (2d+ 1) = 2 log2(n+ 1) + 1.

Page 4: Final Crib

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 5, 2/3/15 Due 2/18/15 in class Page 1/4

Problem 1: Scheduling All Intervals/Interval Partition, Greedy, 25 points.Consider requests R = {1, 2, . . . , n}. The i-th request corresponds to an interval of time startingat s(i) and finishing at f(i). Say that a subset of intervals is compatible if and only if no two ofthem overlap in time. The problem is to schedule all requests using as few resources as possible. Inparticular, a partitioning of R is a collection of sets R1, R2, . . . , Rm with Ri ∩ Rj = ∅, ∀i �= j, andwith ∪m

i=1Ri = R. Say that a partitioning of R is valid if and only if the subset Ri is a compatiblefor all 1 ≤ i ≤ m (we may thus schedule all intervals using m resources, one resource for each setRi.) We want a valid partitioning of R which minimizes m.(a) Suppose that the intervals in R are arranged in the time line, with interval i starting at s(i)and finishing at f(i). Say that the depth d of R is the maximum number of intervals that pass overany single point on the time line. Argue that any valid partitioning of R uses at least d resources(ie m ≥ d.)(b) Give an effficient greedy algorithm that finds a valid partitioning of R that uses exactly dresources. Argue correctness and running time of your algorithm. Hint: Consider the intervalsordered by their starting times.Answer:(a) If for some particular x we have |{i : s(i) ≤ x ≤ f(i), 1 ≤ i ≤ n}| = d, then we clearly need atleast d resources.(b) The algorithm will consider intervals in increasing starting order. We thus assume thats(1) ≤ s(2) ≤ . . . ≤ s(n). The first interval will be assigned to R1 and we mark the finishtime F1 = f(1). At iteration i we will assign interval i. Throughout the algorithm we will bemaintaining all resources that have been used, together with the time until which each resource isreserved. The resources are being maintained in a heap, so that the resource that finishes first isavailable when we consider the starting time of the next interval i.sort the starting times, so that s(1) ≤ s(2) ≤ . . . ≤ s(n);d := 1; R1 := {1}; F1 := f(1); Heap := {(1, R1, F1)};for i := 2 to n

let (k0, Rk0 , Fk0) be the root of Heap;if s(i) > Fk0 then

Rk0 := Rk0 ∪ {i}; Fk0 := f(i); Update((k0, Rk0 , Fk0)Heap);else

d := d+ 1; Rd := {i}; Fd := f(i); Insert((d,Rd, Fd),Heap);The total running time is O(n log n). This is the time for sorting according to starting times, plusn iterations, each of which one heap operation which is O(log n).

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 5, 2/13/15 Due 2/18/15 in class Page 2/4

Problem 2: Mincost Vertex Cover, Dynamic Programming Implementation, 25 points.Let G(V,E) be a graph with costs on its nodes. Thus, for each v ∈ V , let w(v) > 0 is the cost ofa vertex v. Say that C ⊂ V is a vertex cover if an only if every edge in E has at least one of itsendpoints in the set C: |{u, v} ∪ V | ≥ 1, ∀{u, v} ∈ E. Define the cost of a vertex cover C as thesum of the costs of all vertices in C: W (C) =

∑v∈C w(v).

(a) Suppose that G(V,E) is a line, that is V = {1, . . . , n} and E = {{1, 2}, {2, 3}, . . . , {(n−1), n}}.Give an efficient algorithm that finds a minimum cost vertex cover of G. Argue correctness andrunning time.(b) Suppose that G(V,E) is a cycle, that is V = {1, . . . , n} and E = {{1, 2}, {2, 3}, . . . , {(n −1), n}} ∪ {{n, 1}}. Give an efficient algorithm that finds a minimum cost vertex cover of G. Arguecorrectness and running time.Answer:(a) Let OPT(n) be the cost of the optimal vertex cover for edges {{1, 2}, {2, 3}, . . . , {(n− 1), n}},and let C(n) be the set of vertices belonging to this cover. Let OPTout(n) be the cost of thecheapest vertex cover for edges {{1, 2}, {2, 3}, . . . , {(n−1), n}} that does not contain vertex n, andlet Cout(n) be the set of vertices belonging to this cover. Realize that Cout(n) must contain vertex(n− 1), since edge {(n− 1), n} must be covered. Let OPTin(n) be the cost of the cheapest vertexcover for edges {{1, 2}, {2, 3}, . . . , {(n− 1), n}} that contains vertex n, and let C,in(n) be the set ofvertices belonging to this cover. Clearly OPT(n) = min{OPTout(n),OPTin(n)}.

More generally, for 2 ≤ k ≤ n let OPT(k) be the cost of the optimal vertex cover for edges{{1, 2}, {2, 3}, . . . , {(k − 1), k}}, and let C(k) be the set of vertices belonging to this cover. LetOPTout(k) be the cost of the cheapest vertex cover for edges {{1, 2}, {2, 3}, . . . , {(k − 1), k}} thatdoes not contain vertex k, and let Cout(k) be the set of vertices belonging to this cover. Realizethat Cout(k) must contain vertex (k−1), since edge {(k−1), k} must be covered. Let OPTin(k) bethe cost of the cheapest vertex cover for edges {{1, 2}, {2, 3}, . . . , {(k− 1), k}} that contains vertexk, and let Cin(k) be the set of vertices belonging to this cover.

Clearly C2,out = {1} and OPTout(2) = w(1), while C2,in = {2} and OPTin(2) = w(2).Now, for 3 ≤ k ≤ n, we may write:

Cout(k) = Cin(k − 1)

Cin(k) =

{{k} ∪ Cout(k − 1) if w(k) + OPTout(k − 1) ≤ w(k) + OPTin(k − 1){k} ∪ Cin(k − 1) if w(k) + OPTout(k − 1) > w(k) + OPTin(k − 1)

Therefore, a linear algorithm that computes OPT(n) = min{OPTout(n),OPTin(n)} is amemoization of the above recursions that compute Cout(k) and Cin(k), for k := 2 to n.Initialization: C2,out := {1}; OPTout(2) := w(1); C2,in := {2}; OPTin(2) = w(2);Main Loop: for k := 2 to n begin

Cout(k) = Cin(k − 1); OPTout(k) = OPTin(k − 1);

if w(k) + OPTout(k − 1) ≤ w(k) + OPTin(k − 1) then

{Cin(k) := {k} ∪ Cout(k − 1)OPTin(k) := w(k) + OPTout(k − 1)

else

{Cin(k) := {k} ∪ Cin(k − 1)OPTin(k) := w(k) + OPTin(k − 1)

end;

if OPTout(n) < OPTin(n) then

{C(n) := Cout(n)OPT(n) := OPTout(n)

else

{C(n) := Cin(n)OPT(n) := OPTin(n)

(b) In part (a) we gave a linear time algorithm to find the mincost vertex cover of a weighted line.In addition, the algorithm was computing the mincost vertex covers conatining and not containingan end vertex of the line. In this part we will find the mincost vertex cover of a weighted cycle byreducing this problem to three separate mincost vertex cover problems on suitably defined weightedlines, ans taking the smaller of three suitale solutions.

In particular, for the cycle {{1, 2}, {2, 3}, . . . , {(n − 1), n}} ∪ {{n, 1}}, edge {n, 1}} must becovered by any vertex cover. There are three cases: (i)Vertex 1 belongs to the vertex cover of thecycle and vertex n does not belong to the vertex cover of the cycle. (ii)Vertex n belongs to thevertex cover of the cycle and vertex 1 does not belong to the vertex cover of the cycle. (iii)Bothvertices 1 and n belong to the vertex cover.

For case (i) above, we consider the line graph {{2, 3}, . . . , {(n−1), n}} and compute Ci,out(n−1)and OPTi,out(n − 1). We let X := w(1) + OPTi,out(n − 1). For case (ii) above, we consider theline graph {{1, 2}, {2, 3}, . . . , {(n − 2), (n − 1)}} (with vertices considered from higher to lower)and compute Cii,out(n − 1) and OPTii,out(n − 1). We let Y := w(n) + OPTii,out(n − 1). For case(iii) above, we consider the line graph {{2, 3}, . . . , {(n− 2), (n− 1)}} and compute Ciii(n− 2) andw(1) + w(n) + OPTiii(n− 2).

If X = max{X,Y, Z} then the mincost vertex cover of the cycle is {1} ∪ Ci,out(n − 1) and hascost w(1) + OPTi,out(n − 1). If Y = max{X,Y, Z} then the mincost vertex cover of the cycle is{n} ∪ Cii,out(n − 1) and has cost w(n) + OPTii,out(n − 1). If Z = max{X,Y, Z} then the mincostvertex cover of the cycle is {1, n} ∪ Ciii(n− 2) and has cost w(1) + w(n) + OPTiii(n− 2).

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 5, 2/13/15 Due 2/18/15 in class Page 3/4

Problem 3: Floyd-Warshall, 25 points.Let G(V,E) be a graph where each edge (i, j) has a cost wij . Look at the pseudocode of theFloyd-Warshall algorithm in CRLS.(a) Assuming that G does not have a negative cycle, modify Floyd-Warshall’s algorithm to computethe predecessor matrices Π(k).(b) Modify Floyd-Warshal’s algorithm to decide if G has a negative cycle and, if a negative cycleexists, output all edges of a negative cycle.Answer:(a) Floyd-Warshall (W ) with predecessor matrix1 n ← rows(W )2a D(0) ← W2b for i ← 1 to n

for j ← 1 to nif ((i = j) or (i, j) ∈ E) then π(0)(j) ← i

3 for k ← 1 to n4 for i ← 1 to n5 j ← 1 to n

6 if d(k−1)ij ≤ d

(k−1)ik + d

(k−1)ij then

{d(k)ij ← d

(k−1)ij

π(k)ij ← π

(k−1)ij

else

{d(k)ij ← d

(k−1)ik + d

(k−1)ij

π(k)ij ← π

(k−1)kj

(b) Floyd-Warshall (W ) detecting negative cycle1 n ← rows(W )2a D(0) ← W2b for i ← 1 to n

for j ← 1 to nif ((i = j) or (i, j) ∈ E) then π(0)(j) ← i

2c NegCycle := false; N ← 03 for k ← 1 to n4 for i ← 1 to n5 j ← 1 to n6a if (NegCycle=false)

begin

6b if d(k−1)ij ≤ d

(k−1)ik + d

(k−1)ij then

{d(k)ij ← d

(k−1)ij

π(k)ij ← π

(k−1)ij

else

{d(k)ij ← d

(k−1)ik + d

(k−1)ij

π(k)ij ← π

(k−1)kj

6c if((i = j) and d

(k)ij < 0

)then {NegCycle := true;N ← i}

7 if (NegCycle = true) then beginreturn(N)

repeat{x := π

(n)NN (N); return(N)

}until (x = N)

end

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 5, 2/13/15 Due 2/18/15 in class Page 4/4

Problem 4: Floyd-Warshall, 25 points.Let G(V,E), |V | = n, be a graph where each edge (i, j) has a cost wij . We know that Floyd-Warshall requires that G has no negative cycle. Now consider the following proposition for findingcheapest paths in graphs whose weight matrices contain negative cycles. First find the minimumcost edge we = min1≤i≤n,1≤j≤n{wij}. If we ≥ 0 then all edges of G have non-negative costs andwe may apply Floyd-Warshall. If we < 0 then define a new cost matrix W ′ with w′

ij = wij − we.Now the weight of all edges has been increases by −we, thus all edges have non-negative weightsand W ′ does not have a negative cycle. Apply Floyd-Warshall of W ′ and simultaneously computethe predecessor matrix Π′(n). The predecessor matrix Π′(n) can be used as a predecessor matrix ofshortest paths. Is the above algorithm correct? If yes then give a proof. If no then give a counterexample.Answer:The shortest paths suggested by the predecessor matrix Π′(n) that corresponds to W ′ do notnecessarily correspond to shortest paths in the original graph W . Here is a counter example.Suppose that for vertices x, y and z of W there were edges x → y, x → z and z → y in W withcosts wxy = 3, wxz = 1 and szy = 1. Thus, the shortest path from x to y in W had cost 2 andconsisted of taking edge x → z followed by edge z → y. Now suppose a large weight, say 100, isadded to all the edges. In graph W ′, the edge x → z has cost 101, the edge z → y has cost 101,and the edge x → y has cost 103. In W ′ the shortest path from x to y consists of the edge x → y,and it is different from the shortest path in the original graph W .

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 6, 2/27/15 Due 3/6/15 in class Page 1a/6

Problem : DFS in Directed Graph (10 points).(a) Run DFS in the graph of file DFSDirGraph.pdf, where vertices and edges are explored in lex-icographic order. For every vertex, indicate the start and finish times. Indicate DFS tree edges.Incicate back edges, forward edges and cross edges.

(b) Indicate how strongly connected components are identified using DFS in the reverse graph GT .

Page 5: Final Crib

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 6, 2/27/15 Due 3/6/15 in class Page 1c/6

(c) In the strongly connected component algorithm we explore GT treating vertices in reverse thanthe order that they finished when DFS run on G. Was the use of GT necessary? If we did a secondDFS on G treating vertices in reverse than the order that they finished when DFS run for the firsttime on G, would the strongly connected components be identified?

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 6, 2/27/15 Due 3/6/15 in class Page 2/6

Problem 2: Topological Sorting (10 points).We know that, if G(V,E) is a directed acyclic graph then arranging the nodes in reverse finishorder according to any DFS produces an ordering where all edges are directed from lower to highernodes. Now suppose that G(V,E) is a general directed graph, thus G may have cycles, and thereis no ordering of its nodes such that all edges are directed from lower to higher nodes. However,we still want an ordering of the nodes such that the number of edges directed from higher to loweris minimized. Does arranging nodes in reverse finish order according to DFS produce such anordering? If yes then give a proof. If no then give a counter-example.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 6, 2/27/15 Due 3/6/15 in class Page 3/6

Problem 3: Cycles in Undirected Graphs (20 points).Give an O(|V |) algorithm that determines if an undirected graph G(V,E) in adjacency list represen-tation has a cycle. Your algorithm should run in time O(|V |) independent of |E|. Argue correctnessand running time. Hint: Argue that G has a cycle if and only if DFS discovers a single non-tree edge.

∈ V mark v unvisited;while there exists unvisisted v ∈ V

if (CYCLE = false) then DFS-visit(v);return(CYCLE)DFS-visit(v)mark v visited;if (CYCLE = false) then

for all u ∈ A(v) if (u is visited) then CYCLE := true else DFS-visit(u)

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 6, 2/27/15 Due 3/6/15 in class Page 4/6

Problem 4: Testing Bipartiteness (20 points).An undirected graph G(V,E) is bipartite if and only if V can be partitioned to VL and VR (ieVL ∪ VR = V and VL ∩ VR = ∅) so that all edges in E have one endpoint in VL and the other end-point in VR. Clearly, if a graph contains an odd length cycle then it is not bipartite, thus an oddlength cycle is a certificate of non-bipartiteness. Give an O(|V |+ |E|) algorithm that determines ifG is bipartite. If the graph is not bipartite then the algorithm should output an odd length cycle.If the graph is bipartite then the algorithm finds a partition of V to VL and VR so that all edges inE have one endpoint in VL and the other endpoint in VR. Argue correctness and running time.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 6, 2/27/15 Due 3/6/15 in class Page 5/6

Problem 5: DFS Application (20 points).Let G(V,E) be a directed graph given in adjacency list representation. In addition, each nodev ∈ V has as associated value val(v). For each node v define R(v) to be the set of all nodes that arereachable from v. For each node v define MaxVal(v) = max{val(u) : u ∈ R(v)}. Assuming G(V,E)is acyclic, give an O(|V |+ |E|) algorithm to compute MaxVal(v), for all v ∈ V . Argue correctnessand running time. Hint: Use topological sorting and compute max{val(u) : (v, u) ∈ E}.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 6, 2/27/15 Due 3/6/15 in class Page 6/6

Problem 6: (20 points).Let G(V,E) be a directed graph in adjacency matrix representation. We say that a vertex v is asupersink if and only if there is an edge (u, v) for every vertex u �= v, and there is no edge (v, u).Give an O(|V |) running time algorithm that decides if a graph given in its adjacency matrix rep-resentation has a supersink. (Note that the running time does not even look at all the entries ofthe |V | × |V | adjacency matrix representation of the graph.) Argue correctness and running time.Hint: Suppose that a21 = 1, a31 = 1, a41 = 1, but a51 = 0. What can you infer about nodes 1, 2,3, 4 and 5?

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 7, 3/13/15 Due 3/25/15 in class Page 1/6

Problem 1, MST, 20 pointsLet G(V,E) be an undirected connected graph with distinct costs on its edges. Let ce be the costassociated with every e ∈ E. Argue that the following is a mincost spanning tree algorithm for G.

Let e1, . . . , em be the edges sorted in decreasing order of cost.

T := E;for i := 1 to m

Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 7, 3/13/15 Due 3/25/15 in class Page 2/6

Problem 2, MST, 20 pointsLet G(V,E) be an undirected connected graph with distinct costs on its edges. Let ce be the costassociated with every e ∈ E. Let T be a spanning tree of G. Argue that the following is a mincostspanning tree algorithm for G.while (∃e ∈ T and ∃e′ ∈ E \ T ) such that ( ce′ < ce and T ∪ {e′} \ {e}) is a spanning tree of G

T := T ∪ {e′} \ {e}Answer:

Page 6: Final Crib

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 7, 3/13/15 Due 3/25/15 in class Page 3/6

Problem 3, Bottleneck Spanning Tree, 20 pointsThe bottleneck spanning tree of G is a spanning tree of G whose largest cost edge is minimum overall spanning trees of G. We say that the value of a bottleneck spanning tree is the cost of thelargest cost edge.(a) Argue that the mincost spanning tree of G is also a bottleneck spanning tree of G.

(b) Give an O(|V | + |E|) algorithm that, given an integer b, decides if the value of a bottleneckspanning tree is at most b.

Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 7, 3/13/15 Due 3/25/15 in class Page 4/6

Problem 4, Dijkstra, 20 pointsLet G(V,E) be a directed graph G(V,E) with costs on its edges and let s ∈ V be a source vertex. Ingeneral, Dijkstra’s algorithm finds shortest paths out of s when all the edge costs are non-negative.(a) Give a counter-example of a graph with an edge of negative cost where Dijkstra’s algorithmfails.(b) Argue that Dijkstra’s algorithm works if the cost of some of the edges leaving s have negativecosts, and all other edges have non-negative costs.Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 7, 3/13/15 Due 3/25/15 in class Page 5/6

Problem 5, Review Question, Dynamic Programming, 10 pointsLet G(V,E), |V | = n, be an acyclic directed graph. Give an O(|V | + |E|) algorithmm that deter-mines if there is a path of length (n− 1) in G.Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 7, 3/13/15 Due 3/25/15 in class Page 6/6

2

Problem 6, Review Question, Strong Majority, 10 pointsLet a(1), a(2), . . . , a(n) be an array of n integers. We say that an integer x is a strong majorityelement if and only if |{i : a(i) = x}| > n . Argue that the linear algorithm below correctly decidesif the input array has a strong majority element.

Count:=0;for i := 1 to n

if (Count= 0) then Candidate := a(i);if (a(i) = Candidate) then (Count := Count+ 1) else (Count := Count− 1);

m := 0;if (Count > 0) then for i := 1 to n

if (a(i) = Candidate) then m := m+ 1;if m > n

2 then return(Candidate) else return(Nil)Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 8, 3/27/15 Due 4/03/15 in class Page 1/5

Problem 1, IS≤PCLIQUE, 20 pointsRecall that IS (Independet Set) is the following problem. On input an undirected graphG(V, E) and an integer

k with 1≤ k≤ |V |, doesG have an independent set containing at least k vertices?Next we define the CLIQUEproblem. WhereG(V, E) is an undirected graph, we say that a subset of its nodes C⊆ V is a clique if an only ifbetween every pair of verices in C there is an edge (of E). The size of the clique is |C|. Now, the CLIQUE is thefollowing problem. On input an undirected graphG(V, E) and an integer k with 1≤ k≤ |V |, doesG have aCLIQUE of size at least k?Show that IS≤PCLIQUE.Answer:We have to show that, if there is a polytime algorithm for CLIQUE, then there is a polytimealgorithms for IS. The reduction will be based on the fact below that relates the structure ofG with the

structure of its complement. In particular, the complement ofG(V, E) is the graphG′(V, E′) which has thesame set of vertices asG, and {u, v} ∈ E′ if and only if {u, v} �∈ E. That is, if {u, v} is not an edge in E then {u,v} is an edge in E′ , and if {u, v} is an edge in E then {u, v} is not an edge in E′.Fact: LetG(V, E) be an undirected graph and letG(V, E′) be the complement ofG. LetX⊆ V be a subset of

the vertices. ThenX is an independent set inG if and only ifX is a clique inG′. Proof. Follows immediatelyfrom the definition of independent set, clique, and complement graph.

The reduction from (IS) to (CLIQUE) is now obvious. For an instance of (IS) where the input is a graph G(V,E) and an integer k (and the question is if G has an independent set of size at least k) we constrcut the

complement graph G′(V, E′) which has the same set of vertices as G, and {u, v} ∈ E′ if and only if {u, v} �∈ E.We now ask if G′(V, E′) has a CLIQUE of size at least k. In particular, if G′(V, E′) has a CLIQUE of size atleast k, then G has an IS of size at least k. If G′(V, E′) does not have a CLIQUE of size at least k, then G doesnot have an IS of size at least k.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 8, 3/27/15 Due 4/03/15 in class Page 2/5

Problem 2, Hamilton Cycles and Hamilton Paths, 20 pointsRecall that a Hamilton Cycle in a directed graphG(V, E) is a cycle that uses edges in E and goes through eachvertex in V exactly once. Recall that, in the Directed Hamilton Cycle problem HC(Dir), on input a directed

graphG(V, E) we want to know ifG has a Hamilton Cycle. In lecture we showed that 3SAT≤PHC(Dir).Now, for a directed graphG(V, E), a Hamilton Path is a simple path that uses edges in E and visits each vertexin V exactly once. The Directed Hamilton Path problem HP(Dir) is defined as follows. On input a directedgraphG(V, E), doesG have a Hamilton Path?

Show that 3SAT≤PHP(Dir).Answer:We use the exact same graph construction from the reduction 3SAT≤PHC(Dir), except we do notconnect the terminal vertex t back with the source vertex s.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 8, 3/27/15 Due 4/03/15 in class Page 3/5

Problem 3, Hamiltonicity: Directed and Undirected, 20 pointsRecall that a Hamilton Cycle in a directed graph G(V,E) is a cycle that uses edges in E and goesthrough each vertex in V exactly once. Recall that, in the Directed Hamilton Cycle problem HC(Dir), oninput a directed graph G(V,E) we want to know if G has a Hamilton Cycle.A Hamilton Cycle in an undirected graph G(V,E) is a cycle that uses edges in E and goes through eachvertex in V exactly once. In the Hamilton Cycle problem HC, on input an undirected graphG(V,E) wewant to know if G has a Hamilton Cycle.

Show that HC(Dir)≤PHC.Answer:We need to show that if we can decide HC in polynomial time, then we can decide HD(Dir) inpolynomial time.

LetG(V, E) be a directed graph for which we want to decide if it has a Hamilton cycle. We con-struct an

undirected graphG′(V ′, E′) as follows.For every vertex v ∈ V we introduce three vertices vin, vo, and vout in V ′.For every vertex vo∈ V we introduce two edges: {vin, vo} ∈ E′ and {vo, vout} ∈ E′.For every edge (u, v) ∈ E we introduce edge {uout, vin} ∈ E′.

It is now obvious thatG(V, E) had a Hamilton cycle if and only ifG′(V, E′) has a Hamilton cycle.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 8, 3/27/15 Due 4/03/15 in class Page 4/5

Problem 4, Hardness of Long Paths, 20 pointsThe Long Path Problem (LongP) is defined as follows. On input an undirected, unweighted graph

G(V,E), does G have a simple path of legth at least |V |2 ?

Show that HP≤PLongP.Answer:Let G(V,E) be a graph for which we wan to decide if it has a path of length (|V | − 1).We constrcut a graph G′(V ′, E′) as follows.Let X be a new set of |X| = x isolated vertices (ie, every vertex in X will never be connected toany other vertex.) Set V ′ = V ∪X and set E′ = E.Clearly, if l is the length of the longest path in G then l is also the length of the longest path in G′.We therefore need x to satisfy:l < (|V | − 1) ⇒ l < |V |+x

2 (1) and l ≥ (|V | − 1) ⇒ l ≥ |V |+x2 (2).

For (1) we have: if l < (|V | − 1) then the maximum value of l is (|V | − 2)

and (1) becomes (|V | − 2) < |V |+x2 .

For (2) we have: if l ≥ (|V | − 1) then the minimum value of l is (|V | − 1)

and (2) becomes (|V | − 1) ≥ |V |+x2 .

(1) and (2) combined imply (|V | − 2) < |V |+x2 ≤ (|V | − 1)

which implies |V |+x2 = (|V | − 1).

We may now set x = |V |−2 and realize that for this value of x we have, indeed, |V |+x2 = (|V |−1).

Therefore, if we want to decide if G(V,E) has a Hamilton path, we construct G′(V ′, E′), whereV ′ = V ∪X with |X| = (|V |−2), and E′ = E. Thus the number of vertices of G′ is |V ′| = 2|V |−2,and the length of the longest path in G′ is the same as the length of the lonest path in G. Thus,G has a path of length (|V | − 1) if and only if G′ has a path of length |V ′|/2.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 8, 3/27/15 Due 4/03/15 in class Page 5/5

Problem 5, Euler Cycles, 20 pointsLet G(V,E) be a directed strongly connected graph. An Euler Cycle of G is a sequence of con-secutive edges of E that (a)starts and finishes on the same vertex and (b)every edge of the graphappears in the sequence exactly once.(a) Prove that G is Eulerial (ie G has an Euler Cycle) if and only if every vertex v ∈ V has thesame number of incoming and outgoing edges.(b) Give an efficient time algorithm that, on input an Eulerian directed graph G(V,E) finds anEuler Cycle. Justify correctness and running time.Answer:(a) Clearly, if there exists a single vertex where the out degree is different than the in degree, thegraph cannot have an Euler Cycle. For the other direction, namely that if for every vertex, thein degree is equal to the out degree, then the graph has an Euler Cycle, we give a DFS basedalgorithm in part (b).(b) Since the graph is strongly connected, DFS starting fro any vertex results in a single tree onall V vertices. Starting from a vertex v0 ∈ V , we construct such a tree T rooted at v0. That is,all edges of T are directed from the leaves to the root v0, and every vertex v �= v0 has a uniqueoutgoing edge in the tree T . We may now construct an Euler Cycle by strating at v0 and, when weare at any vertex v, we choose the next vertex u as follows:If there exists an edge v → u that is untraversed and does not belong to T , then the next vertex isu. Otherwise, if the only untraversed vertex v → u belongs to T , then the next vertex is u.The running time to construct T in O(E+V ). The rest of the execution is just a scanning throughthe edges which is O(E).

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 9, 4/8/15 Due 4/15/15 in class Page 1/5

Problem 1, VC≤p Set Cover, 20 pointsRecall the unweighted Set Cover problem. X = {e1, e2, . . . , en} is a ground set of n elements.F = {S1, S2, . . . , Sm} is a collection of subsets of X, ie Sj ⊆ X, for 1 ≤ j ≤ m. A set cover C is asubset of F such that every element of X belongs to a set in C. Given k, such that 1 ≤ k ≤ m, wewant to know if there exists a set cover that has at most k sets.Recall the unweighted Vertex Cover (VC) problem. G(V,E) is an undirected graph. A vertex coverCV is a subset of V such that every edge in E has at least one of its endpoints in CV . Given k,such that 1 ≤ k ≤ |V |, we want to know if there exists a vertex cover that has at most k vertices.Show that VC≤PSet-Cover.Answer:Let G(V,E) be an undirected graph and let k be an integer, with 1 ≤ k ≤ |V |.We construct an instance of set cover (X,F, k′) as follows:For every edge ei ∈ E we have a point ei ∈ X.For every vertex v ∈ V we have a set Sv ∈ F which contains all points ei ∈ X corresponding toedges ei ∈ E that are incident to v.We may now set k′ = k, and observe that G(V,E) has a vertex cover of size at most k if and onlyif (X,F, k) has a set cover of size at most k.

Page 7: Final Crib

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 9, 4/8/15 Due 4/15/15 in class Page 2/5

Problem 2, Weigheted Set Cover, 20 pointsThe weighted set cover problem is as follows. X = {e1, e2, . . . , en} is a ground set of n elements.F = {S1, S2, . . . , Sm} is a collection of subsets of X, ie Sj ⊆ X, for 1 ≤ j ≤ m. Each set Sj has apositive cost cost(Sj), for 1 ≤ j ≤ m. A set cover C is a subset of F such that every element of Xbelongs to a set in C. The cost of a set cover C is cost(C) =

∑S∈C cost(S). Let OPT be the cost of

a set cover whose cost in minimized oved all possible set covers. Give a polynomial time algorithmthat finds a set cover C such that cost(C) ≤ HnOPT. Justify correctness of the approximationbound.Hint: Straightforward extension of unweighted set cover algorithm given in lecture.Answer:

APPROX-SET-COVERinitialization: U := X; (all elements are uncovered)

C := ∅; (no set has been added to the set cover)while U �= ∅ (while there exist uncovered elements)

for every set S ∈ F \ C define effectiveness(S) = |U∩S|cost(S) ;

(the effectiveness of an unpicked set S for the current iterationis the number of uncovered elements that belong to S divided by the cost of the set )pick S ∈ F \ C that minimizes effectiveness(S);

for all elements that belong to U ∩ S set price(e) := cost(S)|U∩S| ;

for all elements that belong to U ∩ S set first(e) := S;C := C ∪ {S}; (add S to the set cover)U := U \ S; (elements that belong to S are no longer uncovered)

return(C);

Theorem: cost(C) ≤ HnOPT.Proof of Theorem. Let COPT be a set cover with cost(COPT) = OPT. We combine Facts 1 and2 (stated in the next page) and get:

HnOPT = Hn

∑S∗∈COPT

cost(S∗)

=∑

S∗∈COPT

Hncost(S∗)

≥∑

S∗∈COPT

∑e∈S∗

price(e) , by Fact 2 below

≥∑e∈X

price(e)because the price of each element e

may appear more than oncein the double summation above

= cost(C) , byFact1below.

Fact 1: cost(C) =∑

e∈S price(e).Proof of Fact 1.

cost(C) =∑S∈C

cost(S)

=∑S∈C

∑e ∈ X

first(e) = S

price(e)notice that the price of each element e appears

exactly once in this double summation,namely with the set S = first(e)

=∑e∈X

price(e) .

Fact 2: If COPT is a set cover with cost(COPT) = OPT and S∗ ∈ COPT

then∑

e∈S∗ price(e) ≤ Hncost(S∗).Proof of Fact 2.

Let e∗1, e∗2, . . . , e∗k be the elements of S∗ in the order in which they were covered when sets wereadded to C by the APPROX-SET-COVER algorithm. Now |S∗| = k ≤ n and realize that:

price(e∗1) ≤ cost(S∗)|S∗| = cost(S∗)

k , since e∗1 was the first element of S∗ that was covered, hence, at that

point S∗ itself could have covered it with effectiveness |S∗|cost(S∗) .

price(e∗2) ≤ cost(S∗)|S∗|−1 = cost(S∗)

k−1 , since e∗2 was the second element of S∗ that was covered, hence, at

that point S∗ itself could have covered it with effectiveness at least |S∗|−1cost(S∗) .

Continuing this arguing, we see that price(e∗i ) ≤ cost(S∗)|S∗|−(i−1) = cost(S∗)

k−(i−1) , for all i ≤ k, and finally

price(e∗k) ≤cost(S∗)k−(k−1) =

cost(S∗)1 .

The above combined imply

∑e∈S∗

price(e) =k∑

i=1

price(e∗i )

≤k∑

i=1

cost(S∗)k − (i− 1)

= cost(S∗)k∑

i=1

cost(S∗)k − (i− 1)

= cost(S∗)(1

k+

1

k − 1+ . . .+

1

2+ 1

)

= cost(S∗)(1 +

1

2+ . . .+

1

k − 1+

1

k

)

≤ cost(S∗)(1 +

1

2+ . . .+

1

n− 1+

1

n

)= cost(S∗)Hn

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 9, 4/8/15 Due 4/15/15 in class Page 3/5

Problem 3, MAX CUT, Factor 2 Approximation, 20 pointsLet G(V,E) be an undirected, unweighted graph. A cut is a partition of V to S ⊂ V and V \ S.The size of the cut is the number of edges that have one endpoint in S and the other endpoint inV \ S. Let OPT be the size of a cut whose size is maximized over all cuts. Cosider the followinggreedy algorithm that tries to construct a cut of large size:

Initially A := ∅; B := ∅;Initially all vertices in v ∈ V are undecided;Initially, for some {u, v} ∈ E add v to A and u to B,

A := A ∪ {v} and B := B ∪ {u}, and mark u and v decided;while ∃ undecided vertices

let u be an undecided vertex;let dA(u) := |{v ∈ A : {u, v} ∈ E}| and dB(u) := |{v ∈ B : {u, v} ∈ E}|;if dA(u) ≥ dB(u) then B := B ∪ {u} else A := A ∪ {u};mark u decided;

output ( S := A; V \ S := B ) ;Argue that this is a factor 1/2 approximation algorithm.That is, argue that |{{u, v} ∈ E : u ∈ A, v ∈ B}| ≥ OPT

2 .

Hint: Argue that |{{u, v} ∈ E : u ∈ A, v ∈ B}| ≥ |E|2 .

Answer:Let ECROSS = {{u, v} ∈ E : u ∈ A and v ∈ B}.Let EIN = {{u, v} ∈ E : either (u ∈ A and v ∈ A) or (u ∈ B and v ∈ B)}.For the first edge, the algorithm places its endpoints in different sets, so the first edge belongs toECROSS. When a new vertex comes it is placed in one of A or B so that it introduces at least asmany edges in ECROSS than in EIN. Thus, at the end of the algorithm, we have

|ECROSS| ≥ |EIN|

We alse have|E| = |ECROSS|+ |EIN|

Thus

|ECROSS| ≥|E|2

Finally, since the maximum cut may, in the best case, involve all edges in E (but, clearly, no more)

OPT ≤ |E|

The last two bounds combined give us

|ECROSS| ≥|E|2

≥ OPT

2

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Homework 9, 4/8/15 Due 4/15/15 in class Page 5/5

Problem 5 Independent Set, Special Case in Polynomial Time, 20 pointsLet G(V,E) be an undirected, unweighted graph that is a tree. That is, G is connected and has nocycles. Give a polynomial time algorithm that finds a maximum cardinality independent set for G.Justify your answer.Answer:Fact 1: If the graph consists of a single edge, then the maximum cardinality independent set con-sists of exactly one of the endpoits (either one.)Fact 2: If the graph has a non leaf v connected to a leaves u1, u2, . . . , uk, then there is a maximumindependent set that does not contain v and contains all of u1, u2, . . . , uk.Proof: It is obvious that a maximum independent set must contain either v or the entire setu1, u2, . . . , uk. If the maximum independent set contains v, then k = 1 (or else removing v andadding u1, u2, . . . , uk would result in an independent set of larger size.) But then, we can remove vfrom the maximum independent set, add the leaf v1, and the size of the independent set does notchange, thus we still have a maximum independent set.

MAX-IND-SET-for-TREE G(V,E)IS := ∅;while exists node u with a single neighbor v; (a leaf)

let u1, u2, . . . , uk be all the leafs incident to v;IS := IS ∪ {u1, u2, . . . , uk};remove {u1, v}, {u2, v}, . . ., {uk, v} from E;if v is connected to other vertices x1, . . . , xk′ that are non leaves then

remove {v, x1}, . . ., {v, xk′} from E;if we ar left with a set of isolated vertices Y , then add them all to the ind set IS := IS ∪ Y ;

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 1, 2/6/15 Page 1/4

Problem 1: 30 points.Consider the function Mystery defined below.

Mystery(n)if n > 1 then begin

print(”x”);Mystery(n− 1);Mystery(n− 1);Mystery(n− 1);end

If we call Mystery(n), where n is an integer n > 1, how many x’s (as a function of n) doescall Mystery(n) print? Justify your answer/show your work (ie give recurrence and solve bysubstitution.)Answer:(10 points for writing correct recursion and base case, 20 points for correct solution to recursion.)We have to solve T (n) = 3T (n− 1) + 1 with T (2) = 1. We will solve it by substitution:

T (n) = 3T (n− 1) + 1

= 3(3T (n− 2) + 1) + 1

= 32T (n− 2) + 3 + 1

= 32(3T (n− 3) + 1) + 3 + 1

= 33T (n− 3) + 32 + 3 + 1

= 3kT (n− k) + (3k−1 + . . .+ 32 + 3 + 1)

= 3kT (n− k) +3k − 1

3− 1= 3kT (n− k) +

3k − 1

2stop when n− k = 2 ⇔ k = n− 2

= 3n−2T (2) +3n−2 − 1

2

= 3n−2 +3n−2 − 1

2

=(2× 3n−2 + 3n−2 +−1

)/2

=(3× 3n−2 − 1

)/2

=(3n−1 − 1

)/2

So T (n) =(3n−1 − 1

)/2 (which is also consistent with 2 = T (2) =

(32−1 − 1

)/2 = 2.)

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 1, 2/6/15 Page 2/4

Problem 2: 30 points.Consider the function Mystery defined below.

Mystery(n)if n > 1 then begin

print(”x”);Mystery(n2 );Mystery(n2 );Mystery(n2 );Mystery(n2 );end

If we call Mystery(n), where n is an integer n > 2 and n is a power of 2, how many x’s, as apolynomial function of n, does call Mystery(n) print? Justify your answer/show your work (ie giverecurrence and solve by substitution.)Answer:(10 points for writing correct recursion and base case, 20 points for correct solution to recursion.)We have to solve T (n) = 4T (n2 ) + 1 with T (4) = T (22) = 5 (since 4 = 22 is the smallest integer forwhich we may call Mystery. We will solve it by substitution:

T (n) = 4T (n

2) + 1

= 4

(4T (

n

22) + 1

)+ 1

= 42T (n

22) + 4 + 1

= 42(T (

n

23) + 1

)+ 4 + 1

= 43T (n

23) + 42 + 4 + 1

= 4kT (n

2k) + 4k−1 + . . .+ 42 + 4 + 1

= 4kT (n

2k) +

4k − 1

4− 1

= 4kT (n

2k) +

4k − 1

3

stop whenn

2k= 22 ⇔ n

22= 2k ⇔ k = log2 n− 2

= 4log2 n−2T (4) +4log2 n−2 − 1

3

=

(3× 5× 4log2 n

42+

4log2 n

42− 1

)/3

=

(16× 4log2 n

42− 1

)/3

=(4log2 n − 1

)/3 =

(22 log2 n − 1

)/3 =

(n2 − 1

)/3 .

So T (n) =(n2 − 1

)/3 (which is also consistent with 5 = T (4) =

(42 − 1

)/3 = 5.)

Page 8: Final Crib

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 1, 2/6/15 Page 3/4

Problem 3: 30 points.Suppose that you are given an array A with n distinct unsorted integer entries, where n = 2m forsome integer m ≥ 1. Let S(1) be the set containing the smallest n/2 elements of A.For example, if n = 16 = 24 and the arrayA is (100, 40, 120, 20, 50, 30, 85, 80, 150, 200, 60, 70, 2, 3, 101, 102),then S(1) = {40, 20, 50, 30, 60, 70, 2, 3}.In general, for positive integer k with 1 ≤ k ≤ m, let S(k) be the set containing the smallest n/2k

elements of A.For example, if n = 16 = 24 and the arrayA is (100, 40, 120, 20, 50, 30, 85, 80, 150, 200, 60, 70, 2, 3, 101, 102),S(1) = {40, 20, 50, 30, 60, 70, 2, 3}, S(2) = {20, 30, 2, 3}, S(3) = {2, 3} and S(4) = {2}.(a, 10 points) Argue that for any integer m ≥ 1, if n = 2m, then on input an array A of n distinctunsorted integers, the elements of S(1) can be identified using O(n) comparisons.(b, 20 points) Argue that for any integer m ≥ 1, if n = 2m, then on input an array A of n distinctunsorted integers, the elements of all the sets S(k), for all 1 ≤ k ≤ m, can be identified and listedusing O(n) comparisons.Answer:Main Fact: There is a constant c such that KSelect with input any array X which has N elementslooking for the K-th element, 1 ≤ K ≤ N runs using at most cN comparisons. Stressing here thatc is independent on N .Corollary: There is a constant c such that on input any arrayX which has N elements can findthe N/2 smallest elements of A using at most (c+ 1)N comparisons. To see this, use at most cNcomparisons to find the middle element, and use an additional scanning of X to find all elementsthat are smaller than the middle element.(a) It follows from the above Corollary that we can find S(1) using at most (c+ 1)n comparisons.(b) We may now recurse on S(1) and find S(2) using at most (c + 1)n/2, and so on, recurse onS(k) to find S(k+1) using at most (c+1)n/2k comparisons. We may thus find all of the sets S(k),for all 1 ≤ k ≤ m = log2 n, with number of comparisons bounded by:

m∑k=1

(c+ 1)n

2k= (c+ 1)n

m∑k=1

1

2k

= (c+ 1)n

(1− 1

2k+1

1− 12

)

= (c+ 1)n

(1− 1

2k+1

12

)

= 2(c+ 1)n

(1− 1

2k+1

)< 2(c+ 1)n

= O(n)

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 1, 2/6/15 Page 4/4

Problem 4: 10 points.Let S1, . . . , Sn be an array of n distinct integers, where n > 2 is a positive even integer.The pseudocode below first updates two arrays A1, . . . , An

2and B1, . . . , Bn

2.

Then, a variable ”Alice” is updated using the array A,and a variable ”Bob” is updated using array B.Characterize the values that variables ”Alice” and ”Bob” have, at the end of the execution of thepseudocode. Give a short justification of your answer.

for i := 1 to n2 begin

Ai := max{Si, Sn2+i};

Bi := min{Si, Sn2+i};

end;Alice := A1;Bob := B1;for i := 1 step 1 to n

2if (Ai > Alice) then Alice := Ai;

for i := 1 step 1 to n2

if (Bi < Bob) then Bob := Bi;Answer: Alice is the value of the maximum element of the array S1, ..., Sn and Bob is the valueof the minimum element of the array S1, ..., Sn. We argue this in two steps:First notice that A1, ..., An

2contains the maximum element of the array S1, ..., Sn and B1, ..., Bn

2

contains the value of the minimum element of the array S1, ..., Sn. This is obvious, since for somei, the maximum value element must have been placed in Ai := max{Si, Sn

2+i}, while for some i′,

the minimum value element must have been placed in Bi := min{Si′ , Sn2+i′}.

Next notice that Alice takes the value of the maximum element of A1, ..., An2using the standard

method of scanning the array once from left to right and updating the max as necessary, and BoBtakes the value of the minimum element of B1, ..., Bn

2also using the standard method of scanning

the array once from left to right and updating the max as necessary.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 2, Sample Page 1/4

Problem 1, Recursion, 25 points.Consider the function Mystery defined below.

Mystery(n)if n > 1 then begin

for i := 1 to n12 print(”x”);

Mystery(n4 );end

If we call Mystery(n), where n is a power of 4 and n > 1, how many x’s (as a function of n)does call Mystery(n) print? Justify your answer/show your work (ie give recurrence and solve bysubstitution.)Answer: n is a power of 4, so n = 4m for some m ≥ 1. We observe that T (4) = 2 and

T (n) = T (n4 ) + n12 for all n ≥ 42. We will solve this recurrence by substitution.

T (n) = T (n

4) + n

12

= T (n

42) +

(n

4

) 12

+ n12

= T (n

43) +

(n

42

) 12

+

(n

4

) 12

+ n12

= T (n

4k) +

(n

4k−1

) 12

+ . . .

(n

42

) 12

+

(n

4

) 12

+ n12

= T (n

4k) + n

12

(1

4k−12

+ . . .+1

422

+1

412

+ 1

)

= T (n

4k) + n

12

(1

2k−1+ . . .+

1

22+

1

2+ 1

)

= T (n

4k) + n

12

(1− 1

2k

1− 12

)

= T (n

4k) + 2n

12

(1− 1

2k

)

stop whenn

4k= 4 ⇔ 1

4k=

4

n⇔

(1

4k

) 12

=

(4

n

) 12

⇔ 1

2k=

2

n12

= T (4) + 2n12

(1− 2

n12

)

= 2 + 2n12 − 4 =

= 2n12 − 2 .

So Mystery prints 2n12 − 2 x’s (which is consistent with T (4) = 2 since 2× 4

12 − 2 = 2.)

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 2, Sample Page 2/4

Problem 2, Dynamic Programming, 25 points.Let G(V,E) be a directed acyclic graph given in adjacency list representation and in topologicallysorted order. That is, where the set of vertices is V = {1, 2, . . . , n}, all edges i → j ∈ E havei < j. Let L(k) be the length of the longest path that starts at k, 1 ≤ k ≤ n. Give an O(|V |+ |E|)algorithm that computes L(k) for all k, 1 ≤ k ≤ n.Answer:Sinks do not have outgoing edges, so no path starts at a sink, so for all sinks s we have L(s) = 0.For every non-sink vertex k, the maximum length path starting at k is a path whose firstedge is k → j ∈ E. Therefore, the maximum length path starting at k is determined asL(k) = 1+max{L(j) : k → j ∈ E}. For the correct computation of L(k) it is necessary that, for alledges k → j ∈ E, the value L(j) has been computed before the computation of L(k) starts. Sincethe graph is acyclic and presented topologically sorted, we know that all edges L(j) : k → j ∈ Ehave k < j. Therefore, if we compute the L(k)’s in reverse order n, (n−1), . . . , 1, we are guaranteedthat when the computation of L(k) is about to start, for all edges k → j ∈ E, the value L(j) hasbeen already computed.Initialization: for k := 1 to n set L(k) := 0;Main Loop: for k := n downto 1

for all j ∈ Adj(k) do L(k) := max{L(k), 1 + L(j)};The running time of the above algorithm is O(|V | + |E|). To see this, realize that each edge isused exactly once at the second line of the main loop, in particular, when updating the value of itsstarting point.

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 2, Sample Page 3/4

Problem 3, DFS, 25 points.(a) An undirected graph G(V,E) is bipartite if and only if V can be partitioned to VL and VR (ieVL ∪ VR = V and VL ∩ VR = ∅) so that all edges in E have one endpoint in VL and the otherendpoint in VR. Describe an O(|V | + |E|) algorithm that decides if an undirected graph given inits adjacency list representation is bipartite.(b) An undirected graph G(V,E) is near-bipartite if and only if there exists e ∈ E such thatremoving e gives a bipartite graph Ge(V,E \ {e}). Describe a polynomial time algorithm that thatdecides if an undirected graph given in its adjacency list representation is near-bipartite. Arguecorrectness and running time.Answer:(a) We give an algorithm that decides if a graph is bipartite by doing one of the following:Either the algorithm finds a partitioning of V to VL and VR such that all edges have one endpointin VL and the other point in VR.Or the algorithm finds an odd length cycle, thus establishing that no such partitioning exists andthe graph is not bipartite.The algorithm achieves the above as follows:First we do DFS in G, color each root of each tree in the DFS forest VL, and as new vertices arediscovered, color each vertex a different color than the color of its parent (if the parent is coloredVL then the vertex is colored VR, while if the parent is colored VR then the vertex is colored VL.)Next we examine all edges {u, v} that were not tree edges.If the endpoints of every such edges have different colors, then the coloring assigned by DFS givesa proper bipartition of vertices, and the graph is bipartite.If there is a non-tree-edge whose endpoints u and v have the same color, and assuming wlog thatv is an ancestor of u in the DFS tree, then there is a path from u to v that has an odd number ofvertices (starting from u, following parents of the DFS tree until we reach v.) Together with theedge {u, v}, this gives a cycle of odd length. This cycle is proof that G is not bipartite.Since the complexity of DFS is O(|V |+|E|), the running time of the entire algorithm is O(|V |+|E|).(b) We consider each one of the graphs Ge(V,E\{e}) and test them for bipartiteness in O(|V |+|E|).If, for some e ∈ E, Ge is bipartite then G is near-bipartite.If, for all e ∈ E, Ge is not bipartite then G is not near-bipartite.The total running time is O(|E|(|V |+ |E|)).

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 2, Sample Page 4/4

Problem 4, DFS, 25 points.Let G(V,E) be a directed graph. The component graph GSCC(V SCC, ESCC) is defined as follows.Suppose that G has strongly connected components C1, C2, . . . , Ck. The vertex set of GSCC isV SCC = {v1, v2, . . . , vk}. There is an edge vi → vj ∈ ESCC if and only if for some x ∈ Ci and forsome y ∈ Cj we have x → y ∈ E.(a) Given G in its adjacency list representation, describe an O(|V | + |E|) running time algorithmto compute the adjacency list representation of GSCC .(b) The adjacency list representation of GSCC may contain redundancies. In particular, for an edgevi → vj ∈ ESCC, vj may be appearing several times in the adjacency list of vi. Give an efficientmethod to eliminate all such redundancies. Analyze the running timeof your method.Answer:(a) First compute the strongly connected components C1, C2, . . . , Ck.This can be done is O(|V |+ |E|) running two DFS as follows: The first DFS is on G. The secondDFS is on the reverse of G, where vertices are visited in order reverse than their finish times in thefirst DFS. The vertices of each tree component of the second DFS correspond to a distinct stronglyconnected component of G.We now have the vertex-set V SCC = {v1, v2, . . . , vk}, one vertex for each strongly connectedcomponent. To find the edges in ESCC we scan the edges in E once. For each edge u → v ∈ E, if uand v belong to different strongly connected components, say v ∈ Ci and v ∈ Cj with i �= j, thenwe add an edge vi → vj to ESCC.The complexity of this algorithm is O(|V |+ |E|).(b) We may eliminate redundancies by sorting the nodes in the adjacency list of each vi ∈ V SCC.Then, multiple occurrences of an incident node vj will appear in consecutive positions. We caneliminate such multiplicities with a secong pass of the adjacency lists. There are at most |V |adjacency lists. Each list has length at most |V |. Sorting each adjacency list takes O(|V | log |V |)and the total running time is O(|V |2 log |V |).We get a better running time if we keep the adjacency lists of GSCC sorted as we are inserting newvertices. Each insertion will take O(log |V |) (using binary search), and we avoid multiplicities. Wehave to examine |E| edges of G, each edge takes O(log |V |) to update the corresponding adjacencylist of GSCC, and the total running time becomes O(|V |+ |E|) for the identification of the stronglyconnected components, and O(|E| log |V |) for the construction of GSCC without edge multiplicities.Thus the total running time becomes O(|V |+ |E| log |V |).

Page 9: Final Crib

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 2, 3-11-15 Page 1/4

Problem 1, Recursion, 25 points.Consider the function Mystery defined below.

Mystery(n)if n ≥ 1 then begin

for i := 1 to n12 print(”x”);

Mystery(n4 );Mystery(n4 );end

(a) How many x’s does Mystery(1) print? How many x’s does Mystery(4) print?(b) If we call Mystery(n), where n is a power of 4 and n ≥ 1, how many x’s (as a function of n)does call Mystery(n) print? Justify your answer/show your work (ie give recurrence and solve bysubstitution.) Hint: Using 4k = 22k and taking logs base 2 makes calculations easier.Answer: (a) Mystery(1) prints one x. Mystery(4) prints four x’s.

(b) We have to solve T (n) = 2T (n4 ) + n12 for all n ≥ 1, with T (1) = 1. Solve by substitution.

T (n) = 2T

(n

4

)+ n

12

= 2

(2T

(n

42

)+

(n

4

) 12

)+ n

12

= 22T

(n

42

)+ 2n

12

= 22(2T

(n

43

)+

(n

42

) 12

)+ 2n

12

= 23T

(n

43

)+ 3n

12

= 2kT

(n

4k

)+ kn

12

stop whenn

4k= 1 ⇔ n = 4k ⇔ n = 22k ⇔ 2k = log2 n ⇔ k =

log2 n

2

= 2log2 n

2 T (1) +log2 n

2n

12

= n12 +

log2 n

2n

12

= n12

(1 +

log2 n

2

).

So Mystery prints n12

(1 + log2 n

2

)x’s (which is consistent with T (1) = 1 since 1

12

(1 + log2 1

2

)= 1

and T (4) = 4 since 412

(1 + log2 4

2

)= 4 .)

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 2, 3-11-15 Page 2/4

Problem 2, Graph Representation, 25 points.Let G(V,E) be an undirected graph given in adjacency list representation (without redundancies,ie each vertex appears at most once in an adjacency list.) The degree of a vertex v ∈ V is thenumber of vertices u that are adjacent to v: deg(v) = |{u : u ∈ Adj(v)}|. The twodegree of a vertexv ∈ V is the sum of the degrees of the vertices that are adjacent to v: twodeg(v) =

∑uAdj(v) deg(u).

(a) Give an O(|V |+ |E|) time algorithm that computes deg(v), for all v ∈ V .(b) Give an O(|V |+ |E|) time algorithm that computes twodeg(v), for all v ∈ V .Answer:(a) To compute degrees scan the adjacency lists of all vertices. Since there are no redundancies,every vertex in an adjacency list of a vertex v adds one to the degree of v.for all v ∈ V set deg(v) := 0;for all v ∈ V

for all u ∈ Adj(v) set deg(v) := deg(v) + 1;The total time is O(|V |+ |E|), each vertex is considered once in the first and second lines, and eachedge {u, v} is considered twice in the second line, once in the adjacency list of u and once in theadjacency list of v.(b) To compute twodegrees, after the degrees have been computed in part (a), again scan theadjacency lists of all vertices. For every vertex u in an adjacency list of a vertex v add deg(u) tothe twodegree of v.for all v ∈ V set twodeg(v) := 0;for all v ∈ V

for all u ∈ Adj(v) set twodeg(v) := twodeg(v) + deg(u);The total running time is O(|V |+ |E|), arguing exactly as in part (a).

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 2, 3-11-15 Page 3/4

Problem 3, DFS, 25 points.Give an O(|V |+ |E|) algorithm which takes as input a directed graph G = (V,E), and determineswhether or not there exists a vertex v ∈ V from which all other vertices are reachable. Justifycorrectness and running time. Hint: First solve the problem for the case where G is acyclic.Answer:

If G(V,E) is acyclic, then it has a topological sorting. The only vertex of V that can possiblyreach all other vertices is the vertex that is first in topologically sorted order, say r (since all othervertices are ahead of r in topological order and thus cannot have an edge to reach r.) We may thusdo DFS starting at r. We know that all vertices reacheble from r will belong to the tree rootedat r. So we just check if the DFS tree rooted at r contains all of V . If yes, then all vertices canbe reached from r. If no, then there is no vertex from which all vertices in V can be reached.The total running time involves two runs of DFS (one for topological sorting and one for checkingreachability from r thus the total running time is O(|V |+ |E|).

For general G(V,E) we first find the strongly connected components of G and the componentgraph GSCC(V SCC , ESCC). This can be done in O(|V | + |E|) using DFS. It is now obvious that,since GSCC is acyclic, there exists a vertex v ∈ V from which all other vertices are reachable ifand only if all of V SCC can be reached from the vertex r ∈ V SCC that is first in a topologicalsorting of V SCC . The approach involves a constant number of DFS calls, so the total running timeis O(|V |+ |E|).

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 2, 3-11-15 Page 4/4

Problem 4, Dynamic Programming, 25 points.Let G(V,E), V = {1, 2, . . . , n}, be a directed acyclic graph given in adjacency list representationand in topologically sorted order (that is, if i → j ∈ E then i < j.) Describe an O(|V |+ |E|) timealgorithm that finds the length of the longest path from 1 to n. If there is no path from 1 to nthen your algorithm should output Nil. Justify correctness and running time of your algorithm.Hint: Consider finding the length of the longest path from every vertex k to n, 1 ≤ k ≤ n.Answer:Let L(k) be the length of the longest path from node k to node n, 1 ≤ k ≤ n.L(n) = 0 and if, for some k < n, there is no path from k to n then L(k) = Nil.Suppose that, for some k ≤ n, L(j) has been correctly computed for all j > k.Since the graph is acyclic and presented topologically sorted order, all edges out of k (which arethe possible first steps of a path from k to n) are of the form k → j, j > k.So we can update L(k) by looking at all of k’s neighbors, say j, and the corresponding L(j)’s:

L(k) =

{Nil if L(j) = Nil for all k → j ∈ Emax{1 + L(j) : k → j ∈ E andL(j) �= Nil} if L(j) �= Nil for some k → j ∈ E

The memoization algorithm is then straightforward:Initialization: L(n) := 0; for all 1 ≤ k ≤ n set L(k) := Nil;Main Loop: for k := (n− 1) down to 1

for all j ∈ Adj(k)

if L(j) �= Nil then L(k) :=

{1 + L(j) if L(k) = Nil

max{L(k), 1 + L(j)} if L(k) �= Nil

Each vertex is examined once in initialization and in the first line of the main loop. and each edgeis examined once in the second line of the main loop. Thus the running time is O(|V |+ |E|).

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 3, Sample Page 2/4

Problem 2, Maxcost Spanning Tree, 25 points.Let G(V,E) be an undirected connected weighted graph, where the weights of all edges are distinctand positive: we > 0, for all e = {u, v} ∈ E. Give an efficient algorithm that finds a spanning treeof maximum cost (ie the sum of the weights of the edges of the spanning tree is maximized, overall spanning trees of the graph.) Justify correctness and running time.Answer:STEP 1: Construct G′ on the same set of vertices and edges as G (thus the same set of spanningtrees), but with new costs of edges w′

e = −we.STEP 2: T = MST in G′ using Kruskal or Prim (note: they work for arbitraty costs on the edges.)STEP 3: Output T as maxcost spanning tree of G.Correctness follows because Kruskal and Prim’s algorithms work for arbitraty costs on the edges,these costs can be positive or negative.Efficiency follows because the main computational task in the above algorithm is one call of anMST algorithm (in STEP 2).

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 3, Sample Page 1/4

Problem 1, MST, Dijkstra, 25 points.(a) Let G(V,E) be a directed weighted graph, where the weights of all edges are positive: wuv > 0,for all u → v ∈ E. Let s ∈ V be a source vertex. Dijkstra’s algorithm with source s finds thecheapest path from s to every other vertex v ∈ V . In particular, for every vertex v ∈ V , Dijkstracomputes a predecessor vertex π(v) such that v → π(v) ∈ E, and such that the collection of edgesT = ∪v∈V {v → π(v)} form a tree rooted at s with the list of ancestors of v in the tree giving acheapest path from v to s. Consider a graph G′(V,E) on the same set of vertices as G and with thesame set of edges as G. However, for some c > 0, the weights of edges of G′ are w′

uv = wuv+c. Nowsuppose that we apply Dijkstra’s algorithm with source s to the graph G′, and let T ′ be resultingtree. Is T = T ′? If yes, then give a proof. If no, then give a counter-example.Answer:No.Counterexample: Graph on the three nodes {s, a, b} with directed edges e1 = s → a, e2 = s → band e3 = b → a. The costs are w(e1) = 3, w(e2) = 1 and w(e3) = 1. The cheapest path from s toa is s → b → a at cost 2.Now add 10 to the costs of all edges to get w′(e1) = 13, w′(e2) = 11 and w′(e3) = 11. The cheapestpath from s to a is s → a at cost 13 (the path s → b → a has new cost 22 and is no longer shortestpath.)

(b) Let G(V,E) be an undirected connected weighted graph, where the weights of all edges aredistinct and positive: we > 0, for all e = {u, v} ∈ E. Let T be the mincost spanning tree of G.Consider a graph G′(V,E) on the same set of vertices as G and with the same set of edges as G.However, for some c > 0, the weights of edges of G′ are w′

e = we + c. Is T still a mincost spanningtree of G′? If yes, then give a proof. If no, then give a counter-example.Answer: Yes.Every spanning tree has (n − 1) edges. Thus, if we add the same c to the costs of all edges, thecost of every spanning tree will increase by the same amount, namely c(n− 1).

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 3, Sample Page 4/4

Problem 4, Approximation Algorithm, 25 points.Let G(V,E) be a directed unweighted graph, |V | = n. Let π be a total ordering of V , that is, eachvertex v ∈ V is assigned a unique integer π(v) in the range 1 to n.Let BACK(π) be the number of edges directed from higher order vertices to lower order vertices:

BACK(π) = |{u → v ∈ E : π(u) > π(v)}|.Let OPT=max{BACK(π) : π is a total ordering of V }.Now let π be an arbitrary total ordering of V , and let π′ be the reverse ordering,

ie (π(v) = 1 ⇔ π′(v) = n), (π(v) = 2 ⇔ π′(v) = n− 1), . . ., (π(v) = n ⇔ π′(v) = 1).Argue that max{BACK(π),BACK(π′)} ≥ OPT

2 .Answer:Let BACK = max{BACK(π),BACK(π′)}.Let FORWARD = |E| − BACK, the rest of the edges.

By choice, BACK > FORWARD, thus BACK ≥ |E|2 .

But |E| is the total number of edges of the graph, thus OPT ≤ |E|.The last two bounds combined give

BACK ≥ |E|2

≥ OPT

2

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Springl 2015, Quiz 3, Sample Page 3/4

Problem 3, Hardness Reductions, 25 points.

√ecSomewhatLongPath is the following decision problem. On input an undir ted, unweighted graphG(V,E), the output is YES if the length of the longest√path of G is at least |V |, while the output isNO if all paths of G have length strictly smaller than |V |. Show that HP≤PSomewhatLongPath.(HP is the standard Hamilton Path problem. On input an undirected, unweighted graph G(V,E),the output is YES if the length of the longest path of G is (|V | − 1), while the output is NO if allpaths of G have length strictly smaller than (|V | − 1).Answer:Let G(V,E) be the graph for which we want to decide if the longest path is of length (|V | − 1).We construct G′(V ′, E′) as follows:V ′ is V together with X isolated vertices, where x = |X|, and E′ = E.Let L be the length of the longest path in G.Of course, L is also the length of the longest path in G′.We need( x such that:

L < |V | − 1 ⇔ L <√|V |+ x

)and

(L = |V | − 1 ⇔ L ≥

√|V |+ x

).

The second condition implies (|V | − 1)2 − |V | ≥ x, so we may pick x = (|V | − 1)2 − |V |.It is easy to verify that this coice of x also satisfies the first condition. In particular, if L < |V | − 1then the maximum v ue that L can take is (|V | − 2). For x = (|V | − 1)2 − |V |, the first conditionbecomes (|V | − 2) <

al√|V |+ (|V | − 1)2 − |V | = (|V | − 1), obviously always true.

A

E

B

F

C

G

D

H

1

1

22

6 65

5 5

3 3

4 7

A

E

B

F

C

G

D

H

1

1

22

6 65

5 5

3 3

4 7

6

A

E

B

F

C

G

D

H

1

1

22

6 65

5 5

3 3

4 7

6

Page 10: Final Crib

A

E

B

F

C

G

D

H

1

1

22

6 65

5 5

3 3

4 7

d(A)=0

d(G)=?

d(C)=? d(D)=?

d(E)=? d(F)=?

d(B)=?

d(H)=?

A

E

B

F

10 20

1

2

A

E

B

F

10 20

1

G_1 G_2

e_1

e_2

e_k

T_1 T_2

A

E

B

F

10

2

1

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Practice Final Exam Page 1/10

Problem 1: 10 points.Consider the function Mystery defined below.

Mystery(n)if n > 1 then begin

print(”xxx”);Mystery(n/2);end

If we call Mystery(n), where n is an integer power of 3 and n > 1, how many x’s (as an exactfunction of n) does call Mystery(n) print? Justify your answer/show your work (solve recurrencesusing the substitution method, do not use O() notation.)

Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Practice Final Exam Page 2/10

Problem 2: 10 points *HW2 Problem 4.Let X(1, . . . , n) and Y (1, . . . , n) be two arrays, each containing n numbers already in sorted order.Describe an O(log n)-comparison algorithm to find the median of all 2n elements in arrays x andY . Justify your answer in correctness and running time.

Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Practice Final Exam Page 3/10

Problem 3: 10 points *HW4 Problem 3.Let a(1), . . . , a(n) be an unsorted input array of n distinct positive integers, where n is odd. Awigly arrangement of a is a permutation π of the input such that

a(π(1)) > a(π(2)) < a(π(3)) > a(π(4)) < a(π(5)) . . . a(π(n− 2)) > a(π(n− 1)) < a(π(n)).

For example, on input 100, 20, 2, 5, 200, 50, 40, 201, 300a wigly arrangement is 100, 20, 200, 2, 50, 5, 201, 40, 300since 100 > 20 < 200 > 2 < 50 > 5 < 201 > 40 < 300.

Give an O(n) comparison algorithm that outputs a wigly arrangement of an unsorted input arraya(1), . . . , a(n) of n distimct integers, where n is odd. You may give a simple description of thealgorithm (no pseudocode.) You should include a short argument of correctness and running time.

Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Practice Final Exam Page 4/10

Problem 4: 10 points *Quiz 2 Problem 3.Let G(V,E) be a directed graph. Give an O(|V | + |E|) algorithm that decides if there is a vertexv ∈ V that is reachable from every other vertex u ∈ V , ie there is a path from u to v, for all u ∈ V .Hint: First consider the case where G(V,E) is acyclic.

Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Practice Final Exam Page 5/10

Problem 5: 10 points.

Let G(V,E) be an undirected connected graph, with positive costs ce on its edges. Let G′(V,E)be the same graph as G, but the cost of each edge e whose original cost in G was ce is replaced in

2G′ by ce. For each of the following two statements, decide if it is true or false. If it is true, give ashort explanation. If it is false, give a counterexample.

(a) If T is a minimum cost spanning tree of G, then T is a minimum cost spanning tree of G′.(b) Where s and t are distinct vertices of V , if P is a minimum cost s− t path inG, then P is a

minimum cost s− t path inG′.Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Practice Final Exam Page 6/10

Problem 6: 10 points.

Let G(V,E) be an undirected connected graph with distinct postive costs on its edges. Let T bethe collection of all spanning trees of G. Let Tmin be the unique minimum cost spanning tree of

G. Give a polynomial time algorithm that finds a second minimum cost spanning tree of G, ie a

spanning tree whose cost is minimum among all trees in T \Tmin. You may give a simple descriptionof the algorithm (no pseudocode.) You should include a short argument of correctness and running

time.Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Practice Final Exam Page 7/10

Problem 7: 10 points.Let G(V,E) be a line with weights on its vertices. The MaxIS is the problem of finding anindependent set of G whose sum of the weights of the vertices is maximized over all independentsets of G. Give an example to show that the following ”heaviest first” greedy algorithm does notalways find an independent set of maximum total weight.

Start with S equal to the empty set.While some node remains in G

Pick a node vi of maximum weightAdd vi to SDelete vi and its neighbors from G

End WhileReturn S

Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Practice Final Exam Page 8/10

Problem 8: 10 points *HW7 Problem 5.

Let G(V,E) be a directed graph. Recall that a Hamilton Path in G is a path of length (|V | − 1)that vistis every vertex exactly once. Give a polynomial time algorithm that decides if a directedacyclic graph has a Hamilton Path. Justify correctness and running time of your answer.Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Practice Final Exam Page 9/10

Problem 9: 10 points.MAX3SAT is the following problem. On input a boolean formula in 3CNF, over n variables andcontainingm clauses, output a truth assignment that satisfies k clauses, and such that there is no

truth assignment that satisfies more than k clauses. Show that 3SAT≤PMAX3SAT.

Answer:

Last Name: ............................. First Name: .............................. Email: ..............................CS 3510 A, Spring 2015, Practice Final Exam Page 10/10

Problem 10: 10 points *In CLRS.Let G(V,E) be an undirected graph. Let OPT be the size of the smallest vertex cover of G. Givea polynomial time algorithm that finds a vertex cover of G that contains at most 2OPT vertices.Justify your answer.Answer:

≤∈