320 likes | 427 Views
Shortest Paths. Text Discrete Mathematics and Its Applications (5 th Edition) Kenneth H. Rosen Chapter 8.6 Based on slides from Chuck Allison, Michael T. Goodrich, and Roberto Tamassia By Longin Jan Latecki. BOS. NY. CHI. SF. DEN. ATL. LA. MIA. Weighted Graphs.
E N D
Shortest Paths Text Discrete Mathematics and Its Applications(5th Edition) Kenneth H. Rosen Chapter 8.6 Based on slides from Chuck Allison, Michael T. Goodrich, and Roberto Tamassia By Longin Jan Latecki
BOS NY CHI SF DEN ATL LA MIA Weighted Graphs Graphs that have a number assigned to each edge are called weighted graphs.
BOS NY CHI SF DEN ATL LA MIA Weighted Graphs MILES 860 2534 191 1855 722 908 957 760 606 834 349 2451 1090 595
BOS NY CHI SF DEN ATL LA MIA Weighted Graphs FARES $129 $79 $39 $99 $59 $69 $89 $79 $99 $89 $39 $129 $69
BOS NY CHI SF DEN ATL LA MIA Weighted Graphs FLIGHT TIMES 4:05 2:10 0:50 2:55 1:50 2:10 2:20 1:55 1:40 2:45 2:00 3:50 1:15 1:30
Weighted Graphs • A weighted graph is a graph in which each edge (u, v) has a weight w(u, v). Each weight is a real number. • Weights can represent distance, cost, time, capacity, etc. • The length of a path in a weighted graph is the sum of the weights on the edges. • Dijkstra’s Algorithm finds the shortest path between two vertices.
Dijkstra Animation • Demo
processed: 1 0 0 0 0 0 0 fromNode: 1 1 1 distance: 0 15 35 # 20 # # index: 1 2 3 4 5 6 7 1 20 15 5 IndexOfMin 35 75 10 2 3 7 50 40 35 15 4 6 10
processed: 1 1 0 0 0 0 0 fromNode: 1 1 1 distance: 0 15 35 # 20 # # index: 1 2 3 4 5 6 7 1 20 15 5 35 75 10 2 3 7 50 40 35 15 4 6 10 Unprocessed node adjacent to2 is 4. # > 15+40 = 55 so replace # with 55
processed: 1 1 0 0 0 0 0 fromNode: 1 1 2 1 distance: 0 15 35 55 20 # # index: 1 2 3 4 5 6 7 1 20 15 5 IndexOfMin 35 75 10 2 3 7 50 40 35 15 4 6 10
processed: 1 1 0 0 1 0 0 fromNode: 1 1 2 1 distance: 0 15 35 55 20 # # index: 1 2 3 4 5 6 7 1 20 15 5 IndexOfMin 35 75 10 2 3 7 50 40 35 15 4 6 10 Unprocessed node adjacent to5 is 3. 35 > 20+10 = 30 so replace 35 with 30
processed: 1 1 0 0 1 0 0 fromNode: 1 5 2 1 distance: 0 15 30 55 20 # # index: 1 2 3 4 5 6 7 1 20 15 5 IndexOfMin 35 75 10 2 3 7 50 40 35 15 4 6 10 Unprocessed node adjacent to5 is 6. # > 20+50 = 70 so replace # with 70
processed: 1 1 0 0 1 0 0 fromNode: 1 5 2 1 5 distance: 0 15 30 55 20 70 # index: 1 2 3 4 5 6 7 1 20 15 5 IndexOfMin 35 75 10 2 3 7 50 40 35 15 4 6 10 Unprocessed node adjacent to5 is 7. # > 20+75 = 95 so replace # with 95
processed: 1 1 0 0 1 0 0 fromNode: 1 5 2 1 5 5 distance: 0 15 30 55 20 70 95 index: 1 2 3 4 5 6 7 1 20 15 5 IndexOfMin 35 75 10 2 3 7 50 40 35 15 4 6 10
processed: 1 11 0 1 0 0 fromNode: 1 5 2 1 5 5 distance: 0 15 30 55 20 70 95 index: 1 2 3 4 5 6 7 1 20 15 5 IndexOfMin 35 75 10 2 3 7 50 40 35 15 4 6 10 Unprocessed node adjacent to3 is 4. 55 < 30 + 35 = 65 no change in array
processed: 1 11 0 1 0 0 fromNode: 1 5 2 1 5 5 distance: 0 15 30 55 20 70 95 index: 1 2 3 4 5 6 7 1 20 15 5 IndexOfMin 35 75 10 2 3 7 50 40 35 15 4 6 10
processed: 1 1111 0 0 fromNode: 1 5 2 1 5 5 distance: 0 15 30 55 20 70 95 index: 1 2 3 4 5 6 7 1 20 15 5 IndexOfMin 35 75 10 2 3 7 50 40 35 15 4 6 10 Unprocessed node adjacent to4 is 6. 70 > 55 + 10 = 65 so replace 70 with 65
processed: 1 1111 0 0 fromNode: 1 5 2 1 4 5 distance: 0 15 30 55 20 65 95 index: 1 2 3 4 5 6 7 1 20 15 5 IndexOfMin 35 75 10 2 3 7 50 40 35 15 4 6 10
processed: 1 1111 0 0 fromNode: 1 5 2 1 4 5 distance: 0 15 30 55 20 65 95 index: 1 2 3 4 5 6 7 1 20 15 5 IndexOfMin 35 75 10 2 3 7 50 40 35 15 4 6 10
processed: 1 11111 0 fromNode: 1 5 2 1 4 5 distance: 0 15 30 55 20 65 95 index: 1 2 3 4 5 6 7 1 20 15 5 IndexOfMin 35 75 10 2 3 7 50 40 35 15 4 6 10 Unprocessed node adjacent to6 is 7. 95 > 65 + 15 = 80 so replace 95 with 80
processed: 1 11111 0 fromNode: 1 5 2 1 4 6 distance: 0 15 30 55 20 65 80 index: 1 2 3 4 5 6 7 1 20 15 IndexOfMin 5 35 75 10 2 3 7 50 40 35 15 4 6 10
processed: 1 111111 fromNode: 1 5 2 1 4 6 distance: 0 15 30 55 20 65 80 index: 1 2 3 4 5 6 7 1 20 15 IndexOfMin 5 35 75 10 2 3 7 50 40 35 15 4 6 10 All nodes have been processed Algorithm finishes.
Theorems Dijkstra’s algorithm finds the length of a shortest path between two vertices in a connected simple undirected weighted graph. Dijkstra’s algorithm uses O(n2) operations (additions and comparisons) to find the length of the shortest path between two vertices in a connected simple undirected weighted graph.
Problem: shortest path from a to z f b d 5 5 4 7 3 4 1 2 a z 3 4 c 5 e 5 g