Exbook

You might also like

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

473 Algorithms - Exercises

Version 1.0

Sariel Har-Peled February 23, 2006

Contents
1 Exercises - Prerequisite 1.1 Graph Problems . . . . . . . . . . . 1.2 Recurrences . . . . . . . . . . . . . 1.3 Counting . . . . . . . . . . . . . . 1.4 O notation and friends . . . . . . . 1.5 Probability . . . . . . . . . . . . . . 1.6 Basic data-structures and algorithms 1.7 General proof thingies. . . . . . . . 1.8 Miscellaneous . . . . . . . . . . . . 5 5 7 9 9 11 13 15 16 17 17 17 18 18 18 18 19 19 19 19 19 20 20 20 20 21 21 21 22 22 22 23 24 24 25 25

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

Exercises - NP Completeness 2.1 Equivlance of optimization and decision problems . . . . . . . . . . . . 2.1.1 Beware of Greeks bearing gifts . . . . . . . . . . . . . . . . . . 2.1.2 Partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Showing problems are NP-C . . . . . . . . . . . . . . . . . . . 2.2.1 Graph Isomorphisms . . . . . . . . . . . . . . . . . . . . . . . 2.2.2 NP Completeness collection . . . . . . . . . . . . . . . . . . . 2.2.3 LONGEST-PATH . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.4 EXACT-COVER-BY-4-SETS . . . . . . . . . . . . . . . . . . 2.3 Solving special subcases of NP-C problems in polynomial time 2.3.1 Subset Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.2 2SAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.3 2-CNF-SAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.4 Hamiltonian Cycle Revisited . . . . . . . . . . . . . . . . . . . 2.3.5 Partition revisited . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.6 Why Mike can not get it. . . . . . . . . . . . . . . . . . . . . . 2.3.7 NP-Completeness Collection . . . . . . . . . . . . . . . . . . . 2.3.8 Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.9 Partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.10 Minimum Set Cover . . . . . . . . . . . . . . . . . . . . . . . 2.3.11 Bin Packing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.12 Knapsack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.13 Vertex Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.14 Bin Packing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.15 Minimum Set Cover . . . . . . . . . . . . . . . . . . . . . . . 2.3.16 k-Center . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.17 MAX 3SAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

2.3.18 2.3.19 2.3.20 2.3.21 2.3.22 2.3.23 2.3.24 2.3.25 2.3.26 3

Complexity . . . . . . . . . . . . . . . . . . . . . . . . 3SUM . . . . . . . . . . . . . . . . . . . . . . . . . . . Polynomially equivalent. . . . . . . . . . . . . . . . . . PLANAR-3-COLOR . . . . . . . . . . . . . . . . . . . DEGREE-4-PLANAR-3-COLOR . . . . . . . . . . . . Primality and Complexity . . . . . . . . . . . . . . . . Poly time subroutines can lead to exponential algorithms Polynomial time Hmiltonian path . . . . . . . . . . . . GRAPH-ISOMORPHISM . . . . . . . . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

26 26 26 26 27 27 27 27 28 29 29 29 29 30 30 31 31 32 32 32 33 33 33 33 34 34 34 35 36 36 36 36 36 37

Exercises - Network Flow 3.1 Network Flow . . . . . . . . . . . . . . . . . . . . . . . . 3.1.1 The good, the bad, and the middle. . . . . . . . . . 3.1.2 Ad hoc networks . . . . . . . . . . . . . . . . . . 3.1.3 Minimum Flow . . . . . . . . . . . . . . . . . . . 3.1.4 Prove infeasibility. . . . . . . . . . . . . . . . . . 3.1.5 Cellphones and services. . . . . . . . . . . . . . . 3.1.6 Follow the stars . . . . . . . . . . . . . . . . . . . 3.1.7 Flooding . . . . . . . . . . . . . . . . . . . . . . 3.1.8 Capacitation, yeh, yeh, yeh . . . . . . . . . . . . . 3.1.9 Fast Friends . . . . . . . . . . . . . . . . . . . . . 3.1.10 Even More Capacitation . . . . . . . . . . . . . . 3.1.11 Matrices . . . . . . . . . . . . . . . . . . . . . . . 3.1.12 Unique Cut . . . . . . . . . . . . . . . . . . . . . 3.1.13 Transitivity . . . . . . . . . . . . . . . . . . . . . 3.1.14 Census Rounding . . . . . . . . . . . . . . . . . . 3.1.15 Edge Connectivity . . . . . . . . . . . . . . . . . 3.1.16 Maximum Flow By Scaling . . . . . . . . . . . . 3.1.17 Perfect Matching . . . . . . . . . . . . . . . . . . 3.1.18 Number of augmneting paths . . . . . . . . . . . . 3.1.19 Minimum Cut Festival . . . . . . . . . . . . . . . 3.1.20 Independence Matrix . . . . . . . . . . . . . . . . 3.1.21 Scalar Flow Product . . . . . . . . . . . . . . . . 3.1.22 Go to school! . . . . . . . . . . . . . . . . . . . . 3.1.23 The Hopcroft-Karp Bipartite Matching Algorithm .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

Chapter 1

Exercises - Prerequisite
473G - Algorithms - Exercises Sariel Har-Peled February 23, 2006

Version: 1.0 This chapter include problems that are prequisite. Their main purpose is to check whether you are read to take the 473 algorithms class. If you do not have the prerequisites it is your responsibility to ll in the missing gaps in your education.

1.1

Graph Problems

1. A . [20 Points] A tournament is a directed graph with exactly one edge between every pair of vertices. (Think of the nodes as players in a round-robin tournament, where each edge points from the winner to the loser.) A Hamiltonian path is a sequence of directed edges, joined end to end, that visits every vertex exactly once. Prove that every tournament contains at least one Hamiltonian path.
2

A six-vertex tournament containing the Hamiltonian path 6 4 5 2 3 1.

2. G! G! [20 Points] A coloring of a graph G by colors is an assignment to each vertex of G a color which is an integer between 1 and , such that no two vertices that are connected by an edge have the same color. (a) [5 Points] Prove or disprove that if in a graph G the maximum degree is k, then the vertices of the graph can be colored using k + 1 colors. 5

(b) [5 Points] Provide an ecient coloring algorithm for a graph G with n vertices and m edges that uses at most k + 1 colors, where k is the maximum degree in G. What is the running time of you algorithm, if the graph is provided using adjacency lists. What is the running time of your algorithm if the graph is given with an adjacency matrix. (Note, that your algorithm should be as fast as possible.) (c) [5 Points] A directed graph G = (V, E) is a neat graph if there exist an ordering of the vertices of the graph V(G) = v1 , v2 , . . . , vn such that if the edge vi v j is in E(G) then i < j. Prove (by induction) that a DAG (i.e., directed acyclic graph) is a neat graph. (d) [5 Points] A cut (S , T ) in a directed graph G = (V, E) is a partition of V into two disjoint sets S and T . A cut is mixed if there exists s, s S and t, t T such that (s t) E and (t s ) E. Prove that if all the non-trivial cuts (i.e., neither S nor T are empty) are mixed then the graph is not a neat graph. 3. M C D [20 Points] In a land far far away (i.e., Canada), a mad cow decease was spreading among cow farms. The cow farms were, naturally, organized.as a n n grid. The epidemic started when m contaminated cows were dlivered to (some) of the farms. Once one cow in a farm has Mad Cow disease then all the cows in this farm get the disease. For a farm, if two or more of its neighboring farms have the disease than the cows in the farm would get the disease. A farm in the middle of the grid has four neighboring farms (two horizontally next to it, and two verticality next to it). We are interested in how the disease spread if we wait enough time. [5 Points] Show that if m = n then there is a scenario such that all the farms in the n n grid get contaminated. [15 Points] Prove that if m n 1 then (always) not all the farms are conaminated. 4. C . [10 Points] (a) Use induction to prove that in a simple graph, every walk between a pair of vertices, u, v, contains a path between u and v. Recall that a walk is a list of the form v0 , e1 , v1 , e2 , v2 , ..., ek , vk , in which ei has endpoints vi1 and vi . (b) Prove that a graph is connected if and only if for every partition of its vertices into two nonempty sets, there exists an edge that has endpoints in both sets. 5. [10 Points] Consider a 2n 2n chessboard with one (arbitrarily chosen) square removed, as in the following picture (for n = 3):

Prove that any such chessboard can be tiled without gaps or overlaps by L-shapes consisting of 3 squares each. 6. C [10 Points] (a) [5 Points] Let T 1 , T 2 and T 3 be three trees dened over the set of vertices {v1 , . . . , vn }. Prove that the graph G = T 1 T 2 T 3 is colorable using six colors (e is an edge of G if and only if it is an edge in one of trees T 1 , T 2 and T 3 ). (b) [5 Points] Describe an ecient algorithm for computing this coloring. What is the running time of your algorithm? 7. Professor George OJungle has a favorite 26-node binary tree, whose nodes are labeled by letters of the alphabet. The preorder and postorder sequences of nodes are as follows: preorder: M N H C R S K W T G D X I Y A J P O E Z V B U L Q F postorder: C W T K S G R H D N A O E P J Y Z I B Q L F U V X M Draw Professor OJungles binary tree, and give the inorder sequence of nodes.

1.2

Recurrences

