Dt Strutur LECTURE Shortt pth lgorithm Proprti of hortt pth Bllmn-For lgorithm Dijktr lgorithm Chptr in th txtook (pp ). Wight grph -- rminr A wight grph i grph in whih g hv wight (ot) w(v i, v j ) >. A grph i wight grph in whih ll ot r. Two vrti with no g (pth) twn thm n thought of hving n g (pth) with wight. Th ot of pth i th um of th ot of it g: k w p w v i, v i i Exmpl: wight grph Two i proprti of hortt pth Tringl inqulity Lt G=(V,E) wight irt grph, w: E R wight funtion n V our vrtx. Thn, for ll g =(u,v) E: (,v) (,u) + w(u,v) Optiml utrutur of hortt pth Lt p = <v,.. v k > th hortt pth twn v n v k. Th u-pth twn v i n v j, whr i,j k, p ij = <v i,.. v j > i hortt pth. Ngtiv-wight g Shortt pth r wll-fin long thr r no ngtiv-wight yl. In uh yl, th longr th pth, th lowr th vlu th hortt pth h n infinit numr of g! - - Allow ngtiv-wight g, ut illow (or tt) ngtiv-wight yl! Shortt pth n yl Th hortt pth twn ny two vrti h no poitiv-wight yl. Th rprnttion for hortt pth twn vrtx n ll othr vrti i th m th on u in th unwigth BFS: rth-firt tr: G = (V,E ) uh tht V = {v V: [v] null} {} n E = {( [v],v), v V {}} W will prov tht rth-firt tr i hortt-pth tr for it root in whih vrti rhl from r in it n th uniqu impl pth from to v i hortt.
Exmpl: hortt-pth tr Exmpl: hortt-pth tr Etimt itn from our A for BFS on unwight grph, w kp ll whih i th urrnt t timt of th hortt itn twn n v. Initilly, it[] = n it[v] = for ll v, n [v] = null. At ll tim uring th lgorithm, it[v] (,v). At th n, it[v] = (,v) n ( [v],v) E Eg rlxtion Th pro of rlxing n g (u,v) onit of tting whthr it n improv th hortt pth from to v o fr y going through u. Rlx(u,v) if it[v] > it[u] + w(u,v) thn it[v] it[u] + w(u,v) [v] u Proprti of hortt pth n rlxtion Tringl inqulity = (u,v) E: (,v) (,u) + w(u,v) Uppr-ounry proprty v V: it[v] (,v) t ll tim, n it kp ring. No-pth proprty if thr i no pth from to v, thn it[v]= (,v) = Proprti of hortt pth n rlxtion Convrgn proprty if u v i hortt pth in G for om u n v, n it[u]= (,u) t ny tim prior to rlxing g (u,v), thn it[v]= (,v) t ll tim ftrwr. Pth-rlxtion proprty Lt p = <v,.. v k > th hortt pth twn v n v k. Whn th g r rlx in th orr (v, v ), (v, v ), (v k-, v k ), thn it[v k ]= (,v k ). Pror u-grph proprty on it[v]= (,v) v V, th pror ugrph i hortt-pth tr root t.
Two hortt-pth lgorithm. Bllmnn-For lgorithm. Dijktr lgorithm Gnrliztion of BFS Bllmn-For lgorithm: ovrviw Allow ngtiv wight. If thr i ngtiv yl, rturn ngtiv yl xit. Th i: Thr i hortt pth from to ny othr vrtx tht o not ontin non-ngtiv yl (n limint to prou hortr pth). Th mximl numr of g in uh pth with no yl i V, u it n hv t mot V no on th pth if thr i no yl. it i nough to hk pth of up to V g. Bllmn-For lgorithm Bllmn - For( G, ) Initiliz( G, ) for i to V for h g u, v E o if it[v] it[u] w u, v it[ v ] it[ u] w( u, v ) [ v ] u for h g u, v E if it v u w u, v rturn " ngtiv yl" Exmpl: Bllmn-For lgorithm () - - - Eg orr (,) (,) (,) (,) (,) (,) (,) (,) (,) (,) Exmpl: Bllmn-For lgorithm () Exmpl: Bllmn-For lgorithm () Eg Eg orr orr - (,) - (,) (,) (,) - (,) - (,) - (,) (,) (,) - (,) (,) (,) (,) (,) (,) (,) (,) (,) (,) (,)
Exmpl: Bllmn-For lgorithm () - - - Eg orr (,) (,) (,) (,) (,) (,) (,) (,) (,) (,) Exmpl: Bllmn-For lgorithm () Eg orr - (,) (,) - (,) (,) - (,) (,) (,) (,) (,) - (,) Bllmn-For lgorithm: proprti Th firt p ovr th g only nighor of r fft (-g pth). All hortt pth with on g r foun. Th on p hortt pth with g r foun. Aftr V - p, ll poil pth r hk. Clim: w n to upt ny vrtx in th lt p if n only if thr i ngtiv yl rhl from in G. Bllmn For lgorithm: proof () On irtion w lry know: if w n to upt n g in th lt itrtion thn thr i ngtiv yl, u w prov for tht if thr r no ngtiv yl, n th hortt pth r wll fin, w fin thm in th V itrtion. W lim tht if thr i ngtiv yl, w will iovr prolm in th lt itrtion. Bu, uppo thr i ngtiv yl v,... vk, vk v But th lgorithm o not fin ny prolm in th lt itrtion, whih mn tht for ll g, w hv tht it [v] it[u] w( u, v) for ll g in th yl. Bllmn For lgorithm: proof () Proof y ontrition: for ll g in th yl it[v] it[v] w v, v it[v ] it[v ] w v, v... it[vk ] it[vk- ] w vk, vk k k k it[ vi ] it[ vi] w( vi, vi ) i i i Aftr umming up ovr ll g in th yl, w iovr tht th trm on th lft i qul to th firt trm on th right (jut iffrnt orr of ummtion). W n utrt thm, n w gt tht th yl i tully poitiv, whih i ontrition. Bllmn-For lgorithm: omplxity Viit V vrti O( V ) Prform vrtx rlxtion on ll g O( E ) Ovrll, O( V. E ) tim n O( V + E ) p.
Bllmn-For on DAG Exmpl: Bllmn-For on DAG () For Dirt Ayli Grph (DAG), O( V + E ) rlxtion r uffiint whn th vrti r viit in topologilly ort orr: DAG-Shortt-Pth(G). Topologilly ort th vrti in G. Initiliz G (it[v] n (v)) with our.. for h vrtx u in topologilly ort orr o. for h vrtx v inint to u o. Rlx(u,v) - Vrti ort from lft to right - Exmpl: Bllmn-For on DAG () Exmpl: Bllmn-For on DAG () - - - - Exmpl: Bllmn-For on DAG () Exmpl: Bllmn-For on DAG () - - - -
Exmpl: Bllmn-For on DAG () Exmpl: Bllmn-For on DAG () - - - - Bllmn-For on DAG: orrtn Pth-rlxtion proprty Lt p = <v,.. v k > th hortt pth twn v n v k. Whn th g r rlx in th orr (v, v ), (v, v ), (v k-, v k ), thn it[v k ]= (,v k ). In DAG, w hv th orrt orring! Thrfor, th omplxity i O( V + E ). Dijktr lgorithm: ovrviw I: Do th m BFS for unwight grph, with two iffrn: u th ot th itn funtion u minimum priority quu int of impl quu. Th BFS lgorithm BFS(G, ) ll[] urrnt; it[] = ; [] = null for ll vrti u in V {} o ll[u] not_viit; it[u] = ; [u] = null EnQuu(Q,) whil Q i not mpty o u DQuu(Q) for h v tht i nighor of u o if ll[v] = not_viit thn ll[v] urrnt it[v] it[u] + ; [v] u EnQuu(Q,v) ll[u] viit Exmpl: BFS lgorithm
Exmpl: Dijktr lgorithm Dijktr lgorithm Dijktr(G, ) ll[] urrnt; it[] = ; [u] = null for ll vrti u in V {} o ll[u] not_viit; it[u] = ; [u] = null Q whil Q i not mpty o u Extrt-Min(Q) for h v tht i nighor of u o if ll[v] = not_viit thn ll[v] urrnt if [v] > [u] + w(u,v) thn [v] [u] + w(u,v); [v] = u Inrt-Quu(Q,v) Dt Strutur, ll[u] Spring L. = Jokowiz viit Exmpl: Dijktr lgorithm () Exmpl: Dijktr lgorithm () Exmpl: Dijktr lgorithm () Exmpl: Dijktr lgorithm ()
Exmpl: Dijktr lgorithm () Exmpl: Dijktr lgorithm () Dijktr lgorithm: orrtn () Thorm: Upon trmintion of th Dijktr lgorithm, for h it[v] = (,v) for h vrtx v V, Dfinition: pth from to v i i to pil pth if it i th hortt pth from to v in whih ll vrti (xpt my for v) r ini S. Lmm: At th n of h itrtion of th whil loop, th following two proprti hol:. For h w S, it[w] i th lngth of th hortt pth from to w whih ty ini S.. For h w V S, it(w) i th lngth of th hortt pil pth from to w. Dijktr lgorithm: orrtn () Proof: y inution on th iz of S. For S =, it i lrly tru: it[v] = xpt for th nighor of, whih ontin th lngth of th hortt pil pth. Inution tp: uppo tht in th lt itrtion no v w to S. By th inution umption, it[v] i th lngth of th hortt pil pth to v. It i lo th lngth of th gnrl hortt pth to v, in if thr i hortr pth to v ping through no of S, n x i th firt no of S in tht pth, thn x woul hv n lt n not v. So th firt proprty till hol. Th Dt Strutur, thorm Spring L. Jokowiz follow whn S = V. Dijktr lgorithm: orrtn () Proprty : Lt x S. Conir th hortt nw pil pth to w If it on t inlu v, it[x] i th lngth of tht pth y th inution umption from th lt itrtion in it[x] i not hng in th finl itrtion. If it o inlu v, thn v n ithr no in th mil or th lt no for x. Not tht v nnot no in th mil in thn th pth woul p from to v to y in S, ut y proprty, th hortt pth to y woul hv n ini S v n not inlu. If v i th lt no for x on th pth, thn it[x] ontin th itn of tht pth, y th utitution it[x] = it[v] + w(v,x) in th lgorithm. Dijktr lgorithm: omplxity Th lgorithm prform V Extrt-Min oprtion n E Inrt-Quu oprtion. Whn th priority quu i implmnt hp, inrt tk O(lg V ) n Extrt-Min tk O(lg( V ). Th totl tim i O( V lg V ) + O( E lg V ) = O( E lg V ) Whn E = O( V ), thi i not optiml. In thi, thr r mny mor inrt thn xtrt oprtion. Solution: Implmnt th priority quu n rry! Inrt will tk O() n Extrt-Min O( V ) O( V ) + O( E ) = O( V ) whih i ttr thn th hp long E i O( V /lg ( V )).
Applition: iffrn ontrint Givn ytm of m iffrn ontrint ovr n vril, fin olution if on xit. x i x j k for i, j n n k m Contrint grph G: h vril x i i vrtx, h ontrint x i x j k i irt g from x i to x j with wight k. Whn G o not hv ngtiv yl, th minimum pth itn of th vrti r th olution to th ytm of ontrint iffrn. Exmpl: iffrn ontrint () x x x x - x x x x x x x x - x x - x x - Solution: x = (-,-,,-,-) x - x - - x x - x Exmpl: iffrn ontrint Solution: x = (-,-,,-,-) x - - - - - x x x x - - - Why o thi work? Thorm: Lt Ax t of m iffrn ontrint ovr n vril, n G=(V,E) it orrponing ontrint grph. If G h no ngtiv wight yl, thn x = ( (v,v ), (v,v ),, (v,v n )) i fil olution for th ytm. If G h ngtiv yl, thn thr i no fil olution. Proof outlin: For ll g (v i,v j ) in E: (v,v j ) (v,v i ) + w(v i,v j ) (v,v j ) (v,v i ) w(v i,v j ) x j x j w(v i,v j ) Summry Solving th hortt-pth prolm on wight grph i prform y rlxtion, on th pth tringl inqulity: for ll g =(u,v) E: (,v) (,u) + w(u,v) Two lgorithm for olving th prolm: Bllmn For: for h vrtx, rlxtion on ll g. Tk O( E. V ) tim. Work on grph with nonngtiv yl. Dijktr: BFS-lik, tk O( E lg V ) tim.