16 - Shortest Path Algorithms
16 - Shortest Path Algorithms
2
Relaxation
• For each vertex v in the graph, we maintain Dv,
the estimate of the shortest path from s
(initialized to at the start)
• Relaxing an edge (u,v) with cost duv means
testing whether we can improve the shortest
path to v found so far by going through u
u v u v
2 2 Relax (u,v,G)
5 9 5 6
if Dv > Du+duv then
Relax(u,v) Relax(u,v) Dv = Du+duv
Parentv = u
5 2 7 5 2 6
u v u v
3
Dijkstra's Algorithm
• Non-negative edge weights
• Use Q, a priority queue keyed by Dv (BFS used
FIFO queue, here we use a PQ, which is re-
organized whenever some D decreases)
• Basic idea
– maintain a set S of solved vertices
– at each step select "closest" vertex u, add it to S, and
relax all edges from u
4
Dijkstra’s Pseudo Code
• Input: Graph G, start vertex s
Dijkstra(G(V,E),s)
01 for each vertex u V
02 Du
03 Parentu nil
04 Ds 0
05 S // Set S : already solved vertices
06 Q.init(V) // Q, intially contains all nodes in G
07 while not Q.isEmpty()
08 u Q.extractMin() //chose u with minimum Du
09 S S {u}
10 for each v u.adjacent() do
11 Relax(u, v, G)
relaxing
edges
5
Dijkstra’s Example
Dijkstra(G(V,E),s) Pv=
Pu=
01 for each vertex u V u v
1
02 Du 10
03 Parentu nil Ps=
9
04 Ds 0 s 0 2 3
05 S
4 6
06 Q.init(V) 5 7
07 while not Q.isEmpty() 2
08 u Q.extractMin() //grey Px= x y Py=
09 S S {u} //black
10 for each v u.adjacent() do u s v
1
11 Relax(u, v, G) 10 10
9
Relax (u,v,G) s 0 2 3
4 6
if Dv > Du+duv then 5 7
Dv = Du+duv 5 2
s x y
Parentv = u
6
Dijkstra’s Example (2)
Dijkstra(G(V,E),s)
u x v x
01 for each vertex u V 1
02 Du 10 8 14
03 Parentu nil 9
04 Ds 0 s 0 2 3
4 6
05 S 7
06 Q.init(V) 5
07 while not Q.isEmpty()
5 2 7 x
08 u Q.extractMin() //grey x s y
09 S S {u} //black
10 for each v u.adjacent() do x u v y
1
11 Relax(u, v, G)
10 8 13
9
Relax (u,v,G) s 0 2 3
4 6
7
if Dv > Du+duv then 5
Dv = Du+duv 5 2 7 x
s x y
Parentv = u
7
Dijkstra’s Example (3)
Dijkstra(G(V,E),s)
u x v u
01 for each vertex u V
1
02 Du 10 8 9
03 Parentu nil 9
04 Ds 0 s 0 2 3
4 6
05 S 7
06 Q.init(V) 5
07 while not Q.isEmpty() 5 2 7 x
08 u Q.extractMin() //grey x s y
09 S S {u} //black
10 for each v u.adjacent() do x u v u
1
11 Relax(u, v, G)
10 8 9
9
Relax (u,v,G) s 0 2 3
4 6
7
if Dv > Du+duv then 5
Dv = Du+duv 5 2 7 x
s x y
Parentv = u
8
Dijkstra's Algorithm
• Dijkstra’s Complexity:
– O(|V| x |E|)
9
Bellman-Ford Algorithm
• Dijkstra’s doesn’t work when there are negative
edges:
4
s x
2 6
v y
-9
Relax (u,v,G)
Bellman-Ford(G,s) if Dv > Du+duv then
01 for each vertex u V Dv = Du+duv
02 Du
03 Parentu NIL Parentv = u
04 Ds 0
05 for i 1 to |V|-1 do
06 for each edge (u,v) E do
07 Relax (u,v,G)
08 for each edge (u,v) E do
09 if Dv > Du+duv then
10 return false
11 return true
Cycle
detection
12
Bellman-Ford Example(1)
Bellman-Ford(G,s) t 5 x
for each vertex u V -2
01
02 Du
6
-3
03 Parentu NIL
04 Ds 0 s 0 8
-4 7
2
05 for i 1 to |V|-1 do 7
06 for each edge (u,v) E do 9
07 Relax (u,v,G) y z
08 for each edge (u,v) E do
09 if Dv > Du+duv then t 2 x
10 return false
11 return true 3
1
5 8
Relax (u,v,G) s 0 4
9
if Dv > Du+duv then 6 10
Dv = Du+duv 7
Parentv = u y z
Iteration 1
Iteration 2
Iteration 3 (no relax)
Iteration 4 (no relax)
14
Negative Length cycle example
s 2 x
0 -3
5
y
15
Bellman-Ford Algorithm
• Bellman-Ford Complexity:
– (|V|-1)|E| + |E| = O(|V|x|E|)
16
Floyd-Warshall Algorithm
• Find all pair shortest paths
ds,t(0) = w(s,t)
w(s,t) if k = 0
ds,t(k) =
min{ds,t(k-1), ds,k(k-1) + dk,t(k-1)} if k > 0
D(k) = [ds,t(k)]
Floyd-Warshall Algorithm
FloydWarshall(matrix W, integer n)
for k 1 to n do
for i 1 to n do
for j 1 to n do
dij(k) min(dij(k-1), dik(k-1) + dkj(k-1))
end
end
// D(k) is formed
end
return D(n)
Floyd-Warshall Algorithm – Example(1)
2
3
7
4
0 3 8 -4
1 8 3
0 1 7
1
-4
2 -5 4 0
5
6
4 2 -5 0
6 0
Floyd-Warshall Algorithm – Example(2)
0 3 8 -4 0 0 0 0
0 1 7 0 0 0
D(0)= 4 0 (0)= 0 0
2 -5 0 0 0 0
6 0 0 0
0 3 8 -4 0 0 0 0
0 1 7 0 0 0
D(1)= 4 0 (1)= 0 0
2 5 -5 0 -2 0 1 0 0 1
6 0 0 0
Floyd-Warshall Algorithm – Example(3)
0 3 8 -4 0 0 0 0
0 1 7 0 0 0
D(1)= 4 0 (1)= 0 0
2 5 -5 0 -2 0 1 0 0 1
6 0 0 0
0 3 8 4 -4 0 0 0 2 0
0 1 7 0 0 0
D(2)= 4 0 5 11 (2)= 0 0 2 2
2 5 -5 0 -2 0 1 0 0 1
6 0 0 0
Floyd-Warshall Algorithm – Example(4)
0 3 8 4 -4 0 0 0 2 0
0 1 7 0 0 0
D(2)= 4 0 5 11 (2)= 0 0 2 2
2 5 -5 0 -2 0 1 0 0 1
6 0 0 0
0 3 8 4 -4 0 0 0 2 0
0 1 7 0 0 0
D(3)= 4 0 5 11 (3)= 0 0 2 2
2 -1 -5 0 -2 0 3 0 0 1
6 0 0 0
Floyd-Warshall Algorithm – Example(5)
0 3 8 4 -4 0 0 0 2 0
0 1 7 0 0 0
D(3)= 4 0 5 11 (3)= 0 0 2 2
2 -1 -5 0 -2 0 3 0 0 1
6 0 0 0
0 3 -1 4 -4 0 0 4 2 0
3 0 -4 1 -1 4 0 4 0 1
D(4)= 7 4 0 5 3 (4)= 4 0 0 2 1
2 -1 -5 0 -2 0 3 0 0 1
8 5 1 6 0 4 3 4 0 0
Floyd-Warshall Algorithm – Example(6)
0 3 -1 4 -4 0 0 4 2 0
3 0 -4 1 -1 4 0 4 0 1
D(4)= 7 4 0 5 3 (4)= 4 0 0 2 1
2 -1 -5 0 -2 0 3 0 0 1
8 5 1 6 0 4 3 4 0 0
0 3 -1 2 -4 0 0 4 5 0
3 0 -4 1 -1 4 0 4 0 1
D(5)= 7 4 0 5 3 (5)= 4 0 0 2 1
2 -1 -5 0 -2 0 3 0 0 1
8 5 1 6 0 4 3 4 0 0