0% found this document useful (0 votes)
16 views

Dijkstra's Algorithm

The document discusses Dijkstra's algorithm for solving the single-source shortest path problem on graphs where edge weights are non-negative. It explains the key ideas behind the algorithm, proves its correctness, and analyzes its running time of O(E+VlogV) when using a binary heap priority queue.

Uploaded by

Koka Noodles
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Dijkstra's Algorithm

The document discusses Dijkstra's algorithm for solving the single-source shortest path problem on graphs where edge weights are non-negative. It explains the key ideas behind the algorithm, proves its correctness, and analyzes its running time of O(E+VlogV) when using a binary heap priority queue.

Uploaded by

Koka Noodles
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to Algorithms: 6.

006
Massachusetts Institute of Technology
Instructors: Erik Demaine, Jason Ku, and Justin Solomon Lecture 13: Dijkstra’s Algorithm

Lecture 13: Dijkstra’s Algorithm

Review
• Single-Source Shortest Paths on weighted graphs
• Previously: O(|V | + |E|)-time algorithms for small positive weights or DAGs
• Last time: Bellman-Ford, O(|V ||E|)-time algorithm for general graphs with negative weights
• Today: faster for general graphs with non-negative edge weights, i.e., for e ∈ E, w(e) ≥ 0

Restrictions SSSP Algorithm


Graph Weights Name Running Time O(·) Lecture
General Unweighted BFS |V | + |E| L09
DAG Any DAG Relaxation |V | + |E| L11
General Any Bellman-Ford |V | · |E| L12
General Non-negative Dijkstra |V | log |V | + |E| L13 (Today!)

Non-negative Edge Weights


