Lecture 11B - Single Source Shortest Paths, All Pairs Shortest Paths
Lecture 11B - Single Source Shortest Paths, All Pairs Shortest Paths
Haidong Xue
Summer 2012, at GSU
Single-source shortest paths
• A path of a weighted, directed graph is a
sequence of vertices: < 𝑣1 , 𝑣2 , … , 𝑣𝑘 >
• The weight of a path is the sum of weights of
edges that make the path:
weight(< 𝑣1 , 𝑣2 , … , 𝑣𝑘 > )=σ𝑘−1
𝑖=1 𝑤(𝑣𝑖 ,𝑣𝑖+1 )
Single-source shortest paths
1
2 3
10
9
3 2
1 6 4
5
7
4 5
10+2=12
Single-source shortest paths
1
2 3
10
9
3 2
1 6 4
5
7
4 5
10+2 + 3 + 2 = 17
Single-source shortest paths
1
2 3
10
9
3 2
1 6 4
5
7
4 5
10 + 2 + ∞ = ∞
Single-source shortest paths
1
2 3
10
9
3 2
1 -6 4
5
7
4 5
Idea:
• Solve it using dynamic programming
• For all the paths have at most 0 edge, find all the shortest paths
• For all the paths have at most 1 edge, find all the shortest paths
• …
• For all the paths have at most |V|-1 edge, find all the shortest
paths
Bellman-Ford algorithm
Bellman-Ford(G, s)
for each v in G.V{ //Initialize 0-edge shortest paths
if(v==s) 𝑑𝑠,𝑣 =0; else 𝑑𝑠,𝑣 = ∞; //set the 0-edge shortest distance
from s to v
𝜋𝑠,𝑣 = NIL; //set the predecessor of v on the shortest path
}
Repeat |G.V|-1 times { //bottom-up construct 0-to-(|V|-1)-edges shortest paths
for each edge (u, v) in G.E{
if(𝑑𝑠,𝑣 > 𝑑𝑠,𝑢 + 𝑤(𝑢,𝑣) ){
𝑑𝑠,𝑣 = 𝑑𝑠,𝑢 + 𝑤(𝑢,𝑣) ;
𝜋𝑠,𝑣 = 𝑢;
}
}
for each edge (u, v) in G.E{ //test negative cycle
If (𝑑𝑠,𝑣 > 𝑑𝑠,𝑢 + 𝑤(𝑢,𝑣) ) return false; // there is no solution
}
return true;
T(n)=O(VE)=O(𝑉 3 )
Bellman-Ford algorithm
e.g.
20
0 ∞ ∞
10 1
1 2 3
5
∞ ∞
-2
2 3
6
-3
0
8
1 7
-4
7 2
4 5
∞ ∞
9
Bellman-Ford algorithm
Calculate all at most 1 edge
shortest paths 5
∞ /6 ∞ /∞
-2
2 3
6
-3
0 /0
8
1 7
-4
7 2
4 5
∞ /∞
/7 ∞ /∞
9
Bellman-Ford algorithm
Calculate all at most 2 edges
shortest paths 5
6 /6 ∞ /11
/4
-2
2 3
6
-3
0 /0
8
1 7
-4
7 2
4 5
7 /7 ∞ /2
9
Bellman-Ford algorithm
Calculate all at most 3 edges
shortest paths 5
/2
6 /6 4 /4
-2
2 3
6
-3
0 /0
8
1 7
-4
7 2
4 5
7 /7 2 /2
9
Bellman-Ford algorithm
Calculate all at most 4 edges
shortest paths 5
2 /2 4 /4
-2
2 3
6
-3
0 /0
8
1 7
-4
7 2
4 5
7 /7 2 /-2
9
Bellman-Ford algorithm
Final result:
5
2 4
-2
2 3
6
-3
0
8
1 7
-4
7 2
4 5
7 -2
9
What is the shortest path 1, 4, 3, 2, 5
from 1 to 5?
What is the shortest path
What is weight of this path? -2 from 1 to 2, 3, and 4?
Dijkstra’s Algorithm
• A greedy algorithm
• Dijkstra (G, s)
for each v in G.V{
if(v==s) 𝑑𝑠,𝑣 =0; else 𝑑𝑠,𝑣 = ∞; //set the 0-edge shortest distance
from s to v
𝜋𝑠,𝑣 = NIL; //set the predecessor of v on the shortest path
}
S=∅;//the set of vertices whose final shortest-path
weights have already been determined
Q=G.V;
while(Q ≠ ∅){
u=Extract-Min(Q);
S=S ∪ {𝑢};
for all (u, v){//the greedy choice
if(𝑑𝑠,𝑣 > 𝑑𝑠,𝑢 + 𝑤(𝑢,𝑣) ){
𝑑𝑠,𝑣 = 𝑑𝑠,𝑢 + 𝑤(𝑢,𝑣) ;
𝜋𝑠,𝑣 = 𝑢;
}
}
}
T(n)=O(𝑉 2 )
Dijkstra’s Algorithm
The shortest path of the vertex with smallest distance is
determined
1
∞ /10 ∞
2 3
10
0 9
3 2
1 6 4
5
7
4 5
∞ /5 ∞
2
Dijkstra’s Algorithm
Choose a vertex whose shortest path is now determined
1
10 /8 ∞ /14
2 3
10
0 9
3 2
1 6 4
5
7
4 5
5 ∞ /7
2
Dijkstra’s Algorithm
Choose a vertex whose shortest path is now determined
1
8 14 /13
2 3
10
0 /0 9
3 2
1 6 4
5
7
4 5
5 7
2
Dijkstra’s Algorithm
Choose a vertex whose shortest path is now determined
1
8 13 /9
2 3
10
0 9
3 2
1 6 4
5
7
4 5
5 /5 7
2
Dijkstra’s Algorithm
Choose a vertex whose shortest path is now determined
1
8 9
2 3
10
0 9
3 2
1 6 4
5
7
4 5
5 7 /7
2
Dijkstra’s Algorithm
Final result:
1
8 9
2 3
10
0 9
3 2
1 6 4
5
7
4 5
5 7
2
1 2
9
1 2 3 4
3
1 0 ∞ ∞ ∞
6 4
2 ∞ 0 ∞ 4
3 3 9 0 2
3 4 4 ∞ 6 ∞ 0
2
All-pairs shortest paths
D(0)
1 2 3 4
1 0 ∞ ∞ ∞
1 2 2 ∞ 0 ∞ 4
3 3 2 0 9
2
3 4 ∞ 6 ∞ 0
6 4
What are the weights of shortest paths with
intermediate vertex 1?
3 D(1)
4
1 2 3 4
9
1 0 ∞ ∞ ∞
2 ∞ 0 ∞ 4
3 3 2 0 9
4 ∞ 6 ∞ 0
All-pairs shortest paths
D(1)
1 2 3 4
1 0 ∞ ∞ ∞
1 2 2 ∞ 0 ∞ 4
3 3 2 0 9
2
3 4 ∞ 6 ∞ 0
6 4
What are the weights of shortest paths with
intermediate vertices 1 and 2?
3 D(2)
4
1 2 3 4
9
1 0 ∞ ∞ ∞
2 ∞ 0 ∞ 4
3 3 2 0 6
4 ∞ 6 ∞ 0
All-pairs shortest paths
D(2)
1 2 3 4
1 0 ∞ ∞ ∞
1 2 2 ∞
∞ 0 4
2
3 3 2 0 6
3
4 ∞ 6 ∞ 0
6 4
What are the weights of shortest paths with
intermediate vertices 1, 2 and 3?
3 4 D(3)
1 2 3 4
9
1 0 ∞ ∞ ∞
2 ∞ 0 ∞ 4
3 3 2 0 6
4 ∞ 6 ∞ 0
All-pairs shortest paths
D(3)
1 2 3 4
1 0 ∞ ∞ ∞
1 2 2 ∞ 0 ∞ 4
3 3 2 0 6
2
3 4 ∞ 6 ∞ 0
6 4
What are the weights of shortest paths with
intermediate vertices 1, 2, 3 and 4?
D(4)
3 4
1 2 3 4
9 1 0 ∞ ∞ ∞
2 ∞ 0 ∞ 4
3 3 2 0 6
4 ∞ 6 ∞ 0
All-pairs shortest paths
Add predecessor information to reconstruct a shortest path
If updated the predecessor i-j in D(k) is the same as the predecessor k-j in D(k-1)
D(0) D(1)
1 2 3 4 1 2 3 4
1 0/n ∞/𝑛 ∞/𝑛 ∞/𝑛 1 0/n ∞/𝑛 ∞/𝑛 ∞/𝑛
2 ∞/𝑛 0/n ∞/𝑛 4/2 2 ∞/𝑛 0/n ∞/𝑛 4/2
3 3/3 2/3 0/n 9/3 3 3/3 2/3 0/n 9/3
4 ∞/𝑛 6/4 ∞/𝑛 0/n 4 ∞/𝑛 6/4 ∞/𝑛 0/n
D(2) D(3)
1 2 3 4 1 2 3 4
1 0/n ∞/𝑛 ∞/𝑛 ∞/𝑛 1 0/n ∞/𝑛 ∞/𝑛 ∞/𝑛
2 ∞/𝑛 0/n ∞/𝑛 4/2 2 ∞/𝑛 0/n ∞/𝑛 4/2
3 3/3 2/3 0/n 6/2 3 3/3 2/3 0/n 6/2
4 ∞/𝑛 6/4 ∞/𝑛 0/n 4 ∞/𝑛 6/4 ∞/𝑛 0/n
All-pairs shortest paths
D(4)
1 2
1 2 3 4
1 0/n ∞/𝑛 ∞/𝑛 ∞/𝑛 2
3
2 ∞/𝑛 0/n ∞/𝑛 4/2 6 4
3 3/3 2/3 0/n 6/2
4 ∞/𝑛 6/4 ∞/𝑛 0/n
3 4
9
What is the shortest path 3, 2, 4
from 3 to 4?
What is weight of this path? 6