Prim s Algorithm CS 461, Ltur 17 Jr Si Univrsity o Nw Mxio In Prim s lgorithm, th st A mintin y th lgorithm orms singl tr. Th tr strts rom n ritrry root vrtx n grows until it spns ll th vrtis in V At h stp, light g is to th tr A whih onnts A to n isolt vrtx o G A = (V, A) By our Corollry, this rul s only s gs to A, so whn th lgorithm trmints, it will rturn MST 2 Toy s Outlin Exmpl Run 2 3 18 12 18 12 2 3 2 3 18 3 14 14 4 Prim s Algorithm Brth First Srh Dpth First Srh Prim s lgorithm run on th xmpl grph, strting with th ottom vrtx. At h stg, thik gs r in A, n rrow points long A s s g, n sh gs r uslss. 1 3
An Implmnttion Prim s To implmnt Prim s lgorithm, w kp ll gs jnt to A in hp Whn w pull th minimum-wight g o th hp, w irst hk to s i oth its npoints r in A I not, w th g to A n thn th nighoring gs to th hp I w implmnt Prim s lgorithm this wy, its running tim is O( E log E ) = O( E log V ) Howvr, w n o ttr W will rk up th lgorithm into two prts, Prim-Init n Prim-Loop Prim(V,E,s){ Prim-Init(V,E,s); Prim-Loop(V,E,s); 4 6 Prim s Algorithm Prim-Init W n sp things up y notiing tht th lgorithm visits h vrtx only on Rthr thn kping th gs in th hp, w will kp hp o vrtis, whr th ky o h vrtx v is th wight o th minimum-wight g twn v n A (or ininity i thr is no suh g) Eh tim w nw g to A, w my n to rs th ky o som nighoring vrtis Prim-Init(V,E,s){ or h vrtx v in V - {s{ i ((v,s) is in E){ g(v) = (v,s); ky(v) = w((v,s)); ls{ g(v) = NULL; ky(v) = ininity; Hp-Insrt(v); 5 7
Prim-Loop Not Prim-Loop(V,E,s){ A = {; or (i = 1 to V - 1){ v = Hp-ExtrtMin(); g(v) to A; or (h g (u,v) in E){ i (u is not in A AND ky(u) > w(u,v)){ g(u) = (u,v); Hp-DrsKy(u,w(u,v)); rturn A; This nlysis ssums tht it is st to in ll th gs tht r inint to givn vrtx W hv not yt isuss how w n o this This rings us to isussion o how to rprsnt grph in omputr 8 Runtim? Grph Rprsnttion Th runtim o Prim s is omint y th ost o th hp oprtions Insrt, ExtrtMin n DrsKy Insrt n ExtrtMin r h ll O( V ) tims DrsKy is ll O( E ) tims, t most twi or h g I w us Fioni Hp, th mortiz osts o Insrt n DrsKy is O(1) n th mortiz ost o ExtrtMin is O(log V ) Thus th ovrll run tim o Prim s is O( E + V log V ) This is str thn Kruskl s unlss E = O( V ) Thr r two ommon t struturs us to xpliity rprsnt grphs Ajny Mtris Ajny Lists 9 11
Ajny Mtrix Th jny mtrix o grph G is V V mtrix o 0 s n 1 s For n jny mtrix A, th ntry A[i, j] is 1 i (i, j) E n 0 othrwis For unirt grphs, th jny mtrix is lwys symmtri: A[i, j] = A[j, i]. Also th igonl lmnts A[i, i] r ll zros Exmpl Rprsnttions g h i 0 1 1 0 0 0 0 0 0 1 0 1 1 1 0 0 0 0 1 1 0 1 1 0 0 0 0 0 1 1 0 1 1 0 0 0 0 1 1 1 0 1 0 0 0 0 0 0 1 1 0 0 0 0 g 0 0 0 0 0 0 0 1 0 h 0 0 0 0 0 0 1 0 1 i 0 0 0 0 0 0 1 1 0 g h i h g g Ajny mtrix n jny list rprsnttions or th xmpl grph. i i h 12 14 Exmpl Grph Ajny Mtrix g h i Givn n jny mtrix, w n i in Θ(1) tim whthr two vrtis r onnt y n g. W n lso list ll th nighors o vrtx in Θ( V ) tim y snning th row orrsponing to tht vrtx This is optiml in th worst s, howvr i vrtx hs w nighors, w still n to xmin vry ntry in th row to in thm ll Also, jny mtris rquir Θ( V 2 ) sp, rgrlss o how mny gs th grph hs, so it is only sp iint or vry ns grphs 13 15
Ajny Lists Tk Awy For sprs grphs grphs with rltivly w gs w r ttr o with jny lists An jny list is n rry o link lists, on list pr vrtx Eh link list stors th nighors o th orrsponing vrtx I w us th right typ o hp n th right grph rprsnttion, thn Prim s lgorithm tks O( E + V log V ) This omprs vorly with Kruskl s lgorithm whih tks O( E log V ) Kruskl s n Prims lgorithms r th two min lgorithms or ining th minimum spnning tr o onnt grph Thr r mny, mny othr typs o prolms in on grphs... 18 Ajny Lists Trvrsing Grph Th totl sp rquir or n jny list is O( V + E ) Listing ll th nighors o no v tks O(1+g(v)) tim W n trmin i (u, v) is n g in O(1 + g(u)) tim y snning th nighor list o u Not tht w n sp things up y storing th nighors o no not in lists ut rthr in hsh tls Thn w n trmin i n g is in th grph in xpt O(1) tim n still list ll th nighors o no v in O(1 + g(v)) tim Suppos w wnt to visit vry no in onnt grph (rprsnt ithr xpliitly or impliitly) Th simplst wy to o this is n lgorithm ll pth-irst srh W n writ this lgorithm rursivly or itrtivly - it s th sm oth wys, th itrtiv vrsion just mks th stk xpliit Both vrsions o th lgorithm r initilly pss sour vrtx v 17 19
Rursiv DFS Gnri Trvrs RursivDFS(v){ i ($v$ is unmrk){ mrk $v$; or h g (v,w){ RursivDFS(w); DFS is on instn o gnrl mily o grph trvrsl lgorithms This gnri grph trvrsl lgorithm stors st o nit gs in t strutur w ll ll g A g is just somthing w n put stu into n ltr tk stu out o - stks, quus n hps r ll xmpls o gs. 20 22 Itrtiv DFS Gnri Trvrs ItrtivDFS(s){ Push(s); whil (stk not mpty){ v = Pop(); i (v is unmrk){ mrk v; or h g (v,w){ Push(w); Trvrs(s){ put (nil,s) in g; whil (th g is not mpty){ tk som g (p,v) rom th g i (v is unmrk) mrk v; prnt(v) = p; or h g (v,w){ put (v,w) into th g; 21 23
Anlysis Proo Noti tht w r kping gs in th g inst o vrtis This is us w wnt to rmmr whn w visit vrtx v or th irst tim, whih prviously-visit vrtx p put v into th g This vrtx p is ll th prnt o v It s ovious tht no no is mrk mor thn on W nxt show tht h vrtx is mrk t lst on. Lt v s vrtx n lt s u v th pth rom s to v with th minimum numr o gs. (Sin th grph is onnt suh pth lwys xists) I th lgorithm mrks u, thn it must put (u, v) in th g, so it must ltr tk (u, v) out o th g, t whih point v must mrk Thus y inution on th shortst-pth istn rom s, th lgorithm mrks vry vrtx in th grph 24 Lmm Proo Trvrs(s) mrks h vrtx in onnt grph xtly on, n th st o gs (v, prnt(v)), with prnt(v) not nil, orm spnning tr o th grph. Cll n g (v, prnt(v)) with prnt(v) nil prnt g It now rmins to shown tht th prnt gs orm spnning tr o th grph For ny no v, th pth o prnt gs v prnt(v) prnt(prnt(v)) vntully ls k to s, so th st o prnt gs orm onnt grph. Sin vry no xpt s hs uniqu prnt g, th totl numr o prnt gs is xtly on lss thn th totl numr o vrtis Thus th prnt gs orm spnning tr (w ll show this in th in-lss xris) 25 27
DFS n BFS DFS vs BFS I w implmnt th g y using stk, w hv Dpth First Srh I w implmnt th g y using quu, w hv Brth First Srh Not tht DFS trs tn to long n skinny whil BFS trs r short n t In ition, th BFS tr ontins shortst pths rom th strt vrtx s to vry othr vrtx in its onnt omponnt. (hr w in th lngth o pth to th numr o gs in th pth) 28 Anlysis Finl Not Not tht i w us jny lists or th grph, th ovrh or th or loop is only onstnt pr g (no mttr how w implmnt th g) I w implmnt th g using ithr stks or quus, h oprtion on th g tks onstnt tim Hn th ovrll runtim is O( V + E ) = O( E ) Now ssum th gs r wight I w implmnt th g using priority quu, lwys xtrting th minimum wight g rom th g, thn w hv vrsion o Prim s lgorithm Eh xtrtion rom th g now tks O( E ) tim so th totl running tim is O( V + E log E ) 29 31
Exmpl A pth-irst spnning tr n rth-irst spnning tr o on omponnt o th xmpl grph, with strt vrtx. 32 In Clss Exris Consir onnt grph tht hs n vrtis n n 1 gs. Prov y inution on n tht suh grph is tr. Q: Wht is th s s? Q: Wht is th inutiv hypothsis? Q: Wht is th inutiv stp? 33