• Idea! Generalize BFS approach to weighted graphs:
– Grow a sphere centered at source s
– Repeatedly explore closer vertices before further ones
– But how to explore closer vertices if you don’t know distances beforehand? :(

• Observation 1: If weights non-negative, monotonic distance increase along shortest paths


– i.e., if vertex u appears on a shortest path from s to v, then δ(s, u) ≤ δ(s, v)
– Let Vx ⊂ V be the subset of vertices reachable within distance ≤ x from s
– If v ∈ Vx , then any shortest path from s to v only contains vertices from Vx
– Perhaps grow Vx one vertex at a time! (but growing for every x is slow if weights large)
• Observation 2: Can solve SSSP fast if given order of vertices in increasing distance from s
– Remove edges that go against this order (since cannot participate in shortest paths)
– May still have cycles if zero-weight edges: repeatedly collapse into single vertices
– Compute δ(s, v) for each v ∈ V using DAG relaxation in O(|V | + |E|) time
2 Lecture 13: Dijkstra’s Algorithm

Dijkstra’s Algorithm
• Named for famous Dutch computer scientist Edsger Dijkstra (actually Dÿkstra!)

• Idea! Relax edges from each vertex in increasing order of distance from source s

• Idea! Efficiently find next vertex in the order using a data structure

• Changeable Priority Queue Q on items with keys and unique IDs, supporting operations:
Q.build(X) initialize Q with items in iterator X
Q.delete min() remove an item with minimum key
Q.decrease key(id, k) find stored item with ID id and change key to k

• Implement by cross-linking a Priority Queue Q0 and a Dictionary D mapping IDs into Q0

• Assume vertex IDs are integers from 0 to |V | − 1 so can use a direct access array for D

• For brevity, say item x is the tuple (x.id, x.key)

• Set d(s, v) = ∞ for all v ∈ V , then set d(s, s) = 0

• Build changeable priority queue Q with an item (v, d(s, v)) for each vertex v ∈ V

• While Q not empty, delete an item (u, d(s, u)) from Q that has minimum d(s, u)

– For vertex v in outgoing adjacencies Adj+ (u):


∗ If d(s, v) > d(s, u) + w(u, v):
· Relax edge (u, v), i.e., set d(s, v) = d(s, u) + w(u, v)
· Decrease the key of v in Q to new estimate d(s, v)

• Run Dijkstra on example


Lecture 13: Dijkstra’s Algorithm 3

Example

Delete d(s, v)
v from Q s a b c d 2
G a b
s 0 ∞ ∞ ∞ ∞ 10
c 10 ∞ 3 ∞
d 7 11 5 s 4 1 5 7
8
a 7 10 3
b 9 c d
2
δ(s, v) 0 7 9 3 5

Correctness
• Claim: At end of Dijkstra’s algorithm, d(s, v) = δ(s, v) for all v ∈ V

• Proof:

– If relaxation sets d(s, v) to δ(s, v), then d(s, v) = δ(s, v) at the end of the algorithm
∗ Relaxation can only decrease estimates d(s, v)
∗ Relaxation is safe, i.e., maintains that each d(s, v) is weight of a path to v (or ∞)
– Suffices to show d(s, v) = δ(s, v) when vertex v is removed from Q
∗ Proof by induction on first k vertices removed from Q
∗ Base Case (k = 1): s is first vertex removed from Q, and d(s, s) = 0 = δ(s, s)
∗ Inductive Step: Assume true for k < k 0 , consider k 0 th vertex v 0 removed from Q
∗ Consider some shortest path π from s to v 0 , with w(π) = δ(s, v 0 )
∗ Let (x, y) be the first edge in π where y is not among first k 0 − 1 (perhaps y = v 0 )
∗ When x was removed from Q, d(s, x) = δ(s, x) by induction, so:

d(s, y) ≤ δ(s, x) + w(x, y) relaxed edge (x, y) when removed x


= δ(s, y) subpaths of shortest paths are shortest paths
≤ δ(s, v 0 ) non-negative edge weights
≤ d(s, v 0 ) relaxation is safe
≤ d(s, y) v 0 is vertex with minimum d(s, v 0 ) in Q

∗ So d(s, v 0 ) = δ(s, v 0 ), as desired


4 Lecture 13: Dijkstra’s Algorithm

Running Time
• Count operations on changeable priority queue Q, assuming it contains n items:
Operation Time Occurrences in Dijkstra
Q.build(X) (n = |X|) Bn 1
Q.delete min() Mn |V |
Q.decrease key(id, k) Dn |E|
• Total running time is O(B|V | + |V | · M|V | + |E| · D|V | )
• Assume pruned graph to search only vertices reachable from the source, so |V | = O(|E|)

Priority Queue Q0 Q Operations O(·) Dijkstra O(·)


on n items build(X) delete min() decrease key(id, k) n = |V | = O(|E|)
Array n n 1 |V |2
Binary Heap n log n(a) log n |E| log |V |
Fibonacci Heap n log n(a) 1(a) |E| + |V | log |V |

• If graph is dense, i.e., |E| = Θ(|V |2 ), using an Array for Q0 yields O(|V |2 ) time
• If graph is sparse, i.e., |E| = Θ(|V |), using a Binary Heap for Q0 yields O(|V | log |V |) time
• A Fibonacci Heap is theoretically good in all cases, but is not used much in practice
• We won’t discuss Fibonacci Heaps in 6.006 (see 6.854 or CLRS chapter 19 for details)
• You should assume Dijkstra runs in O(|E|+|V | log |V |) time when using in theory problems

Summary: Weighted Single-Source Shortest Paths


Restrictions SSSP Algorithm
Graph Weights Name Running Time O(·)
General Unweighted BFS |V | + |E|
DAG Any DAG Relaxation |V | + |E|
General Non-negative Dijkstra |V | log |V | + |E|
General Any Bellman-Ford |V | · |E|

• What about All-Pairs Shortest Paths?


• Doing a SSSP algorithm |V | times is actually pretty good, since output has size O(|V |2 )
• Can do better than |V | · O(|V | · |E|) for general graphs with negative weights (next time!)
MIT OpenCourseWare
https://ocw.mit.edu

6.006 Introduction to Algorithms


Spring 2020

For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms

You might also like