Outlin Computr Sin 33 Computtion o Minimum-Cost Spnnin Trs Prim's Alorithm Introution Mik Joson Dprtmnt o Computr Sin Univrsity o Clry Ltur #33 3 Alorithm Gnrl Constrution Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 / Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 / Introution Computtion o Costs o Spnnin Trs in Wiht Grphs Motivtion: Givn st o sits (rprsnt y vrtis o rph), onnt ths ll s hply s possil (usin onntions rprsnt y th s o wiht rph). Gols or Toy: prsnttion o th nitions n to ormlly n prolm motivt y th ov prsnttion o n lorithm (Prim's Alorithm) or solvin th prolm Rrn: Introution to Alorithms, Chptr 3 Txt, Stion 3.6 (prolm), 3.6. (Prim's Alorithm) Rll tht i G = (V ; E) is onnt, unirt rph, thn spnnin tr o G is surph G = ( V ; E) suh tht V = V (so G inlus ll th vrtis in G) G is tr Suppos now tht G = (V ; E) is onnt wiht rph with wiht untion w : E 7! N; n tht G = (V ; E ) is spnnin tr o G Th ost o G, w(g ), is th sum o th wihts o th s in G, tht is, w(g ) = X E w(): Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 3 / Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 /
Suppos G is wiht rph with wihts s shown low. Th ost o th ollowin spnnin tr, G = (V ; E ), is 8. 3 3 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 / Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 6 / Minimum-Cost Spnnin Trs Th ost o th ollowin spnnin tr, G = (V ; E ), is 6. Suppos (G ; w) is wiht rph. A surph G o G is minimum-ost spnnin tr o (G ; w) i th ollowin proprtis r stis. G is spnnin tr o G. 3 w(g ) w(g ) or vry spnnin tr G o G. : In th prvious xmpl, G is lrly not minimum-ost spnnin tr, us G is spnnin tr o G suh tht w(g ) > w(g ). It n shown tht G is minimum-ost spnnin tr o (G ; w). Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 7 / Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 8 /
Alorithm Gnrl Constrution Existn o Minimum-Cost Spnnin Tr Builin Minimum-Cost Spnnin Tr Lmm Lt G wiht rph with wiht untion w I G is onnt thn G hs minimum-ost spnnin tr (whih is not nssrily uniqu). Proo. G hs t lst on spnnin tr, us: G only hs nit numr o spnnin trs, us: Thus, thr must xist spnnin tr o G whos ost is lss thn or qul to tht o ny othr spnnin tr. To onstrut minimum-ost spnnin tr o G = (V ; E): Strt with G = ( V ; E), whr V V n E = ;. Not: G is surph o som minimum-ost spnnin tr o (G ; w). Rptly vrtis (i nssry) n s nsurin tht G is still surph o minimum-ost spnnin tr s you o so. Continu oin this until V = V n j Ej = jv j (so tht G is spnnin tr o G). Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 9 / Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 0 / Alorithm Gnrl Constrution Alorithm Builin Minimum-Cost Spnnin Tr Spition o Rquirmnts Aitionl Nots: This n on in svrl irnt wys, n thr r t lst two irnt lorithms tht us this pproh to solv this prolm. Th lorithm to prsnt hr ins with V = s or som vrtx s V, n mks sur tht G is lwys tr. As rsult, this lorithm is struturlly vry similr to Dijkstr's Alorithm to omput minimum-ost pths (whih w hv lry isuss in lss). Pr-Conition G = (V ; E) is onnt rph with wiht untion w Post-Conition: is untion : V! V [ NIL I E = ((v); v) j v V n (v) 6= NIL thn (V ; E) is minimum-ost spnnin tr or G Th rph G = (V ; E) n its wiht untion hv not n hn Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 / Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 /
Alorithm Alorithm Dt Struturs Psuoo Th lorithm (to prsnt nxt) will us priority quu to stor inormtion out wihts o s tht r in onsir or inlusion Th priority quu will MinHp: th ntry with th smllst priority will t th top o th hp Eh no in th priority quu will stor vrtx in G n th wiht o n inint to this vrtx Th wiht will us s th vrtx's priority An rry-s rprsnttion o th priority quu will us A son rry will us to lot h ntry o th priority quu or ivn no in onstnt tim Not: Th t struturs will, thror, look vry muh lik th t struturs us y Dijkstr's lorithm. MST-Prim(G ; w ; s) or v V o olour[v] = whit [v] = + [v] = NIL n or Initiliz n mpty priority quu Q olour[s] = ry [s] = 0 s with priority 0 to Q Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 3 / Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 / Alorithm Alorithm Psuoo, Continu Psuoo, Conlu whil (Q is not mpty) o (u; ) = xtrt-min(q) Not: = [u] or h v Aj[u] o i (olour[v] == whit) thn [v] = w((u; v)) olour[v] = ry; [v] = u v with priority [v] to Q ls i (olour[v] == ry) thn Upt inormtion out v (Shown on nxt sli) n i n or olour[u] = lk n whil rturn Uptin Inormtion Aout v i (w((u; v)) < [v]) thn ol = [v] [v] = w((u; v)) [v] = u Us Drs-Priority to rpl (v ; ol) in Q with (v ; [v]) n i Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 / Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 6 /
(Stp ) Consir th xution o MST-Prim(G, ): 3 3 3 3 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 7 / Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 8 / (Stp ) (Stp 3) 3 3 3 3 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 9 / Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 0 /
(Stp ) (Stp ) 3 3 3 3 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 / Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 / (Stp 6) (Stp 7) 3 3 3 3 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 3 / Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #33 /