Shortest Paths Problems CS420 lecture twelve Given a - - PDF document

shortest paths problems cs420 lecture twelve
SMART_READER_LITE
LIVE PREVIEW

Shortest Paths Problems CS420 lecture twelve Given a - - PDF document

7/20/10 Shortest Paths Problems CS420 lecture twelve Given a weighted directed graph G=(V,E) find the shortest path Shortest Paths


slide-1
SLIDE 1

7/20/10 ¡ 1 ¡

CS420 ¡lecture ¡twelve ¡ Shortest ¡Paths ¡

wim ¡bohm ¡cs ¡csu ¡

Shortest ¡Paths ¡Problems ¡

  • Given ¡a ¡weighted ¡directed ¡graph ¡G=(V,E) ¡ ¡

¡ ¡ ¡ ¡find ¡the ¡shortest ¡path ¡ ¡

– path ¡length ¡is ¡the ¡sum ¡of ¡its ¡edge ¡weights. ¡ ¡

  • The ¡shortest ¡path ¡(SP) ¡from ¡u ¡to ¡v ¡is ¡∞ ¡if ¡there ¡

is ¡no ¡path ¡from ¡u ¡to ¡v. ¡ ¡

VariaIons ¡

¡1) ¡SSSP ¡(Single ¡source ¡SP): ¡find ¡the ¡SP ¡from ¡ some ¡node ¡s ¡to ¡all ¡nodes ¡in ¡the ¡graph. ¡ ¡ ¡2) ¡SPSP ¡(single ¡pair ¡SP): ¡find ¡the ¡SP ¡from ¡some ¡u ¡ to ¡some ¡v. ¡ ¡ We ¡can ¡use ¡1) ¡to ¡solve ¡2), ¡also ¡there ¡is ¡no ¡ ¡ asymptoIcally ¡faster ¡algorithm ¡for ¡2) ¡than ¡that ¡ ¡ for ¡1). ¡

VariaIons ¡

3) ¡SDSP ¡(single ¡desInaIon ¡SP) ¡can ¡use ¡1) ¡by ¡ reversing ¡its ¡edges. ¡ 4) ¡APSP ¡(all ¡pair ¡SPs) ¡could ¡be ¡solved ¡by ¡|V| ¡ applicaIons ¡of ¡1), ¡but ¡can ¡be ¡solved ¡faster. ¡ We ¡will ¡thus ¡concentrate ¡on ¡SSSP ¡and ¡APSP. ¡ ¡

slide-2
SLIDE 2

7/20/10 ¡ 2 ¡

OpImal ¡Substructure ¡

  • Op1mal ¡Substructure ¡

¡ ¡ ¡ ¡ ¡ ¡the ¡property ¡that ¡the ¡SP ¡from ¡u ¡to ¡v ¡via ¡w ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡consists ¡of ¡a ¡SP ¡from ¡u ¡to ¡w, ¡and ¡a ¡SP ¡from ¡w ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡to ¡v ¡ ¡ ¡ ¡ ¡ ¡or ¡that ¡a ¡sub ¡path ¡from ¡p ¡to ¡q ¡is ¡a ¡SP ¡from ¡p ¡ ¡ ¡ ¡ ¡ ¡ ¡to ¡q. ¡

NegaIve ¡weight ¡edges ¡and ¡cycles ¡

  • NegaIve ¡weight ¡cycles ¡create ¡an ¡ill ¡defined ¡
  • problem. ¡Why? ¡
  • If ¡some ¡weights ¡in ¡the ¡graph ¡are ¡negaIve ¡but ¡

no ¡cycle ¡in ¡the ¡graph ¡has ¡a ¡negaIve ¡length, ¡ the ¡SP ¡problem ¡stays ¡well ¡defined. ¡

  • Some ¡algorithms ¡(Dijkstra) ¡assume ¡all ¡weights ¡

to ¡be ¡posiIve, ¡some ¡(Bellman ¡Ford) ¡allow ¡ negaIve ¡weights ¡but ¡not ¡negaIve ¡length ¡

  • cycles. ¡

Zero ¡length ¡cycles ¡

  • ¡We ¡have ¡already ¡ruled ¡out ¡negaIve ¡length ¡

