Single-Source Shortest Path Jeff Chastine How does Google Maps work? Jeff Chastine Single-Source Shortest Path • How many ways can I go from SPSU to KSU? • How can I represent the map? – What are vertices? – What are edges? – What are weights? Jeff Chastine Single-Source Shortest Path • For this problem – Disallow cycles – Still have weight function 𝑤: 𝐸 → 𝑅 – Calculate path p and shortest path 𝜕 where 𝑘 𝑤 𝑝 = 𝑤(𝑣𝑖−1 , 𝑣𝑖 ) 𝑖=1 𝑝 𝜕 𝑢, 𝑣 = min 𝑤 𝑝 : 𝑢 → 𝑣 𝑖𝑓 ∃ 𝑝 ∞ • Note: Breadth works for non-weighted edges Jeff Chastine Single-Source Shortest Path • Has optimal sub-structure. Why? 𝑝𝑢𝑤 𝑝𝑥𝑤 𝑝𝑤𝑣 – Path is 𝑢 𝑥 𝑤 𝑣 is fastest – Assume faster way between x and w – Cut and paste faster way and you get optimal! • Graph can’t have a negative cycle. Why? • Can the graph have a positive cycle? • Can the shortest path contain a positive cycle? Jeff Chastine How it’s Done in General • A node v has a predecessor node 𝜋(𝑣) – Tells us how we got there – NIL for first node – End with a directed graph rooted at start node s • Relaxation (pay attention!) – Maintain an upper-bound cost for each node d[v] – Initially, all nodes are marked as ∞ – If we find a cheaper path to node v, relax (update) the cost 𝑑[𝑣] and predecessor 𝜋(𝑣) Jeff Chastine Relaxation Example (using a breadth-first traversal) 2 b 5 11 a ∞ d 3 99 ∞ 0 c ∞ Original Graph Predecessor and Cost INITIALIZE-SINGLE-SOURCE(G, s) Jeff Chastine Relaxation Example 2 b 5 11 a ∞ d 3 99 ∞ 0 c ∞ Original Graph Predecessor and Cost Start with source node 𝑎 Jeff Chastine Relaxation Example 2 b 5 11 a 2 d ∞ 0 3 99 c 99 Original Graph Predecessor and Cost Update connected nodes Jeff Chastine Relaxation Example 2 b 5 11 a 2 d ∞ 0 3 99 c 99 Original Graph Predecessor and Cost Continue with 𝑏 Jeff Chastine Relaxation Example 2 b 5 11 a 2 d 0 7 3 99 c 99 Original Graph Predecessor and Cost Jeff Chastine Relaxation Example 2 b 5 11 a 2 d 0 7 3 99 c 99 Original Graph Predecessor and Cost This guy can chill out… RELAX! Jeff Chastine Relaxation Example 2 b 5 11 a 2 d 0 7 3 99 c 13 Original Graph Predecessor and Cost Jeff Chastine Relaxation Example 2 b 5 11 a 2 d 0 7 3 99 c 13 Original Graph Predecessor and Cost But wait! We’re not done! Jeff Chastine Relaxation Example 2 b 5 11 a 2 d 0 7 3 99 c 13 Original Graph Predecessor and Cost No relaxation Jeff Chastine Relaxation Example 2 b 5 11 a 2 d 0 7 3 99 c 13 Original Graph Predecessor and Cost Jeff Chastine Relaxation Example 2 b 5 11 a 2 d 0 7 3 99 c 13 Original Graph Predecessor and Cost Needs to relax again! Jeff Chastine Relaxation Example 2 b 5 11 a 2 d 0 7 3 99 c 10 Original Graph Predecessor and Cost Needs to relax again! Jeff Chastine Relaxation Example 2 b 5 11 a 2 d 0 7 3 99 c 10 Original Graph Predecessor and Cost Final Predecessor and Cost Jeff Chastine How does Google Maps work? Jeff Chastine Bellman-Ford Algorithm • • • • Works with negatively weighted edges Detects if negative cycle exists Consistently uses relaxation Runs in Θ(𝑉𝐸) Jeff Chastine Bellman-Ford Algorithm BELLMAN-FORD(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 for i ← 1 to |V[G]| -1 3 foreach edge (u, v) ∈ E[G] 4 RELAX(u, v, w) 5 foreach edge (u, v) ∈ E[G] 6 if d[v] > d[u] + w(u, v) 7 then return FALSE 8 return TRUE Jeff Chastine Dijkstra’s Algorithm • Is more efficient than Bellman-Ford • Doesn’t work with negative edges • Has a set S of vertices that it has already traversed. Heapifies V. • In general – Picks vertex u from V - S with minimum estimate – Relaxes everything connected to u – Adds u to S – Repeats until V - S is the empty set Jeff Chastine Dijkstra’s Algorithm DIJKSTRA (G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 S←∅ 3 Q ← V[G] 4 while Q ≠ ∅ 5 do u ← EXTRACT-MIN(Q) 6 S←S ∪ {u} 7 foreach vertex v ∈ Adj[u] 8 do RELAX(u, v, w) Jeff Chastine Summary • Both algorithms use – A predecessor graph – Costs to each node – Relaxation • Bellman-Ford (𝑂(𝐸 𝑉)) – Works with negative weights – Detects negative cycles • Dijkstra (𝑂(𝐸 𝑙𝑔𝑉)) – More efficient – Doesn’t work with negative weights Jeff Chastine