Outlin Computr Sin 33 Computtion o Minimum-Cost Spnnin Trs Prim s Mik Joson Dprtmnt o Computr Sin Univrsity o Clry Ltur #34 Introution Min-Cost Spnnin Trs 3 Gnrl Constrution 4 5 Trmintion n Eiiny 6 Aitionl Commnts n Rrns Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 / 0 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 / 0 Introution Computtion o Min-Cost Spnnin Trs Min-Cost Spnnin Trs 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 initions n to ormlly in prolm motivt y th ov prsnttion o n lorithm (Prim s ) or solvin th prolm Rll tht i G = (V, E) is onnt, unirt rph, thn spnnin tr o G is surph Ĝ = ( V, Ê) suh tht V = V (so Ĝ inlus ll th vrtis in G) Ĝ is tr Suppos now tht G = (V, E) is onnt wiht rph with wiht untion w : E 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 ) = E w(). Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 3 / 0 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 4 / 0
Min-Cost Spnnin Trs Min-Cost Spnnin Trs Suppos G is wiht rph with wihts s shown low. Th ost o th ollowin spnnin tr, G = (V, E ), is 8. 5 3 3 4 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 5 / 0 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 6 / 0 Min-Cost Spnnin Trs Min-Cost Spnnin Trs Minimum-Cost Spnnin Trs Th ost o th ollowin spnnin tr, G = (V, E ), is 6. Suppos (G, w) is wiht rph. 5 A surph G o G is minimum-ost spnnin tr o (G, w) i th ollowin proprtis r stisi. 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 #34 7 / 0 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 8 / 0
Gnrl Constrution Builin Minimum-Cost Spnnin Tr Gnrl Constrution Builin Minimum-Cost Spnnin Tr To onstrut minimum-ost spnnin tr o G = (V, E): Strt with Ĝ = ( V, Ê), whr V V n Ê =. Not: Ĝ is surph o som minimum-ost spnnin tr o (G, w). Rptly vrtis (i nssry) n s nsurin tht Ĝ is still surph o minimum-ost spnnin tr s you o so. Continu oin this until V = V n Ê = V (so tht Ĝ is spnnin tr o Ĝ). 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 Ĝ is lwys tr. As rsult, this lorithm is struturlly vry similr to Dijkstr s to omput minimum-ost pths (whih w hv lry isuss in lss). Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 9 / 0 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 0 / 0 Spiition o Rquirmnts Dt Struturs Pr-Conition G = (V, E) is onnt rph with wiht untion w Post-Conition: π is untion π : V V {NIL} I Ê = {(π(v), v) v V n π(v) NIL} thn (V, Ê) is minimum-ost spnnin tr or G Th rph G = (V, E) n its wiht untion hv not n hn 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. Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 / 0 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 / 0
Psuoo Psuoo, Continu 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 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 π Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 3 / 0 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 4 / 0 Psuoo, Conlu 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 Stp 7: Extrt-Min (rturns (, )) olor lk on! E. on MST (totl ost is 8): 0 π - Q: (mpty) {(π(), ), (π(), ), (π(), ), (π(), ), (π( ), ), (π(), )} = {(, ), (, ), (, ), (, ), (, ), (, )} Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 5 / 0 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 6 / 0
Trmintion n Eiiny Trmintion n Eiiny Trmintion n Eiiny Trmintion n Eiiny (ont.) Clim: I MST-Prim is xut on wiht unirt rph G = (V, E) thn th lorithm trmints tr prormin O(( V + E ) lo V ) stps in th worst s. Proo. This is virtully intil to th proo o th orrsponin rsult or Dijkstr s lorithm (to omput minimum-ost pths). Th numr o oprtions on th priority quu, n th numr o oprtions tht o not involv this t strutur, r h in Θ( V + E ) in th worst s (y th rumnt tht hs n ppli to th lst thr lorithms onsir). Proo (ontinu). Sin th siz o th priority quu nvr xs V n sin th only oprtions on th priority quu us r insrtions, rss o ky vlus, n xtrtions o th minimum (top priority) lmnt, th ost o h oprtion on th t strutur is in O(lo V ). It ollows immitly tht th totl numr o stps is in O(( V + E ) lo V ), s lim. O( V lo V + E ) usin Fioni hp (mortiz) Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 7 / 0 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 8 / 0 Aitionl Commnts n Rrns Aitionl Commnts Rrns Aitionl Commnts n Rrns On Gry s Prim s lorithm is n xmpl o ry lorithm: A lol optimiztion prolm (inin minimum-ost spnnin tr) is solv y mkin squn o lol ry hois (y xtnin tr with s whos wihts r s smll s possil). Provin orrtnss o ry lorithms is otn hllnin. In, ry huristis r otn inorrt. On th othr hn, whn thy r orrt, ry lorithms r rquntly simplr n mor iint thn othr lorithms or th sm omputtion. S CPSC 43 or mor out ry lorithms! Furthr Rin n Jv Co: Introution to s, Chptr 3 Chptr 3 inlus Prim s lorithm lon with nothr ry lorithm or this prolm (Kruskl s lorithm). Dt Struturs & s in Jv, Chptr 4 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 9 / 0 Mik Joson (Univrsity o Clry) Computr Sin 33 Ltur #34 0 / 0