cycles, ¡a ¡shortest ¡path ¡cannot ¡contain ¡a ¡ posiIve ¡length ¡cycle, ¡what ¡about ¡zero ¡length ¡ cycles? ¡ ¡

  • We ¡can ¡remove ¡zero ¡length ¡cycles ¡and ¡

produce ¡a ¡path ¡with ¡the ¡same ¡length. ¡Hence ¡ we ¡can ¡assume ¡ ¡that ¡shortest ¡paths ¡have ¡no ¡ cycles ¡and ¡thus ¡have ¡at ¡most ¡|V|-­‑1 ¡edges. ¡

SSSP ¡and ¡Shortest ¡path ¡trees ¡

  • For ¡each ¡vertex ¡v ¡on ¡the ¡shortest ¡path ¡(or ¡the ¡

shortest ¡path ¡in ¡construcIon) ¡we ¡maintain ¡its ¡ predecessor ¡π(v), ¡a ¡node ¡or ¡nil. ¡

  • The ¡predecessor ¡sub ¡graph ¡Gπ ¡= ¡(Vπ, ¡Eπ) ¡has ¡

the ¡verIces ¡v ¡and ¡edges ¡π(v) ¡≠nil ¡plus ¡the ¡ source ¡s. ¡ ¡

  • Shortest ¡path ¡algorithms ¡make ¡Gπ ¡a ¡shortest ¡

path ¡tree ¡with ¡root ¡s ¡with ¡shortest ¡paths ¡to ¡all ¡ verIces ¡ ¡reachable ¡from ¡s. ¡ ¡

slide-3
SLIDE 3

7/20/10 ¡ 3 ¡

d(v) ¡

  • For ¡each ¡vertex ¡v ¡we ¡maintain ¡d(v), ¡the ¡

shortest-­‑path ¡esImate: ¡an ¡upper ¡bound ¡on ¡ the ¡shortest ¡path ¡length ¡to ¡v. ¡ ¡

  • We ¡iniIalize ¡d-­‑s ¡and ¡π-­‑s ¡as ¡follows: ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Init-­‑SingleSource ¡(G,s){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡each ¡vertex ¡v ¡{d[v]=∞; ¡π[v]=nil} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡d[s]=0; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡

∞ ¡

  • ArithmeIc ¡with ¡∞: ¡

¡ ¡ ¡ ¡ ¡ ¡if ¡a ¡≠ ¡-­‑∞, ¡we ¡have ¡a+∞ ¡= ¡∞+a ¡= ¡∞ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡a ¡≠ ¡∞, ¡a+(-­‑∞)=(-­‑∞)+a=-­‑∞. ¡ ¡

Relax ¡

  • Shortest ¡paths ¡algorithms ¡use ¡the ¡operaIon ¡

relax ¡edge ¡(u,v): ¡tesIng ¡whether ¡we ¡can ¡ improve ¡a ¡path ¡from ¡s ¡to ¡v ¡by ¡using ¡edge ¡(u,v) ¡ and, ¡if ¡so, ¡updaIng ¡d[v] ¡and ¡π[v]: ¡ ¡ ¡ ¡ ¡ ¡Relax(u,v,w) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(d[v] ¡> ¡(r ¡= ¡d[u]+w(u,v))) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{d[v] ¡= ¡r; ¡ ¡π[v]= ¡u} ¡ ¡ ¡ ¡ ¡ ¡} ¡

δ(s,v) ¡

  • The ¡final ¡value ¡for ¡d[v] ¡is ¡denoted ¡as ¡δ(s,v), ¡

the ¡shortest ¡path ¡from ¡s ¡to ¡v. ¡

  • Assuming ¡the ¡algorithm ¡starts ¡with ¡ ¡

¡ ¡ ¡Init-­‑SingleSource ¡and ¡only ¡does ¡updates ¡on ¡d ¡ and ¡π ¡using ¡Relax, ¡the ¡following ¡properIes ¡of ¡ shortest ¡paths ¡and ¡relax ¡can ¡be ¡proved ¡ (Cormen ¡et. ¡al., ¡ch. ¡24.5). ¡

slide-4
SLIDE 4

7/20/10 ¡ 4 ¡

ProperIes ¡

  • Triangle ¡Inequality: ¡

