Mnmum Spnnng Trs Spnnng Tr A tr (.., connctd, cyclc grph) whch contns ll th vrtcs of th grph Mnmum Spnnng Tr Spnnng tr wth th mnmum sum of wghts 1 1 Spnnng forst If grph s not connctd, thn thr s spnnng tr for ch connctd componnt of th grph
Applctons of MST Fnd th lst xpnsv wy to connct st of cts, trmnls, computrs, tc. 3
Exmpl Problm A town hs st of houss nd st of rods A rod conncts nd only houss A rod connctng houss u nd v hs rpr cost w(u, v) 1 1 Gol: Rpr nough (nd no mor) rods such tht: 1. Evryon stys connctd.., cn rch vry hous from ll othr houss. Totl rpr cost s mnmum
Mnmum Spnnng Trs A connctd, undrctd grph: Vrtcs = houss, Edgs = rods A wght w(u, v) on ch dg (u, v) E 1 1 Fnd T E such tht: 1. T conncts ll vrtcs. w(t) = Σ (u,v)t w(u, v) s mnmzd
Proprts of Mnmum Spnnng Trs Mnmum spnnng tr s not unqu MST hs no cycls (by dfnton) : # of dgs n MST: V - 1
Prm s Algorthm Strts from n rbtrry root : V A = {} At ch stp: Fnd lght dg crossng (V A, V - V A ) Add ths dg to st A (Th dgs n st A lwys form sngl tr) Rpt untl th tr spns ll vrtcs 1 1
How to Fnd Lght Edgs Quckly? Us prorty quu Q: Contns vrtcs not yt ncludd n th tr,.., (V V A ) V A = {}, Q = {b, c, d,, f, g, h, } W ssoct ky wth ch vrtx v: ky[v] = mnmum wght of ny dg (u, v) connctng v to V A Ky[] = mn(w 1,w ) w 1 w 1 1
How to Fnd Lght Edgs Quckly? (cont.) Aftr ddng nw nod to V A w updt th wghts of ll th nods djcnt to t.g., ftr ddng to th tr, Ky[b]= nd Ky[h]= Ky[v] = f v s not djcnt to ny vrtcs n V A 1 1
Exmpl 1 1 1 1 0 Q = {, b, c, d,, f, g, h, } V A = Extrct-MIN(Q) ky [b] = [b] = ky [h] = [h] = Q = {b, c, d,, f, g, h, } V A = {} Extrct-MIN(Q) b
Exmpl 1 1 1 1 Q = {c, d,, f, g, h, } V A = {, b} ky [c] = [c] = b ky [h] = [h] = - unchngd Extrct-MIN(Q) c Q = {d,, f, g, h, } V A = {, b, c} ky [d] = [d] = c ky [f] = [f] = c ky [] = [] = c Extrct-MIN(Q)
Exmpl 1 1 1 1 Q = {d,, f, g, h} V A = {, b, c, } ky [h] = [h] = ky [g] = [g] = Extrct-MIN(Q) f Q = {d,, g, h} V A = {, b, c,, f} ky [g] = [g] = f ky [d] = [d] = c unchngd ky [] = [] = f Extrct-MIN(Q) g 1
Exmpl 1 1 1 Q = {d,, h} V A = {, b, c,, f, g} ky [h] = 1 [h] = g 1 Extrct-MIN(Q) h 1 1 1 Q = {d, } V A = {, b, c,, f, g, h} Extrct-MIN(Q) d 13
Exmpl Q = {} V A = {, b, c,, f, g, h, d} 1 1 1 ky [] = [] = d Extrct-MIN(Q) Q = V A = {, b, c,, f, g, h, d, } 1
PRIM(V, E, w, r) 1. Q. for ch u V 3. do ky[u]. π[u] NIL 5. INSERT(Q, u). DECREASE-KEY(Q, r, 0) % ky[r] 0. whl Q. do u EXTRACT-MIN(Q). for ch v Adj[u]. do f v Q nd w(u, v) < ky[v]. thn π[v] u Totl tm: O(VlgV + ElgV) = O(ElgV) O(V) f Q s mplmntd s mn-hp Excutd V tms Tks O(lgV) Excutd O(E) tms totl 1. DECREASE-KEY(Q, v, w(u, v)) O(lgV) Constnt Tks O(lgV) Mn-hp oprtons: O(VlgV) O(ElgV)
Prm s Algorthm Totl tm: O(E lgv ) Prm s lgorthm s grdy lgorthm Grdy lgorthms fnd solutons bsd on squnc of chocs whch r loclly optml t ch stp. Nvrthlss, Prm s grdy strtgy producs globlly optmum soluton! 1
Kruskl s Algorthm How s t dffrnt from Prm s lgorthm? Prm s lgorthm grows on tr ll th tm Kruskl s lgorthm grows multpl trs (.., forst) t th sm tm. tr1 Trs r mrgd togthr usng sf dgs u Snc n MST hs xctly V - 1 dgs, ftr V - 1 mrgs, w would hv only on componnt v tr 1
Kruskl s Algorthm Strt wth ch vrtx bng ts own componnt Rptdly mrg two componnts nto on by choosng th lght dg tht conncts thm Whch componnts to consdr t ch trton? Scn th st of dgs n monotonclly ncrsng ordr by wght 1 1 W would dd dg (c, f) 1
Exmpl 1 1 1: (h, g) : (, h), (b, c) : (c, ), (g, f) : (d, ) : (, b), (c, f) : (, f) : (, g) : (b, h) : (c, d), (, h) 1: (d, f) {}, {b}, {c}, {d}, {}, {f}, {g}, {h}, {} 1. Add (h, g). Add (c, ) 3. Add (g, f). Add (, b) 5. Add (c, f). Ignor (, g). Add (c, d). Ignor (, h). Add (, h). Ignor (b, c). Add (d, ) 1. Ignor (, f) 13. Ignor (b, h) 1. Ignor (d, f) {g, h}, {}, {b}, {c},{d},{},{f},{} {g, h}, {c, }, {}, {b}, {d}, {}, {f} {g, h, f}, {c, }, {}, {b}, {d}, {} {g, h, f}, {c, }, {, b}, {d}, {} {g, h, f, c, }, {, b}, {d}, {} {g, h, f, c, }, {, b}, {d}, {} {g, h, f, c,, d}, {, b}, {} {g, h, f, c,, d}, {, b}, {} {g, h, f, c,, d,, b}, {} {g, h, f, c,, d,, b}, {} {g, h, f, c,, d,, b, } {g, h, f, c,, d,, b, } {g, h, f, c,, d,, b, } {g, h, f, c,, d,, b, }
Implmntton of Kruskl s Algorthm Uss dsjont-st dt structur (s Chptr 1) to dtrmn whthr n dg conncts vrtcs n dffrnt componnts 1 1 W would dd dg (c, f) 0
Oprtons on Dsjont Dt Sts MAKE-SET(u) crts nw st whos only mmbr s u FIND-SET(u) rturns rprsnttv lmnt from th st tht contns u Any of th lmnts of th st tht hs prtculr proprty E.g.: S u = {r, s, t, u}, th proprty s tht th lmnt b th frst on lphbtclly FIND-SET(u) = r FIND-SET(s) = r FIND-SET hs to rturn th sm vlu for gvn st 1
Oprtons on Dsjont Dt Sts UNION(u, v) unts th dynmc sts tht contn 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} Runnng tm for FIND-SET nd UNION dpnds on mplmntton. W hd sn rlr tht FIND-SET cn b don n O(lgn) tm nd UNION oprton cn b don n O(1) (s Chptr 1)
KRUSKAL(V, E, w) 1. A. for ch vrtx v V 3. do MAKE-SET(v) O(V). sort E nto non-dcrsng ordr by w 5. for ch (u, v) tkn from th sortd lst. do f FIND-SET(u) FIND-SET(v). thn A A {(u, v)}. UNION(u, v). rturn A O(ElgE) O(E) O(lgV) Runnng tm: O(V+ElgE+ElgV) O(ElgE) dpndnt on th mplmntton of th dsjont-st dt structur Snc E=O(V ), w hv lge=o(lgv)=o(lgv)
Kruskl s Algorthm O(E lge ) or O(E lgv ) Kruskl s lgorthm s grdy It producs globlly optmum soluton