Minimum Spnning Trs
Minimum Spnning Trs Problm A town hs st of houss nd st of rods A rod conncts nd only houss A rod conncting houss u nd v hs rpir cost w(u, v) Gol: Rpir nough (nd no mor) rods such tht: 1. Evryon stys connctd: cn rch vry hous from ll othr houss, nd. Totl rpir cost is minimum 11 b c d i 6 1 h g f 1
Minimum Spnning Trs A connctd, undirctd grph: Vrtics = houss, Edgs = rods A wight w(u, v) on ch dg (u, v) E Find T E such tht: 1. T conncts ll vrtics. w(t) = Σ (u,v) T w(u, v) is minimizd b c d 11 i 1 h 1 g f 3
Minimum Spnning Trs T forms tr = spnning tr A spnning tr whos wight is minimum ovr ll spnning trs is clld minimum spnning tr, or MST. b c d 11 i 1 g 1 g f
Proprtis of Minimum Spnning Trs Minimum spnning trs r not uniqu Cn rplc (b, c) with (, h) to obtin diffrnt spnning tr with th sm cost MST hv no cycls W cn tk out n dg of cycl, nd still hv th vrtics connctd whil rducing th cost b c d 11 i 1 h 1 g f # of dgs in MST: V - 1 5
Growing MST Minimum-spnning-tr problm: find MST for connctd, undirctd grph, with wight function ssocitd with its dgs A gnric solution: Build st A of dgs (initilly mpty) Incrmntlly dd dgs to A such tht thy would blong to MST An dg (u, v) is sf for A if nd only if A {(u, v)} is lso subst of som MST 11 b c d i 6 1 h g f 1 W will dd only sf dgs 6
Gnric MST lgorithm 1. A. whil A is not spnning tr 3. do find n dg (u, v) tht is sf for A. A A {(u, v)} 5. rturn A How do w find sf dgs? b c d 11 i 1 h 1 g f
Finding Sf Edgs Lt s look t dg (h, g) Is it sf for A initilly? Ltr on: S b c d 11 i 1 h 1 g f V - S Lt S V b ny st of vrtics tht includs h but not g (so tht g is in V - S) In ny MST, thr hs to b on dg (t lst) tht conncts S with V - S Why not choos th dg with minimum wight (h,g)?
Dfinitions A cut (S, V - S) is prtition of vrtics into disjoint sts S nd V - S An dg crosss th cut b c d S 11 i 1 S V- S V- S h g f 1 (S, V - S) if on ndpoint is in S nd th othr in V S A cut rspcts st A of dgs no dg in A crosss th cut An dg is light dg crossing cut its wight is minimum ovr ll dgs crossing th cut For givn cut, thr cn b > 1 light dg crossing it
Thorm Lt A b subst of som MST, (S, V - S) b cut tht rspcts A, nd (u, v) b light dg crossing (S, V - S). Thn (u, v) is sf for A. Proof: Lt T b MST tht includs A Edgs in A r shdd Assum T dos not includ th dg (u, v) Id: construct nothr MST T tht includs A {(u, v)} u v S V - S
Thorm - Proof T contins uniqu pth p btwn u nd v (u, v) forms cycl with dgs on p S (u, v) crosss th cut pth p x must cross th cut (S, V - S) t lst onc: lt (x, y) b tht dg Lt s rmov (x, y) brks T into two componnts. u v p V - S y Adding (u, v) rconncts th componnts T = T - {(x, y)} {(u, v)} 11
Thorm Proof (cont.) T = T - {(x, y)} {(u, v)} Hv to show tht T is MST: S (u, v) is light dg w(u, v) w(x, y) w(t ) = w(t) - w(x, y) + w(u, v) w(t) Sinc T is spnning tr u v p x V - S y w(t) w(t ) T must b n MST s wll 1
Thorm Proof (cont.) Nd to show tht (u, v) is sf for A: i.., (u, v) cn b prt of MST A T nd (x, y) A A T S x A {(u, v)} T Sinc T is n MST u p y (u, v) is sf for A v V - S 13
Discussion In GENERIC-MST: A is forst contining connctd componnts Initilly, ch componnt is singl vrtx Any sf dg mrgs two of ths componnts into on Ech componnt is tr Sinc n MST hs xctly V - 1 dgs - ftr itrting V - 1 tims, w hv only on componnt 1
Th Algorithm of Kruskl Strt with ch vrtx bing its own componnt Rptdly mrg two componnts into on by choosing th light dg tht conncts thm 11 b c d i 6 1 h g f 1 W would dd dg (c, f) Scn th st of dgs in monotoniclly incrsing ordr by wight Uss disjoint-st dt structur to dtrmin whthr n dg conncts vrtics in diffrnt componnts 15
Oprtions on Disjoint Dt Sts MAKE-SET(u) crts nw st whos only mmbr is u FIND-SET(u) rturns rprsnttiv lmnt from th st tht contins u My b ny of th lmnts of th st tht hs prticulr proprty E.g.: S u = {r, s, t, u}, th proprty is tht th lmnt b th first on lphbticlly FIND-SET(u) = r FIND-SET(s) = r FIND-SET hs to rturn th sm vlu for givn st 16
Oprtions on Disjoint Dt Sts UNION(u, v) units th dynmic sts tht contin u nd v, sy S u nd S v E.g.: S u = {r, s, t, u}, S v = {v, x, y} UNION (u, v) = {r, s, t, u, v, x, y} 1
KRUSKAL(V, E, w) 1. A. for ch vrtx v V 3. do MAKE-SET(v). sort E into non-dcrsing ordr by wight w 5. for ch (u, v) tkn from th sortd list 6. do if FIND-SET(u) FIND-SET(v). thn A A {(u, v)}. UNION(u, v). rturn A Running tim: O(E lgv) dpndnt on th implmnttion of th disjoint-st dt structur 1
Exmpl 11 1: (h, g) b c d : (c, i), (g, f) : (, b), (c, f) 6: (i, g) : (c, d), (i, h) i 6 1 h g f 1 : (, h), (b, c) : (d, ) : (, f) 11: (b, h) 1: (d, f) {}, {b}, {c}, {d}, {}, {f}, {g}, {h}, {i} 1. Add (h, g). Add (c, i) 3. Add (g, f). Add (, b) 5. Add (c, f) 6. Ignor (i, g). Add (c, d). Ignor (i, h). Add (, h). Ignor (b, c) 11. Add (d, ) 1. Ignor (, f) 13. Ignor (b, h) 1. Ignor (d, f) {g, h}, {}, {b}, {c}, {d}, {}, {f}, {i} {g, h}, {c, i}, {}, {b}, {d}, {}, {f} {g, h, f}, {c, i}, {}, {b}, {d}, {} {g, h, f}, {c, i}, {, b}, {d}, {} {g, h, f, c, i}, {, b}, {d}, {} {g, h, f, c, i}, {, b}, {d}, {} {g, h, f, c, i, d}, {, b}, {} {g, h, f, c, i, d}, {, b}, {} {g, h, f, c, i, d,, b}, {} {g, h, f, c, i, d,, b}, {} {g, h, f, c, i, d,, b, } {g, h, f, c, i, d,, b, } {g, h, f, c, i, d,, b, } {g, h, f, c, i, d,, b, } 1
Th lgorithm of Prim Th dgs in st A lwys form singl tr Strts from n rbitrry root : V A = {} At ch stp: Find light dg crossing cut (V A, V - V A ) Add this dg to A Rpt until th tr spns ll vrtics Grdy strtgy b c d 11 i 1 h 1 g f At ch stp th dg ddd contributs th minimum mount possibl to th wight of th tr 0
How to Find Light Edgs Quickly? Us priority quu Q: Contins ll vrtics not yt includd in th tr (V V A ) V = {}, Q = {b, c, d,, f, g, h, i} With ch vrtx w ssocit ky: b c d 11 i 1 h g f 1 ky[v] = minimum wight of ny dg (u, v) conncting v to vrtx in th tr Ky of v is if v is not djcnt to ny vrtics in V A Aftr dding nw nod to V A w updt th wights of ll th nods djcnt to it W ddd nod ky[b] =, ky[h] = 1
PRIM(V, E, w, r) 1. Q b c d. for ch u V 0 3. do ky[u] 11 i 1. π[u] NIL 5. INSERT(Q, u) h g f 1 6. DECREASE-KEY(Q, r, 0) 0. whil Q Q = {, b, c, d,, f, g, h, i}. do u EXTRACT-MIN(Q) V A =. for ch v Adj[u] Extrct-MIN(Q). do if v Q nd w(u, v) < ky[v] 11. thn π[v] u 1. DECREASE-KEY(Q, v, w(u, v))
Exmpl b c d 11 i 1 h g f 1 0 Q = {, b, c, d,, f, g, h, i} V A = Extrct-MIN(Q) b c d 11 i 1 h g f 1 ky [b] = π [b] = ky [h] = π [h] = Q = {b, c, d,, f, g, h, i} V A = {} Extrct-MIN(Q) b 3
Exmpl b c d 11 i 1 h g f 1 b c d 11 i 1 h g f 1 ky [c] = π [c] = b ky [h] = π [h] = - unchngd Q = {c, d,, f, g, h, i} V A = {, b} Extrct-MIN(Q) c ky [d] = π [d] = c ky [f] = π [f] = c ky [i] = π [i] = c Q = {d,, f, g, h, i} V A = {, b, c} Extrct-MIN(Q) i
Exmpl b c d 11 i 1 h g f 1 b c d 11 i 1 h 1 g f ky [h] = π [h] = i ky [g] = 6 π [g] = i Q = {d,, f, g, h} V A = {, b, c, i} Extrct-MIN(Q) f ky [g] = π [g] = f ky [d] = π [d] = c unchngd ky [] = π [] = f Q = {d,, g, h} V A = {, b, c, i, f} Extrct-MIN(Q) g 5
Exmpl b c d 11 i 1 h 1 g f 1 b c d 11 i 1 h 1 g f 1 ky [h] = 1 π [h] = g 1 Q = {d,, h} V A = {, b, c, i, f, g} Extrct-MIN(Q) h Q = {d, } V A = {, b, c, i, f, g, h} Extrct-MIN(Q) d 6
Exmpl b c d 11 i 1 h 1 g f 1 ky [] = π [] = f Q = {} V A = {, b, c, i, f, g, h, d} Extrct-MIN(Q) Q = V A = {, b, c, i, f, g, h, d, }
PRIM(V, E, w, r) 1. Q. for ch u V 3. do ky[u]. π[u] NIL 5. INSERT(Q, u) 6. DECREASE-KEY(Q, r, 0) ky[r] 0. whil Q. do u EXTRACT-MIN(Q). for ch v Adj[u]. do if v Q nd w(u, v) < ky[v] 11. thn π[v] u Totl tim: O(VlgV + ElgV) = O(ElgV) O(V) if Q is implmntd s min-hp Excutd V tims Tks O(lgV) Excutd O(E) tims Constnt 1. DECREASE-KEY(Q, v, w(u, v)) Min-hp oprtions: O(VlgV) Tks O(lgV) O(ElgV)