Coping With NP-Compltnss Chaptr 0 Extning th Limits o Tractability Q. Suppos I n to solv an NP-complt problm. What shoul I o? A. Thory says you'r unlikly to in poly-tim algorithm. Must sacriic on o thr sir aturs. Solv problm to optimality. Solv problm in polynomial tim. Solv arbitrary instancs o th problm. This lctur. Solv som spcial cass o NP-complt problms that aris in practic. Slis by Kvin Wayn. Copyright @ 00 Parson-Aison Wsly. All rights rsrv. Vrtx Covr 0. Fining Small Vrtx Covrs VERTEX COVER: Givn a graph G = (V, E) an an intgr k, is thr a subst o vrtics S V such that S k, an or ach g (u, v) ithr u S, or v S, or both. 6 7 8 k = S = {, 6, 7, 0 9 0
Fining Small Vrtx Covrs Fining Small Vrtx Covrs Q. What i k is small? Brut orc. O(k nk+ ). Try all C(n, k) = O(n k ) substs o siz k. Taks O(k n) tim to chck whthr a subst is a vrtx covr. Goal. Limit xponntial pnncy on k,.g., to O( k k n). Ex. n =,000, k = 0. Brut. k nk+ = 0 inasibl. Bttr. k k n = 07 asibl. Rmark. I k is a constant, algorithm is poly-tim; i k is a small constant, thn it's also practical. Claim. Lt u-v b an g o G. G has a vrtx covr o siz k i at last on o G { u an G { v has a vrtx covr o siz k-. P. Suppos G has a vrtx covr S o siz k. S contains ithr u or v (or both). Assum it contains u. S { u is a vrtx covr o G { u. lt v an all incint gs P. Suppos S is a vrtx covr o G { u o siz k-. Thn S { u is a vrtx covr o G. Claim. I G has a vrtx covr o siz k, it has k(n-) gs. P. Each vrtx covrs at most n- gs. 6 Fining Small Vrtx Covrs: Algorithm Fining Small Vrtx Covrs: Rcursion Tr Claim. Th ollowing algorithm trmins i G has a vrtx covr o siz k in O( k kn) tim. boolan Vrtx-Covr(G, k) { i (G contains no gs) rturn tru i (G contains kn gs) rturn als lt (u, v) b any g o G a = Vrtx-Covr(G - {u, k-) b = Vrtx-Covr(G - {v, k-) rturn a or b $ c i k = 0 & T(n, k) " % cn i k = & ' T(n,k #)+ ckn i k > k k- ( T(n, k) " k ck n k- P. Corrctnss ollows rom prvious two claims. Thr ar k+ nos in th rcursion tr; ach invocation taks O(kn) tim. k- k- k - i k- k- 0 0 0 0 0 0 0 0 7 8
Inpnnt St on Trs 0. Solving NP-Har Problms on Trs Inpnnt st on trs. Givn a tr, in a maximum carinality subst o nos such that no two shar an g. Fact. A tr on at last two nos has at last two la nos. gr = Ky obsrvation. I v is a la, thr xists a maximum siz inpnnt st containing v. u P. (xchang argumnt) Consir a max carinality inpnnt st S. I v S, w'r on. I u S an v S, thn S { v is inpnnt S not maximum. IF u S an v S, thn S { v { u is inpnnt. v 0 Inpnnt St on Trs: Gry Algorithm Wight Inpnnt St on Trs Thorm. Th ollowing gry algorithm ins a maximum carinality inpnnt st in orsts (an hnc trs). Inpnnt-St-In-A-Forst(F) { S φ whil (F has at last on g) { Lt = (u, v) b an g such that v is a la A v to S Dlt rom F nos u an v, an all gs incint to thm. rturn S P. Corrctnss ollows rom th prvious ky obsrvation. Rmark. Can implmnt in O(n) tim by consiring nos in postorr. Wight inpnnt st on trs. Givn a tr an no wights w v > 0, in an inpnnt st S that maximizs Σ v S w v. Obsrvation. I (u, v) is an g such that v is a la no, thn ithr OPT inclus u, or it inclus all la nos incint to u. Dynamic programming solution. Root tr at som no, say r. OPT in (u) = max wight inpnnt st o subtr root at u, containing u. OPT out (u) = max wight inpnnt st o subtr root at u, not containing u. OPT in (u) = w u + # OPT out (v) v " chilrn(u) OPT out (u) = # max { OPT in (v), OPT out (v) v " chilrn(u) v r u w x chilrn(u) = { v, w, x
Wight Inpnnt St on Trs: Dynamic Programming Algorithm Contxt Thorm. Th ynamic programming algorithm ins a maximum wight inpnnt st in a tr in O(n) tim. Wight-Inpnnt-St-In-A-Tr(T) { Root th tr at a no r orach (no u o T in postorr) { i (u is a la) { M in [u] = w u M out [u] = 0 ls { M in [u] = w u + Σ v chilrn(u) M out [v] M out [u] = Σ v chilrn(u) max(m in [v], M out [v]) rturn max(m in [r], M out [r]) nsurs a no is visit atr all its chilrn P. Taks O(n) tim sinc w visit nos in postorr an xamin ach g xactly onc. can also in inpnnt st itsl (not just valu) Inpnnt st on trs. This structur spcial cas is tractabl bcaus w can in a no that braks th communication among th subproblms in irnt subtrs. u s Chaptr 0., but proc with caution Graphs o boun tr with. Elgant gnralization o trs that: Capturs a rich class o graphs that aris in practic. Enabls composition into inpnnt pics. u Wavlngth-Division Multiplxing 0. Circular Arc Coloring Wavlngth-ivision multiplxing (WDM). Allows m communication strams (arcs) to shar a portion o a ibr optic cabl, provi thy ar transmitt using irnt wavlngths. Ring topology. Spcial cas is whn ntwork is a cycl on n nos. Ba nws. NP-complt, vn on rings. Brut orc. Can trmin i k colors suic in O(k m ) tim by trying all k-colorings. c b a Goal. O((k)) poly(m, n) on rings. n =, m = 6 6
Wavlngth-Division Multiplxing Rviw: Intrval Coloring Wavlngth-ivision multiplxing (WDM). Allows m communication strams (arcs) to shar a portion o a ibr optic cabl, provi thy ar transmitt using irnt wavlngths. Intrval coloring. Gry algorithm ins coloring such that numbr o colors quals pth o schul. maximum numbr o strams at on location Ring topology. Spcial cas is whn ntwork is a cycl on n nos. c j Ba nws. NP-complt, vn on rings. a b g h i Brut orc. Can trmin i k colors suic in O(k m ) tim by trying all k-colorings. Goal. O((k)) poly(m, n) on rings. c b a Circular arc coloring. Wak uality: numbr o colors pth. Strong uality os not hol. n =, m = 6 max pth = min colors = 7 8 (Almost) Transorming Circular Arc Coloring to Intrval Coloring Circular Arc Coloring: Dynamic Programming Algorithm Circular arc coloring. Givn a st o n arcs with pth k, can th arcs b color with k colors? Equivalnt problm. Cut th ntwork btwn nos v an v n. Th arcs can b color with k colors i th intrvals can b color with k colors in such a way that "slic" arcs hav th sam color. Dynamic programming algorithm. Assign istinct color to ach intrval which bgins at cut no v 0. At ach no v i, som intrvals may inish, an othrs may bgin. Enumrat all k-colorings o th intrvals through v i that ar consistnt with th colorings o th intrvals through v i-. Th arcs ar k-colorabl i som coloring o intrvals ning at cut no v 0 is consistnt with original coloring o th sam intrvals. ys v 0 v colors o a', b', an c' must corrspon to colors o a", b", an c" c' b' b' a" b" v v a' c" c" v v 0 v v v v v 0 v 0 v v v v v 0 9 0
Circular Arc Coloring: Running Tim Running tim. O(k! n). n phass o th algorithm. Bottlnck in ach phas is numrating all consistnt colorings. Thr ar at most k intrvals through v i, so thr ar at most k! colorings to consir. Extra Slis Rmark. This algorithm is practical or small valus o k (say k = 0) vn i th numbr o nos n (or paths) is larg. Vrtx Covr Vrtx Covr in Bipartit Graphs Vrtx covr. Givn an unirct graph G = (V, E), a vrtx covr is a subst o vrtics S V such that or ach g (u, v) E, ithr u S or v S or both. ' ' ' S = {,,, ', ' S = ' '
Vrtx Covr Vrtx Covr: König-Egrváry Thorm Wak uality. Lt M b a matching, an lt S b a vrtx covr. Thn, M S. König-Egrváry Thorm. In a bipartit graph, th max carinality o a matching is qual to th min carinality o a vrtx covr. P. Each vrtx can covr at most on g in any matching. ' ' S* = {, ', ', ' S* = ' ' ' M = -', -', -' M = ' M* = -', -', -', -' M* = ' ' ' ' 6 Vrtx Covr: Proo o König-Egrváry Thorm Vrtx Covr: Proo o König-Egrváry Thorm König-Egrváry Thorm. In a bipartit graph, th max carinality o a matching is qual to th min carinality o a vrtx covr. Suics to in matching M an covr S such that M = S. Formulat max low problm as or bipartit matching. Lt M b max carinality matching an lt (A, B) b min cut. König-Egrváry Thorm. In a bipartit graph, th max carinality o a matching is qual to th min carinality o a vrtx covr. Suics to in matching M an covr S such that M = S. Formulat max low problm as or bipartit matching. Lt M b max carinality matching an lt (A, B) b min cut. Din L A = L A, L B = L B, R A = R A, R B = R B. ' Claim. S = L B R A is a vrtx covr. consir (u, v) E ' u L A, v R B impossibl sinc ininit capacity thus, ithr u L B or v R A or both s ' t Claim. S = M. max-low min-cut thorm M = cap(a, B) ' only gs o orm (s, u) or (v, t) contribut to cap(a, B) M = cap(a, B) = L B + R A = S. ' 7 8
Rgistr Allocation Rgistr Allocation Rgistr. On o k o high-sp mmory locations in computr's CPU. say Rgistr allocator. Part o an optimizing compilr that controls which variabls ar sav in th rgistrs as compil program xcuts. variabls or tmporaris Intrrnc graph. Nos ar "liv rangs." Eg u-v i thr xists an opration whr both u an v ar "liv" at th sam tim. Obsrvation. [Chaitin, 98] Can solv rgistr allocation problm i intrrnc graph is k-colorabl. Spilling. I graph is not k-colorabl (or w can't in a k-coloring), w "spill" crtain variabls to main mmory an swap back as n. typically inrquntly us variabls that ar not in innr loops 0 A Usul Proprty Chaitin's Algorithm Rmark. Rgistr allocation problm is NP-har. Ky act. I a no v in graph G has wr than k nighbors, G is k-colorabl i G { v is k-colorabl. lt v an all incint gs P. Dlt no v rom G an color G { v. I G { v is not k-colorabl, thn nithr is G. I G { v is k-colorabl, thn thr is at last on rmaining color lt or v. Vrtx-Color(G, k) { say, no with wst nighbors whil (G is not mpty) { Pick a no v with wr than k nighbors Push v on stack Dlt v an all its incint gs whil (stack is not mpty) { Pop nxt no v rom th stack Assign v a color irnt rom its nighboring nos which hav alray bn color v k = k = G is -colorabl vn though all nos hav gr
Chaitin's Algorithm Thorm. [Kmp 879, Chaitin 98] Chaitin's algorithm proucs a k-coloring o any graph with max gr k-. P. Follows rom ky act sinc ach no has wr than k nighbors. algorithm succs in k-coloring many graphs with max gr k Rmark. I algorithm nvr ncountrs a graph whr all nos hav gr k, thn it proucs a k-coloring. Practic. Chaitin's algorithm (an variants) ar xtrmly ctiv an wily us in ral compilrs or rgistr allocation.