Lecture 1 (18.11.2012) Varous Technques Author: Jaros law B lasok The followng lecture s dvded nto three parts, n the frst one we wll consder the Subset Sum problem and see how to solve t usng varaton over the well-known meet-n-the-mddle technque. The second part wll consst of nterestng applcaton of dvde-and-conquer n desgnng moderately exponental algorthms. In thrd part we wll get famlar wth the memozaton technque and see how to apply t to the Perfect Matchng Count problem. 1 Subset Sum problem 1.1 Introducton Subset Sum Input: Sequence of n ntegers a 1, a 2, a n and an nteger S Queston: Is there any subset of ndces { 1, k } such that a 1 + a 2 + + a k = S It s well known that the above problem s NP-complete, there s trval soluton wth computatonal complexty O (2 n ) and space complexty O (1). There s also classc dynamc pseudopolynomal algorthm whch solves that problem n tme O(nS) and space O(S). Those two are gong to be our base for further mprovements. Our goal at ths pont s to fnd soluton more effectve than 2 n, ndependent on S, havng n mnd space complexty (ths s relevant when S s large). 1.2 k-sum problem Let us reformulate the above problem and generalze t a lttle n order to have a better sght. Consder the followng problem:
k-sum problem Input: k sequences of ntegers (a j )n =1 for 1 j k and an nteger S. Queston: Are there ndces 1, 2,, k, s. t. k a j j = S j=1 Remark 1. The Subset Sum problem can be reduced to the k-sum problem for any k, where n s 2 n/k. Indeed, gven an nstance of the Subset Sum problem, say: (a 1, a 2, a n ; S) one mght take an nstance of the k-sum problem as (a 1 ) n =1 beng sequence of sums of all subsets of a 1, a 2, a n, (a2 ) n k =1 are all sums of subsets of second block of length n and so on. Now each choce of ndces j n k-sum problem s the same as determnng what s the ntersecton of an unknown subset for the Subset Sum problem wth the blocks k n k,... (k + 1) n k 1. Remark 2. The k-sum problem can be solved n tme O(n k ) (trvally). Note that the remark above alone s useless for our purpose: after reducng Subset Sum to k-sum as above and applyng ths trval soluton we acqure soluton wth complexty O (2 n ) to Subset Sum. 1.3 2-Sum problem In order to get better algorthm for the subset sum problem, we focus on tryng to solve the k-sum problem for some partcular k better than n O(n k ). Lets start wth 2-Sum. For fxed 1 we do know we have chosen a 1 1, what we want to check s f there exsts S a 1 1 n the second sequence. It suffces then to sort the second sequence n advance, to make those queres easy. Corollary 3. The 2-Sum problem can be solved n tme O(n log n) and lnear space. Corollary 4. The Subset Sum problem can be solved n tme O (2 n 2 ) and space O (2 n 2 ). 1.4 4-Sum Our next goal s to mprove space complexty, preservng tme complexty.
Lets make precse algorthm for k = 2: Sort frst sequence n the ncreasng, and second one n the decreasng order. Now one can solve the k-sum problem lnearly, movng two ponters smultaneously, as below: whle n&j n do z = a 1 + a2 j f z = S then END end f f z < S then Increment end f f z > S then Increment j end f end whle Lets now try to solve the 4-Sum problem n O(n 2 ) tme and lnear space. It would gve us Subset Sum soluton n tme O (2 n 2 ) and space O (2 n 4 ) va reducton from above. We want to modfy the algorthm from above: t would be suffcent to be able to effcently generate all pars of ndces 1, 2 n the order of ncreasng value a 1 1 + a 2 2 we would do the same wth 3, 4, generatng them n decreasng order and apply the algorthm from above. Indeed, one can generate all such pars n O(n) space and O(n 2 log n) tme. Lets sort both sequences a 1 and a 2 n the ncreasng order, and mantan a heap wth pars of actve ndces: at any pont n the heap for every 1 {1 n} there s exactly one par of actve ndces ( 1, 2 ): 2 beng the lowest s.t. that par have not yet been generated. Heap s ordered by a 1 1 + a 2 2, now at each pont there s exactly n elements n heap. When we re asked for the next par, one just takes the frst actve par from the heap (.e. the one wth smallest value a 1 1 + a 2 2 and put the par ( 1, 2 + 1) nto the heap as actve. That leads us to the followng corollares, proved by Schroeppel and Shamr [3]. Corollary 5. The 4-Sum problem can be solved n O(n 2 log n) tme and lnear space. Corollary 6. The Subset Sum problem can be solved n O (2 n 2 ) tme and O (2 n 4 ) space. Those are currently best-known bounds for ths problem. There are
two mportant open queston around ths topc: s there any algorthm to the Subset Sum problem whch solves t n polynomal space, and tme complexty O ((2 ε) n )? Is t possble to acheve better tme complexty than O (2 n 2 )? 2 Dvde and conquer 2.1 Introducton; Travelng Salesman Problem Lets take a look nto the very well known dvde-and-conquer technque and ts applcaton n desgnng moderate exponental algorthm for one of the most famous NP-hard problems. Travelng Salesman Problem Input: Drected, weghted graph G Queston: Cheapest Hamlton cycle n G The dynamc programmng soluton presented n one of the prevous lectures for the Hamlton Cycle problem s stll vald: for every state n dynamc programmng, one keeps nformaton about cheapest partal soluton. That leads to a soluton wth tme complexty O (2 n ) and space complexty O (2 n ). The soluton based on the ncluson-excluson prncple can t be extended to ths generalzed case. 2.2 Soluton Our goal now s to get an algorthm of tme complexty O (c n ) and polynomal space complexty. It s obvous that our problem s polynomally equvalent to the problem of fndng the cheapest Hamlton path between s and t for fxed s, t. We wll consder ths problem from now on. It would suffce to guess properly a vertex v whch s n the mddle of the cheapest path, then for every other vertex of the graph guess on whch sde of ths vertex t s on ths path. Now we could solve two smaller problems ndependently. More precsely: for each vertex v V, and for each dvson V \{s, t, v} = V 1 V 2 nto two sets s.t. V 1 V 2 = and V 1 = n 3 2, solve recursvely ndependent nstances: (G[V 1 {s, v}], s, v) (G[V 2 {v, t}], v, t)
Ths gves us recursve equaton of tme complexty: Wth rather easy soluton: T (n) = n 2 n 2 T (n/2) T (n) = O (n log n 4 n ) Corollary 7. There s an algorthm for the Travelng Salesman Problem of tme complexty O ((4 + ε) n ) and space complexty O (1). 3 Memozaton 3.1 Introducton; Perfect Matchng Countng problem At ths part of lecture we wll dscuss memozaton, whch s effectvely another way of lookng at the dynamc programmng method. Wth a careful examnaton, one mght recognze that not all states of dynamc programmng are relevant to the fnal soluton, so wth a top-down mplementaton and more precse analyss t s possble to acqure a better tme complexty sometmes by cost of a lttle worse space complexty. We wll see an example of how one can use memozaton to solve the Perfect Matchng Countng problem, defned as follows: Perfect Matchng Countng Input: An undrected graph G wth n vertces Queston: Calculate how many there are dfferent 1-regular subgraphs of G on n vertces (perfect matchngs of G) 3.2 Soluton There s an obvous dynamc soluton for the problem posted above: for every subset X of set V one wants to calculate T [X] as the number of perfect matchngs n G[X]. Now a soluton for our problem s just T [V ], and there s a recursve formula for calculatng T [X]: for a set X we fx a vertex v X wth the smallest ndex. Now: T [X] = t[x \ {u, v}] uv E Theres an obvous upper bound of O (2 n ) on the complexty of the above algorthm, but maybe we could do better than ths, and examne whch felds of T [X] are relevant. The followng lemma wll be helpful:
Lemma 8. If recurson touches a set X of sze n 2, X {v 1, v 2,, v } = Proof. Trval, as at every pont we remove two vertces, one of them havng smallest ndex among all remanng. Now t follows drectly from the lemma above, that number of states we are tryng to estmate does not exceed: n/2 ( ) n One can easly (but somehow strkngly) bound ths sum from above: n/2 ( ) n ( ) n = F n+1 whle the latter equalty follows from smple nducton: ( ) n ( ) n 1 ( ) n 1 = + 1 ( ) n 2 = F n + = 1 = F n + F n 1 = F n+1 Ths leads to the followng algorthm by Kovsto [2]. Corollary 9. There exst an algorthm for the Perfect Matchng Countng problem whch runs n O (( 1+ 5 2 ) n ) tme and space complexty. Informal, but useful hnt: Durng desgnng ths knd of algorthms often suffces to see that number of subsets of sze roughly n 2 whch are vsted durng recurson s strctly less (asymptotcally) than 2 n. 4 Perfect Matchng Countng once agan In ths secton there wll be presented a sketch of a soluton n tme O (2 n/2 ) for the Perfect Matchng Countng problem. Space could be lmted to polynomal. The algorthm below s a smplfcaton of the algorthm of Björklund [1]. Lets take a graph G, mark ts edges as black, then add n/2 red edges: v 1 v 2, v 3 v 4, ; that leads us to a multgraph G wth black and red edges.
Remark 10. The number of perfect matchngs n G equals the number of cycle covers of G, where every cycle s alternatng (even edges are black, odds are red). We want to calculate ths number faster (n tme O (2 n/2 )). Lets use the ncluson-excluson prncple, as n one of the prevous lectures. Defne Ω as the set of all tuples of closed alternatng walks of sum of length n. A Ω are those whch contan the -th red edge. What we want to calculate s A, and t s farly smple by ncluson-excluson prncple. Yet, t s stll not a soluton, as same matchng s taken nto account more than once. If we take Ω d beng d-tuples as above, our number of perfect matchng s roughly: A d /d! (1) d 1 n 2 One mnor ssue one has to work out are automorphsms of any sngle cycle, but t s possble to get through t. References [1] Andreas Björklund. Countng perfect matchngs as fast as ryser. In Yuval Raban, edtor, SODA, pages 914 921. SIAM, 2012. [2] Mkko Kovsto. Parttonng nto sets of bounded cardnalty. In Janer Chen and Fedor V. Fomn, edtors, IWPEC, volume 5917 of Lecture Notes n Computer Scence, pages 258 263. Sprnger, 2009. [3] Rchard Schroeppel and Ad Shamr. A T=O(2 n/2 ), S=O(2 n/4 ) algorthm for certan NP-complete problems. SIAM J. Comput., 10(3):456 464, 1981.