0% found this document useful (0 votes)
64 views13 pages

All Pairs Shortest Path: Presented By: Sudeep Sharma

The document discusses Floyd's algorithm for finding the shortest paths between all pairs of vertices in a graph. It presents the problem, defines the graph as one containing negative edges but no negative cycles. It then shows the representation using a weight matrix and provides the recursive definition of Floyd's algorithm, which works by iteratively updating the weight matrix to find the shortest paths using intermediate vertices. Pseudocode for Floyd's algorithm is also presented.

Uploaded by

Sudeep Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views13 pages

All Pairs Shortest Path: Presented By: Sudeep Sharma

The document discusses Floyd's algorithm for finding the shortest paths between all pairs of vertices in a graph. It presents the problem, defines the graph as one containing negative edges but no negative cycles. It then shows the representation using a weight matrix and provides the recursive definition of Floyd's algorithm, which works by iteratively updating the weight matrix to find the shortest paths using intermediate vertices. Pseudocode for Floyd's algorithm is also presented.

Uploaded by

Sudeep Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

All pairs shortest path

Presented by :
SUDEEP SHARMA
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
Floyds
Algorithm 2
The weight matrix and the graph
Floyds
Algorithm 3
1 2 3 4 5
1 0 1 1 5
2 9 0 3 2
3 0 4
4 2 0 3
5 3 0




v
1
v
2
v
3
v
4
v
5
3
2
2
4
1
3
1
9
3
5
The Recursive Definition:

Case 1: A shortest path from v
i
to v
j
restricted to using only vertices
from {v
1
,v
2
,,v
k
} as intermediate vertices does not use v
k
. Then
D
(k)
[i,j]= D
(k-1)
[i,j].

Case 2: A shortest path from v
i
to v
j
restricted to using only vertices
from {v
1
,v
2
,,v
k
} as intermediate vertices does use v
k
. Then
D
(k)
[i,j]= D
(k-1)
[i,k]+ D
(k-1)
[k,j].

Floyds
Algorithm 4
V
i

V
j

V
k

Shortest Path using intermediate vertices { V
1, . . .
V
k -1
}
Shortest path using intermediate vertices
{V
1
, . . . V
k

}

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] }.
Floyds
Algorithm 5
V
i

V
j

V
k

Shortest Path using intermediate vertices { V
1, . . .
V
k -1
}
Shortest path using intermediate vertices
{V
1
, . . . V
k

}

Example
Floyds
Algorithm 6
W = D
0
=
4 0 5
2 0


-3 0
1 2 3
1
2
3
0 0 0
0 0 0
0 0 0
1 2 3
1
2
3
P =
1
2
3
5
-3
2
4
k = 1
Vertex 1 can be
intermediate
node
D
1
[2,3] = min( D
0
[2,3], D
0
[2,1]+D
0
[1,3] )
= min (, 7)
= 7


D
1
[3,2] = min( D
0
[3,2], D
0
[3,1]+D
0
[1,2] )
= min (-3,)
= -3

Floyds
Algorithm 7
D
1
=
4 0 5
2 0 7

-3 0
1 2 3
1
2
3
0 0 0
0 0 1
0 0 0
1 2 3
1
2
3
P =
1
2
3
5
-3
2
4
4 0 5
2 0


-3 0
1 2 3
1
2
3
D
0
=
k = 2
Vertices 1, 2
can be
intermediate
D
2
[1,3] = min( D
1
[1,3], D
1
[1,2]+D
1
[2,3] )
= min (5, 4+7)
= 5



D
2
[3,1] = min( D
1
[3,1], D
1
[3,2]+D
1
[2,1] )
= min (, -3+2)
= -1
Floyds
Algorithm 8
D
2
=
4 0 5
2 0 7
-1 -3 0
1 2 3
1
2
3
0 0 0
0 0 1
2 0 0
1 2 3
1
2
3
P =
1
2
3
5
-3
2
4
D
1
=
4 0 5
2 0 7

-3 0
1 2 3
1
2
3
k = 3
Vertices 1, 2, 3
can be
intermediate
D
3
[1,2] = min(D
2
[1,2], D
2
[1,3]+D
2
[3,2] )
= min (4, 5+(-3))
= 2


D
3
[2,1] = min(D
2
[2,1], D
2
[2,3]+D
2
[3,1] )
= min (2, 7+ (-1))
= 2
Floyds
Algorithm 9
D
3
=
2 0 5
2 0 7
-1 -3 0
1 2 3
1
2
3
3 0 0
0 0 1
2 0 0
1 2 3
1
2
3
P =
D
2
=
4 0 5
2 0 7
-1 -3 0
1 2 3
1
2
3
1
2
3
5
-3
2
4
Floyds
Algorithm
10
10
Pseudo-code
Time complexity:
Floyd's Algorithm: Using 2 D
matrices
Floyd
1. D W // initialize D array to W [ ]
2. P 0 // initialize P array to [0]
3. for k 1 to n
// Computing D from D
4. do for i 1 to n
5. do for j 1 to n
6. if (D[ i, j ] > D[ i, k ] + D[ k, j ] )
7. then D[ i, j ] D[ i, k ] + D[ k, j ]
8. P[ i, j ] k;
9. else D[ i, j ] D[ i, j ]
10. Move D to D.
Floyds
Algorithm 11
Example 2
Floyds
Algorithm
12
Floyds
Algorithm
13

You might also like