1. R [20 Points] Solve the following recurrences. State tight asymptotic bounds for each function in the form ( f (n)) for some recognizable function f (n). You do not need to turn in proofs (in fact, please dont turn in proofs), but you should do them anyway just for practice. Assume reasonable but nontrivial base cases if none are supplied. More exact solutions are better. (a) [2 Points] A(n) = A n/3 + log n + n (b) [2 Points] B(n) = min 3 + B(k) + B(n k) .
0<k<n

(c) [2 Points] C(n) = 3C( n/2 5) + n/ log n (d) [2 Points] D(n) = 1) + 1 (e) [2 Points] E(n) = E( 3n/4 ) + n
n n3 D(n

(f) [2 Points] F(n) = F( log n ) + log n (g) [2 Points] G(n) = n + n G( n ) (h) [2 Points] H(n) = log(H(n 1)) + 1 (i) [2 Points] I(n) = 5I n +1 (j) [2 Points] J(n) = 3J(n/4) + 1 2. R II [20 Points] Solve the following recurrences. State tight asymptotic bounds for each function in the form ( f (n)) for some recognizable function f (n). You do not need to turn in proofs (in fact, please dont turn in proofs), but you should do them anyway just for practice. Assume reasonable but nontrivial base cases if none are supplied. More exact solutions are better. 7

(a) [1 Points] A(n) = A n/3 + 5 + log n + n log log n (b) [1 Points] B(n) = min 3 + B(k) + B(n k) .
0<k<n

(c) [1 Points] C(n) = 3C( n/2 5) + n/ log n (d) [1 Points] D(n) = 1) + 1 (e) [1 Points] E(n) = E( 3n/4 ) + 1/ n
n n5 D(n

(f) [1 Points] F(n) = F( log2 n ) + log n (g) [1 Points] G(n) = n + 7 n G( n ) (h) [1 Points] H(n) = log2 (H(n 1)) + 1 (i) [1 Points] I(n) = I n1/4 + 1 (j) [1 Points] J(n) = J n n/ log n + 1 3. R III [20 Points] Solve the following recurrences. State tight asymptotic bounds for each function in the form ( f (n)) for some recognizable function f (n). You do not need to turn in proofs (in fact, please dont turn in proofs), but you should do them anyway just for practice. Assume reasonable but nontrivial base cases if none are supplied. (a) A(n) = A(n/2) + n (b) B(n) = 2B(n/2) + n (c) C(n) = n + (d) D(n) = (f) F(n) =
1 2

C(n 1) + C(3n/4) D(k) + D(n k) + n

n/3<k<2n/3

max

(e) E(n) = 2E(n/2) + n/ lg n


F(n1) F(n2) ,

where F(1) = 1 and F(2) = 2. [Hint:


1 2

(g) G(n) = G(n/2) + G(n/4) + G(n/6) + G(n/12) + n (h) H(n) = n + n H( n) (j) J(n) = 8J(n 1) 15J(n 2) + 1

1 4

1 6

1 12

= 1.]

(i) I(n) = (n 1)(I(n 1) + I(n 2)), where F(0) = F(1) = 1

4. [10 Points] Evaluate the following summations; simplify your answers as much as possible. Signicant partial credit will be given for answers in the form ( f (n)) for some recognizable function f (n).
n i i

(a) [2 Points]
i=1 j=1 k= j n i i

1 i 1 j 1 k 8

(b) [2 Points]
i=1 j=1 k= j n i i

(c) [2 Points]
i=1 j=1 k= j

(d) [2 Points]
i=1 j=1 k=1 n i j

1 k 1 jk

(e) [2 Points]
i=1 j=1 k=1

5. This problem asks you to simplify some recursively dened boolean formulas as much as possible. In each case, prove that your answer is correct. Each proof can be just a few sentences long, but it must be a proof. (a) Suppose 0 = p, 1 = q, and n = (n2 n1 ) for all n 2. Simplify n as much as possible. [Hint: What is 5 ?] (b) Suppose 0 = p, 1 = q, and n = (n2 n1 ) for all n 2. Simplify n as much as possible. [Hint: What is 5 ?] (c) Suppose 0 = p, 1 = q, and n = (n2 n1 ) for all n 2. Simplify n as much as possible. [Hint: What is 5 ?] (d) Suppose 0 = p, 1 = q, and n = (n2 n1 ) for all n 2, where is some boolean function with two arguments. Find a boolean function such that n = m if and only if n m is a multiple of 4. [Hint: There is only one such function.]

1.3
1.

Counting
(a) A domino is a 2 1 or 1 2 rectangle. How many dierent ways are there to completely ll a 2 n rectangle with n dominos? Set up a recurrence relation and give an exact closed-form solution. (b) A slab is a three-dimensional box with dimensions 1 2 2, 2 1 2, or 2 2 1. How many dierent ways are there to ll a 2 2 n box with n slabs? Set up a recurrence relation and give an exact closed-form solution.

A 2 10 rectangle lled with ten dominos, and a 2 2 10 box lled with ten slabs.

1.4 O notation and friends


1. S [20 Points] Sort the following 25 functions from asymptotically smallest to asymptotically largest, indicating ties if there are any. You do not need to turn in proofs (in fact, please dont turn in proofs), but you should 9

do them anyway just for practice. n4.5 (n 1)4.5 cos n + 2 lg 22


2n

n lg(lg n) 2lg n n3/(2 lg n) nlg lg n

n2.1 (lg n)! e n 12 + lg lg(n) lg(201) n

lg (n/8) (lg n)lg n


n i=1 i

1 + lg lg lg n n5
n 2 i=1 i 1 15n 1 + 154 n(lg n)4

n7/(2n) n1/ lg lg n

(lg(2 + n))lg n n1/125

To simplify notation, write f (n) g(n) to mean f (n) = o(g(n)) and f (n) g(n) to mean f (n) = (g(n)). For example, the functions n2 , n, n , n3 could be sorted either as n n2 n n3 or as 2 2
n 2 n n3 . [Hint: When considering two functions f () and g() it is sometime useful to 2 n consider the functions ln f () and ln g().]

2. S II [20 Points] Sort the following 25 functions from asymptotically smallest to asymptotically largest, indicating ties if there are any. You do not need to turn in proofs (in fact, please dont turn in proofs), but you should do them anyway just for practice. n5.5 (n 1)5.5 cos n + 2 lg 22
n

n lg(lg n) 2lg

n2.2 lg(n!)
n n

lg (n/7) (lg

1 + lg lg n n4
n 1 i=1 i2 1 154n 1 + 154 n(lg n)3

n)lg n

n 1 i=1 i

n3/(2n) n1/ lg lg n

n3/(2 lg n) nlg lg n

lg lg(n!) lg(200) n

(lg(7 + n))lg n n1/1234

To simplify notation, write f (n) g(n) to mean f (n) = o(g(n)) and f (n) g(n) to mean f (n) = n3 or as n2 n (g(n)). For example, the functions n2 , n, n , n3 could be sorted either as n 2 2 n
n 2

n2

n3 .

3. O . [10 Points] (a) Let fi (n) be a sequence of functions, such that for every i, fi (n) = o( n) (namely, limn 0). Let g(n) =
n i=1 fi (n). fi (n) n

Prove or disprove: g(n) =

o(n3/2 ).

(b) If f1 (n) = O(g1 (n)) and f2 (n) = O(g2 (n)). Prove or disprove: f1 (n) + f2 (n) = O(g1 (n) + g2 (n)) f1 (n) f2 (n) = O(g1 (n) g2 (n)) f1 (n) f2 (n) = O(g1 (n)g2 (n) ) 4. S . (a) Prove that 2
lg n + lg n

= (n2 ).
lg n
lg lg n

(b) Prove or disprove: 2 (c) Prove or disprove: 22

=2

lg n

. . 10

= 22

lg lg n

(d) Prove or disprove: If f (n) = O(g(n)), then log( f (n)) = O(log(g(n))). (e) Prove or disprove: If f (n) = O(g(n)), then 2 f (n) = O(2g(n) ). (f) Prove that logk n = o(n1/k ) for any positive integer k.

1.5

Probability

1. [20 Points] There are n balls (numbered from 1 to n) and n boxes (numbered from 1 to n). We put each ball in a randomly selected box. (a) [4 Points] A box may contain more than one ball. Suppose X is the number on the box that has the smallest number among all nonempty boxes. What is the expectation of X? (b) [4 Points] What is the expected number of bins that have exactly one ball in them? (Hint: Compute the probability of a specic bin to contain exactly one ball and then use some properties of expectation.) (c) [8 Points] We put the balls into the boxes in such a way that there is exactly one ball in each box. If the number written on a ball is the same as the number written on the box containing the ball, we say there is a match. What is the expected number of matches? (d) [4 Points] What is the probability that there are exactly k matches? (1 k < n) [Hint: If you have to appeal to intuition or common sense, your answers are probably wrong!] 2. I S [20 Points] There is an array A with n unsorted distinct numbers in it. IS(A) sorts the array using an iterative algorithm. In each iteration, it picks randomly (and uniformly) two indices i, j in the ranges {1, . . . , n}. Next, if A[min(i, j)] > A[max(i, j)] it swaps A[i] and A[ j]. The algorithm magically stop once the array is sorted. (a) [5 Points] Prove that after (at most) n! swaps performed by the algorithm, the array A is sorted. (b) [5 Points] Prove that after at most (say) 6n3 swaps performed by the algorithm, the array A is sorted. (There might be an easy solution, but I dont see it.) (c) [5 Points] Prove that if A is not sorted, than the probability for a swap in the next iteration is at least 2/n2 . (d) [5 Points] Prove that if A is not sorted, then the expected number of iterations till the next swap is n2 /2. [Hint: use geometric random variable.] (e) [5 Points] Prove that the expected number of iterations performed by the algorithm is O(n5 ). [Hint: Use linearity of expectation.] 3. [10 Points] A random walk is a walk on a graph G, generated by starting from a vertex v0 = v V(G), and in the i-th stage, for i > 0, randomly selecting one of the neighbors of vi1 and setting vi to be this vertex. A walk v0 , v1 , . . . , vm is of length m. 11

(a) For a vertex u V(G), let Pu (m, v) be the probability that a random walk of length m, starting from u, visits v (i.e., vi = v for some i). Prove that a graph G with n vertices is connected, if and only if, for any two vertices u, v V(G), we have Pu (n 1, v) > 0. (b) Prove that a graph G with n vertices is connected if and only if for any pair of vertices u, v V(G), we have limm Pu (m, v) = 1. 4. R E [10 Points] You are in a shop trying to buy green tea. There n dierent types of green tea that you are considering: T 1 , . . . , T n . You have a coin, and you decide to randomly choose one of them using random coin ips. Because of the dierent prices of the dierent teas, you decide that you want to choose the ith tea with probability pi (of course, n pi = 1). i=1 Describe an algorithm that chooses a tea according to this distribution, using only coin ips. Compute the expected number of coin ips your algorithm uses. (Your algorithm should minimize the number of coin ips it uses, since if you ip coins too many times in the shop, you might be arrested.) 5. [20 Points] There are n balls (numbered from 1 to n) and n boxes (numbered from 1 to n). We put each ball in a randomly selected box. (a) [8 Points] A box may contain more than one ball. Suppose X is the number on the box that has the smallest number among all nonempty boxes. What is the expectation of X? (Its OK to just give a big expression.) (b) [8 Points] We put the balls into the boxes in such a way that there is exactly one ball in each box. If the number written on a ball is the same as the number written on the box containing the ball, we say there is a match. What is the expected number of matches? (c) [4 Points] What is the probability that there are exactly k matches? (1 k < n) [Hint: If you have to appeal to intuition or common sense, your answers are probably wrong!] 6. R? [10 Points] We toss a fair coin n times. What is the expected number of runs? Runs are consecutive tosses with the same result. For example, the toss sequence HHHTTHTH has 5 runs. 7. Penn and Teller have a special deck of fty-two cards, with no face cards and nothing but clubsthe ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, . . . , 52 of clubs. (Theyre big cards.) Penn shues the deck until each each of the 52! possible orderings of the cards is equally likely. He then takes cards one at a time from the top of the deck and gives them to Teller, stopping as soon as he gives Teller the ve of clubs. (a) On average, how many cards does Penn give Teller? (b) On average, what is the smallest-numbered card that Penn gives Teller? (c) On average, what is the largest-numbered card that Penn gives Teller? [Hint: Solve for an n-card deck and then set n = 52.] In each case, give exact answers and prove that they are correct. If you have to appeal to intuition or common sense, your answers are probably wrong! 12

8. Alice and Bob each have a fair n-sided die. Alice rolls her die once. Bob then repeatedly throws his die until he rolls a number at least as big as the number Alice rolled. Each time Bob rolls, he pays Alice $1. (For example, if Alice rolls a 5, and Bob rolls a 4, then a 3, then a 1, then a 5, the game ends and Alice gets $4. If Alice rolls a 1, then no matter what Bob rolls, the game will end immediately, and Alice will get $1.) Exactly how much money does Alice expect to win at this game? Prove that your answer is correct. If you have to appeal to intuition or common sense, your answer is probably wrong!

1.6

Basic data-structures and algorithms


Describe a data structure that supports storing temperatures. The operations on the data structure are as follows: Insert(t, d) Insert the temperature t that was measured on date d. Each temperature is a real number between 100 and 150. For example, insert(22, 1/20/03). Average(d1 , d2 ) report what is the average of all temperatures that were measured between date d1 and date d2 . Each operation should take time O(log n), where n is the number of dates stored in the data structure. You can assume that a date is just an integer which species the number of days since the rst of January 1970.

1. [10 Points]

2. B . [10 Points] Suppose we have a binary search tree. You perform a long sequence of operations on the binary tree (insertion, deletions, searches, etc), and the maximum depth of the tree during those operations is at most h. Modify the binary search tree T so that it supports the following operations. Implementing some of those operations would require you to modify the information stored in each node of the tree, and the way insertions/deletions are being handled in the tree. For each of the following, describe separately the changes made in detail, and the algorithms for answering those queries. (Note, that under the modied version of the binary search tree, insertion and deletion should still take O(h) time, where h is the maximum height of the tree during all the execution of the algorithm.) (a) [2 Points] Find the smallest element stored in T in O(h) time. (b) [2 Points] Given a query k, nd the k-th smallest element stored in T in O(h) time. (c) [3 Points] Given a query [a, b], nd the number of elements stored in T with their values being in the range [a, b], in O(h) time. (d) [3 Points] Given a query [a, b], report (i.e., printout) all the elements stored in T in the range [a, b], in O(h + u) time, where u is the number of elements printed out. 3. E . [10 Points] Prove that for any nonnegative parameters a and b, the following algorithms terminate and produce identical output. Also, provide bounds on the running times of those algorithms. Can you imagine any reason why WE would be preferable to FE? 13

SlowEuclid(a, b) : if b > a return SE(b, a) else if b = 0 return a else return SE(b, a b)

FastEuclid(a, b) : if b = 0 return a else return FE(b, a mod b)

WeirdEuclid(a, b) : if b = 0 return a if a = 0 return b if a is even and b is even return 2WE(a/2, b/2) if a is even and b is odd return WE(a/2, b) if a is odd and b is even return WE(a, b/2) if b > a return WE(b a, a) else return WE(a b, b) 4. T . Every year, upon their arrival at Hogwarts School of Witchcraft and Wizardry, new students are sorted into one of four houses (Gryndor, Huepu, Ravenclaw, or Slytherin) by the Hogwarts Sorting Hat. The student puts the Hat on their head, and the Hat tells the student which house they will join. This year, a failed experiment by Fred and George Weasley lled almost all of Hogwarts with sticky brown goo, mere moments before the annual Sorting. As a result, the Sorting had to take place in the basement hallways, where there was so little room to move that the students had to stand in a long line. After everyone learned what house they were in, the students tried to group together by house, but there was too little room in the hallway for more than one student to move at a time. Fortunately, the Sorting Hat took CS Course many years ago, so it knew how to group the students as quickly as possible. What method did the Sorting Hat use? (a) More formally, you are given an array of n items, where each item has one of four possible values, possibly with a pointer to some additional data. Describe an algorithmx that rearranges the items into four clusters in O(n) time using only O(1) extra space.
G
Harry

H
Ann

R
Bob

R
Tina

G
Chad

G
Bill

R
Lisa

G
Ekta

H
Bart

H
Jim

R
John

S
Jeff

R
Liz

R
Mary

H
Dawn

G
Nick

S
Kim

H
Fox

G
Dana

G
Mel

G
Harry

G
Ekta

G
Bill

G
Chad

G
Nick

G
Mel

G
Dana

H
Fox

H
Ann

H
Jim

H
Dawn

H
Bart

R
Lisa

R
Tina

R
John

R
Bob

R
Liz

R
Mary

S
Kim

S
Jeff

Since youve read the Homework Instructions, you know what the phrase describe an algorithm means. Right?

14

(b) Describe an algorithm for the case where there are k possible values (i.e., 1, 2, . . . , k) that rearranges the items using only O(log k) extra space. How fast is your algorithm? (A faster algorithm would get more credit) (c) Describe a faster algorithm (if possible) for the case when O(k) extra space is allowed. How fast is your algorithm? (d) Provide a fast algorithm that uses only O(1) additional space for the case where there are k possible values.

5. S ? Suppose you have a pointer to the head of singly linked list. Normally, each node in the list only has a pointer to the next element, and the last nodes pointer is N. Unfortunately, your list might have been corrupted by a bug in somebody elses codey , so that the last node has a pointer back to some other node in the list instead.

Top: A standard linked list. Bottom: A corrupted linked list.

Describe an algorithm that determines whether the linked list is corrupted or not. Your algorithm must not modify the list. For full credit, your algorithm should run in O(n) time, where n is the number of nodes in the list, and use O(1) extra space (not counting the list itself).

1.7

General proof thingies.

1. Cz [20 Points] During the sweetcorn festival in Urbana, you had been kidnapped by an extreme anti corn organization called Al Corona. To punish you, they give you several sacks with a total of (n + 1)n/2 cobs of corn in them, and an innite supply of empty sacks. Next, they ask you to play the following game: At every point in time, you take a cob from every non-empty sack, and you put this set of cobs into a new sack. The game terminates when you have n non-empty sacks, with the ith sack having i cobs in it, for i = 1, . . . , n. For example, if we started with {1, 5} (i.e., one sack has 1 cob, the other 5), we would have the following sequence of steps: {2, 4}, {1, 2, 3} and the game ends. (a) [5 Points] Prove that the game terminates if you start from a conguration where all the cobs are in a single sack. (b) [5 Points] Provide a bound, as tight as possible, on the number of steps in the game till it terminates in the case where you start with a single sack. (c) [5 Points] (hard) Prove that the game terminates if you start from an arbitrary conguration where the cobs might be in several sacks.
y z

After all, your code is always completely 100% bug-free. Isnt that right, Mr. Gates? Cornication - Conversion into, or formation of, horn; a becoming like horn. Source: Websters Revised Unabridged Dictionary

15

(d) [5 Points] Provide a bound, as tight as possible, on the number of steps in the game till it terminates in the general case. 2. Recall the standard recursive denition of the Fibonacci numbers: F0 = 0, F1 = 1, and Fn = Fn1 + Fn2 for all n 2. Prove the following identities for all positive integers n and m. (a) Fn is even if and only if n is divisible by 3. (b) (c)
n i=0 F i = F n+2 2F Fn n+1 F n1

1 = (1)n+1

(d) If n is an integer multiple of m, then Fn is an integer multiple of Fm . 3. S . (a) Prove the following identity by induction: 2n = n
n k=0

n n . k nk

(b) Give a non-inductive combinatorial proof of the same identity, by showing that the two sides of the equation count exactly the same thing in two dierent ways. There is a correct one-sentence proof.

1.8
1.

Miscellaneous
An ant is walking along a rubber band, starting at the left end. Once every second, the ant walks one inch to the right, and then you make the rubber band one inch longer by pulling on the right end. The rubber band stretches uniformly, so stretching the rubber band also pulls the ant to the right. The initial length of the rubber band is n inches, so after t seconds, the rubber band is n + t inches long.

t=0

t=1

t=2

Every second, the ant walks an inch, and then the rubber band is stretched an inch longer.

(a) How far has the ant moved after t seconds, as a function of n and t? Set up a recurrence and (for full credit) give an exact closed-form solution. [Hint: What fraction of the rubber bands length has the ant walked?] (b) How long does it take the ant to get to the right end of the rubber band? For full credit, give an answer of the form f (n) + (1) for some explicit function f (n).

16

Chapter 2

Exercises - NP Completeness
473G - Algorithms - Exercises Sariel Har-Peled February 23, 2006

Version: 1.02

2.1
2.1.1

Equivlance of optimization and decision problems


Beware of Greeks bearing gifts

(The expression beware of Greeks bearing gifts is Based on Virgils Aeneid: Quidquid id est, timeo Danaos et dona ferentes, which means literally Whatever it is, I fear Greeks even when they bring gifts..) The faun, the brother of the P satyr, came to visit you on labor day, and left you with two black boxes. 1. [10 Points] The rst black box, was a black box that can solves the following decision problem in polynomial time: Problem: M T C Instance: A nite set A of possible diagnoses, a collection C of subsets of A, representing binary tests, and a positive integer J |C|. Question: Is there a subcollection C C with |C | J such that, for every pair ai , a j of possible diagnoses from A, there is some test c C for which ai , a j c = 1 (that is, a test c that distinguishes between ai and a j )? Show how to use this black box, how to solve in polynomial time the optimization version of this problem (i.e., nding and outputting the smallest possible set C ). 2. [10 Points] The second box was a black box for solving S I. Problem: S I Instance: Two graphs, G = (V1 , E1 ) and H = (V2 , E2 ). Question: Does G contain a subgraph isomorphic to H, that is, a subset V V1 and a subset E E1 such that |V| = |V2 |, |e| = |E2 |, and there exists a one-to-one function f : V2 V satisfying {u, v} E2 if and only if { f (u), f (v)} E? Show how to use this black box, to compute the subgraph isomorphism (i.e., you are given G and H, and you have to output f ) in polynomial time. 17

2.1.2

Partition

The P satyr, the uncle of the deduction fairy, had visited you on winter break and gave you, as a token of appreciation, a black-box that can solve P in polynomial time (note that this black box solves the decision problem). Let S be a given set of n integer numbers. Describe a polynomial time algorithm that computes, using the black box, a partition of S if such a solution exists. Namely, your algorithm should output a subset T S , such that s= s.
sT sS \T

2.2
2.2.1

Showing problems are NP-C


Graph Isomorphisms

1. [5 Points] Show that the following problem is NP-C. Problem: SUBGRAPH ISOMORPHISM Instance: Graphs G = (V1 , E1 ), H = (V2 , E2 ). Question: Does G contain a subgraph isomorphic to H, i.e., a subset V V1 and a subset E E1 such that |V| = |V2 |, |E| = |E2 |, and there exists a one-to-one function f : V2 V satisfying {u, v} E2 if and only if { f (u), f (v)} E? 2. [5 Points] Show that the following problem is NP-C. Problem: LARGEST COMMON SUBGRAPH Instance: Graphs G = (V1 , E1 ), H = (V2 , E2 ), positive integer K. Question: Do there exists subsets E1 E1 and E2 E2 with |E1 | = |E2 | K such that the two subgraphs G = (V1 , E1 ) and H = (V2 , E2 ) are isomorphic?

2.2.2

NP Completeness collection

1. [5 Points] Problem: MINIMUM SET COVER Instance: Collection C of subsets of a nite set S and an integer k. Question: Are there k sets S 1 , . . . , S k in C such that S k S i ? i=1 2. [5 Points] Problem: BIN PACKING Instance: Finite set U of items, a size s(u) Z + for each u U, an integer bin Z capacity B, and a positive integer K. Question: Is there a partition of U int disjoint sets U1 , . . . , U K such that the sum of the sizes of the items inside each Ui is B or less? 3. [5 Points] 18

Problem: TILING Instance: Finite set RECT S of rectangles and a rectangle R in the plane. Question: Is there a way of placing the rectangles of RECT S inside R, so that no pair of the rectangles intersect, and all the rectangles have their edges parallel of the edges of R? 4. [5 Points] Problem: HITTING SET Instance: A collection C of subsets of a set S , a positive integer K. Question: Does S contain a hitting set for C of size K or less, that is, a subset S S with |S | K and such that S contains at least one element from each subset in C.

2.2.3

LONGEST-PATH

Show that the problem of deciding whether an unweighted undirected graph has a path of length greater than k is NP-C.

2.2.4

EXACT-COVER-BY-4-SETS

The EXACT-COVER-BY-3-SETS problem is denes as the following: given a nite set X with |X| = 3q and a collection C of 3-element subsets of X, does C contain an exact cover for X, that is, a subcollection C C such that every element of X occurs in exactly one member of C ? Given that EXACT-COVEqR-BY-3-SETS is NP-C, show that EXACT-COVER-BY-4-SETS is also NP-C.

2.3
2.3.1

Solving special subcases of NP-C problems in polynomial time


Subset Sum
Problem: S S Instance: S - set of positive integers, t: - an integer number Question: Is there a subset X S such that x=t?
xX

Given an instance of S S, provide an algorithm that solves it in polynomial time in n, and M, where M = max sS s. Why this does not imply that P = NP?

2.3.2 2SAT
Given an instance of 2SAT (this is a problem similar to 3SAT where every clause has at most two variables), one can try to solve it by backtracking. 19

1. [1 Points] Prove that if a formula F is not satisable, and F is formed by adding clauses to F , then the formula F is not satisable. (Duh?) We refer to F as a subformula of F. 2. [3 Points] Given an assignment xi b to one of the variables of a 2SAT instance F (where b is either 0 or 1), describe a polynomial time algorithm that computes a subformula F of F, such that (i) F does not have the variable xi in it, (ii) F is a 2SAT formula, (iii) F is satisable i there is a satisfying assignment for F with xi = b, and (iv) F is a subformula of F. How fast is your algorithm? 3. [6 Points] Describe a polynomial time algorithm that solves the 2SAT problem (using (b)). How fast is your algorithm?

2.3.3

2-CNF-SAT

Prove that deciding satisability when all clauses have at most 2 literals is in P.

2.3.4

Hamiltonian Cycle Revisited

Let Cn denote the cycle graph over n vertices (i.e., V(Cn ) = {1, . . . , n}, and E(Cn ) = {{1, 2} , {2, 3} , . . . , {n 1, n} {n, 1}}). k k Let Cn denote the graph where {i, j} E(Cn ) i i and j are in distance at most k in Cn . k Let G be a graph, such that G is a subgraph of Cn , where k is a small constant. Describe a polynomial time algorithm (in n) that outputs a Hamiltonian cycle if such a cycle exists in G. How fast is your algorithm, as a function of n and k?

2.3.5

Partition revisited

Let S be an instance of partition, such that n = |S |, and M = max sS s. Show a polynomial time (in n and M) algorithm that solves partition.

2.3.6

Why Mike can not get it.

[10 Points] Problem: N-3SAT Instance: A 3CNF formula F Question: Is F not satisable? (Namely, for all inputs for F, it evaluates to FALSE.) 1. Prove that N-3SAT is co NP. 2. Here is a proof that N-3SAT is in NP: If the answer to the given instance is Yes, we provide the following proof to the verier: We list every possible assignment, and for each assignment, we list the output (which is FALSE). Given this proof, of length L, the verier can easily verify it in polynomial time in L. QED. What is wrong with this proof? 3. Show that given a black-box that can solves N-3SAT, one can nd the satisfying assignment of a formula F in polynomial time, using polynomial number of calls to the black-box (if such an assignment exists). 20

2.3.7

NP-Completeness Collection

[20 Points] Prove that the following problems are NP-Complete. Problem: MINIMUM SET COVER 1. Instance: Collection C of subsets of a nite set S and an integer k. Question: Are there k sets S 1 , . . . , S k in C such that S k S i ? i=1 Problem: HITTING SET 2.

Instance: A collection C of subsets of a set S , a positive integer K. Question: Does S contain a hitting set for C of size K or less, that is, a subset S S with |S | K and such that S contains at least one element from each subset in C. Problem: H P 3. Instance: Graph G = (V, E) Question: Does G contains a Hamiltonian path? (Namely a path that visits all vertices of G.) Problem: M D S T4. Instance: Graph G = (V, E) and integer k Question: Does G contains a spanning tree T where every node in T is of degree at most k?

2.3.8

Independence

[10 Points] Let G = (V, E) be an undirected graph over n vertices. Assume that you are given a numbering : V {1, . . . , n} (i.e., every vertex have a unique number), such that for any edge i j E, we have |(i) ( j)| 20. Either prove that it is NP-Hard to nd the largest independent set in G, or provide a polynomial time algorithm.

2.3.9

Partition

We already know the following problem is NP-C: Problem: SUBSET SUM Instance: A nite set A and a size s(a) Z + for each a A, an integer B. Z Question: Is there a subset A A such that aA s(a) = B? Now lets consider the following problem: Problem: PARTITION Instance: A nite set A and a size s(a) Z + for each a A. Z Question: Is there a subset A A such that s(a) =
aA aA\A

s(a)?

Show that PARTITION is NP-C. 21

2.3.10

Minimum Set Cover

[15 Points] Problem: MINIMUM SET COVER Instance: Collection C of subsets of a nite set S and an integer k. Question: Are there k sets S 1 , . . . , S k in C such that S k S i ? i=1 1. [5 Points] Prove that MINIMUM SET COVER problem is NP-C 2. [5 Points] Prove that the following problem is NP-C. Problem: HITTING SET Instance: A collection C of subsets of a set S , a positive integer K. Question: Does S contain a hitting set for C of size K or less, that is, a subset S S with |S | K and such that S contains at least one element from each subset in C. 3. [5 Points] Hitting set on the line Given a set I of n intervals on the real line, show a O(n log n) time algorithm that computes the smallest set of points X on the real line, such that for every interval I I there is a point p X, such that p I.

2.3.11

Bin Packing
Problem: BIN PACKING Instance: Finite set U of items, a size s(u) Z + for each u U, an integer bin capacity Z B, and a positive integer K. Question: Is there a partition of U into disjoint sets U1 , . . . , U K such that the sum of the sizes of the items inside each Ui is B or less?

1. [5 Points] Show that the BIN PACKING problem is NP-C 2. [5 Points] Show that the following problem is NP-C. Problem: TILING Instance: Finite set RECT S of rectangles and a rectangle R in the plane. Question: Is there a way of placing all the rectangles of RECT S inside R, so that no pair of the rectangles intersect in their interior, and all the rectangles have their edges parallel of the edges of R?

2.3.12

Knapsack

1. [5 Points] Show that the following problem is NP-C. Problem: KNAPSACK Instance: A nite set U, a size s(u) Z + and a value v(u) Z + for each Z Z u U, a size constraint B Z +, and a value goal K Z +. Z Z Question: Is there a subset U U such that uU s(u) B and uU v(u) B. 22

2. [5 Points] Show that the following problem is NP-C. Problem: MULTIPROCESSOR SCHEDULING Instance: A nite set A of tasks, a length l(a) Z + for each a A, a number Z + + m Z of processors, and a deadline D Z . Z Z Question: Is there a partition A = A1 A2 Am of A into m disjoint sets such that max{ aAi l(a) : 1 i m} D? 3. Scheduling with prots and deadlines Suppose you have one machine and a set of n tasks a1 , a2 , ..., an . Each task a j has a processing time t j , a prot p j , and a deadline d j . The machine can process only one task at a time, and task a j must run uninterruptedly for t j consecutive time units to complete. If you complete task a j by its deadline d j , you receive a prot p j . But you receive no prot if you complete it after its deadline. As an optimization problem, you are given the processing times, prots and deadlines for a set of n tasks, and you wish to nd a schedule that completes all the tasks and returns the greatest amount of prot. (a) [3 Points] State this problem as a decision problem. (b) [2 Points] Show that the decision problem is NP-C.

2.3.13

Vertex Cover
Problem: VERTEX COVER Instance: A graph G = (V, E) and a positive integer K |V|. Question: Is there a vertex cover of size K or less for G, that is, a subset V V such that |V | K and for each edge {u, v} E, at least one of u and v belongs to V ?

1. Show that VERTEX COVER is NP-C. Hint: Do a reduction from INDEPENDENT SET to VERTEX COVER. 2. Show a polynomial approximation algorithm to the V-C problem which is a factor 2 approximation of the optimal solution. Namely, your algorithm should output a set X V, such that X is a vertex cover, and |C| 2Kopt , where Kopt is the cardinality of the smallest vertex cover of G.{ 3. Present a linear time algorithm that solves this problem for the case that G is a tree. 4. For a constant k, a graph G is k-separable, if there are k vertices of G, such that if we remove them from G, each one of the remaining connected components has at most (2/3)n vertices, and furthermore each one of those connected components is also k-separable. (More formally, a graph G = (V, E) is k-separable, if for any subset of vertices S V, there exists a subset M S , such that each connected component of GS \M has at most (2/3)|S | vertices, and |M| k.) Show that given a graph G which is k-separable, one can compute the optimal VERTEX COVER in nO(k) time.
It was very recently shown (I. Dinur and S. Safra. On the importance of being biased. Manuscript. http://www.math.ias.edu/iritd/mypapers/vc.pdf, 2001.) that doing better than 1.3600 approximation to VERTEX COVER is NP-Hard. In your free time you can try and improve this constant. Good luck.
{

23

2.3.14

Bin Packing
Problem: BIN PACKING Instance: Finite set U of items, a size s(u) Z + for each u U, an integer bin capacity Z B, and a positive integer K. Question: Is there a partition of U int disjoint sets U1 , . . . , U K such that the sum of the sizes of the items inside each Ui is B or less?

1. Show that the BIN PACKING problem is NP-C 2. In the optimization variant of BIN PACKING one has to nd the minimum number of bins needed to contain all elements of U. Present an algorithm that is a factor two approximation to optimal solution. Namely, it outputs a partition of U into M bins, such that the total size of each bin is at most B, and M kopt , where kopt is the minimum number of bins of size B needed to store all the given elements of U. 3. Assume that B is bounded by an integer constant m. Describe a polynomial algorithm that computes the solution that uses the minimum number of bins to store all the elements. 4. Show that the following problem is NP-C. Problem: TILING Instance: Finite set RECT S of rectangles and a rectangle R in the plane. Question: Is there a way of placing the rectangles of RECT S inside R, so that no pair of the rectangles intersect, and all the rectangles have their edges parallel of the edges of R? 5. Assume that RECT S is a set of squares that can be arranged as to tile R completely. Present a polynomial time algorithm that computes a subset T RECT S, and a tiling of T , so that this tiling of T covers, say, 10% of the area of R.

2.3.15

Minimum Set Cover


Problem: MINIMUM SET COVER Instance: Collection C of subsets of a nite set S and an integer k. Question: Are there k sets S 1 , . . . , S k in C such that S k S i ? i=1

1. Prove that MINIMUM SET COVER problem is NP-C 2. The greedy approximation algorithm for MINIMUM SET COVER, works by taking the largest set in X C, remove all all the elements of X from S and also from each subset of C. The algorithm repeat this until all the elements of S are removed. Prove that the number of elements not covered after kopt iterations is at most n/2, where kopt is the smallest number of sets of C needed to cover S , and n = |S |. 3. Prove the greedy algorithm is O(log n) factor optimal approximation. 4. Prove that the following problem is NP-C. 24

Problem: HITTING SET Instance: A collection C of subsets of a set S , a positive integer K. Question: Does S contain a hitting set for C of size K or less, that is, a subset S S with |S | K and such that S contains at least one element from each subset in C. 5. Given a set I of n intervals on the real line, show a O(n log n) time algorithm that computes the smallest set of points X on the real line, such that for every interval I I there is a point p X, such that p I.

2.3.16 k-Center
Problem: k-CENTER Instance: A set P of n points in the plane, and an integer k and a radius r. Question: Is there a cover of the points of P by k disks of radius (at most) r? 1. Describe an nO(k) time algorithm that solves this problem. 2. There is a very simple and natural algorithm that achieves a 2-approximation for this cover: First it select an arbitrary point as a center (this point is going to be the center of one of the k covering disks). Then it computes the point that it furthest away from the current set of centers as the next center, and it continue in this fashion till it has k-points, which are the resulting centers. The smallest k equal radius disks centered at those points are the required k disks. Show an implementation of this approximation algorithm in O(nk) time. 3. Prove that that the above algorithm is a factor two approximation to the optimal cover. Namely, the radius of the disks output 2ropt , where ropt is the smallest radius, so that we can nd k-disks that cover the point-set. 4. Provide an -approximation algorithm for this problem. Namely, given k and a set of points P in the plane, your algorithm would output k-disks that cover the points and their radius is (1+)ropt , where ropt is the minimum radius of such a cover of P. 5. Prove that dual problem r-DISK-COVER problem is NP-Hard. In this problem, given P and a radius r, one should nd the smallest number of disks of radius r that cover P. 6. Describe an approximation algorithm to the r-DISK COVER problem. Namely, given a point-set P and a radius r, outputs k disks, so that the k disks cover P and are of radius r, and k = O(kopt ), where kopt is the minimal number of disks needed to cover P by disks of radius r.

2.3.17

MAX 3SAT
Problem: MAX SAT Instance: Set U of variables, a collection C of disjunctive clauses of literals where a literal is a variable or a negated variable in U. Question: Find an assignment that maximized the number of clauses of C that are being satised. 25

Consider the Problem MAX SAT.

1. Prove that MAX SAT is NP-Hard. 2. Prove that if each clause has exactly three literals, and we randomly assign to the variables values 0 or 1, then the expected number of satised clauses is (7/8)M, where M = |C|. 3. Show that for any instance of MAX SAT, where each clause has exactly three dierent literals, there exists an assignment that satises at least 7/8 of the clauses. 4. Let (U, C) be an instance of MAX SAT such that each clause has 10 log n distinct variables, where n is the number of clauses. Prove that there exists a satisfying assignment. Namely, there exists an assignment that satisfy all the clauses of C.

2.3.18

Complexity

1. Prove that P -NP. 2. Show that if NP -NP, then every NP-C problem is not a member of -NP.

2.3.19

3SUM

Describe an algorithm that solves the following problem as quickly as possible: Given a set of n numbers, does it contain three elements whose sum is zero? For example, your algorithm should answer T for the set {5, 17, 7, 4, 3, 2, 4}, since 5 + 7 + (2) = 0, and F for the set {6, 7, 4, 13, 2, 5, 13}.

2.3.20

Polynomially equivalent.

Consider the following pairs of problems: 1. MIN SPANNING TREE and MAX SPANNING TREE 2. SHORTEST PATH and LONGEST PATH 3. TRAVELING SALESMAN PROBLEM and VACATION TOUR PROBLEM (the longest tour is sought). 4. MIN CUT and MAX CUT (between s and t) 5. EDGE COVER and VERTEX COVER 6. TRANSITIVE REDUCTION and MIN EQUIVALENT DIGRAPH (all of these seem dual or opposites, except the last, which are just two versions of minimal representation of a graph). Which of these pairs are polytime equivalent and which are not? Why?

2.3.21

PLANAR-3-COLOR

Using 3COLORABLE, and the gadget in gure 2.3.21, prove that the problem of deciding whether a planar graph can be 3-colored is NP-C. Hint: show that the gadget can be 3-colored, and then replace any crossings in a planar embedding with the gadget appropriately. 26

Figure 2.1: Gadget for PLANAR-3-COLOR.

Figure 2.2: Gadget for DEGREE-4-PLANAR-3-COLOR.

2.3.22

DEGREE-4-PLANAR-3-COLOR

Using the previous result, and the gadget in gure 2.3.22, prove that the problem of deciding whether a planar graph with no vertex of degree greater than four can be 3-colored is NP-C. Hint: show that you can replace any vertex with degree greater than 4 with a collection of gadgets connected in such a way that no degree is greater than four.

2.3.23

Primality and Complexity

Prove that PRIMALITY (Given n, is n prime?) is in NP -NP. Hint: -NP is easy (whats a certicate for showing that a number is composite?). For NP, consider a certicate involving primitive roots and recursively their primitive roots. Show that knowing this tree of primitive roots can be checked to be correct and used to show that n is prime, and that this check takes poly time.

2.3.24

Poly time subroutines can lead to exponential algorithms

Show that an algorithm that makes at most a constant number of calls to polynomial-time subroutines runs in polynomial time, but that a polynomial number of calls to polynomial-time subroutines may result in an exponential-time algorithm.

2.3.25

Polynomial time Hmiltonian path

1. Prove that if G is an undirected bipartite graph with an odd number of vertices, then G is nonhamiltonian. Give a polynomial time algorithm algorithm for nding a hamiltonian cycle in an undirected bipartite graph or establishing that it does not exist. 2. Show that the hamiltonian-path problem can be solved in polynomial time on directed acyclic graphs by giving an ecient algorithm for the problem. 3. Explain why the results in previous questions do not contradict the facts that both HAM-CYCLE and HAM-PATH are NP-C problems. 27

2.3.26

GRAPH-ISOMORPHISM

Consider the problem of deciding whether one graph is isomorphic to another. 1. Give a brute force algorithm to decide this. 2. Give a dynamic programming algorithm to decide this. 3. Give an ecient probabilistic algorithm to decide this. 4. Either prove that this problem is NP-C, give a poly time algorithm for it, or prove that neither case occurs.

28

Chapter 3

Exercises - Network Flow


473G - Algorithms - Exercises Sariel Har-Peled February 23, 2006

Version: 1.02 This chapter include problems that are realted to network ow.

3.1
3.1.1

Network Flow
The good, the bad, and the middle.

[10 Points] Suppose youre looking at a ow network G with source s and sink t, and you want to be able to express something like the following intuitive notion: Some nodesare clearly on the source side of the main bottlenecks; some nodes are clearly on the sink side of the main bottlenecks; and some nodes are in the middle. However, G can have many minimum cuts, so we have to be careful in how we try making this idea precise. Heres one way to divide the nodes of G into three categories of this sort. We say a node v is upstream if, for all minimum s-t cuts (A, B), we have v A that is, v lies on the source side of every minimum cut. We say a node v is downstream if, for all minimum s-t cuts (A, B), we have v B that is, v lies on the sink side of every minimum cut. We say a node v is central if it is neither upstream nor downstream; there is at least one minimum s-t cut (A, B) for which v A, and at least one minimum s-t cut (A , B ) for which v B . Give an algorithm that takes a ow network G and classies each of its nodes as being upstream, downstream, or central. The running time of your algorithm should be within a constant factor of the time required to compute a single maximum ow.

3.1.2

Ad hoc networks

[20 Points] Ad hoc networks are made up of low-powered wireless devices, have been proposed for situations like natural disasters in which the coordinators of a rescue eort might want to monitor conditions in a hard-toreach area. The idea is that a large collection of these wireless devices could be dropped into such an area from an airplane and then congured into a functioning network. 29

Note that were talking about (a) relatively inexpensive devices that are (b) being dropped from an airplane into (c) dangerous territory; and for the combination of reasons (a), (b), and (c), it becomes necessary to include provisions for dealing with the failure of a reasonable number of the nodes. Wed like it to be the case that if one of the devices v detects that it is in danger of failing, it should transmit a representation of its current state to some other device in the network. Each device has a limited transmitting range say it can communicate with other devices that lie within d meters of it. Moreover, since we dont want it to try transmitting its state to a device that has already failed, we should include some redundancy: A device v should have a set of k other devices that it can potentially contact, each within d meters of it. Well call this a back-up set for device v. 1. Suppose youre given a set of n wireless devices, with positions represented by an (x, y) coordinate pair for each. Design an algorithm that determines whether it is possible to choose a back-up set for each device (i.e., k other devices, each within d meters), with the further property that, for some parameter b, no device appears in the back-up set of more than b other devices. The algorithm should output the back-up sets themselves, provided they can be found. 2. The idea that, for each pair of devices v and w, theres a strict dichotomy between being in range or out of range is a simplied abstraction. More accurately, theres a power decay function f () that species, for a pair of devices at distance , the signal strength f () that theyll be able to achieve on their wireless connection. (Well assume that f () decreases with increasing .) We might want to build this into our notion of back-up sets as follows: among the k devices in the back-up set of v, there should be at least one that can be reached with very high signal strength, at least one other that can be reached with moderately high signal strength, and so forth. More concretely, we have values p1 p2 pk , so that if the back-up set for v consists of devices at distances d1 d2 dk , then we should have f (d j ) p j for each j. Give an algorithm that determines whether it is possible to choose a back-up set for each device subject to this more detailed condition, stil requiring that no device should appear in the back-up set of more than b other devices. Again, the algorithm should output the back-up sets themselves, provided they can be found.

3.1.3

Minimum Flow

[10 Points] Give a polynomial-time algorithm for the following minimization analogue of the Maximum-Flow Problem. You are given a directed graph G = (V, E), with a source s V and sink t V, and numbers (capacities) (v, w) for each edge (v, w) E. We dene a ow f , and the value of a ow, as usual, requiring that all nodes except s and t satisfy ow conservation. However, the given numbers are lower bounds on edge ow that is, they require that f (v, w) (v, w) for every edge (v, w) E, and there is no upper bound on ow values on edges. 1. Give a polynomial-time algorithm that nds a feasible ow of minimum possible values. 2. Prove an analogue of the Max-Flow Min-Cut Theorem for this problem (i.e., does min-ow = maxcut?).

3.1.4

Prove infeasibility.

You are trying to solve a circulation problem, but it is not feasible. The problem has demands, but no capacity limits on the edges. More formally, there is a graph G = (V, E), and demands dv for each node v (satisfying 30

dv = 0), and the problem is to decide if there is a ow f such that f (e) 0 and f in (v) f out (v) = dv for all nodes v V. Note that this problem can be solved via the circulation algorithm from Section 7.7 by setting ce = + for all edges e E. (Alternately, it is enough to set ce to be an extremely large number for each edge say, larger than the total of all positive demands dv in the graph.) You want to x up the graph to make the problem feasible, so it would be very useful to know why the problem is not feasible as it stands now. On a closer look, you see that there is a subset U of nodes such that there is no edge into U, and yet vU dv > 0. You quickly realize that the existence of such a set immediately implies that the ow cannot exist: The set U has a positive total demand, and so needs incoming ow, and yet U has no edges into it. In trying to evaluate how far the problem is from being solvable, you wonder how big the demand of a set with no incoming edges can be. Give a polynomial-time algorithm to nd a subset S V of nodes such that there is no edge into S and for which vS dv is as large as possible subject to this condition.
vV

3.1.5

Cellphones and services.

Consider an assignment problem where we have a set of n stations that can provide service, and there is a set of k requests for service. Say, for example, that the stations are cell towers and the requests are cell phones. Each request can be served by a given set of stations. The problem so far can be represented by a bipartite graph G: one side is the stations, the other the customers, and there is an edge (x, y) between customer x and station y if customer x can be served from station y. Assume that each station can serve at most one customer. Using a max-ow computation, we can decide whether or not all customers can be served, or can get an assignment of a subset of customers to stations maximizing the number of served customers. Here we consider a version of the problem with an addition complication: Each customer oers a dierent amount of money for the service. Let U be the set of customers, and assume that customer x U is willing to pay v x 0 for being served. Now the goal is to nd a subset X U maximizing xX v x such that there is an assignment of the customers in X to stations. Consider the following greedy approach. We process customers in order of decreasing value (breaking ties arbitrarily). When considering customer x the algorithm will either promise service to x or reject x in the following greedy fasion. Let X be the set of customers that so far have been promised service. We add x to the set X if and only if there is a way to assign X {x} to servers, and we reject x otherwise. Note that rejected customers will not be considered later. (This is viewed as an advantage: If we need to reject a high-paying customer, at least we can tell him/her early.) However, we do not assign accepting customers to servers in a greedy fasion: we only x the assignment after the set of accepted customers is xed. Does this greedy approach produce an optimal set of customers? Prove that it does, or provide a counterexample.

3.1.6

Follow the stars

[20 Points] Some friends of yours have grown tired of the game Six Degrees of Kevin Bacon (after all, they ask, isnt it just breadth-rst search?) and decide to invent a game with a little more punch, algorithmically speaking. Heres how it works. You start with a set X of n actresses and a set Y of n actors, and two players P0 and P1 . Player P0 names an actress x1 X, player P1 names an actor y1 who has appeared in a movie with x1 , player P0 names an actress x2 who has appeared in a movie with y1 , and so on. Thus, P0 and P1 collectively generate a sequence x1 , y1 , x2 , y2 , . . . such that each actor/actress in the sequence has costarred with the actress/actor immediately preceding. A player Pi (i = 0, 1) loses when it is Pi s turn to move, and he/she cannot name a member of his/her set who hasnt been named before. 31

Suppose you are given a specic pair of such sets X and Y, with complete information on who has appeared in a movie with whom. A strategy for Pi , in our setting, is an algorithm that takes a current sequence x1 , y1 , x2 , y2 , . . . and generates a legal next move for Pi (assuming its Pi s turn to move). Give a polynomial-time algorithm that decides which of the two players can force a win, in a particular instance of this game.

3.1.7

Flooding

[10 Points] Network ow issues come up in dealing with natural disasters and other crises, since major unexpected events often require the movement and evacuation of large numbers of people in a short amount of time. Consider the following scenario. Due to large-scale ooding in a region, paramedics have identied a set of n injured people distributed across the region who need to be rushed to hospitals. There are k hospitals in the region, and each of the n people needs to be brought to a hospital that is within a half-hours driving time of their current location (so dierent people will have dierent options for hospitals, depending on where they are right now). At the same time, one doesnt want to overload any one of the hospitals by sending too many patients its way. The paramedics are in touch by cell phone, and they want to collectively work out whether they can choose a hospital for each of the injured people in such a way that the load on the hospitals is balanced: Each hospital receives at most n/k people. Give a polynomial-time algorithm that takes the given information about the peoples locations and determines whether this is possible.

3.1.8

Capacitation, yeh, yeh, yeh

Suppose you are given a directed graph G = (V, E), with a positive integer capacity ce on each edge e, a designated source s V, and a designated sink t V. You are also given a maximum s-t ow in G, dened by a ow value fe on each edge e. The ow { fe } is acyclic: There is no cycle in G on which all edges carry positive ow. Now suppose we pick a specic edge e E and reduce its capacity by 1 unit. Show how to nd a maximum ow in the resulting capacitated graph in time O(m + n), where m is the number of edges in G and n is the number of nodes.

3.1.9

Fast Friends

[20 Points] Your friends have written a very fast piece of maximum-ow code based on repeatedly nding augmenting paths as in the course lecture notes. However, after youve looked at a bit of output from it, you realize that its not always nding a ow of maximum value. The bug turns out to be pretty easy to nd; your friends hadnt really gotten into the whole backward-edge thing when writing the code, and so their implementation builds a variant of the residual graph that only includes the forwards edges. In other words, it searches for s-t paths in a graph G f consisting only of edges of e for which f (e) < ce , and it terminates when there is no augmenting path consisting entirely of such edges. Well call this the Forward-Edge-Only Algorithm. (Note that we do not try ot prescribe how this algorithms chooses its forward-edge paths; it may choose them in any fashion it wants, provided that it terminates only when there are no forward-edge paths.) Its hard to convince your friends they need to reimplement the code. In addition to its blazing speed, they claim, in fact, that it never returns a ow whose value is less than a xed fraction of optimal. Do you believe this? The crux of their claim can be made precise in the following statement. 32

There is an absolute constant b > 1 (independent of the particular input ow network), so that on every instance of the Maximum-Flow Problem, the Forward-Edge-Only Algorithm is guaranteed to nd a ow of value at least 1/b times the maximum-ow value (regardless of how it chooses its forward-edge paths). Decide whether you think this statement is true or false, and give a proof of either the statement or its negation.

3.1.10

Even More Capacitation

[10 Points] In a standard s t Maximum-Flow Problem, we assume edges have capacities, and there is no limit on how much ow is allowed to pass through a node. In this problem, we consider the variant of the MaximumFlow and Minimum-Cut problems with node capacities. Let G = (V, E) be a directed graph, with source s V, sink t V, and nonnegative node capacities {cv 0} for each v V. Given a ow f in this graph, the ow through a node v is dened as f in (v). We say that a ow is feasible if it satises the usual ow-conservation constraints and the node-capacity constraints: f in (v) cv for all nodes. Give a polynomial-time algorithm to nd an s-t maximum ow in such a node-capacitated network. Dene an s-t cut for node-capacitated networks, and show that the analogue of the Max-Flow Min-Cut Theorem holds true.

3.1.11

Matrices

[10 Points] Let M be an n n matrix with each entry equal to either 0 or 1. Let mi j denote the entry in row i and column j. A diagonal entry is one of the form mii for some i. 1. [2 Points] Give an example of a matrix M that is not rearrangeable, but for which at least one entry in each row and each column is equal to 1. 2. [8 Points] Give a polynomial-time algorithm that determines whether a matrix M with 0-1 entries is rearrangeable.

3.1.12

Unique Cut

[10 Points] Let G = (V, E) be a directed graph, with source s V, sink t V, and nonnegative edge capacities {ce }. Give a polynomial-time algorithm to decide whether G has a unique minimum s-t cut (i.e., an s-t of capacity strictly less than that of all other s-t cuts).

3.1.13

Transitivity
G,k

[10 Points] Given a graph G = (V, E), and a natural number k, we can dene a relation on pairs of vertices of G as follows. If x, y V, we say that x y if there exist k mutually edge-disjoint paths from x to y in G. Is it true that for every G and every k 0, the relation is transitive? That is, is it always the case that if x y and y z, then we have x z? Give a proof or a counterexample. 33
G,k G,k G,k G,k G,k

3.1.14

Census Rounding

[20 Points] You are consulting for an environmental statistics rm. They collect statistics and publish the collected data in a book. The statistics are about populations of dierent regions in the world and are recorded in multiples of one million. Examples of such statistics would look like the following table. Country A B C Total grown-up men 11.998 9.083 2.919 24.000 grown-up women 12.983 10.872 3.145 27.000 children 1.019 2.045 0.936 4.000 total 26.000 22.000 7.000 55.000 We will assume here for simplicity that our data is such that all row and column sums are integers. The Census Rounding Problem is to round all data to integers without changing any row or column sum. Each fractional number can be rounded either up or down. For example, a good rounding for our table data would be as follows. Country A B C Total grown-up men 11.000 10.000 3.000 24.000 grown-up women 13.000 10.000 4.000 27.000 children 1.000 2.000 0.000 4.000 total 26.000 22.000 7.000 55.000 1. [5 Points] Consider rst the special case when all data are between 0 and 1. So you have a matrix of fractional numbers between 0 and 1, and your problem is to round each fraction that is between 0 and 1 to either 0 or 1 without changing the row or column sums. Use a ow computation to check if the desired rounding is possible. 2. [5 Points] Consider the Census Rounding Problem as dened above, where row and column sums are integers, and you want to round each fractional number to either or . Use a ow computation to check if the desired rounding is possible. 3. [10 Points] Prove that the rounding we are looking for in (a) and (b) always exists.

3.1.15

Edge Connectivity

[20 Points] The edge connectivity of an undirected graph is the minimum number k of edges that must be removed to disconnect the graph. For example, the edge connectivity of a tree is 1, and the edge connectivity of a cyclic chain of vertices is 2. Show how the edge connectivity of an undirected graph G = (V, E) can be determined by running a maximum-ow algorithm on at most |V| ow networks, each having O(V) vertices and O(E) edges.

3.1.16

Maximum Flow By Scaling

[20 Points] Let G = (V, E) be a ow network with source s, sink t, and an integer capacity c(u, v) on each edge (u, v) E. Let C = max(u,v)E c(u, v). 1. [2 Points] Argue that a minimum cut of G has capacity at most C|E|. 34

2. [5 Points] For a given number K, show that an augmenting path of capacity at least K can be found in O(E) time, if such a path exists. The following modication of F-F-M can be used to compute a maximum ow in G. M-F-B-S(G, s, t) 1 C max(u,v)E c(u, v) 2 initialize ow f to 0 3 K 2 lg C 4 while K 1 do { 5 while (there exists an augmenting path p of capacity at least K) do { 6 augment ow f along p } 7 K K/2 } 8 return f

3. [3 Points] Argue that M-F-B-S returns a maximum ow. 4. [4 Points] Show that the capacity of a minimum cut of the residual graph G f is at most 2K|E| each time line 4 is executed. 5. [4 Points] Argue that the inner while loop of lines 5-6 is executed O(E) times for each value of K. 6. [2 Points] Conclude that M-F-B-S can be implemented so that it runs in O(E 2 lg C) time.

3.1.17

Perfect Matching

[20 Points] 1. [10 Points] A perfect matching is a matching in which every vertex is matched. Let G = (V, E) be an undirected bipartite graph with vertex partition V = L R, where |L| = |R|. For any X V, dene the neighborhood of X as N(X) = y V (x, y) E for some x X , that is, the set of vertices adjacent to some member of X. Prove Halls theorem: there exists a perfect matching in G if and only if |A| |N(A)| for every subset A L. 2. [10 Points] We say that a bipartite graph G = (V, E), where V = L R, is d-regular if every vertex v V has degree exactly d. Every d-regular bipartite graph has |L| = |R|. Prove that every d-regular bipartite graph has a matching of cardinality |L| by arguing that a minimum cut of the corresponding ow network has capacity |L|. 35

3.1.18

Number of augmneting paths

1. [10 Points] Show that a maximum ow in a network G = (V, E) can always be found by a sequence of at most |E| augmenting paths. [Hint: Determine the paths after nding the maximum ow.] 2. [10 Points] Suppose that a ow network G = (V, E) has symmetric edges, that is, (u, v) E if and only (v, u) E. Show that the Edmonds-Karp algorithm terminates after at most |V||E|/4 iterations. [Hint: For any edge (u,v), consider how both (s, u) and (v, t) change between times at which (u,v) is critical.]

3.1.19

Minimum Cut Festival

[20 Points] 1. Given a multigraph G(V, E), show that an edge can be selected uniform at random from E in time O(n), given access to a source of random bits. 2. For any 1, dene an approximate cut in a multigraph G as any cut whose cardinality is within a multiplicative factor of the cardinality of the min-cut in G. Determine the probability that a single iteration of the randomized algorithm for cuts will produce as output some -approximate cut in G. 3. Using the analysis of the randomized min-cut algorithm, show that the number of distinct min-cuts in a multigraph G cannot exceed n(n 1)/2, where n is the number of vertices in G. 4. Formulate and prove a similar result of the number of -approximate cuts in a multigraph G.

3.1.20

Independence Matrix

[10 Points] Consider a 0 1 matrix H with n1 rows and n2 columns. We refer to a row or a column of the matrix H as a line. We say that a set of 1s in the matrix H is independent if no two of them appear in the same line. We also say that a set of lines in the matrix is a cover of H if they include (i.e., cover) all the 1s in the matrix. Using the max-ow min-cut theorem on an appropriately dened network, show that the maximum number of independent 1s equals the minimum number of lines in the cover.

3.1.21

Scalar Flow Product

[10 Points] Let f be a ow in a network, and let be a real number. The scalar ow product, denoted by f , is a function from V V to IR dened by ( f )(u, v) = f (u, v). Prove that the ows in a network form a convex set. That is, show that if f1 and f2 are ows, then so is f1 + (1 ) f2 for all in the range 0 1.

3.1.22

Go to school!

Professor Adam has two children who, unfortunately, dislike each other. The problem is so severe that not only they refuse to walk to school together, but in fact each one refuses to walk on any block that the other child has stepped on that day. The children have no problem with their paths crossing at a corner. Fortunately both the professors house and the school are on corners, but beyond that he is not sure if it is going to be 36

possible to send both of his children to the same school. The professor has a map of his town. Show how to formulate the problem of determining if both his children can go to the same school as a maximum-ow problem.

3.1.23

The Hopcroft-Karp Bipartite Matching Algorithm

[20 Points] In this problem, we describe a faster algorithm, due to Hopcroft and Karp, for nding a maximum matching in a bipartite graph. The algorithm runs in O( V E) time. Given an undirected, bipartite graph G = (V, E), where V = L R and all edges have exactly one endpoint in L, let M be a matching in G. We say that a simple path P in G is an augmenting path with respect to M if it starts at an unmatched vertex in L, ends at an unmatched vertex in R, and its edges belong alternatively to M and E M. (This denition of an augmenting path is related to, but dierent from, an augmenting path in a ow network.) In this problem, we treat a path as a sequence of edges, rather than as a sequence of vertices. A shortest augmenting path with respect to a matching M is an augmenting path with a minimum number of edges. Given two sets A and B, the symmetric dierence A B is dened as (A B) (B A), that is, the elements that are in exactly one of the two sets. 1. [4 Points] Show that if M is a matching and P is an augmenting path with respect to M, then the symmetric dierence M P is a matching and |M P| = |M| + 1. Show that if P1 , P2 , ..., Pk are vertexdisjoint augmenting paths with respect to M, then the symmetric dierence M (P1 P2 ... Pk ) is a matching with cardinality |M| + k. The general structure of our algorithm is the following: H-K(G) 1 M 2 repeat 3 let P {P1 , P2 , ..., Pk } be a maximum set of vertex-disjoint shortest augmenting paths with respect to M 4 M M (P1 P2 . . . Pk ) 5 until P = 6 return M

The remainder of this problem asks you to analyze the number of iterations in the algorithm (that is, the number of iterations in the repeat loop) and to describe an implementation of line 3. 2. [4 Points] Given two matchings M and M in G, show that every vertex in the graph G = (V, M M ) has degree at most 2. Conclude that G is a disjoint union of simple paths or cycles. Argue that edges in each such simple path or cycle belong alternatively to M or M . Prove that if |M| |M |, then M M contains at least |M | |M| vertex-disjoint augmenting paths with respect to M. Let l be the length of a shortest augmenting path with respect to a matching M, and let P1 , P2 , ..., Pk be a maximum set of vertex-disjoint augmenting paths of length l with respect to M. Let M = M (P1 P2 ... Pk ), and suppose that P is a shortest augmenting path with respect to M . 3. [2 Points] Show that if P is vertex-disjoint from P1 , P2 , ..., Pk , then P has more than l edges. 37

4. [2 Points] Now suppose P is not vertex-disjoint from P1 , P2 , ..., Pk . Let A be the set of edges (M M ) P. Show that A = (P1 P2 ... Pk ) P and that |A| (k + 1)l. Conclude that P has more than l edges. 5. [2 Points] Prove that if a shortest augmenting path for M has length l, the size of the maximum matching is at most |M| + |V|/l. 6. [2 Points] Show that the number of repeat loop iterations in the algorithm is at most 2 V. [Hint: By how much can M grow after iteration number V?] 7. [4 Points] Give an algorithm that runs in O(E) time to nd a maximum set of vertex-disjoint shortest augmenting paths P1 , P2 , ..., Pk for a given matching M. Conclude that the total running time of H-K is O( V E).

38

Index
Problem k-CENTER, 25 BIN PACKING, 18, 22, 24 HITTING SET, 19, 21, 22, 25 Hamiltonian Path, 21 KNAPSACK, 22 LARGEST COMMON SUBGRAPH, 18 MAX SAT, 25 MINIMUM SET COVER, 18, 21, 22, 24 MULTIPROCESSOR SCHEDULING, 23 Max Degree Spanning Tree, 21 Minimum Test Collection, 17 Not-3SAT, 20 PARTITION, 21 SUBGRAPH ISOMORPHISM, 18 SUBSET SUM, 21 Subgraph Isomorphism, 17 Subset Sum, 19 TILING, 19, 22, 24 VERTEX COVER, 23 2SAT, 19, 20 3COLORABLE, 26 3SAT, 19 EXACT-COVER-BY-3-SETS, 19 MAX SAT, 25 Not-3SAT, 20 Partition, 17, 18 PRIMALITY, 27 reduction, 17 Subgraph Isomorphism, 17 Subset Sum, 19

39

You might also like