¡ ¡ ¡ ¡ ¡ ¡For ¡any ¡edge ¡(u,v): ¡δ(s,v) ¡<= ¡δ(s,u) ¡+ ¡w(u,v) ¡ ¡ ¡ ¡ ¡ ¡op1mal ¡sub ¡structure ¡

  • Upper-­‑bound ¡Property: ¡

¡ ¡ ¡ ¡d[v] ¡>= ¡δ(s,v) ¡and ¡once ¡d[v] ¡achieves ¡δ(s,v), ¡it ¡ does ¡not ¡change ¡anymore. ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡nature ¡of ¡relax ¡

More ¡ProperIes ¡

  • No-­‑path ¡property: ¡

¡ ¡ ¡ ¡If ¡there ¡is ¡no ¡path ¡from ¡s ¡to ¡v, ¡d[v]= ¡δ(s,v)=∞ ¡ ¡ ¡ ¡ ¡relax ¡never ¡changed ¡d ¡

  • ¡Convergence ¡property: ¡

¡ ¡ ¡ ¡If ¡s ¡⇒ ¡u ¡→ ¡v ¡is ¡a ¡shortest ¡path ¡and ¡d[u]= ¡δ(s,u) ¡ before ¡relaxing ¡edge ¡(u,v), ¡then ¡d[v] ¡= ¡δ(s,v) ¡

  • agerward. ¡

¡ ¡ ¡op1mal ¡sub ¡structure ¡

and ¡more ¡

  • Path-­‑relaxa1on ¡property: ¡

¡ ¡ ¡ ¡If ¡p ¡= ¡<v0,v1,...,vk> ¡is ¡a ¡shortest ¡path ¡from ¡s=v0 ¡ to ¡vk, ¡and ¡the ¡edges ¡are ¡relaxed ¡in ¡order ¡(v0,v1), ¡ (v1,v2) ¡etc., ¡then ¡d[v]= ¡δ(s,v). ¡

¡ ¡ ¡ ¡ ¡induc1ve ¡argument ¡ ¡

  • Predecessor-­‑subgraph ¡property: ¡

¡ ¡ ¡ ¡Once ¡d[v]= ¡δ(s,v) ¡for ¡all ¡v ¡in ¡V, ¡the ¡predecessor ¡ subgraph ¡is ¡the ¡shortest ¡path ¡tree ¡rooted ¡at ¡s. ¡

¡ ¡ ¡ ¡ ¡nature ¡of ¡Relax ¡

Bellman-­‑Ford ¡SSSP ¡

  • Allows ¡negaIve ¡edge ¡weights ¡
  • Checks ¡for ¡negaIve ¡length ¡cycles ¡
  • returns ¡false ¡if ¡there ¡is ¡a ¡negaIve ¡length ¡cycle ¡
  • otherwise, ¡BF ¡returns ¡true ¡and ¡and ¡the ¡

shortest ¡paths ¡in ¡d ¡and ¡the ¡shortest ¡path ¡tree ¡ in ¡π. ¡ ¡ ¡

slide-5
SLIDE 5

7/20/10 ¡ 5 ¡

