Content
- Dijkstra's Algorithm
- Shortest Paths with Negative Edges
- Bellman-Ford Algorithm
- Shortest Paths in DAGs
Dijkstra's Algorithm
- Shortest path problem
- Adapted from BFS with edge lengths positive
Tricks
- Replace edges with len 1 with multiple edges of len 1 & dummy nodes
- Then run BFS
- Not efficient with more nodes!
Implementation
Dijkstra(G, l, s):
# Input: Graph G = (V, E), directed or undirected;
positive edge lengths {le : e ∈ E}; vertex s ∈ V
# Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u
for all u ∈ V :
dist(u) = ∞
prev(u) = nil
dist(s) = 0
H = makequeue(V) # using dist-values as keys
while H is not empty:
u = deletemin(H) # = |V| times
for all edges (u, v) ∈ E:
if dist(v) > dist(u) + l(u, v):
dist(v) = dist(u) + l(u, v)
prev(v) = u
decreasekey(H, v) # = |V| + |E| times
# Alternative
Initialize dist(s) to 0, other dist(·) values to ∞
R = { } # the "known region"
while R != V:
Pick the node v !∈ R with smallest dist(·)
Add v to R
for all edges (v, z) ∈ E:
if dist(z) > dist(v) + l(v, z):
dist(z) = dist(v) + l(v, z)
=> Binary heap: O((|V| + |E|)log|V|)
Update Operation
dist(v) = min{dist(v), dist(u) + l(u, v)}
- Correct distance to v where u is second-last in shortest path & dist(u) is correct
- Never make dist(v) too small (i.e. never underestimate) = safe
Analysis - Heaps & Array
Implementation | deletemin | insert/decreasekey | \ | V\ | xdeletemin + (\ | V\ | +\ | E\ | )xinsert | ||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Array | O(\ | V\ | ) | O(1) | O(\ | V\ | ^2) | ||||||
Binary heap | O(log\ | V\ | ) | O(log\ | V\ | ) | O((\ | V\ | + \ | E\ | )log\ | V\ | ) |
d-ary heap | O(dlog\ | V\ | /log d) | O(log\ | V\ | /log d) | O(\ | V\ | *d + \ | E\ | log\ | V\ | /logd) |
Fibonacci heap | O(log\ | V\ | ) | O(1) (amortizes) | O(\ | V\ | log\ | V\ | + \ | E\ | ) |
- If G sparse - heap (|E| ~ |V|)
- If G dense - array (|E| ~ |V|^2)
Priority Queue Implementations
- Array
- insert/decreasekey:
O(1)
- deletemin:
O(n)
- insert/decreasekey:
- Binary Heap
- complete binary tree (filled in from left to right, full)
- key value <= childrens'
- insert/decreasekey:
O(log n)
- deletemin:
O(log n)
- deletemin:
- insert/decreasekey:
- array representation: parent @
floor(j/2)
, children @2j
&2j+1
- d-ary heap
- nodes have d children
- h(T) =
Θ(log d n)
=Θ((log n)/(log d))
- insert:
Θ((log n)/(log d))
- deletemin:
Θ(d * log d n)
- insert:
- array representation: parent @
(j-1)/d
, children @{(j-1)d+2, ..., min{n, (j-1)d+d+1}}
Shortest Paths with Negative Edges
Bellman-Ford Algorithm
Update all edges |V|-1
times!
Implementation
Shortest-paths(G, l, s):
# Input: Directed graph G = (V, E);
edge lengths {le : e ∈ E} with no negative cycles;
vertex s ∈ V
# Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u
for all u ∈ V :
dist(u) = ∞
prev(u) = nil
dist(s) = 0
repeat |V| − 1 times:
for all e ∈ E:
update(e)
=> O(|V|*|E|)
Termination
- No update occurred (assume no negative cycles)
- After
|V|-1
times of iterations, apply 1 extra round. If somedist
reduced <- negative cycle
Proofs
- If some path from
v
tot
contains a negative cycle, then there does not exist a cheapest path fromv
tot
. - If
G
has no negative cycles, then there exists a cheapest path fromv
tot
that is simple (and has ≤n – 1
edges). dist(v)
is the cost of somev-t
path; after theith
pass,dist(v)
is no larger than the cost of the cheapestv-t
path using ≤i
edges.- If the successor graph contains a directed cycle, then it is a negative cycle.
Features v.s. Dijkstra's
- Handles negative weights
- Only need local information StackOverflow
Shortest Paths in DAGs
In any path of a DAG, the vertices appear in increasing linearized order.
Implementation
Dag-shortest-paths(G, l, s):
# Input: DagG = (V,E);
edge lengths { le: e ∈ E };
vertex s ∈ V
# Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u
for all u ∈ V:
dist(u) = ∞
prev(u) = nil
dist(s) = 0
Linearize G # DFS
for each u ∈ V, in linearized order:
for all edges (u, v) ∈ E:
update(u, v)
References
- Dasguptap, S., Papadimitriou, C.H., & Vazirani, U.V. Algorithms. Chapter 4.1-4.7.