0//0 Prvw Spnnng Tr Spnnng Tr Mnmum Spnnng Tr Kruskl s Algorthm Prm s Algorthm Corrctnss of Kruskl s Algorthm A spnnng tr T of connctd, undrctd grph G s tr composd of ll th vrtcs nd som (or prhps ll) of th dgs of G. Informlly, spnnng tr of G s slcton of dgs of G tht form tr spnnng vry vrtx. Evry vrtx ls n th tr, but no cycls (or loops) r formd. COSC 0 - Sprng 0 COSC 0 - Sprng 0 Mnmum Spnnng Tr A spnnng tr tht hs mnmum totl wght s clld mnmum spnnng tr for th grph. Tchnclly t s mnmum-wght spnnng tr. If ll dgs hv th sm wght, brdth-frst srch or dpth-frst srch wll yld mnmum spnnng trs. For th rst of ths dscusson, w ssum th dgs hv wghts ssoctd wth thm. Mnmum Spnnng Tr Mnmum-cost spnnng trs hv mny pplctons. Buldng cbl ntworks tht jon n loctons wth mnmum cost. Buldng rod ntwork tht jons n cts wth mnmum cost. Obtnng n ndpndnt st of crcut qutons for n lctrcl ntwork. In pttrn rcognton mnml spnnng trs cn b usd to fnd nosy pxls. COSC 0 - Sprng 0 COSC 0 - Sprng 0 Mnmum Spnnng Tr Consdr ths grph. It hs 0 spnnng trs. Som r: Thr r two mnmumcost spnnng trs, ch wth cost of : COSC 0 - Sprng 0 Mnmum Spnnng Tr Brut Forc opton:. For ll possbl spnnng trs. Clcult th sum of th dg wghts. Kp trck of th tr wth th mnmum wght. Stp () rqurs N- tm, snc ch tr wll hv xctly N- dgs. If thr r M spnnng trs, thn th totl cost wll O(MN). Consdr complt grph wth N(N-) dgs. How bg cn M b? COSC 0 - Sprng 0
0//0 Brut Forc MST For complt grph, t hs bn shown tht thr r N N- possbl spnnng trs! Altrntvly, gvn N tms, you cn buld N N- dstnct trs to connct ths tms. Input: connctd undrctd grph G = (V, E) whr for ch dg (u, v) E, w hv wght w(u, v) Output: Fnd n cyclc subst T E tht conncts ll of th vrtcs nd whos totl wght s mnmzd. COSC 0 - Sprng 0 COSC 0 - Sprng 0 Th Mnmum Spnnng Tr lgorthms w wll look t r grdy lgorthms. Grdy lgorthms lwys mk chocs tht look bst t th momnt. Thy mk loclly optml chocs n th hop tht ths chocs wll ld to globlly optml soluton. It s mportnt to not tht grdy lgorthms do not lwys yld optml solutons. COSC 0 - Sprng 0 Kruskl s Algorthm Kruskl s lgorthm Kruskl s lgorthm s grdy lgorthm, bcus t ch stp t dds to th forst n dg of lst possbl wght. Spcfclly, th lgorthm chooss th lowst wght dg tht jons two dsconnctd trs nto sngl tr. COSC 0 - Sprng 0 0 Kruskl s Algorthm Kruskl (G, w) { A = Strt wth forst whr thr r V trs Sort th dgs of E by non-dcrsng wght w. for ch dg (u, v) E, n ordr by ncrsng wght { If u nd v do not blong to th sm tr { A = A {(u, v)} Mk two trs connct wth dg (u, v). } } } Kruskl s Algorthm 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0
0//0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 5 COSC 0 - Sprng 0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0
0//0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0 0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0
0//0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 5 COSC 0 - Sprng 0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0 0 5
0//0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 5 COSC 0 - Sprng 0
0//0 Kruskl s Algorthm Kruskl s Algorthm 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0 Kruskl s Algorthm (Prm s Algorthm) 0 Prm s lgorthm hs th proprty tht th dgs n th st E lwys form sngl tr. It strts wth ny nod nd xpnds untl th tr spns ll th vrtcs n V. At ch stp, lght dg connctng vrtx n A to vrtx n V-A s ddd to th tr. Ths strtgy s grdy snc n dg wth mnmum mount possbl to th tr s wght s chosn to buld tr. Th ky to mplmntng Prm s lgorthm s to mk t sy to slct nw dg to b ddd to th tr formd by th dgs n A. COSC 0 - Sprng 0 COSC 0 - Sprng 0 0 (Prm s Algorthm) Implmntton of Prm s Algorthm Ech nod u V hs two lmnts: u.ky nd u.prnt. u.ky s usd to fnd out th mnmum possbl conncton to th nod (usng prorty quu, Q) u.prnt s usd for buldng tr. (Prm s Algorthm) Prm(V, E) { Intlz: V nw = {r}, whr r s n rbtrry nod r V, E nw = {} Rpt untl V nw = V { Choos n dg {u, v} wth mnml wght such tht u V nw nd v V - V nw // If thr r multpl dgs wth th sm wght, // ny of thm my b pckd Add v to V nw, nd {(u, v)} to E nw } rturn V nw nd E nw dscrb mnml spnnng tr } COSC 0 - Sprng 0 COSC 0 - Sprng 0
0//0 (Prm s Algorthm) (Prm s Algorthm) 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0 (Prm s Algorthm) (Prm s Algorthm) 0 0 COSC 0 - Sprng 0 5 COSC 0 - Sprng 0 (Prm s Algorthm) (Prm s Algorthm) 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0
0//0 (Prm s Algorthm) (Prm s Algorthm) 0 0 COSC 0 - Sprng 0 COSC 0 - Sprng 0 50 (Prm s Algorthm) (Prm s Algorthm) 0 0 COSC 0 - Sprng 0 5 COSC 0 - Sprng 0 5 (Prm s Algorthm) (Prm s Algorthm) 0 0 COSC 0 - Sprng 0 5 COSC 0 - Sprng 0 5
0//0 (Prm s Algorthm) (Prm s Algorthm) 0 0 COSC 0 - Sprng 0 55 COSC 0 - Sprng 0 5 (Prm s Algorthm) (Prm s Algorthm) 0 0 COSC 0 - Sprng 0 5 COSC 0 - Sprng 0 5 (Prm s Algorthm) (Prm s Algorthm) Runnng tm of Prm s Algorthm 0 Intlzton nd buldng Hp: O(V) Extrct Mn s O(log V), nd thr r V tms for xtrct mn: O(V log V) Th for loop xcuts O(E) tms, nvstgtng whthr or not n Q tk constnt tm by kp bt mtrx for Q: O(E) Dcrsng ky v.ky = w(u, v) tks O(log V) for djustng prorty quu: O(E log V) COSC 0 - Sprng 0 5 COSC 0 - Sprng 0 0 0
0//0 Prm s lgorthm wth n Adjcncy Mtrx A cbl compny wnt to connct fv vllgs to thr ntwork whch currntly xtnds to th mrkt town of Avnford. Wht s th mnmum lngth of cbl ndd? Avnford Brnlgh 5 Fngly 5 Cornwll Donstr Prm s lgorthm wth n Adjcncy Mtrx Not, ths xmpl hs outgong dgs on th columns nd ncomng on th rows, so t s th trnspos of djcncy mtrx mntond n clss. Actully, t s n undrctd, so A T = A. A - - - B - 5 - - C - 5 - - D - - - E - - - 5 F 5 - Edn COSC 0 - Sprng 0 COSC 0 - Sprng 0 Strt t vrtx A. Lbl column A. Dlt row A Slct th smllst ntry n column A (AB, lngth ) Lbl column B Dlt row B Slct th smllst uncovrd ntry n thr column A or column B (AE, lngth ) Avnford Brnlgh A - - - B - 5 - - C - 5 - - D - - - E - - - 5 F 5 - Brnlgh Avnford Edn A - - - B - 5 - - C - 5 - - D - - - E - - - 5 F 5 - COSC 0 - Sprng 0 COSC 0 - Sprng 0 Lbl column E Dlt row E Slct th smllst uncovrd ntry n thr column A, B or E (ED, lngth ) Lbl column D Dlt row D Slct th smllst uncovrd ntry n thr column A, B, D or E (DC, lngth ) Brnlgh Avnford Edn Donstr A - - - B - 5 - - C - 5 - - D - - - E - - - 5 F 5 - Brnlgh Avnford Edn Cornwll Donstr A - - - B - 5 - - C - 5 - - D - - - E - - - 5 F 5 - COSC 0 - Sprng 0 5 COSC 0 - Sprng 0
0//0 Lbl column C 5 Dlt row C Slct th smllst uncovrd ntry n thr column A, B, D, E or C (EF, lngth 5) Avnford Brnlgh Fngly 5 Cornwll Donstr 5 A - - - B - 5 - - C - 5 - - D - - - E - - - 5 F 5 - FINALLY Lbl column F Dlt row F Brnlgh Avnford Cornwll Fngly Donstr 5 5 A - - - B - 5 - - C - 5 - - D - - - E - - - 5 F 5 - Edn Edn COSC 0 - Sprng 0 COSC 0 - Sprng 0 FINALLY Avnford Lbl column F Dlt row F Brnlgh Edn Fngly 5 Cornwll Donstr 5 A - - - B - 5 - - C - 5 - - D - - - E - - - 5 F 5 - Th spnnng tr s shown n th dgrm Lngth + + + + 5 = Km COSC 0 - Sprng 0 Prctc G B. Fnd th brdth-frst spnnng tr nd dpth-frst spnnng tr of th grph G A shown bov.. For th grph G B shown bov, trc th xcuton of Prm's lgorthm s t fnds th mnmum-cost spnnng tr of th grph strtng from vrtx. COSC 0 - Sprng 0 0 Corrctnss of Mnmum Spnnng tr Corrctnss of MST Algorthm Lt G = (V, E) s undrctd wghtd grph. Kruskul s lgorthm strts wth V forsts nd ultmtly crts mnmum spnnng tr T whch ncluds ll vrtcs n V. Prm s lgorthm strts wth tr wth sngl nod nd vntully bulds mnmum spnnng tr. Both lgorthms us grdy mthods to fnd n dg (u, v) whch s loclly optml. Corrctnss of Mnmum Spnnng tr Algorthm mngs st of dgs A whch s subst of mnmum spnnng tr. For ch trton lgorthm pck n dg (u, v) such tht A { (u, v)} s lso subst of mnmum spnnng tr. W cll such n dg sf dg for A. A cut (S, V S) of undrctd grph G = (V, E) s prtton of V. COSC 0 - Sprng 0 COSC 0 - Sprng 0
0//0 Corrctnss of Mnmum Spnnng tr Corrctnss of Mnmum Spnnng tr W sy n dg (u, v) E crosss th cut (S, V S) f on vrtx blongs to S nd othr blongs to V S. W sy cut rspcts st A of dgs f no dgs n A cross th cut. An dg (u, v) s lght dg crossng cut f ts wght w (u, v) s th mnmum of ny dg crossng th cut 0 Lght dg = (c, d) COSC 0 - Sprng 0 COSC 0 - Sprng 0 Corrctnss of Mnmum Spnnng tr Corrctnss of Mnmum Spnnng tr Thorm) Lt G = (V, E) b connctd undrctd grph wth wght w(u, v) for ch (u, v) E. Lt A E whch s prt of mnmum spnnng tr for G. Lt (S, V S) s ny cut of G rspct A. Lt (u, v) b lght dg crossng (S, V S). Thn dg (u, v) s sf for A. Prov) By Contrdcton Lt T b mnmum spnnng tr ncludng A but not lght dg (u, v). Lt s try to buld nothr mnmum spnnng tr T whch ncluds A (u, v) by modfyng T. Snc T s mnmum spnnng tr, thr should b pth from u to v. COSC 0 - Sprng 0 5 COSC 0 - Sprng 0 Corrctnss of Mnmum Spnnng tr Corrctnss of Mnmum Spnnng tr x x u u y v y v Snc u nd v r on oppost sd of cut (S, V S), Thr must t ls on dg (x, y) n T on th pth clos th cut. And lso (x, y) s not n A snc th cut rspct A. If w rmov dg (x, y) from th T. thr r two componnts Addng (u, v) crt nw tr T Snc (u, v) s lght wght dg, w(u, v) w (x, y) Totl wght of nw tr T s w(t ) = w(t) w(x,y) + w(u, v) w(t) If (u, v) s unqu lght wght dg, W(T ) < W(T) contrdcton W cn conclud wth tht dg (u, v) s sf for A. COSC 0 - Sprng 0 COSC 0 - Sprng 0
0//0 Corrctnss of Mnmum Spnnng tr Thorm) Prov Mnmum spnnng tr lgorthm crts mnmum spnnng tr. Prov) Kruskul s nd Prm s lgorthms choos n dg (u, v) tht s sf for A for ch trton. And wth lmm MST lgorthm must crt MST. COSC 0 - Sprng 0