Bellman-­‑Ford(G,w,s){ ¡ ¡ ¡Init-­‑SingleSource(G,s) ¡ ¡ ¡for ¡i ¡= ¡1 ¡to ¡|V|-­‑1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡each ¡(u,v) ¡in ¡E ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Relax(u,v,w) ¡ ¡ ¡// ¡check ¡for ¡negaIve ¡cycles ¡ ¡ ¡for ¡each ¡(u,v) ¡in ¡E ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(d[v]> ¡d[u]+w(u,v) ¡return ¡false ¡ ¡ ¡return ¡true ¡ } ¡ complexity? ¡ Bellman-­‑Ford(G,w,s){ ¡ ¡ ¡Init-­‑SS(G,s) ¡ ¡ ¡for ¡i ¡= ¡1 ¡to ¡|V|-­‑1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡each ¡(u,v) ¡in ¡E ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Relax(u,v,w) ¡ ¡ ¡// ¡check ¡for ¡negaIve ¡cycles ¡ ¡ ¡for ¡each ¡(u,v) ¡in ¡E ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(d[v]> ¡d[u]+w(u,v) ¡return ¡false ¡ ¡ ¡return ¡true ¡ } ¡ complexity ¡ ¡O(|V| ¡* ¡|E| ¡) ¡

Correctness ¡Bellman-­‑Ford ¡

  • Cormen ¡et.al.: ¡proof ¡in ¡the ¡case ¡that ¡there ¡are ¡

no ¡negaIve ¡length ¡cycles ¡is ¡based ¡on ¡path ¡ relaxaIon ¡lemma: ¡for ¡each ¡shortest ¡path ¡the ¡ first ¡edge ¡is ¡found ¡in ¡sweep ¡one, ¡the ¡i-­‑th ¡edge ¡ is ¡found ¡in ¡sweep ¡i. ¡ ¡

  • If ¡ager ¡|V|-­‑1 ¡sweeps ¡there ¡is ¡sIll ¡

improvement ¡then ¡there ¡must ¡be ¡a ¡negaIve ¡ length ¡path. ¡ ¡

0 ¡ s ¡ ∞ ¡ b ¡ ∞ ¡ c ¡ ∞ ¡ a ¡ 6 ¡ 7 ¡

  • ­‑3 ¡

5 ¡

  • ­‑2 ¡

Example ¡BF1. ¡ ¡ ¡ State: ¡right ¡ager ¡Init-­‑SS. ¡ ¡ The ¡nodes ¡contain ¡their ¡d ¡values, ¡ ¡ all ¡π ¡values ¡are ¡nil. ¡ BF ¡does ¡3 ¡sweeps ¡over ¡all ¡edges, ¡ ¡ assume ¡order ¡(a,b) ¡, ¡(b,a), ¡(c,a), ¡(s,b), ¡(s,c). ¡ ¡ What ¡happens ¡in ¡the ¡first ¡sweep? ¡ ¡..... ¡ ¡Relax ¡all ¡edges ¡..... ¡

slide-6
SLIDE 6

7/20/10 ¡ 6 ¡

0 ¡ s ¡ ∞ ¡ b ¡ ∞ ¡ c ¡ ∞ ¡ a ¡ 6 ¡ 7 ¡

  • ­‑3 ¡

5 ¡

  • ­‑2 ¡

Example ¡BF1. ¡ ¡ ¡ State: ¡right ¡ager ¡Init-­‑SS. ¡ ¡ The ¡nodes ¡contain ¡their ¡d ¡values, ¡ ¡ all ¡π ¡values ¡are ¡nil. ¡ BF ¡does ¡3 ¡sweeps ¡over ¡all ¡edges, ¡ ¡ assume ¡order ¡(a,b) ¡, ¡(b,a), ¡(c,a), ¡(s,b), ¡(s,c). ¡ ¡

¡ ¡ ¡ ¡ ¡Relax(u,v,w) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(d[v] ¡> ¡(r ¡= ¡d[u]+w(u,v))) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{d[v] ¡= ¡r; ¡ ¡π[v]= ¡u} ¡ ¡ ¡ ¡ ¡ ¡} ¡

What ¡happens ¡in ¡the ¡first ¡sweep? ¡ ¡In ¡the ¡first ¡sweep: ¡ ¡d[b] ¡> ¡0+6 ¡ ¡ ¡so ¡d[b]=6 ¡and ¡π[b]=s ¡ ¡d[c] ¡> ¡0+7 ¡ ¡ ¡ ¡so ¡d[c]=7 ¡and ¡π[c]=s ¡

0 ¡ s ¡ 6 ¡ b ¡ 7 ¡ c ¡ ∞ ¡ a ¡ 6 ¡ 7 ¡

  • ­‑3 ¡

5 ¡

  • ­‑2 ¡

Example ¡BF1. ¡ ¡ ¡ ¡State: ¡ager ¡sweep ¡1 ¡ BF ¡does ¡3 ¡sweeps ¡over ¡all ¡edges, ¡ ¡ assume ¡order ¡(a,b) ¡, ¡(b,a), ¡(c,a), ¡(s,b), ¡(s,c). ¡ ¡

¡ ¡ ¡ ¡ ¡Relax(u,v,w) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(d[v] ¡> ¡(r ¡= ¡d[u]+w(u,v))) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{d[v] ¡= ¡r; ¡ ¡π[v]= ¡u} ¡ ¡ ¡ ¡ ¡ ¡} ¡

What ¡happens ¡in ¡the ¡second ¡sweep? ¡

