Docsity

Dijkstra's Algorithm: Finding Shortest Paths in Graphs - Prof. David J. Galles, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Dijkstra's Algorithm: Finding Shortest Paths in Graphs - Prof. David J. Galles and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

CS245-2009S-17Shortest PathDijkstra’s Algorithm^ David GallesDepartment of Computer ScienceUniversity of San Francisco

17-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 directedgraph, with symmetric edges Least-cost path may not be the path containing thefewest edges “shortest path” == “least cost path” “path containing fewest edges” = “pathcontaining fewest edges”

17-2:^ Shortest Path Example^ Shortest path

6 =^ path containing fewest edges

B A

C D

21 E

44 Shortest Path from A to E: A, B, C, D, E

17-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?

17-5:^ Single Source Shortest Path^ If all edges have unit weight ...

17-6:^ Single Source Shortest Path^ If all edges have unit weight,^ We can use Breadth First Search to compute theshortest path^ BFS Spanning Tree contains shortest path to eachnode in the graph^ Need to do some more work to create & saveBFS spanning tree^ When edges have differing weights, this obviouslywill not work

17-8:^ Single Source Shortest Path^ A^

B

C^

D^

E

F^

2 G

Start with the vertex A

17-9:^ Single Source Shortest Path^ A^

B

C^

D^

E

F^

2 G

Node^ Distance^ A^0 B C D E F G

Known vertices are circled in red We can now extend the known set by 1 vertex

17-11:^ Single Source Shortest Path^ A^

B

C^

D^

E

F^

2 G

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 roundaboutpath?

17-12:^ Single Source Shortest Path^ A^

B C^

D^ E F^ 2 G (^1 ) 3 10 2 2 (^8 )

Node (^4 ) 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 roundaboutpath?^ No – to get to any other node will cost at least 1^ No negative edge weights, can’t do better than^1

17-14:^ Single Source Shortest Path^ A^

B

C^

D^

E

F^

2 G

Node^ Distance^ A^0 B^2 C D^1 E F G

How do we know that we could not get to Bcheaper than by going through D?

17-15:^ Single Source Shortest Path^ A^

B C^

D^

E F^ 2 G (^1 ) 3 10 2 2 (^8 ) (^4 )

Node^ Distance^ A^0 B^2 C D^1 E F G

How do we know that we could not get to Bcheaper than by 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

17-17:^ Single Source Shortest Path^ A^

B

C^

D^

E

F^

2 G

Node^ Distance^ A^0 B^2 C^3 D^1 E F G

(We also could have added E for this step) Next vertex to add to Known ...

17-18:^ Single Source Shortest Path^ A

B

C^

D^

E

F^

2 G

Node^ Distance^ A^0 B^2 C^3 D^1 E^3 F G

Cost to add F is 8 (through C) Cost to add G is 5 (through D)