Download Shortest Path - Lecture Slides | Data Structures and Algorithms | CS 245 and more Study notes Data Structures and Algorithms in PDF only on Docsity!
Data Structures and Algorithms
Shortest Path^ Chris BrooksDepartment of Computer ScienceUniversity of San Francisco
Department of Computer Science โ University of San Francisco โ p.1/
20-0:^ Computing Shortest Path
Given a directed weighted graph
G^ (all weights
non-negative) and two vertices
x^ and^ y
, find the least-cost
path from
x^ to^ y^ in
G.
Undirected graph is a special case of a directed graph,with symmetric edges Least-cost path may not be the path containing the fewestedges โshortest pathโ == โleast cost pathโ โpath containing fewest edgesโ = โpath containingfewest edgesโ
Department of Computer Science โ University of San Francisco โ p.2/
20-2:^ Shortest Path Example
Shortest path
6 =^ path containing fewest edges
A
B
C D (^21) E
1 2 8
(^454) Shortest Path from A to E: A, B, C, D, E
Department of Computer Science โ University of San Francisco โ p.4/
20-3:^ Single Source Shortest Path
To find the shortest path from vertex
x^ to vertex
y, we
need (worst case) to find the shortest path from
x^ to^ all
other vertices in the graph^ Why?
Department of Computer Science โ University of San Francisco โ p.5/
20-5:^ Single Source Shortest Path
If all edges have unit weight ...
Department of Computer Science โ University of San Francisco โ p.7/
20-6:^ Single Source Shortest Path
If all edges have unit weight, We can use Breadth First Search to compute the shortestpath BFS Spanning Tree contains shortest path to each nodein the graph^ Need to do some more work to create & save BFSspanning tree When edges have differing weights, this obviously will notwork
Department of Computer Science โ University of San Francisco โ p.8/
20-8:^ Single Source Shortest Path
A^
B
C^
D^
E
F^
G
Start with the vertex A
Department of Computer Science โ University of San Francisco โ p.10/
20-9:^ Single Source Shortest Path
A^
B
C^
D^
E
F^
G
Node^ Distance^ A B C D E F G
Known vertices are circled in red We can now extend the known set by 1 vertex
Department of Computer Science โ University of San Francisco โ p.11/
20-11:^
Single Source Shortest Path
A^
B
C^
D^
E
F^
G
Node^ Distance^ A B C D E F G
Why is it safe to add D, with cost 1?^ Could we do better with a more roundabout path?
Department of Computer Science โ University of San Francisco โ p.13/
20-12:^
Single Source Shortest Path
A^
B C^
D^
E F^ 2 G 4
1
3 10 2
2 5
8
4
6 1
Node^ Distance^ A^0 B C D^1 E F G
Why is it safe to add D, with cost 1?^ Could we do better with a more roundabout path?^ No โ to get to any other node will cost at least 1^ No negative edge weights, canโt do better than 1
Department of Computer Science โ University of San Francisco โ p.14/
20-14:^
Single Source Shortest Path
A^
B
C^
D^
E
F^
G
Node^ Distance^ A B C D E F G
How do we know that we could not get to B cheaper thanby going through D?
Department of Computer Science โ University of San Francisco โ p.16/
20-15:^
Single Source Shortest Path
A^
B
C^
D^
E
F^
G 2 4
1
3
10 2
2 5
8
4
6 1
Node^ Distance^ A B C D E F G
0 2 1
How do we know that we could not get to B cheaper thanby going through D?^ Costs 1 to get to D^ Costs at least 2 to get anywhere from D^ Cost
at least
(1+2 = 3) to get to B through D
Department of Computer Science โ University of San Francisco โ p.17/
20-17:^
Single Source Shortest Path
A^
B
C^
D^
E
F^
G
Node^ Distance^ A B C D E F G
(We also could have added E for this step) Next vertex to add to Known ...
Department of Computer Science โ University of San Francisco โ p.19/
20-18:^
Single Source Shortest Path
A^
B
C^
D^
E
F^
G
Node^ Distance^ A B C D E F G
Cost to add F is 8 (through C) Cost to add G is 5 (through D)
Department of Computer Science โ University of San Francisco โ p.20/