0 ¡ s ¡ 6 ¡ b ¡ 7 ¡ c ¡ ∞ ¡ a ¡ 6 ¡ 7 ¡

  • ­‑3 ¡

5 ¡

  • ­‑2 ¡

Example ¡BF1. ¡ ¡ ¡ ¡State: ¡ager ¡sweep ¡1 ¡ BF ¡does ¡3 ¡sweeps ¡over ¡all ¡edges, ¡ ¡ assume ¡order ¡(a,b) ¡, ¡(b,a), ¡(c,a), ¡(s,b), ¡(s,c). ¡ ¡

¡ ¡ ¡ ¡ ¡Relax(u,v,w) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(d[v] ¡> ¡(r ¡= ¡d[u]+w(u,v))) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{d[v] ¡= ¡r; ¡ ¡π[v]= ¡u} ¡ ¡ ¡ ¡ ¡ ¡} ¡

What ¡happens ¡in ¡the ¡second ¡sweep? ¡ In ¡the ¡second ¡sweep: ¡ ¡d[a] ¡> ¡6+5 ¡ ¡so ¡d[a]=11 ¡and ¡π[a]=b ¡ ¡d[a] ¡> ¡7-­‑3 ¡ ¡ ¡so ¡d[a]=4 ¡and ¡π[a]=c ¡ ¡d[b] ¡> ¡4-­‑2 ¡ ¡ ¡so ¡d[b]=2 ¡and ¡π[b]=a ¡

0 ¡ s ¡ 2 ¡ b ¡ 7 ¡ c ¡ 4 ¡ a ¡ 6 ¡ 7 ¡

  • ­‑3 ¡

5 ¡

  • ­‑2 ¡

What ¡happens ¡in ¡the ¡third ¡ ¡sweep? ¡ What ¡is ¡returned? ¡

slide-7
SLIDE 7

7/20/10 ¡ 7 ¡

0 ¡ s ¡ 2 ¡ b ¡ 7 ¡ c ¡ 4 ¡ a ¡ 6 ¡ 7 ¡

  • ­‑3 ¡

5 ¡

  • ­‑2 ¡

In ¡the ¡third ¡sweep ¡nothing ¡changes ¡ BF1 ¡returns ¡true ¡

0 ¡ s ¡ ∞ ¡ b ¡ ∞ ¡ c ¡ ∞ ¡ a ¡ 6 ¡ 7 ¡

  • ­‑3 ¡
  • ­‑5 ¡

¡2 ¡

Example ¡BF2. ¡ ¡ ¡ Slightly ¡changed ¡state: ¡right ¡ager ¡Init-­‑SS. ¡ ¡ The ¡nodes ¡contain ¡their ¡d ¡values, ¡ ¡ all ¡π ¡values ¡are ¡nil. ¡ BF ¡does ¡3 ¡sweeps ¡over ¡all ¡edges, ¡ ¡ assume ¡order ¡(a,b) ¡, ¡(b,a), ¡(c,a), ¡(s,b), ¡(s,c). ¡ ¡

¡ ¡ ¡ ¡ ¡Relax(u,v,w) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(d[v] ¡> ¡(r ¡= ¡d[u]+w(u,v))) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{d[v] ¡= ¡r; ¡ ¡π[v]= ¡u} ¡ ¡ ¡ ¡ ¡ ¡} ¡

What ¡happens ¡in ¡the ¡first ¡sweep? ¡

0 ¡ s ¡ 6 ¡ b ¡ 7 ¡ c ¡ ∞ ¡ a ¡ 6 ¡ 7 ¡

  • ­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

0 ¡ s ¡ 6 ¡ b ¡ 7 ¡ c ¡ 1 ¡ a ¡ 6 ¡ 7 ¡

  • ­‑3 ¡
  • ­‑5 ¡

2 ¡

Example ¡BF2. ¡ ¡ ¡ State ¡ager ¡second ¡sweep ¡ BF ¡does ¡3 ¡sweeps ¡over ¡all ¡edges, ¡ ¡ assume ¡order ¡(a,b) ¡, ¡(b,a), ¡(c,a), ¡(s,b), ¡(s,c). ¡ ¡

¡ ¡ ¡ ¡ ¡Relax(u,v,w) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(d[v] ¡> ¡(r ¡= ¡d[u]+w(u,v))) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{d[v] ¡= ¡r; ¡ ¡π[v]= ¡u} ¡ ¡ ¡ ¡ ¡ ¡} ¡

