Shortest Paths in Graphs CS16: Introduction to Data Structures & Algorithms Spring 2020
Outline ‣ Shortest Paths ‣ Breadth First Search ‣ Dijkstra’s Algorithm 2
What is a Shortest Path? ‣ Given weighted graph G (weights on edges)… ‣ …what is shortest path from node u to v ? ‣ Applications ‣ Google maps ‣ Routing packets on the Internet ‣ Social networks 3
Single Source Shortest Path s (SSSP) ‣ Given a graph and a source node ‣ find the shortest paths to all other nodes 2 B D 4 4 A 1 3 1 3 2 C E 5 4
Simpler Problem: Unit Edges ‣ Let’s start with simpler problem ‣ On graph where every edge has unit cost 1 B D 1 1 1 1 1 A 1 1 C E 1 5
Simpler Problem: Unit Edges ‣ What is shortest path from A to each node? B:[A,B] ‣ D:[A,B,D] or [A,C,D] ‣ C:[A,C] ‣ 1 B D E:[A,B,E] or [A,C, E] ‣ 1 1 1 1 1 A 1 1 C E 1 6
Simpler Problem: Unit Edges ‣ Is there an algorithm we’ve already seen that solves this problem? 1 B D 1 1 1 1 1 A 1 1 C E 1 7
numStops (with BFS) (ORD) 3 ∞ ∞ (LAX) 2 (LAX) 2 PVD JFK ORD BTV SFO (DFW) 3 LGA HNL LAX DFW 0 MIA (HNL) 1 (LAX) 2 (DFW) 3 Have distance Follow ‘previous’ pointers Want a path to get a path 8
Breadth-First Search Use BFS to find shortest path from A to E . ‣ Consider all steps of adding/removing nodes from queue … ‣ …and updating each node’s ‘previous’ pointer. ‣ 1 B D 1 1 A 1 1 1 1 min 1 1 C E 1 Activity #1 9
Breadth-First Search Use BFS to find shortest path from A to E . ‣ Consider all steps of adding/removing nodes from queue … ‣ …and updating each node’s ‘previous’ pointer. ‣ 1 B D 1 1 A 1 1 1 1 min 1 1 C E 1 Activity #1 10
Breadth-First Search Use BFS to find shortest path from A to E . ‣ Consider all steps of adding/removing nodes from queue … ‣ …and updating each node’s ‘previous’ pointer. ‣ 1 B D 1 1 A 1 1 1 0 min 1 1 C E 1 Activity #1 11
Breadth First Search ‣ BFS always reaches target node in fewest steps ‣ Let’s look at path from A to E 1 B D 1 1 1 1 1 A 1 1 C E 1 12
Breadth First Search Simulation ‣ Strategy ‣ BFS uses queue to store nodes to visit ‣ Enqueue start node ‣ Decorate nodes w/ previous pointers to keep track of path Queue 1 B D A 1 1 A 1 1 1 1 1 C E 1 13
Breadth First Search Simulation ‣ Dequeue A ‣ Decorate its neighbors w/ “prev: A ” ‣ Enqueue them prev: A Queue 1 B D B 1 1 C A 1 1 1 1 1 C E 1 prev: A 14
Breadth First Search Simulation ‣ Dequeue B and repeat… ‣ …but ignoring nodes that have been decorated prev: A Queue prev: B 1 B D C 1 1 D A 1 1 1 1 E 1 C E 1 prev: A prev: B 15
Breadth First Search Simulation ‣ Dequeuing C and D has no effect… ‣ …since their neighbors have been decorated prev: A Queue prev: B 1 B D E 1 1 A 1 1 1 1 1 C E 1 prev: A prev: B 16
Breadth First Search Simulation ‣ When we dequeue E … ‣ …we traverse the prev pointers to return paths ‣ shortest path to E : [A,B,E] prev: A Queue prev: B 1 B D E 1 1 A 1 1 1 1 1 C E 1 prev: A prev: B 17
Non-Unit Edge Weights ‣ What if edge weights are not 1 ? ‣ More complicated 2 B D 4 4 A 1 3 1 3 2 C E 5 18
Shortest Path Fill in missing spaces using graph below ‣ Use A as source vertex ‣ 2 B D 4 4 A 1 3 1 2 min 3 2 C E 5 Activity #2 19
Shortest Path Fill in missing spaces using graph below ‣ Use A as source vertex ‣ 2 B D 4 4 A 1 3 1 2 min 3 2 C E 5 Activity #2 20
Shortest Path Fill in missing spaces using graph below ‣ Use A as source vertex ‣ 2 B D 4 4 A 1 3 1 1 min 3 2 C E 5 Activity #2 21
Shortest Path Fill in missing spaces using graph below ‣ Use A as source vertex ‣ 2 B D 4 4 A 1 3 1 0 min 3 2 C E 5 Activity #2 22
Non-unit Edge Weights Shortest Shortest Goal Node Path Distance B [A, C, B] 3 C [A, C] 2 D [A, C, B, D] 5 E [A, C, B, E] 6 2 B D 4 4 A 1 3 1 3 2 C E 5 23
Shortest Path Application ‣ Road trip ‣ Amy, Andy, Prakrit, & Stephanie want to get from PVD to SF… ‣ …following limited set of highways ‣ Cities are nodes and highways are edges ‣ Get to SF using shortest path 24
Our Graph Start 10 PVD 10 CLE 10 CHI NYC 35 15 End 20 5 SF PHL 20 15 STL 10 DC 10 10 15 10 15 PHX ATL LA 20 10 25
Our Graph Start 10 PVD 10 CLE 10 CHI NYC 35 15 End 20 5 SF PHL 20 15 STL 10 DC 10 10 15 10 15 PHX ATL LA 20 10 What is the cost of this path? 26
Our Graph Start 10 PVD 10 CLE 10 CHI NYC 35 15 End 20 5 SF PHL 20 15 STL 10 DC 10 10 15 10 15 PHX ATL LA 20 10 What is the cost of this path? Is there a shorter path? 27
Our Graph Start 10 PVD 10 CLE 10 CHI NYC 35 15 End 20 5 SF PHL 20 15 STL 10 DC 10 10 15 10 15 PHX ATL LA 20 10 What is the cost of this path? Is there a shorter path? 28
Shortest Path ‣ Why does BFS work with unit edges? ‣ Nodes visited in order of total distance from source ‣ We need way to do the same even when edges have distinct weights! ‣ How can we do this? ‣ Hint: we’ll use a data structure we’ve already seen 29
Shortest Path ‣ Use a priority queue! ‣ where priorities are total distances from source ‣ By visiting nodes in order returned by removeMin() … ‣ …you visit nodes in order of how far they are from source ‣ You guarantee shortest path to node because… ‣ …you don’t explore a node until all nodes closer to source have already been explored 30
Dijkstra’s Algorithm ‣ The algorithm is as follows: ‣ Decorate source with distance 0 & all other nodes with ∞ ‣ Add all nodes to priority queue w/ distance as priority ‣ While the priority queue isn’t empty ‣ Remove node from queue with minimal priority ‣ Update distances of the removed node’s neighbors if distances decreased ‣ When algorithm terminates, every node is decorated with minimal cost from source 31
Dijkstra’s Algorithm Example ∞ ∞ 1 ‣ Step 1 B C 7 ‣ Label source w/ dist. 0 8 0 3 ‣ Label other vertices w/ dist. ∞ 2 5 4 S ‣ Add all nodes to Q 2 ∞ ∞ ‣ Step 2 D A 5 ‣ Remove node with min. priority 7 1 ∞ from Q ( S in this example). C B 7 ‣ Calculate dist. from source to 8 0 3 removed node’s neighbors… 2 4 5 S ‣ …by adding adjacent edge weights to S ’s dist. 2 D A ∞ 5 2 32
Dijkstra’s Algorithm Example 10 5 1 ‣ Step 3 B C ‣ While Q isn’t empty, 7 8 0 ‣ repeat previous step 3 2 5 4 S ‣ removing A this time 2 ‣ Priorities of nodes in Q may have 7 D A to be updated 2 5 ‣ ex: B ’ s priority 5 1 6 ‣ Step 4 C B 7 ‣ Repeat again by removing vertex B 8 0 3 2 4 5 ‣ Update distances that are shorter S using this path than before 2 ‣ ex: C now has a distance 6 not 10 D A 7 5 2 33
Dijkstra’s Algorithm Example 6 5 1 ‣ Step 5 B C 7 ‣ Repeat 8 0 3 2 5 4 ‣ this time removing C S 2 ‣ Step 6 7 D A 2 5 ‣ After removing D… 5 1 6 C B ‣ …every node has been 7 visited… 8 0 3 2 4 5 S ‣ …and decorated w/ shortest dist. to source 2 D A 7 5 2 34
Dijkstra’s Algorithm Example ‣ Previous example decorated nodes with shortest distance but did not “create” paths ‣ How could you enhance algorithm to return the shortest path to a particular node? ‣ Previous pointers! ‣ Let’s do another example 35
Dijkstra’s Example 2 B D 4 4 A 1 3 1 3 2 C E 5 A B C D E 0 ∞ ∞ ∞ ∞ 36
Dijkstra’s Example 2 B D 4 4 A 1 3 1 3 2 C E 5 A B C D E 0 4 2 ∞ ∞ 37
Dijkstra’s Example 2 B D 4 4 A 1 3 1 3 2 C E 5 A B C D E 0 3 2 6 7 38
Dijkstra’s Example 2 B D 4 4 A 1 3 1 3 2 C E 5 A B C D E 0 3 2 5 6 39
Dijkstra’s Example 2 B D 4 4 A 1 3 1 3 2 C E 5 A B C D E 0 3 2 5 6 40
Simulate Dijkstra’s 2 min Activity #3 41
Simulate Dijkstra’s 2 min Activity #3 42
Simulate Dijkstra’s 1 min Activity #3 43
Simulate Dijkstra’s 0 min Activity #3 44
Dijkstra’s Algorithm ‣ Comes up with an optimal solution ‣ shortest path to each node ‣ Like many optimization algorithms, uses dynamic programming ‣ overlapping subproblems (distances to nodes) ‣ solved in a particular order (closest first) ‣ Dijkstra’s is greedy ‣ at each step, considers next closest node ‣ Greedy algorithms not always optimal, usually fast 45
Recommend
More recommend