Relaxation is Safe
Lemma: The relaxation algorithm maintains the invariant that d[v] ≥ δ(s, v) for all v ∈ V.
Proof: By induction on the number of steps.
Consider RELAX(u, v, w). By induction d[u] ≥ δ(s, u). By the triangle inequality, δ(s, v) ≤ δ(s, u) + δ(u, v). This means that δ(s, v) ≤ d[u] + w(u, v), since d[u] ≥ δ(s, u) and w(u, v) ≥ δ(u, v). So setting d[v] = d[u] + w(u, v) is safe.
DAGS
Can’t have negative cycles because there are no cycles!
- Topologically sort the DAG. The path from u to v implies that u is before v in the linear ordering.
- One pass over vertices in topologically sorted order relaxing each edge that leaves each vertex. Θ(V + E) time
Dijkstra's Algorithm
For each edge (u, v) E, assume w(u, v) ≥ 0, maintain a set S of vertices whose final shortest path weights have been determined. Repeatedly select u V − S with minimum shortest path estimate, add u to S, relax all edges out of u.
Strategy: Dijkstra is a greedy algorithm: choose the closest vertex in V − S to add to set S.
Correctness: The key observation is that each time a vertex u is added to set S, we have d[u] = δ(s, u).
Dijkstra(G, W, s):
Initialize(G, s)
S ← φ
Q ← V[G] //Insert into Q
while Q != φ
do u ← EXTRACT-MIN(Q)
S = S ∪ {u}
for each vertex v in Adj[u]
do RELAX(u, v, w)
Dijkstra Complexity
Θ(v) inserts into priority queue
Θ(v) EXTRACT-MIN operations
Θ(E) DECREASE KEY operations
Array impl:
Θ(v) time for extra min
Θ(1) for decrease key
Total: Θ(V.V + E.1) = Θ(V^2 + E) = Θ(V^2)
Binary min-heap:
Θ(lgV) for extract min
Θ(lg V ) for decrease key
Total: Θ(V lg V + E lg V )
Fibonacci heap:
Θ(lgV) for extract min
Θ(1) for decrease key amortized cost
Total: Θ(V lg V + E)