Floyd’s Algorithm
All pairs shortest path
All pairs shortest path
• The problem: find the shortest path between every
pair of vertices of a graph
• The graph: may contain negative edges but no
negative cycles
• A representation: a weight matrix where
W(i,j)=0 if i=j.
W(i,j)= if there is no edge between i and j.
W(i,j)=“weight of edge”
• Note: we have shown principle of optimality
applies to shortest path problems
The weight matrix and the graph
1 2 3 4 5
1 0 1 1 5 1
3 v1 v2
2 9 0 3 2 9
5 1 3
3 0 4 2
v5
4 2 0 3 3 2
v4 v3
5 3 0 5 x5 4
The subproblems
• How can we define the shortest distance di,j in terms
of “smaller” problems?
• One way is to restrict the paths to only include
vertices from a restricted subset.
• Initially, the subset is empty.
• Then, it is incrementally increased until it includes
all the vertices.
The subproblems
• Let D(k)[i,j]=weight of a shortest path from vi to vj
using only vertices from {v1,v2,…,vk} as
intermediate vertices in the path
– D(0)=W
– D(n)=D which is the goal matrix
• How do we compute D(k) from D(k-1) ?
The Recursive Definition:
Case 1: A shortest path from vi to vj restricted to using
only vertices from {v1,v2,…,vk} as intermediate vertices
does not use vk. Then D(k)[i,j]= D(k-1)[i,j].
Case 2: A shortest path from vi to vj restricted to using
only vertices from {v1,v2,…,vk} as intermediate vertices
does use vk. Then D(k)[i,j]= D(k-1)[i,k]+ D(k-1)[k,j].
Shortest path using intermediate vertices
{V1, . . . Vk } Vk
Vj
Vi
Shortest Path using intermediate vertices { V1, . . . Vk -1 }
The recursive definition
• Since
D(k)[i,j]= D(k-1)[i,j] or
D(k)[i,j]= D(k-1)[i,k]+ D(k-1)[k,j].
We conclude:
D(k)[i,j]= min{ D(k-1)[i,j], D(k-1)[i,k]+ D(k-1)[k,j] }.
Shortest path using intermediate vertices
{V1, . . . Vk } Vk
Vj
Vi
Shortest Path using intermediate vertices { V1, . . . Vk -1 }
Floyd's Algorithm Using n+1 D matrices
Floyd//Computes shortest distance between all pairs of
//nodes, and saves P to enable finding shortest paths
1. D0 W // initialize D array to W [ ]
2. P // initialize P array{ For every direct path put row number ( that
is previous vertex) }
3. for k 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5 3 4 0 4 3 4 6 6 4 0 4 3
5 5 1 4 5 0 1 5 5 6 4 5 0 1
6 6 6 6 3 1 0 6 6 6 6 3 1 0
D6 1 2 3 4 5 6
Path(index q, r) 1 0 2 2 4 3 1
if (P[ q, r ]q)
Path(q, P[q, r]) 2 2 0 2 4 5 1
println( “v”+ P[q, r])
Path(P[q, r], r) 3 2 2 0 2 5 1
return;
//no intermediate nodes 4 4 4 2 0 3 3
else return 5 3 5 5 3 0 4
6 1 1 1 3 4 0
The final distance matrix and P
The values in parenthesis are the non zero P values.
The call tree for Path(1, 4)
path(index q, r)
if (P[ q, r ]q)
Path(1, 4) path(q, P[q, r])
println( “v”+ P[q, r])
path(P[q, r], r)
return;
//no intermediate nodes
else return
Path(1, 6) Path(6, 4)
Print
P(1, 6)=0 v6
Path(6, 3) Print Path(3, 4)
v3
P(6, 3)=0 P(3, 4)=0
The intermediate nodes on the shortest path from 1 to 4 are v6, v3.
The shortest path is v1, v6, v3, v4.