S 445 Shortst Paths n Graphs lon frat Sls courtsy of rk man an arola Wnk Paths n raphs onsr a raph G = (V, ) wth -wht functon w : R. Th wht of path p = v v v k s fn to xampl: k = w ( p) = w( v, v + ). v 4 v 5 v v 4 v 5 w(p) = Shortst paths shortst path from u to v s a path of mnmum wht from u to v. Th shortstpath wht from u to v s fn as δ(u, v) = mn{w(p) : p s a path from u to v}. lso call stanc of u from v Not: δ(u, v) = f no path from u to v xsts.
Th stanc s not always wll fn cycl wth natv wht s call (surprs) a natv cycl Th stanc twn nos mht not fn n th prsnc of natv cycls. xampl: v v 4 5 v v 4 v 5 δ(v, v 5 )=? Optmal sustructur Thorm. supath of a shortst path s a shortst path. Proof. ut an past: s u v t Tranl nqualty Thorm. For all u, v, x V, w hav Proof. u δ(u, v) δ(u, x) + δ(x, v). δ(u, v) v δ(u, x) δ(x, v) x Not: Ths os not mply that w(u,v) w(u,x)+w(x,v) u x v
Prorty Quu ky[v] ky[rht[v]] ky[v] ky[lft[v]] 5 Insrt(x), xtract_mn cras(x,δ) 5 5 5 45 ll on n O(lo n) FS Gvn a raph G(V,) whr th cost of ach s. FS fns th stanc δ(s, v) from th sourc s, to vry vrtx v s c f h ffrnt way to thnk aout FS al Thnk aout th rppls n th pool aftr a pul was ropp at th sourc. vry vrtx s scovr onc th frontr hts t. Sourc s c f h Frontr Th stanc δ(s, v) s th tm whn th vrtx s ht.
ffrnt way to thnk aout jkstra l Now th s hav whts. (assum ntrs) an thnk aout th rppls n th pool aftr a pul was ropp at th sourc. Mofy th Graph Rplac an (s,) of s wht y s of f h wht. Rplac an (u,v) wth wht w(u,v) y s of wht. Th stanc δ(s, v) s th tm whn th vrtx s ht. c Run FS jkstra al s an ffcnt way to smulat ths procss Snl-sourc shortst paths Prolm. From a vn sourc vrtx s V, fn th shortst-path whts δ(s, v) for all v V. If all whts w(u, v) ar nonnatv, all shortst-path whts must xst. W ll us jkstra s. I: Gry.. Mantan a st S of vrtcs whos shortst-path stancs from s ar known. lso mantan stanc stmats to th othr vrtcs.. t ach stp a to S th vrtx v V S whos stanc stmat from s s mnmal.. Upat th stanc stmats of vrtcs ajacnt to v. S s p V v Intrnal paths - fnton. Lt S a st of vrtcs (that contans s ). W say that path p s ntrnal to S f all ts vrtcs, xclun may th last on, ar n S.. stanc stmaton: Th mantans for vry vrtx v th valu [v], whch s th lnth of th shortst path from s to v, whch s ntrnal to S. 4. Wll show: If v s n S, thn [v]= δ(s, v) S V s p v
jkstra s [s] for ach v V {s} o [v] S Q V Q s a prorty quu mantann V S, sort as on [ ] whl Q o u XTRT-MIN(Q) S S {u} for ach v j[u] /* all nrs of u */ o f [v] > [u] + w(u, v) thn [v] [u] + w(u, v) Implct RS-KY rlaxaton stp xampl of jkstra s Graph wth nonnatv whts: Intalz: xampl of jkstra s S: {}
xampl of jkstra s XTRT-MIN(Q): S: { } xampl of jkstra s Rlax all s lavn : - - S: { } xampl of jkstra s XTRT-MIN(Q): - - S: {, }
xampl of jkstra s Rlax all s lavn : - - 5 5 S: {, } xampl of jkstra s XTRT-MIN(Q): - - 5 5 S: {,, } xampl of jkstra s Rlax all s lavn : 5 5 S: {,, }
xampl of jkstra s XTRT-MIN(Q): 5 5 S: {,,, } xampl of jkstra s Rlax all s lavn : 5 9 9 5 S: {,,, } xampl of jkstra s XTRT-MIN(Q): 5 9 9 5 S: {,,,, }
orrctnss Part I Lmma. t any sta of th, an for vry vrtx v V, t s always tru that [v] δ(s, v). Proof. It s tru aftr ntalzaton (trvally). Suppos not. Lt v th frst (chronolocally) vrtx for whch [v] < δ(s, v), an lt u th vrtx that caus [v] to chan: [v] = [u] + w(u, v). Thn, [v] < δ(s, v) supposton δ(s, u) + δ(u, v) tranl nqualty δ(s,u) + w(u, v) sh. path spcfc path [u] + w(u, v) v s frst volaton ontracton. Hanwav: [v] s th lnth of a path to v, whl δ(s,v) s th lnth of th shortst path to v. Mor accuratly. [v] s th lnth of shortst ntrnal path. orrctnss Part II Thorm. Whn th trmnats, [v] = δ(s, v), v V. Proof. It suffcs to show that [v] = δ(s, v), whn v s a to S. Suppos u s th frst vrtx a to S for whch [u] > δ(s, u). Rcall: [u] δ(s, u) always. Lt y th frst vrtx n V S alon a shortst path from s to u, an lt x ts prcssor: S, just for an u. s x y u orrctnss Part II (contnu) s S x y u ssumpton: [u] > δ(s, u). Snc u s th frst vrtx volatn th clam nvarant, [x] = δ(s, x). Snc supaths of shortst paths ar shortst paths, δ(s, y)= δ(s, x) + w(x, y) Whn x jon S, w prform a rlaxaton stp: [y]=mn{[y], [x]+w(x,y)} so [y]=δ(s, y) If u s y w ar on. So assum u s not y. W hav [y] = δ(s, y) δ(s, u) < [u]. ut, [u] [y] y our choc of u, a contracton.
nalyss of jkstra V tms r(u) tms whl Q o u XTRT-MIN(Q) S S {u} for ach v j[u] o f [v] > [u] + w(u, v) thn [v] [u] + w(u, v) Hanshakn Lmma Θ() mplct RS_KY s. Tm = Θ(V) T XTRT-MIN + Θ() T RS-KY nalyss of jkstra (contnu) Tm = Θ(V) T XTRT-MIN + Θ() T RS-KY Q T XTRT-MIN T RS-KY Total array O(V) O() O(V ) nary hap Fonacc hap O(l V) O(l V) O( l V) O(l V) amortz O() amortz O( + V l V) worst cas How to fn th actual shortst paths? Stor a prcssor tr: [s] ; for ach v V π[v] NULL for ach v V {s} o [v] S Q V Q s a prorty quu mantann V S whl Q o u XTRT-MIN(Q) S S {u} for ach v j[u] o f [v] > [u] + w(u, v) thn [v] [u] + w(u, v) π[v] u //Proucn s of // th shortst paths tr onvnc yourslf that n th s {(v, π [v ]) v n V } Form a tr. What s ts root? Why ar thr no cycls?
xampl of jkstra s Graph wth nonnatv whts: Intalz: xampl of jkstra s S: {} xampl of jkstra s XTRT-MIN(Q): S: { } π: - - - - -
xampl of jkstra s Rlax all s lavn : - - S: { } π: - Α Α - - xampl of jkstra s XTRT-MIN(Q): - - S: {, } π: - Α Α - - xampl of jkstra s Rlax all s lavn : - - 5 5 S: {, } π: - Α
xampl of jkstra s XTRT-MIN(Q): - - 5 5 S: {,, } π: - Α xampl of jkstra s Rlax all s lavn : 5 5 S: {,, } π: - Α xampl of jkstra s XTRT-MIN(Q): 5 5 S: {,,, } π: - Α
xampl of jkstra s Rlax all s lavn : 5 9 5 S: {,,, } 9 π: - Α xampl of jkstra s XTRT-MIN(Q): 5 9 9 5 S: {,,,, } π: - Α Unwht raphs Suppos w(u, v) = for all (u, v). Us a smpl FIFO quu nsta of a prorty quu. Hr [v] s an stmaton to th stanc rath-frst sarch [s] =. for all v V-s, [v]= NQUU(Q, s) whl Q o u QUU(Q) for ach v j[u] //scan th nrs of u" o f [v] = //f not sn for thn [v] [u] + NQUU(Q, v) nalyss: Tm = O(V + ).
xampl of rath-frst sarch a f h c xampl of rath-frst sarch a f h c a xampl of rath-frst sarch a f h c a
xampl of rath-frst sarch a c a c f h xampl of rath-frst sarch a c a c f h xampl of rath-frst sarch a c a c f h
xampl of rath-frst sarch a f c a c h xampl of rath-frst sarch a 4 f h c 4 a c f xampl of rath-frst sarch 4 4 a f h c 4 4 a c f h
xampl of rath-frst sarch 4 4 a f h c 4 a c f h xampl of rath-frst sarch 4 4 a f h c a c f h xampl of rath-frst sarch 4 4 a f h c a c f h