In ¡the ¡second ¡sweep: ¡ d[a] ¡> ¡6-­‑5 ¡ ¡ ¡so ¡ ¡d[a]=1 ¡and ¡π[a]=b ¡

0 ¡ s ¡ 6 ¡ b ¡ 7 ¡ c ¡ 1 ¡ a ¡ 6 ¡ 7 ¡

  • ­‑3 ¡
  • ­‑5 ¡

2 ¡

Example ¡BF2. ¡ ¡ ¡ State ¡ager ¡second ¡sweep ¡ BF ¡does ¡3 ¡sweeps ¡over ¡all ¡edges, ¡ ¡ assume ¡order ¡(a,b) ¡, ¡(b,a), ¡(c,a), ¡(s,b), ¡(s,c). ¡ ¡

¡ ¡ ¡ ¡ ¡Relax(u,v,w) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(d[v] ¡> ¡(r ¡= ¡d[u]+w(u,v))) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{d[v] ¡= ¡r; ¡ ¡π[v]= ¡u} ¡ ¡ ¡ ¡ ¡ ¡} ¡

What ¡happens ¡in ¡the ¡third ¡sweep? ¡

0 ¡ s ¡ 3 ¡ b ¡ 7 ¡ c ¡ 1 ¡ a ¡ 6 ¡ 7 ¡

  • ­‑3 ¡
  • ­‑5 ¡

2 ¡

Example ¡BF2. ¡ ¡ ¡ State ¡ager ¡third ¡sweep ¡ BF ¡does ¡3 ¡sweeps ¡over ¡all ¡edges, ¡ ¡ assume ¡order ¡(a,b) ¡, ¡(b,a), ¡(c,a), ¡(s,b), ¡(s,c). ¡ ¡

¡ ¡ ¡ ¡ ¡Relax(u,v,w) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(d[v] ¡> ¡(r ¡= ¡d[u]+w(u,v))) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{d[v] ¡= ¡r; ¡ ¡π[v]= ¡u} ¡ ¡ ¡ ¡ ¡ ¡} ¡

In ¡sweep ¡three ¡we ¡can ¡now ¡improve ¡d [b] ¡but ¡the ¡predecessor ¡sub ¡graph ¡is ¡ broken!! ¡ ¡ We ¡now ¡can ¡keep ¡lowering ¡ ¡ d[a] ¡and ¡and ¡d[b] ¡in ¡subsequent ¡

  • sweeps. ¡The ¡Bellman-­‑Ford ¡algorithm ¡

detects ¡this ¡in ¡its ¡last ¡sweep ¡and ¡ returns ¡false. ¡

SSSP ¡in ¡a ¡DAG ¡

  • By ¡relaxing ¡edges ¡in ¡a ¡DAG ¡in ¡topological ¡sort ¡order ¡ ¡ ¡

¡ ¡ ¡ ¡we ¡need ¡to ¡only ¡relax ¡each ¡edge ¡once ¡ ¡

  • because ¡in ¡any ¡path ¡p ¡= ¡<v0,v1,...,vk>, ¡(v0,v1) ¡ ¡

¡ ¡ ¡ ¡topologically ¡precedes ¡ ¡(v1,v2) ¡etc. ¡ ¡

  • So ¡the ¡path ¡relaxaIon ¡property ¡implies ¡d[v]= ¡δ(s,v) ¡ ¡

¡ ¡ ¡ ¡for ¡all ¡v. ¡

slide-9
SLIDE 9

7/20/10 ¡ 9 ¡

Dijkstra's ¡SSSP ¡

  • Dijkstra's ¡greedy ¡algorithm ¡solves ¡SSSP ¡for ¡

directed ¡graphs ¡with ¡non-­‑negaIve ¡edges. ¡ ¡

  • Very ¡much ¡like ¡in ¡Primm's ¡minimal ¡spanning ¡

tree ¡algorithm, ¡a ¡set ¡S ¡of ¡verIces ¡whose ¡ minimal ¡path ¡has ¡been ¡determined ¡is ¡ stepwise ¡extended, ¡and ¡distances ¡to ¡yet ¡ unreached ¡verIces ¡are ¡updated. ¡ ¡

  • The ¡verIces ¡are ¡maintained ¡in ¡a ¡min ¡priority ¡

