11.06a.Dijkstra's_algorithm
11.06a.Dijkstra's_algorithm
Dijkstra’s algorithm
ece.uwaterloo.ca
[email protected]
Outline
Dijkstra’s algorithm
Strategy
Strategy
Strategy
Strategy
Strategy
Strategy
Strategy
Strategy
Strategy
Strategy
By observation:
– The path (A, B, F, E) is longer than (A, B, E)
– The path (A, B, F, C) is shorter than the path (A, C)
All-pairs Shortest Path
14
Strategy
Strategy
Dijkstra’s algorithm
Dijkstra’s algorithm
– Continue iterating until all vertices are visited or all remaining vertices
have a distance to them of infinity
18
Example
http://thunderbird37.com/tag/parker-brothers/
19
Example
http://thunderbird37.com/tag/parker-brothers/
20
Example
Example
http://thunderbird37.com/tag/parker-brothers/
22
23
Example
Example
Example
Find the shortest distance from Kamchatka (K) to every other region
26
Example
Example
We visit vertex K
Example
Example
Example
Example
Example
Example
We already have a shorter path (K, L), but we update the other
three
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Where to next?
Example
Example
Where to next?
– Does it matter if we visit vertex F first or vertex J first?
Example
Example
Example
Example
Example
Example
Example
Example
Example
Vertex Previous
A B
B E
C E
D E
E H
F G
G I
H K
I H
J K
K Ø
L K
62
Questions:
– What if at some point, all unvisited vertices have a distance ∞?
• This means that the graph is unconnected
• We have found the shortest paths to all vertices in the connected subgraph
containing the source vertex
– What if we just want to find the shortest path between vertices vj and vk?
• Apply the same algorithm, but stop when we are visiting vertex vk
– Does the algorithm change if we have a directed graph?
• No
63
We iterate |V| – 1 times, each time finding next closest vertex to the source
– Iterating through the table requires is Q(|V|) time
– Each time we find a vertex, we must check all of its neighbors
– With an adjacency matrix, the run time is Q(|V|(|V| + |V|)) = Q(|V|2)
– With an adjacency list, the run time is Q(|V|2 + |E|) = Q(|V|2) as |E| = O(|V|2)
Can we do better?
– Recall, we only need the closest vertex
– How about a priority queue?
• Assume we are using a binary heap
• We will have to update the heap structure—this requires additional work
64
We iterate |V| times, each time finding the closest vertex to the source
– Place the distances into a priority queue
– The size of the priority queue is O(|V|)
– Thus, the work required for this is O(|V| ln(|V|))
Thus, the total run time is O(|V| ln(|V|) + |E| ln(|V|)) = O(|E| ln(|V|))
All-pairs Shortest Path
65
Triangle Inequality
http://xkcd.com/85/
69
Negative Weights
http://xkcd.com/69/
70
Summary
References
Mark A. Weiss,
Data Structures and Algorithm Analysis in C++, 3rd Ed., Addison Wesley, 2006, Ch.?, p.?.
These slides are provided for the ECE 250 Algorithms and Data Structures course. The
material in it reflects Douglas W. Harder’s best judgment in light of the information available to
him at the time of preparation. Any reliance on these course slides by any party for any other
purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for
damages, if any, suffered by any party as a result of decisions made or actions based on these
course slides for any other purpose than that for which it was intended.