Title: All Pairs Shortest Path
1All Pairs Shortest Path
Instructor Yao-Ting Huang
Bioinformatics Laboratory, Department of Computer
Science Information Engineering, National Chung
Cheng University.
2All pair shortest Path Problem
3Another Dynamic Programming Idea
Let P(i, j) be the shortest path between i and j.
j
i
4Dynamic Programming Idea
Input weighted, directed graph D(V,E,w) of n
vertices, say V1,2, , n. Let P(i, j) be the
shortest path between i and j. DP-related
Question How can we decompose the problem into
some simpler ones?
j
i
5Dynamic Programming Idea
Input weighted, directed graph D(V,E,w) of n
vertices, say V1,2,,n. Let P(i, j) be the
shortest path between i and j. DP-related
Question How can we decompose the problem into
some simpler ones? A It should go through some
vertex k.
j
k
i
6Dynamic Programming Idea
Let P(i, j) be the shortest path between i and
j. Let Vk1, 2, , k. Sk(i, j) length of a
shortest path between i and j whose interior
vertices are all in Vk.
j
k
i
7Dynamic Programming Idea
Let P(i,j) be the shortest path between i and
j. Let Vk1,2, , k. Sk(i, j) length of the
shortest path between i and j whose interior
vertices are all in Vk. What is the length of the
shortest path between i and j?
j
k
i
8Dynamic Programming Idea
Let P(i,j) be the shortest path between i and
j. Let Vk1,2,k. Sk(i, j) length of the
shortest path between i and j whose interior
vertices are all in Vk. What is the length of the
shortest path between i and j? It is Sn(i, j).
j
k
i
9Dynamic Programming Idea
Let Vk1,2,k. Sk(i, j) length of the
shortest path between i and j whose interior
vertices are all in Vk. If k is on the shortest
path from i to j, Sk(i, j) ???
j
k
i
10Key Observation
- Sk(i, j) is determined by the shortest paths
whose intermediate from 1, , k-1 - Case1 If k is not an intermediate vertex of P
- Path P is a shortest path from i to j with
intermediates from 1,k-1 - Case2 If k is an intermediate vertex of path P
- Path P can be broken down into i ? p1? k ? p2 ? j
- P1 is the shortest path from i to k with all
intermediate in the set 1,2,,k - P2 is the shortest path from k to j with 1,2,,k
11Key Observation
P2All intermediate vertices in 1,2,..,k-1
P1All intermediate vertices in 1,2,..,k-1
Sk-1(i, k)
Sk-1(k, j)
k
p1
p2
i
j
P All intermediate vertices in 1,2,..,k
Sk (i, j)
12Dynamic Programming Idea
Let Vk1,2,k. Sk(i, j) length of the
shortest path between i and j whose interior
vertices are all in Vk. If k is on the shortest
path from i to j, Sk(i, j) Sk-1(i, k) Sk-1(k,
j)
j
k
i
13Dynamic Programming Idea
- Let Vk1,2,k.
- Sk(i, j) length of the shortest path between i
and j whose interior vertices are all in Vk. - If k is on the shortest path from i to j,
- Sk(i, j) Sk-1(i, k) Sk-1(k ,j).
- If k is not on the shortest path from i to j
- Sk(i, j) Sk-1(i, j).
j
k
i
14Dynamic Programming Formula
- Let Vk1,2,k.
- Sk(i, j) length of the shortest path between i
and j whose interior vertices are all in Vk. - S0(i, j) w(i, j).
- Sk(i, j) min Sk-1(i, j), Sk-1(i, k) Sk-1(k,
j) .
j
k
i
15Floyd-Warshall Algorithm
- Negative edges is allowed
- But assume that no negative-weight cycle
- Dynamic Programming
- A recursive solution
- Computing from bottom-up
- Constructing a shortest path
16Dynamic Programming Algorithm
Floyd(W1n,1n,S1n,1n) For i 1 to n For
j 1 to n if lti, jgt in E then S(i, j) ?
W(i, j) else S(i, j) ? 8 For k 1 to n For i
1 to n For j 1 to n if Si,
j gt Si, k Sk, j then Si, j
? Si, k Sk, j
17Dynamic Programming
Sk (k1,2,3,4,5)
i
k
j
i
j
k
18A detailed example
j1
2
5
i1
4
2
1
2
1
6
S0W
4
3
3
2
1
2
4
5
5
3
19A detailed example
For k 1 to n For i 1 to n For j 1
to n if Si, j gt Si, k Sk, j
then Si, j ? Si, k Sk, j
j1
2
5
4
2
1
i1
0
4
8
8
3
1
6
2
0
6
8
8
2
4
3
3
2
S0W
8
8
8
0
1
1
2
8
0
4
2
3
4
5
3
8
8
1
8
0
5
20A detailed example
j1
2
5
i1
0
4
8
8
3
4
2
1
2
0
6
8
8
2
1
6
8
8
8
S0W
0
1
4
3
3
2
8
0
4
2
3
1
2
8
8
1
8
0
4
5
5
3
i1
0
4
8
8
3
2
0
6
8
8
2
8
S1
0
1
0
4
2
3
0
8
8
1
8
5
21A detailed example
j1
2
5
i1
0
4
8
8
3
4
2
1
2
0
6
8
8
2
1
6
8
8
8
S0W
0
1
4
3
3
2
8
1
0
4
2
3
2
8
8
1
8
4
0
5
5
3
i1
0
4
8
8
3
2
0
6
8
8
2
8
4
5
S1
0
1
0
4
2
3
8
0
8
8
1
8
5
22A detailed example
j1
2
5
i1
0
4
8
8
3
4
2
1
2
0
6
8
8
2
1
6
8
8
8
S0W
0
1
4
3
3
2
8
1
0
4
2
3
2
8
8
1
8
4
0
5
5
3
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
23Extracting the Shortest Paths
- How can we retrieve the shortest paths?
- Maintain a predecessor pi, j.
- If the shortest path does not pass through any
intermediate vertex, then the predecessor pi, j
0.
0
4
6
5
3
0
3
7
4
2
7
4
5
0
1
7
0
3
2
3
0
6
5
1
2
24How do we retrieve the actual path?
- We maintain Pki, j as follows
- P0i, j 0 for all i, j in V
- Pki, j Pk-1i, j, if Ski, j Sk-1i, j
- Pki, j k, if Ski, j ? Sk-1i, j
25How do we retrieve the actual path?
- We maintain Pki,j as follows
- P0i, j 0 for all i,j in V
- Pki, j Pk-1i,j, if Ski,j Sk-1i,j
- Pki, j k, if Ski,j ? Sk-1i,j
- To reconstruct the shortest path in Pni, j
- If Pni, j0, edge lti, jgt is the shortest path.
- If Pni, jk, then k is an interior vertex on
the path, other interior vertices can be obtained
by checking Pni, k and Pnk, j.
26Floyds Algorithm (with matrix P)
Floyd(W1n,1n,S1n,1n,P1n,1n) For i 1
to n For j 1 to n Pi,j ? 0 if
lti,jgt in E then Si,j ? Wi,j else Si,j ?
8 For k 1 to n For i 1 to n For j
1 to n if Si,j gt Si,k Sk,j
then Si,j ? Si,k Sk,j
Pi,j ? k
27A detailed example
j1
2
5
i1
0
4
0
2
0
4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
How do we retrieve the shortest path from 3 to 5?
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
28A detailed example
j1
2
5
i1
0
4
0
2
0
4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
How do we retrieve the shortest path from 3 to
5? P3,52, so 325
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
29A detailed example
j1
2
5
i1
0
4
0
2
0
4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
How do we retrieve the shortest path from 3 to
5? P3,52, so 325 P3,21,so
3125 P3,1P2,5P1,20 So 3?1?2?5.
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
30Analysis
- Running time is clearly T(?)
- T(n3) -gt T(V3)
- Faster than previous algorithms.
O(V4),O(V3lgV)