queue, ¡keyed ¡on ¡their ¡d ¡value. ¡

Back ¡to ¡Primm ¡MST ¡

  • In ¡lecture ¡8 ¡ ¡Greedy ¡algorithms ¡we ¡sketched ¡

Primm's ¡MST: ¡

– the ¡growing ¡MST ¡A ¡is ¡a ¡tree ¡ – start ¡with ¡arbitrary ¡point, ¡pick ¡minimal ¡emanaIng ¡ edge ¡ – keep ¡adding ¡minimal ¡distance ¡to ¡A ¡nodes, ¡ adjusIng ¡the ¡minimal ¡distance ¡of ¡the ¡points ¡not ¡in ¡ to ¡A ¡ – store ¡V ¡in ¡min ¡priority ¡Queue ¡

MST-­‑Prim(G,w,s) ¡// ¡G=(V,E), ¡weights ¡w, ¡root ¡s ¡ ¡init_SingleSource(G,s) ¡ ¡Q=V; ¡// ¡in ¡iniIal ¡priority ¡Queue ¡form, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡eg ¡heapified ¡ ¡while ¡Q ¡not ¡empty ¡ ¡ ¡ ¡ ¡ ¡u ¡= ¡extract-­‑min(Q) ¡// ¡first ¡one ¡will ¡be ¡s ¡ ¡ ¡ ¡ ¡ ¡for ¡each ¡v ¡in ¡adj(u) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡v ¡in ¡Q ¡and ¡w(u,v) ¡< ¡u.d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡v.d ¡= ¡w(u,v) ¡; ¡v.π ¡= ¡u ¡ ¡

Complexity ¡Primm ¡

  • Q: ¡binary ¡min ¡heap ¡(see ¡sorIng ¡lecture) ¡

– ¡we ¡can ¡build ¡the ¡heap ¡in ¡O(|V|) ¡Ime ¡

  • The ¡while ¡loop ¡executes ¡|V| ¡Imes ¡ ¡

– each ¡extract-­‑min ¡takes ¡O(lg|V|) ¡Ime ¡totalling ¡ ¡ ¡ ¡ ¡ ¡O(|V|lg|V|) ¡ ¡Ime ¡

slide-10
SLIDE 10

7/20/10 ¡ 10 ¡

Complexity ¡Primm ¡cont' ¡

  • The ¡inner ¡for ¡loop ¡executes ¡|E| ¡1mes ¡in ¡total ¡

