Shortest Paths in Graphs
CS16: Introduction to Data Structures & Algorithms Spring 2020
Shortest Paths in Graphs CS16: Introduction to Data Structures - - PowerPoint PPT Presentation
Shortest Paths in Graphs CS16: Introduction to Data Structures & Algorithms Spring 2020 Outline Shortest Paths Breadth First Search Dijkstras Algorithm 2 What is a Shortest Path? Given weighted graph G (weights on
Shortest Paths in Graphs
CS16: Introduction to Data Structures & Algorithms Spring 2020
Outline
What is a Shortest Path?
Single Source Shortest Paths (SSSP)
A B C D E
4 2 3 1 2 3 1 5 4
Simpler Problem: Unit Edges
A B C D E
1 1 1 1 1 1 1 1 1
Simpler Problem: Unit Edges
A B C D E
1 1 1 1 1 1 1 1 1
Simpler Problem: Unit Edges
solves this problem?
7A B C D E
1 1 1 1 1 1 1 1 1
numStops (with BFS)
8 ORD PVD MIA DFW SFO LAX LGA HNL BTV JFK∞ ∞ 1 2 2 2 3 3 3 (HNL) (LAX) (LAX) (LAX) (ORD) (DFW) (DFW)
Have distance Want a path Follow ‘previous’ pointers to get a path
Breadth-First Search
9Activity #1
A B C D E
1 1 1 1 1 1 1 1 1
Breadth-First Search
10Activity #1
A B C D E
1 1 1 1 1 1 1 1 1
Breadth-First Search
11Activity #1
A B C D E
1 1 1 1 1 1 1 1 1
Breadth First Search
A B C D E
1 1 1 1 1 1 1 1 1
Breadth First Search Simulation
A B C D E
1 1 1 1 1 1 1 1 1
QueueA
Breadth First Search Simulation
A B C D E
1 1 1 1 1 1 1 1 1
QueueB C
prev: A prev: ABreadth First Search Simulation
A B C D E
1 1 1 1 1 1 1 1 1
QueueC D E
prev: A prev: A prev: B prev: BBreadth First Search Simulation
A B C D E
1 1 1 1 1 1 1 1 1
QueueE
prev: A prev: A prev: B prev: BBreadth First Search Simulation
A B C D E
1 1 1 1 1 1 1 1 1
QueueE
prev: A prev: A prev: B prev: BNon-Unit Edge Weights
A B C D E
4 2 3 1 2 3 1 5 4
Shortest Path
19Activity #2
A B C D E
4 2 3 1 2 3 1 5 4Shortest Path
20Activity #2
A B C D E
4 2 3 1 2 3 1 5 4Shortest Path
21Activity #2
A B C D E
4 2 3 1 2 3 1 5 4Shortest Path
22Activity #2
A B C D E
4 2 3 1 2 3 1 5 4Non-unit Edge Weights
23 Goal Node Shortest Path Shortest Distance B [A, C, B] 3 C [A, C] 2 D [A, C, B, D] 5 E [A, C, B, E] 6A B C D E
4 2 3 1 2 3 1 5 4
Shortest Path Application
PVD to SF…
Our Graph
25 ATL SF LA CLE DC PHL NYC PVD PHX STL CHI 10 10 35 15 10 10 10 20 15 5 10 20 15 15 10 10 Start End 20Our Graph
26 ATL SF LA CLE DC PHL NYC PVD PHX STL CHI 10 10 35 15 10 10 10 20 15 5 10 20 15 15 10 10 Start End 20What is the cost of this path?
Our Graph
27 ATL SF LA CLE DC PHL NYC PVD PHX STL CHI 10 10 35 15 10 10 10 20 15 5 10 20 15 15 10 10 Start End 20What is the cost of this path? Is there a shorter path?
Our Graph
28 ATL SF LA CLE DC PHL NYC PVD PHX STL CHI 10 10 35 15 10 10 10 20 15 10 20 15 15 10 Start End 20What is the cost of this path? Is there a shorter path?
Shortest Path
have distinct weights!
Shortest Path
removeMin()…
source
source have already been explored
30Dijkstra’s Algorithm
decreased
with minimal cost from source
31Dijkstra’s Algorithm Example
from Q (S in this example).
removed node’s neighbors…
weights to S’s dist.
32 S B A D C 7 8 2 3 2 1 5 5 4∞ ∞ ∞ ∞
S B A D C 7 8 2 3 2 1 5 5 4 7 2∞ ∞
Dijkstra’s Algorithm Example
Dijkstra’s Algorithm Example
visited…
shortest dist. to source
34 S B A D C 7 8 2 3 2 1 5 5 4 5 2 6 7 S B A D C 7 8 2 3 2 1 5 5 4 5 2 6 7Dijkstra’s Algorithm Example
shortest distance but did not “create” paths
shortest path to a particular node?
Dijkstra’s Example
36A B C D E
4 2 3 1 2 3 1 5 4
A B C D E ∞ ∞ ∞ ∞Dijkstra’s Example
37A B C D E
4 2 3 1 2 3 1 5 4
A B C D E 4 2 ∞ ∞Dijkstra’s Example
38A B C D E
4 2 3 1 2 3 1 5 4
A B C D E 3 2 6 7Dijkstra’s Example
39A B C D E
4 2 3 1 2 3 1 5 4
A B C D E 3 2 5 6Dijkstra’s Example
40A B C D E
4 2 3 1 2 3 1 5 4
A B C D E 3 2 5 6Simulate Dijkstra’s
41Activity #3
Simulate Dijkstra’s
42Activity #3
Simulate Dijkstra’s
43Activity #3
Simulate Dijkstra’s
44Activity #3
Dijkstra’s Algorithm
45programming
Dijkstra Pseudo-Code
46 function dijkstra(G, s): // Input: graph G with vertices V, and source s // Output: Nothing // Purpose: Decorate nodes with shortest distance from s for v in V: v.dist = infinity // Initialize distance decorations v.prev = null // Initialize previous pointers to null s.dist = 0 // Set distance to start to 0 PQ = PriorityQueue(V) // Use v.dist as priorities while PQ not empty: u = PQ.removeMin() for all edges (u, v): //each edge coming out of u if u.dist + cost(u, v) < v.dist: // cost() is weight v.dist = u.dist + cost(u,v) // Replace as necessary v.prev = u // Maintain pointers for path PQ.decreaseKey(v, v.dist)Runtime of Dijkstra w/ Heap
47Activity #3
function dijkstra(G, s): for v in V: // 1. O(__) v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) // 2. O(__) while PQ not empty: // 3. O(__) u = PQ.removeMin() // 4. O(__) for all edges (u, v): // 5. O(__) if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist) // 6. O(__) Total:Runtime of Dijkstra w/ Heap
48Activity #3
function dijkstra(G, s): for v in V: // 1. O(__) v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) // 2. O(__) while PQ not empty: // 3. O(__) u = PQ.removeMin() // 4. O(__) for all edges (u, v): // 5. O(__) if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist) // 6. O(__) Total:Runtime of Dijkstra w/ Heap
49Activity #3
function dijkstra(G, s): for v in V: // 1. O(__) v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) // 2. O(__) while PQ not empty: // 3. O(__) u = PQ.removeMin() // 4. O(__) for all edges (u, v): // 5. O(__) if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist) // 6. O(__) Total:Runtime of Dijkstra w/ Heap
50Activity #3
function dijkstra(G, s): for v in V: // 1. O(__) v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) // 2. O(__) while PQ not empty: // 3. O(__) u = PQ.removeMin() // 4. O(__) for all edges (u, v): // 5. O(__) if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist) // 6. O(__) Total:Dijkstra Runtime
51 function dijkstra(G, s): for v in V: v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) while PQ not empty: u = PQ.removeMin() for all edges (u, v): if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist)O(|V|)
O(|V|)
dependsO(|E|)
total dependsDijkstra Runtime
Dijkstra Runtime w/ Array or List
53 function dijkstra(G, s): for v in V: v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) while PQ not empty: u = PQ.removeMin() for all edges (u, v): if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist)O(|V|)
O(|V|) O(|V|) O(1) O(|E|)
totalO(|V|)
Dijkstra Runtime w/ Array or List
O(|V | + |V | + |V |2 + |E|) = O(|V |2 + |E|) = O(|V |2)
<latexit sha1_base64="s/PpJtLJ9GM1Gg8qkhx28g4UfY=">ACInicbVDLSgMxFM3UV62vUZdugkVpEcpMEXyAUBTBnRXsA9paMmnahmYyQ5IRSt/ceOvuHhayX4MWamQ6mtB5J7Odeknscn1GpLOvbSCwsLi2vJFdTa+sbm1vm9k5ZeoHApIQ95omqgyRhlJOSoqRqi8Ich1GKk7vKvQrj0RI6vF71fdJw0UdTtsUI6Wlpnl+mxmW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EKPYND/rLQ8HLuEKMyRlzbZ81RgoShmZJSqB5L4CPdQh9Q05cglsjGIdhzBA620YNsT+nAFI3V6YoBcKfuoztdpLpy1gvF/7xaoNqnjQHlfqAIx+OH2gGDyoNhYLBFBcGK9TVBWFD9V4i7SCsdKwpHYI9u/I8KeVzZzn7jhduIzTSI9sA8ywAYnoABuQBGUAZP4AW8gXfj2Xg1PoyvcWvCiGd2wR8YP7+84p79</latexit><latexit sha1_base64="s/PpJtLJ9GM1Gg8qkhx28g4UfY=">ACInicbVDLSgMxFM3UV62vUZdugkVpEcpMEXyAUBTBnRXsA9paMmnahmYyQ5IRSt/ceOvuHhayX4MWamQ6mtB5J7Odeknscn1GpLOvbSCwsLi2vJFdTa+sbm1vm9k5ZeoHApIQ95omqgyRhlJOSoqRqi8Ich1GKk7vKvQrj0RI6vF71fdJw0UdTtsUI6Wlpnl+mxmWh/AITu6HfFivh1l4eAEjd6LU61NatmrZwVAc4TOyZpEKPYND/rLQ8HLuEKMyRlzbZ81RgoShmZJSqB5L4CPdQh9Q05cglsjGIdhzBA620YNsT+nAFI3V6YoBcKfuoztdpLpy1gvF/7xaoNqnjQHlfqAIx+OH2gGDyoNhYLBFBcGK9TVBWFD9V4i7SCsdKwpHYI9u/I8KeVzZzn7jhduIzTSI9sA8ywAYnoABuQBGUAZP4AW8gXfj2Xg1PoyvcWvCiGd2wR8YP7+84p79</latexit>Dijkstra Runtime w/ Heap
O(log|V|) time (so no need to scan heap to find entry)
Dijkstra Runtime w/ Heap
56 function dijkstra(G, s): for v in V: v.dist = infinity v.prev = null s.dist = 0 PQ = PriorityQueue(V) while PQ not empty: u = PQ.removeMin() for all edges (u, v): if v.dist > u.dist + cost(u, v): v.dist = u.dist + cost(u,v) v.prev = u PQ.decreaseKey(v, v.dist)O(|V|)
O(|V|) O(log|V|) O(log|V|) O(|E|)
totalO(|V|log|V|)
Dijkstra Runtime w/ Heap
O(|V | + |V | log |V |+|V | log |V | + |E| log |V |) = O(|V | + |V | log |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L5yABMJDN3heQZS0y0+aK8CcH9/eS/oHlau6y5D2fl+nWRg6UwCGoABecgzq4Aw3QBAR8WEWrZO1Zn/auvW8fzKS2lXl2wI+yK18M5rhE</latexit>O ✓ V log |V |
◆
Dijkstra’s on Graph with Negative Edges
58Activity #5
D A B C 2Dijkstra’s on Graph with Negative Edges
59Activity #5
D A B C 2Dijkstra’s on Graph with Negative Edges
60Activity #5
D A B C 2Dijkstra isn’ t perfect!
Negative Edge Weights
Bellman-Ford Algorithm
weights
edge weights