Lecture14-Single-SourceShortestPaths

# Lecture14-Single-SourceShortestPaths - Single-Source...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Single-Source Shortest Paths Shortest-path problem •ºI –+ • G=(V,E) G Weighted Directed Graph( G G )G Weight function w: ERª ˆ ¶ ’… E G v0 G vk G Path( G • G p=(v0,v1,…,vk) G G ) Single-Source Shortest Paths 2 Shortest-path problem •i •i w( p ) = ∑i =1 w(vi −1 , vi ) k u G v. B ·ª p min{w( p ) : u → v}, ∃a path from u to v. δ (u , v) = ∞, otherwise. Single-Source Shortest Paths 3 Shortest-path tree rooted at s •i G=(V,E) G Shortest-path tree rooted at s ( G ’ª s) ¸ · x ) G’=(V’,E’))¸ ’ª ·x G – V’ G ª s) ¸ ’ ·x – G’ G sG Rooted Tree G – G G’ G s G v G simple path G GG sG vG ª · )¸ x Single-Source Shortest Paths 4 Shortest-path tree rooted at s i s 1 t 1 v 10 1 1 10 y 1 10 10 x 10 u 10 v 1 y w 1 t 1 1 10 s 10 x w z z Original Graph G Shortest-path tree rooted at s 5 Single-Source Shortest Paths Predecessor graph •i G=(V,E) h ’ª · W Ç Gπ=(Vπ,EW π)Ç h ’ª · – π[s]=NIL G s∈Vπ G ’ª · W π h Ç – G π[v]≠NIL G (π[v],v)∈Eπ G v∈Vπ G • Shortest-path tree rooted at s G Predecessor graph G G Single-Source Shortest Paths 6 Predecessor graph i π[s] NIL 1 t 1 v 10 1 1 10 y 1 π[t] s s π[u] NIL 10 10 x 10 π[v] t π[w] s 1 π[x] t s π[y] x 10 π[z] v w 1 u 10 v t 1 10 y 1 x w z Single-Source Shortest Paths z 7 Original Graph G Shortest-path tree rooted at s Initialize-Single-Source i •i G •G G •G GªI– · º sc˜p l– . ¡ *p d[v]x ª E¨* · sG vG π[v]x ª E¨* · vx ª E¨* · s G vx ª E¨ · d[v]=∞ G π[v]=NIL G d[s]=0 G s Single-Source Shortest Paths 8 Initialize-Single-Source i Initialize-Single-Source(G,s) { for each vertex v∈V[G] do d[v]∞ π[v]NIL d[s]0 } Single-Source Shortest Paths 9 Relaxation i • ýP ª · ^ -Ø* (u,v) P Relax(u,v,w) { if d[v]>d[u]+w(u,v) then d[v]d[u]+w(u,v) π[v]u } Single-Source Shortest Paths 10 Relaxation i Relax(u,v,w) G s 4 u 4 u w(u,v) 7 v s if w(u,v)=4 (>3) ª xÈ ·, G sv G w(u,v) 6 v Relax(u,v,w) G s if w(u,v)=2 (<3) G G sª v È ·, x π[v]u 4 u Single-Source Shortest Paths w(u,v) 7 v 11 ª·WÌh · W •Ì hª* · W Ì hª ª · •W Ì h Relaxation i (u,v) G δ(s,v)≤δ(s,u)+w(u,v) G δ(s,v)≤d[v] G d[v] G ’ W v h sª · Ì G d[v]=δ(s,v) G Relaxation G G d[v] G Single-Source Shortest Paths 12 ª·WÌ¨ · W •Ì ¨ª* i ª · •Ì ¨ W G sW v ¨ ’ª · Ì ª· WÌ ¨ s G v ¨Ìª* ·W Relaxation i d[v]=δ(s,v)=∞ G (u,v) G d[u]=δ(s,u) G Relax(u,v,w) G d[v]=δ(s,v) G Single-Source Shortest Paths 13 ª·W8 Ê Relaxation i • Path-relaxation G G p=(v0,v1,…,vk) G s=v0· vW k n ’ª Ê ª·Wn Ê Relax(v0,v1,w) G Relax(v1,v2,w)… G Relax(vk-1,vk,w) G d[vk]=δ(s,vk) G • Predecessor graph G ’ ª · W Ên Relaxation Ê n ’ª · W vG d[v]=δ(s,v)W n ’ª · Ê Predecessor graph Gπ G Shortest-path tree rooted at s G Single-Source Shortest Paths 14 Bellman-Ford i – g¡ · *p W ˜IÆ º • +ª Bellman-Ford(G,w,s) { Initialize-Single-Source(G,s) for i = 1 to |V-1| do for each edge (u,v)∈E do Relex(u,v,w) for each edge (u,v)∈E do if d[v]>d[u]+w(u,v) then return false fª · // ˆ WÉ return true fª · // ˆ WÉ } Single-Source Shortest Paths 15 Bellman-Ford ø * ª·W Ë 5 (a) 6 s0 7 8 ∞ -2 -3 -4 ∞ 2 9 ∞ ∞ 7 (b) 6 s0 7 8 6 5 -2 -3 -4 7 2 9 ∞ ∞ 7 Single-Source Shortest Paths 16 Bellman-Ford x * ªµì j 5 (c) 6 s0 7 8 6 -2 -3 -4 7 2 9 2 4 7 (d) (e) 6 s0 7 8 2 5 -2 -3 -4 7 2 9 -2 4 7 Single-Source Shortest Paths 17 Bellman-Ford ¸ ª·W Ê •· Í J ¨ Relaxation G Shortest-path tree rootedª atW s ¸ ’· Ê path G ’ª · W Ê ¸ path-relaxation ¸ ª ·W Ê |V|-1 G ’ ª · W ¸Ê Shortest simple path G v G d[v]=δ(s,v) G Single-Source Shortest Paths 18 Bellman-Ford h ª ·u . •· Ì = ø – Initialize-Single-Source G ’– x·h W ª O(|V|) G O(|V||E|) G –G O(|E|) · Ì ]À •W ª x·h O(|V||E|) G Single-Source Shortest Paths 19 O(|V|) G Relaxation G Single-source shortest paths in DAGs • i Bellman-Fordº –I– Relaxationº –– I DAG-Shortest-Path(G,w,s) { Topologically sort V[G] Initialize-Single-Source(G,s) for each u taken in topological order do for each v∈adj[u] do Relax(u,v,w) } µ ì •j xªf O(|V|+|E|) G Single-Source Shortest Paths 20 DAG-Shortest-Path G (a ) 5s 2 7 -1 -2 ∞ 0 ∞ ∞ ∞ ∞ 3 4 2 6 1 (b ) 5s 2 7 -1 -2 ∞ 0 ∞ ∞ ∞ ∞ 3 4 2 6 1 Single-Source Shortest Paths 21 DAG-Shortest-Path G (c ) 5s 2 ∞ 0 3 6 2 7 6 4 -1 2 1 ∞ -2 ∞ (d ) 5s 2 ∞ 0 3 6 2 7 6 4 -1 2 1 6 -2 4 Single-Source Shortest Paths 22 DAG-Shortest-Path G (e ) 5s 2 ∞ 0 3 6 2 7 6 4 -1 2 (f) 5s 2 ∞ 0 3 6 2 7 6 4 -1 2 (g ) 6 2 7 6 4 -1 2 Single-Source Shortest Paths 23 1 5 -2 4 1 5 -2 3 5s 2 ∞ 0 3 1 5 -2 3 Dijkstra i •I ˜ • i Bellman-fordº –I– G Relaxation· δ ’ ª \S •G •–º – I Single-Source Shortest Paths 24 Priority queue G Dijkstra i Q: Priority queue with d as the key Dijkstra(G,w,s) { Initialize-Single-Source(G,s) Q=V[G] while Q is not empty do u=Extract-Min(Q) for each v∈adj[u] do Relax(u,v,w) } Single-Source Shortest Paths 25 Dijkstra ˜ * ª µñP (a) 10 s 2 0 5 ∞ 3 1 ∞ 9 4 7 ∞ 2 ∞ 6 s (b) 10 2 0 5 10 3 1 ∞ 9 4 7 5 2 ∞ 6 Single-Source Shortest Paths 26 Dijkstra ø * ª ·\C (c) 10 s 0 5 2 8 3 1 14 9 4 7 5 2 7 6 s (d) 10 0 5 2 8 3 1 11 9 4 7 5 2 7 6 Single-Source Shortest Paths 27 Dijkstra x * ª ·\@ (e) 10 s 2 0 5 8 3 1 9 9 4 7 5 2 7 6 s (e) 10 2 0 5 8 3 1 9 9 4 7 5 2 7 6 Single-Source Shortest Paths 28 Dijkstra ¸ ª ·WÎ · W •Î ¸ª ’ª · W Î ¸ •G •G G •G G G Priority queue ¦x Linear array ¸ ’ª · W Î Binary ’ª · W Î ¸ heap O(|V|2) G O(|E|log|V|) G Fibonacci ’heap ¸ ª · WÎ Single-Source Shortest Paths O(|E|+|V|log|V|) 29 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online