– ¡because ¡each ¡vertex ¡is ¡extracted ¡only ¡once ¡and ¡ therefore ¡each ¡edge ¡in ¡the ¡adjacency ¡list ¡represenIng ¡ G ¡is ¡considered ¡once ¡ – v.d ¡= ¡w(u,v) ¡in ¡the ¡condiIonal ¡in ¡the ¡innermost ¡loop ¡ involves ¡a ¡decrease-­‑key ¡opera1on ¡in ¡the ¡heap ¡taking ¡ O(lg|V|) ¡Ime ¡ ¡ ¡

  • Hence, ¡complexity ¡when ¡using ¡priority ¡min ¡heap: ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O((|V|+|E|)(lg(|V|)) ¡which ¡is ¡O(|E|lg|V|) ¡ ¡ Dijkstra(G,w,s) ¡{ ¡// ¡very ¡much ¡like ¡Primm ¡ ¡ ¡S ¡= ¡empty; ¡ ¡ ¡Q ¡ ¡= ¡V(G); ¡// ¡min ¡priority ¡queue ¡ ¡ ¡while ¡(Q ¡not ¡empty) ¡{ ¡ ¡ ¡ ¡ ¡ ¡u=Extract-­‑min(Q); ¡ ¡ ¡ ¡ ¡ ¡S=S∪{u} ¡ ¡ ¡ ¡ ¡ ¡for ¡each ¡adjacent ¡v ¡of ¡u ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Relax(u,v,w)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be ¡{1,2,...,n} ¡and ¡let ¡

Vk ¡be ¡the ¡subset ¡{1,2,...k}. ¡ ¡

  • The ¡algorithm ¡uses ¡a ¡recurrence, ¡which ¡in ¡step ¡

k ¡considers ¡paths ¡from ¡all ¡i ¡to ¡all ¡j ¡ ¡ ¡ ¡ ¡either ¡ ¡(don't ¡take ¡node ¡k ¡into ¡account) ¡ ¡ ¡ ¡ ¡ ¡with ¡intermediate ¡nodes ¡from ¡Vk-­‑1 ¡only ¡ ¡ ¡ ¡ ¡or ¡ ¡ ¡(do ¡take ¡node ¡k ¡into ¡account) ¡ ¡ ¡ ¡ ¡ ¡with ¡intermediate ¡nodes ¡ ¡Vk-­‑1 ¡and ¡also ¡node ¡k ¡ ¡

k ¡intermediate ¡node ¡in ¡Vk? ¡

  • If ¡k ¡is ¡not ¡an ¡intermediate ¡node ¡then ¡the ¡

shortest ¡path ¡from ¡i ¡to ¡j ¡in ¡step ¡k ¡is ¡equal ¡to ¡ the ¡shortest ¡path ¡in ¡step ¡k-­‑1. ¡ ¡

  • If ¡k ¡is ¡an ¡intermediate ¡node ¡then ¡the ¡shortest ¡

path ¡from ¡i ¡to ¡j ¡in ¡step ¡k ¡is ¡the ¡shortest ¡from ¡i ¡ to ¡k ¡in ¡step ¡k-­‑1 ¡plus ¡the ¡shortest ¡path ¡from ¡k ¡ to ¡j ¡in ¡step ¡k-­‑1. ¡ ¡

Vk:via ¡set ¡ ¡via ¡which ¡paths ¡from ¡i ¡to ¡j ¡go ¡

  • equaIon ¡form ¡of ¡previous ¡slide: ¡

¡ ¡ ¡dij

(k) ¡= ¡ ¡wij ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡k=0 ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡min(dij

(k-­‑1), ¡dik (k-­‑1)+ ¡dkj (k-­‑1)) ¡ ¡ ¡ ¡if ¡k>0 ¡

Floyd ¡Warshall ¡

¡ ¡ ¡Floyd-­‑Warshall(W) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡D=W ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡k ¡= ¡1 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡i ¡= ¡1 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡j ¡= ¡1 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡D[i,j]=min(D[i,j], ¡D[i,k]+D[k,j] ¡

slide-13
SLIDE 13

7/20/10 ¡ 13 ¡

Predecessors ¡in ¡Floyd-­‑Warshall ¡

  • A ¡recurrence ¡for ¡the ¡predecessor ¡π ¡can ¡be ¡

derived ¡much ¡like ¡the ¡recurrence ¡for ¡d. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡πij

(0) ¡ ¡= ¡nil ¡if ¡i=j ¡or ¡(i,j) ¡not ¡in ¡E ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡i ¡ ¡otherwise ¡ ¡ ¡ ¡ ¡ ¡πij

(k) ¡ ¡= ¡πij (k-­‑1) ¡if ¡dij (k-­‑1) ¡≤ ¡dik (k-­‑1) ¡+ ¡dkj (k-­‑1) ¡ ¡ ¡ ¡ ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡πkj

(k-­‑1) ¡if ¡dij (k-­‑1)> ¡dik (k-­‑1) ¡+ ¡dkj (k-­‑1) ¡

  • Code ¡for ¡compuIng ¡the ¡predecessor ¡is ¡easily ¡

incorporated ¡in ¡the ¡Floyd-­‑Warshall ¡funcIon. ¡

transiIve ¡closure ¡ ¡

  • Given ¡a ¡graph ¡G=(V,E), ¡the ¡transi1ve ¡closure ¡

graph ¡G*=(V,E*) ¡has ¡an ¡edge ¡(i,j) ¡if ¡there ¡is ¡a ¡ path ¡from ¡i ¡to ¡j ¡in ¡G. ¡ ¡ ¡

  • G* ¡can ¡be ¡computed ¡using ¡Floyd-­‑Warshall ¡by ¡ ¡

– iniIally ¡assigning ¡Dij=1 ¡if ¡(i,j) ¡in ¡E ¡and ¡0 ¡otherwise, ¡ ¡ – and ¡replacing ¡min ¡ ¡by ¡or ¡and ¡+ ¡by ¡and ¡in ¡the ¡loop. ¡