Desgn and Analyss of Algorthms May 4, 2015 Massachusetts Insttute of Technology 6.046J/18.410J Profs. Erk Demane, Srn Devadas, and Nancy Lynch Problem Set 9 Solutons Problem Set 9 Solutons Ths problem set s due at 11:59pm on Thursday, Aprl 30, 2015. Exercse 9-1. Read CLRS, Chapter 35. Exercse 9-2. Exercse 35.2-3. Exercse 9-3. Exercse 35.4-2. Exercse 9-4. Read Prof. Demane s notes on fxed-parameter algorthms. Problem 9-1. Knapsack [25 ponts] In the Knapsack problem, we are gven a set A = {a 1,..., a n } of tems, where each a has a specfed postve nteger sze s and a specfed postve nteger value v. We are also gven a postve nteger knapsack capacty B. Assume that s B for every. The problem s to fnd a subset of A whose total sze s at most B and for whch the total value s maxmzed. In ths problem, we wll consder approxmaton algorthms to solve the Knapsack problem. Notaton: For any subset S of A, we wrte s S for the total of all the szes n S and v S for the total of all the values n S. Let Opt denote an optmal soluton to the problem. (a) [5 ponts] Consder the followng greedy algorthm Alg 1 to solve the Knapsack problem: Order all the tems a n non-ncreasng order of ther densty, whch s the rato of value to sze, v s. Make a sngle pass through the lst, from hghest to lowest densty. For each tem encountered, f t stll fts, nclude t, otherwse exclude t. Prove that algorthm Alg 1 does not guarantee any constant approxmaton rato. That s, for any postve nteger k, there s an nput to the algorthm for whch the total value of the set of tems returned by the algorthm s at most v Opt. k Soluton: Fx any k. Consder the two-tem nput lst a 1, a 2, where v 1 = 2, s 1 = 1, v 1 v v 2 2 = 2k, and s k = 2k, wth knapsack capacty B = 2k. Snce = 2 and = 1, s 1 s 2 Alg 1 consders the tems n order a 1, a 2, and ncludes a 1 and excludes a 2. The total value acheved s only 2, but the largest achevable total value s 2k.
2 Problem Set 9 Solutons (b) [7 ponts] Consder the followng algorthm Alg 2. If the total sze of all the tems s B, then nclude all the tems. If not, then order all the tems n non-ncreasng order of ther denstes. Wthout loss of generalty, assume that ths orderng s the same as the orderng of the tem ndces. Fnd the smallest ndex n the ordered lst such that the total sze of the frst tems exceeds B (.e., 1 1 s j > B, but s j B). If v > v j, then return {a }. Otherwse, return {a 1,..., a 1 }. Prove that Alg 2 always yelds a 2-approxmaton to the optmal soluton. Soluton: We know that fractonal knapsack problem can be solved va greedy algorthm; the optmal soluton for fractonal knapsack problem takes frst 1 tems, and takes some fracton α of tem. That s, fractonal knapsack optmal soluton v fopt s 1 v fopt = v j + αv. Snce every knapsack problem s a vald fractonal knapsack problem, we know that v Opt v fopt. Now consder the soluton output by Alg 2. 1 1 v fopt max( v j, v ) max( v j, αv ), 2 because at least one of the two terms needs to be larger than half of v fopt. Therefore, we have 1 v Opt v fopt 2 max( v j, v ), whch shows that Alg 2 s a 2-approxmaton algorthm. Soluton: Ths s an alternate soluton. Assume for contradcton that there s some nput nstance for whch Alg 2 does not acheve a 2-approxmaton to the optmal soluton. Then for as specfed n the 1 algorthm, the maxmum of v and v j s strctly less than v Opt. That s, both 2 1 v Opt v and v j are strctly less than. Then v j < v Opt. We also know that 2 s j > B. Let C = {a 1,..., a } Opt, D = Opt {a 1,..., a }, and E = {a 1,..., a } Opt. We know that v C = v j v E, and v D = v Opt v E. Snce v j < v Opt, we now have that v C < v D. We also know that s C > s D, snce s j > B and s D B. < v D Therefore, the densty of C s strctly less than the densty of D (.e., v C ). s C s D
Problem Set 9 Solutons 3 Now recall that the denstes of {a 1,..., a n } are non-ncreasng. Consder j v s j j j:v C (v j s v s j ) = j. We know that v j j:v j C s s j s j s v C v v D v j v C v v D s C s s D s j s C s s D j j:v j C v j j:v j C s j v for a j C, and thus v j s v s j > 0. Ths mples that, and smlar argument holds for. Therefore,. Ths contradcts our result from the prevous paragraph that says v C < v D, and Alg 2 must be a 2-approxmaton. s C s D (c) [5 ponts] Let A = {a 1,..., a n } be the nput ordered arbtrarly, and V be the largest value for any tem; then nv s an upper bound on the total value that could be acheved by any soluton. For every {1,..., n} and v {1,..., nv }, defne S,v to be the smallest total sze of a subset of {a 1,..., a } whose total value s exactly v; S,v = f no such subset exsts. Gve a dynamc programmng algorthm Alg 3 to solve Knapsack exactly. Specfcally, gve a recurrence to compute all values of S,v, and explan how to use ths to solve the Knapsack problem. Analyze ts tme complexty. Soluton: S 1,v = s 1 f v 1 = v, and = otherwse. For 1 n 1: S +1,v = mn{s,v, s +1 + S,v v+1 } f v +1 v, S,v otherwse. The computaton of all of these values takes tme O(n 2 V ). As usual for dynamc programmng algorthms, we can add bookkeepng to calculate the actual subsets as we go along. The maxmum total value achevable s max vls n,v B. The fnal output s a subset of A whose total value s ths maxmum. Snce the tme complexty O(n 2 V ) depends lnearly on V, whch s represented n bnary n the nput to the problem, Alg 3 s a pseudo-polynomal-tme algorthm. (d) [8 ponts] Fnally, we develop Alg 4, whch s a Fully Polynomal Tme Approxmaton Scheme (FPTAS) for Knapsack. The dea s to use an exact dynamc programmng algorthm lke the one n Part (c), but nstead of usng the gven (possbly large) values for the tems, we use versons of the gven values that are sutably scaled and rounded down. As n Part (c), order A = {a 1,..., a n } arbtrarly, and let V be the largest value for any tem. For any ε, 0 < ε < 1, Alg 4 behaves as follows: = l( v For each tem a wth value v, defne a scaled value v )( n)j. V ε Usng these scaled values (and the gven szes), run Alg 3 and output the set C of tems that t returns. Prove that Alg 4 s a FPTAS for Knapsack. Soluton:
4 Problem Set 9 Solutons By Part (c), the runnng tme of the algorthm s O(n 2 ( V )( n ) ) = V ε O(n2 n ), whch ε s polynomal n n and 1. ε It remans to show that the set C returned by the algorthm has total value v C (1 ε)v Opt. Let K denote εv, so each v = v v n. It follows that K K v K. Consderng all the elements of O, we get v O pt K v Opt nk. Now consder the set C that s returned by the dynamc programmng step. We have that v C Kv C. Also, snce the set C s optmal n terms of the scaled values, we have v C v O pt. Therefore, we have: v C Kv C Kv O pt v Opt nk = v Opt εv v Opt εv Opt = (1 ε)v Opt. Ths s as needed. Problem 9-2. Fxed-Parameter Algorthms [25 ponts] We consder the Tournament Edge Reversal problem. Defne a tournament to be a drected graph T = (V, E) such that, for every par of vertces u, v V, exactly one of (u, v) and (v, u) s n E. Furthermore, defne a cycle cover to be a set A E of drected edges of a tournament T such that, every drected cycle of T contans at least one edge from A. (a) [5 ponts] Let a mnmal cycle cover of T be a cycle cover wth the least number of edges. Prove that reversng all the edges of a mnmal cycle cover A turns T nto an acyclc tournament. (Hnt: Any edge e A must be the only edge n A on some drected cycle of T.) Soluton: Let T be the new tournament. Suppose for contradcton that T contans a cycle C. Let F be the set of edges of C that were obtaned by reversng the edges of A, and let F be ther reversals, whch are edges n T. For each e F, let C e be a drected cycle of T that e covers. From the hnt, we know e s the only edge n A that s also n C e. Now we can construct a cycle C n T, consstng entrely of edges of T that dd not get reversed: Include all reverse of edges n C F (whch are vald edges n T ), and for each e C F, nclude all the edges of C e except for e. Ths yelds a cycle C n T that contans no edges n A, whch s a contradcton. In the Tournament Edge Reversal problem, we are gven a tournament T and a postve nteger k, and the objectve s to decde whether T has a cycle cover of sze at most k. (b) [15 ponts] Show that ths problem has a kernel wth at most k 2 + 2k vertces. (Hnt: Defne a trangle to be a drected cycle of length 3. Consder the number of tmes that a node or an edge appears n dfferent trangles.) Soluton:
Problem Set 9 Solutons 5 Part (a) mples that a tournament T has a cycle cover of sze at most k f and only f t can be turned nto an acyclc tournament by reversng drectons of at most k edges. We wll use ths characterzaton for the kernel. We gve two smple reducton rules: Rule 1: If an edge e s contaned n k + 1 trangles, then reverse e and reduce k by 1. Ths rule s safe because f we do not reverse e, we must reverse at least one edge from each of k + 1 trangles contanng e. Thus e belongs to every cycle cover of sze k. Rule 2: If a vertex v s not contaned n any trangle, then delete v from T. To see why ths s safe, let X be the set of vertces u such that T contans the edge (v, u) (the outgong neghbors of v), and let Y be the set of vertces u such that T contans the edge (u, v) (the ncomng neghbors of v). X and Y partton the vertces n V {v}. Snce v s not contaned n any trangle, there s no edge from X to Y. Thus, every drected cycle n T s ether wholly contaned wthn the subgraph nduced by X or the subgraph nduced by Y. Therefore, removng v and ts ncdent edges from T does not affect the sze of the mnmum cycle cover. Thus, startng wth nstance (T ; k) = (T 0 ; k 0 ), we apply our reducton rules repeatedly untl they cannot be appled any longer, obtanng a sequence of equvalent nstances (T 1 ; k 1 ), (T 2 ; k 2 ),..., (T m ; k m ) = (T ; k ), where nether Rule s applcable to the fnal nstance (T ; k ). Clam: If T has a cycle cover A of sze k, then T has at most k (k + 2) vertces. Proof of clam: Snce Rule 2 s not applcable, every vertex of T s n a trangle, whch must contan some edge n A. Snce Rule 1 s not applcable, for every edge e A, there are at most k vertces other than e s endponts that are n trangles contanng e. Snce A k, t follows that T has at most k (k + 2) vertces. Thus, after reducng, we consder the fnal nstance (T, k ). If T has more than k (k + 2) vertces, then t cannot have a cycle cover of sze k, and s a no-nstance. By equvalence, (T, k) s also a no-nstance. We return no n ths case. Otherwse, we get the desred kernel wth at most k 2 + 2k vertces. (c) [5 ponts] Obtan a FPT algorthm for the Tournament Edge Reversal problem. Soluton: Gven an nstance (T, k) for the Tournament Edge Reversal problem, use the algorthm from Part (b) to ether determne that (T, k) s a no-nstance or obtan a kernel (T, k ) where k k and T has at most k (k + 2) vertces. In the former case, answer no. In the latter case, run any algorthm wth runnng tme g(k ), for any functon g, to solve the Tournament Edge Reversal problem on (T, k ). Return the result as the answer. To reduce the problem, we have to check for all trangles, and check membershp of all edges and vertces n the trangles. For vertces, to check all trangles, t takes O( ( ) V 2 ) per vertex. For edges, t takes V per edge. Each reducton step thus takes
6 Problem Set 9 Solutons O( V 3 + V E )). There are at most V +E reducton steps snce each step removes an edge or a vertex. In total, reducton takes O( V 4 + V 3 E + V 2 E + V E 2 ). Snce O( E ) = O( V 2 ) n tournaments, ths s O( V 5 ). To fnd the mnmum cycle cover, one soluton s to check all possble subsets of edges n T. There are O(2 k/2 (k / +2) 2 ) subsets, and for each subset we need to run a cycle fndng algorthm, such as DFS, to ensure all cycles are covered. Therefore, the fnal tme complexty s O( V 5 )+2 O(k4).
MIT OpenCourseWare http://ocw.mt.edu 6.046J / 18.410J Desgn and Analyss of Algorthms Sprng 2015 For nformaton about ctng these materals or our Terms of Use, vst: http://ocw.mt.edu/terms.