Download presentation
Presentation is loading. Please wait.
1
Single-Source Shortest Path
Jeff Chastine
2
How does Google Maps work?
Jeff Chastine
3
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
4
Single-Source Shortest Path
For this problem Disallow cycles Still have weight function π€:πΈβπ
Calculate path p and shortest path π where Note: Breadth works for non-weighted edges π€ π = π=1 π π€( π£ πβ1 , π£ π ) π π’,π£ = min π€ π :π’ π π£ ππ β π β Jeff Chastine
5
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
6
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
7
Relaxation Example (using a breadth-first traversal)
β 2 5 a 11 d β 99 3 c β Original Graph Predecessor and Cost INITIALIZE-SINGLE-SOURCE(G, s) Jeff Chastine
8
Start with source node π
Relaxation Example b β 2 5 a 11 d β 99 3 c β Original Graph Predecessor and Cost Start with source node π Jeff Chastine
9
Update connected nodes
Relaxation Example b 2 2 5 a 11 d β 99 3 c 99 Original Graph Predecessor and Cost Update connected nodes Jeff Chastine
10
Relaxation Example b 2 a d β c 99 Continue with π Original Graph
5 a 11 d β 99 3 c 99 Original Graph Predecessor and Cost Continue with π Jeff Chastine
11
Relaxation Example b 2 a d 7 c 99 Original Graph Predecessor and Cost
5 a 11 d 7 99 3 c 99 Original Graph Predecessor and Cost Jeff Chastine
12
Relaxation Example b 2 a d 7 c 99 This guy can chill out⦠RELAX!
5 a 11 d 7 99 3 c 99 Original Graph Predecessor and Cost This guy can chill out⦠RELAX! Jeff Chastine
13
Relaxation Example b 2 a d 7 c 13 Original Graph Predecessor and Cost
5 a 11 d 7 99 3 c 13 Original Graph Predecessor and Cost Jeff Chastine
14
Relaxation Example But wait! Weβre not done! b 2 a d 7 c 13
5 a 11 d 7 99 3 c 13 Original Graph Predecessor and Cost But wait! Weβre not done! Jeff Chastine
15
Relaxation Example No relaxation b 2 a d 7 c 13 Original Graph
5 a 11 d 7 99 3 c 13 Original Graph Predecessor and Cost No relaxation Jeff Chastine
16
Relaxation Example b 2 a d 7 c 13 Original Graph Predecessor and Cost
5 a 11 d 7 99 3 c 13 Original Graph Predecessor and Cost Jeff Chastine
17
Relaxation Example Needs to relax again! b 2 a d 7 c 13 Original Graph
5 a 11 d 7 99 3 c 13 Original Graph Predecessor and Cost Needs to relax again! Jeff Chastine
18
Relaxation Example Needs to relax again! b 2 a d 7 c 10 Original Graph
5 a 11 d 7 99 3 c 10 Original Graph Predecessor and Cost Needs to relax again! Jeff Chastine
19
Final Predecessor and Cost
Relaxation Example b 2 2 5 a 11 d 7 99 3 c 10 Original Graph Predecessor and Cost Final Predecessor and Cost Jeff Chastine
20
How does Google Maps work?
PWND! Jeff Chastine
21
Bellman-Ford Algorithm
Works with negatively weighted edges Detects if negative cycle exists Consistently uses relaxation Runs in Ξ(ππΈ) Jeff Chastine
22
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s) INITIALIZE-SINGLE-SOURCE(G, s) for i β 1 to |V[G]| -1 foreach edge (u, v) β E[G] RELAX(u, v, w) if d[v] > d[u] + w(u, v) then return FALSE return TRUE Jeff Chastine
23
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
24
Dijkstraβs Algorithm DIJKSTRA (G, w, s) INITIALIZE-SINGLE-SOURCE(G, s)
Q β V[G] while Q β β
do u β EXTRACT-MIN(Q) SβS βͺ {u} foreach vertex v β Adj[u] do RELAX(u, v, w) Jeff Chastine
25
Summary Both algorithms use Bellman-Ford (π(πΈ π)) Dijkstra (π(πΈ πππ))
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.