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

An explanation of dijkstra's algorithm, a popular algorithm used to find the shortest path between two vertices in a weighted graph. The concept of shortest path, the difference between shortest path and path with fewest edges, and the process of implementing dijkstra's algorithm. It also discusses the differences between single source shortest path and all-source shortest path, and introduces floyd's algorithm as an alternative for finding shortest paths.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-mhf
koofers-user-mhf 🇺🇸

10 documents

1 / 78

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and Algorithms
CS245-2009S-17
Shortest Path
Dijkstra’s Algorithm
David Galles
Department of Computer Science
University of San Francisco
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e

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)