0% found this document useful (0 votes)
42 views53 pages

Design & Analysis of Algorithms: DR Anwar Ghani

This document summarizes the dynamic programming algorithm and its applications. It discusses the following key points in 3 sentences: Dynamic programming is an optimization technique that works by breaking problems down into overlapping subproblems, solving the subproblems recursively in a bottom-up manner, and storing the results in a table for future use in constructing an optimal solution. The principle of optimality states that optimal solutions can be built from optimal solutions to subproblems. The Floyd-Warshall algorithm uses dynamic programming to find the shortest paths between all pairs of vertices in a graph by iteratively considering direct and indirect paths through intermediate vertices in O(n3) time.

Uploaded by

Mahmood Syed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
42 views53 pages

Design & Analysis of Algorithms: DR Anwar Ghani

This document summarizes the dynamic programming algorithm and its applications. It discusses the following key points in 3 sentences: Dynamic programming is an optimization technique that works by breaking problems down into overlapping subproblems, solving the subproblems recursively in a bottom-up manner, and storing the results in a table for future use in constructing an optimal solution. The principle of optimality states that optimal solutions can be built from optimal solutions to subproblems. The Floyd-Warshall algorithm uses dynamic programming to find the shortest paths between all pairs of vertices in a graph by iteratively considering direct and indirect paths through intermediate vertices in O(n3) time.

Uploaded by

Mahmood Syed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 53

Design & Analysis of Algorithms

Dr Anwar Ghani

Semester: Fall 2017

Department of Computer Science & Software Engineering,


International Islamic University, Islamabad.
Dynamic Programming
Dynamic Programming
Topics
• Introduction to Dynamic Programming

•Optimality Principle

• Applications of DP

• All Pairs Shortest Paths Problem


Dynamic Programming
Strategy
Dynamic Programming is a technique for solving optimization problems. Generally, such
problems have many feasible solutions, each having some associated cost. The dynamic
programming method determines the best solution, that is, the one which has the maximum or
minimum cost.
The algorithm works by splitting a problem into a sequence of optimization subproblems.
The subproblems are solved, and then combined to obtain solution to the main problem.

The dynamic programming appears to have same approach as divide-and-conquer algorithm.


There are, however, some subtle differences. Unlike the divide-and-conquer method, the
dynamic programming splits the problem into overlapping subproblems , which are solved in
bottom up fashion. The optimal solutions to subproblems are stored into a table, which is
used repeatedly to build the final or global optimal solution.
Dynamic Programming
Solution
In general, solution of problem is obtained by using the dynamic programming algorithm
in the following manner:
1) A recursion is set up to express the running time of the main
problem in terms running times of overlapping subproblems

2) The recursion is solved in bottom-up manner, by using the


initial conditions and previously computed values. The results
are saved in a table

3) The tabular solutions to sub-problems are used to construct


the solution to the main solution
Dynamic Programming
Building Solution
The steps involved in building a DP solution are depicted in the diagram. First, solution to the
smallest subproblem is obtained using the initial conditions. The result is saved in a table.
Using this table, the optimum solution is obtained for the next level of subproblem. This
procedure is continued until the solution to the main problem is found.

Initial conditions

Dynamic Programming procedure


Optimality Principle
Dynamic Programming
Optimality Principle
The dynamic programming approach is based on the principle of optimality. The principle
states: “Optimal solution to a problem can be found, if there exists an optimal solution to a
sub-problem”

Dynamic Programming solution is feasible when principle of optimality holds true for a
given problem.

The principle may not be applicable to all kinds of optimization problems. This condition
places a restriction on the wider applicability of dynamic programming algorithm.
Optimality Principle
Shortest Path
¾ The principle of optimality holds true for shortest paths in a graph

Example: Consider the weighted graph shown in the diagram


It can be seen that the shortest distance the between the vertices A and D is 35
(i) The shortest path from A to D is A→B→C→D = 20+5+10=35
(ii) The shortest path from A to C is A→B→C=20+5=25
(iii) The shortest path from A to B is A→B=20.
It follows that each of the sub-paths from vertex A to vertex D is also the shortest path
between intermediate vertices. Thus, the principle of optimality holds true for shortest
distances in the sample graph.
50
A D

20 10

B C
5

Sample weighted graph. Shortest


paths are shown with bold lines
Optimality Principle
Longest Path
¾ The principle of optimality does not hold true for longest paths in a graph

Example: Consider the sample weighted graph shown in the diagram


The longest path from C to A, is
C→D→A= 10+50 = 60
The sub-path C→D=10 is not the longest path. In fact, the longest path simple path
from C to D consists of sub-paths C→B→A→D = 75

50
A D

20 10

5
B C

Sample weighted graph. The longest


paths are shown with bold lines
Dynamic Programming
Applications

The DP has several applications. Some common useful applications are:

• Shortest distances in a network

• Longest common subsequences in strings

• Optimal matrix chain multiplication

• Optimal triangulation of a polygon

• Optimized binary search trees

• Optimal Packaging of Bins

• Optimal Assembly line scheduling


All-Pairs Shortest Paths
All Pairs Shortest Paths
Approaches
In networking problems it is often required to find the shortest distances between all pairs of
vertices. There are different algorithms to solve this problem. Some of the methods are as
follows.
1) A simple Brute Force method can be used to compute all possible distances between
different vertices, and then select the smallest computed distance for each pair. This
method is not feasible for a large network, because the running time grows exponentially
with the size of the graph

2) An efficient algorithm to determines shortest distances from a start vertex to all other
vertices in a weighted directed or undirected graph, was proposed by Dijkstra. The
working of Dijkstra’s algorithm is similar to that of Prim’s. Like Prim’s algorithm, it
systematically computes the aggregate shortest distances by considering links between
two sets of vertices . The Dijkstra’s algorithm has time complexity of O(n2) for graph
with n vertices. It can be applied to solve the problem of all-pairs shortest distances, by
selecting each of the vertices as the start vertex. In this way, shortest distances for the
entire graph can be obtained in time O(n3)

3) Another algorithm, known as Floyd-Warshall algorithm, provides shortest distances


between all pairs of vertices. It is based on dynamic programming technique. The
algorithm runs in time O(n3).
Weight Matrix
Definition
The Floyd-Warshall algorithm uses a weight matrix to compute the intermediate shortest paths
Let G=(V,E) be a weighted graph, with weight w( i, j ) associated with the edge ( vi , vj ) . The
weight matrix, W =[ wij ] is defined as follows

0 if i = j ( i, j= 1…n=|V| )

wij = ∞ if (vi , vj) ∉E ( No link between the pair of vertices)

w(i, j) if (v , v ) ∈ E
i j (Vertices are linked)

¾ The infinity symbol ’ ∞’ represents missing links in the graph. In actual implementation
it may be replaced by some very large number.
Floyd Warshall Algorithm
All-Pairs Shortest Distances
The Floyd Warshall algorithm for computing the shortest distances consists of the
following steps:

Step #1:Store the weight matrix W into the table D(0). Initialize a table P to store intermediate vertices
along the shortest paths between all pair of vertices

Step #2: Using table D(0) compute shortest distances by considering direct paths as well as all other
paths that pass through the vertex v1. Store the shortest distances in table D(1). If the shortest path
between vertices vi and vj passes through the vertex vk, then store the intermediate vertex vk in table P
Step #3: Using table D(1), compute shortest distances between all pairs of vertices, by considering direct
paths as well as indirect paths through the vertex v2. Store these distances in table D(2). By principle of
optimality, the table D(2) would contain shortest paths that may be direct or pass through vertices v1, v2.
If the shortest path between vertices vi and vj passes through the vertex vk, then store the intermediate
vertex vk in table P

Step #4: Repeat the above steps to compute the tables D(3), D(4)……D(n).. At each stage update the table
P by copying the intermediate vertex.

¾ At the end the table D(n) would contain shortest distances between all pairs of vertices. The table P
would contain the intermediate vertices that link the last segments of the shortest paths to the end
vertices.
All-Pairs Shortest Paths
Example
Floyd-Warshall Algorithm
Example
Consider the sample weighted graph depicted in figure (i). The weight matrix for the graph
is shown in figure (ii). The missing link between a pair of vertices are indicated by the
infinity symbol ∞.

1
b 7 a b c d e
a
a 0 1 ∞ 9 5
8
5 b ∞ 0 7 9 ∞
9 9
6 2 c
c ∞ 8 0 6 ∞
6 d ∞ ∞ 2 0 10
e 10
d
e 6 ∞ ∞ ∞ 0
(i) Sample weighted graph
(ii) Weight Matrix for the
sample graph

¾ The use of Floyd-Warshall algorithm is demonstrated in the following diagrams..


Floyd-Warshall Algorithm
Distances from vertex a to other vertices, using table D(0)
(1) Distance from a to b D(0) D(1) P 1 b 7
Direct : a → b=1 a b c d e a b c d e a b c d e a
Indirect: a → a → b 5 8
a 0 1 ∞ 9 5 a 0 1 a - - 9 9
=0+1 =1 6 c
b ∞ 0 7 9 ∞ b b 2
c ∞ 8 0 6 ∞ c c 10 6
e d
d ∞ ∞ 2 0 10 d d
e 6 ∞ ∞ ∞ 0 e e
(2) Distance from a to c 1
a b c d e a b c d e a b c d e a b 7
Direct: a → c= ∞ a 0 1 ∞ 9 5 a 0 1 ∞ a - - - 5
9 8
Indirect: a → a → c b ∞ 0 7 9 ∞ b b 6 9
c
=0+∞ =∞ c ∞ 8 0 6 ∞ c c 2
6
e 10
No link, direct or through a d ∞ ∞ 2 0 10 d d d
e 6 ∞ ∞ ∞ 0 e e
(3) Distance from a to d 1 b 7
a b c d e a b c d e a b c d e a
Direct: a → d=9 5
a 0 1 ∞ 9 5 a 0 1 ∞ 9 a - - - - 9 8
Indirect: a → a → d 6 9
b ∞ 0 7 9 ∞ b b c
=0+0=9 2
c ∞ 8 0 6 ∞ c c 10 6
e d
d ∞ ∞ 2 0 10 d d
e 6 ∞ ∞ ∞ 0 e e
(4) Distance from a to e 1 b 7
Direct: a → e=5 a b c d e a b c d e a b c d e a
a 0 1 ∞ 9 5 a - - - - - 5
Indirect: a → a → e a 0 1 ∞ 9 5 9 9
8
b ∞ 0 7 9 ∞ b 6 c
=0+5 =5 b
2
c ∞ 8 0 6 ∞ c c 10 6
e d
d ∞ ∞ 2 0 10 d d
e 6 ∞ ∞ ∞ 0 e e End vertex
Start vertex

Distances D(0) : Distances matrix D(1): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertex a for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex b to other vertices, direct or via vertex a , using table D(0)
(5) Distance from b to a D(0) D(1) P 1 7
a b
Direct : b → a=∞ a b c d e a b c d e a b c d e 5
Indirect: b → a → a a 0 1 ∞ 9 5 9 8
a 0 1 ∞ 9 5 a - - - - - 6 9
= ∞+0=∞ b ∞ 0 7 9 ∞ c
b ∞ b - 2
No link, direct or via a c ∞ 8 0 6 ∞ c 10 6
c e d
d ∞ ∞ 2 0 10 d d
e 6 ∞ ∞ ∞ 0 e e

(6) Distance from b to c a b c d e a b c d e 1 b 7


a b c d e a
Direct: b → c= 7 (minimum) a 0 1 ∞ 9 5 a - - - - -
a 0 1 ∞ 9 5 5
9 8
Indirect: b → a → c b ∞ 0 7 9 ∞ b - - - 9
b ∞ 0 7 6 c
=∞+∞=∞ c ∞ 8 0 6 ∞ c 2
c 6
e 10
d ∞ ∞ 2 0 10 d d d
e 6 ∞ ∞ ∞ 0 e e
(7) Distance from b to d 1 b 7
a b c d e a b c d e a b c d e a
Direct: b → d=9 (minimum) a 0 1 ∞ 9 5 a - - - - - 5
a 0 1 ∞ 9 5 9 9
8
Indirect: b → a → d b ∞ 0 7 9 ∞ b - - - - 6 c
b ∞ 0 7 9
=∞ + 9 = ∞ c ∞ 8 0 6 ∞ c 2
c 10 6
e d
d ∞ ∞ 2 0 10 d d
e 6 ∞ ∞ ∞ 0 e e

(8) Distance from b to e 1 b 7


a b c d e a b c d e a b c d e a
Direct: b → e=∞ a 0 1 ∞ 9 5 a - - - - - 5
a 0 1 ∞ 9 5 9 8
Indirect: b → a → e b ∞ 0 7 9 ∞ b - - - - - 6 9
c
b ∞ 0 7 9 ∞
=∞+5 =∞ c ∞ 8 0 6 ∞ c 2
c 10 6
No link direct or through a d ∞ ∞ 2 0 10 d e d
d
e 6 ∞ ∞ ∞ 0 e e
Start vertex End vertex

Distances D(0) : Distances matrix D(1): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertex a for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex c to other vertices, direct or via vertex a , using table D(0)
(9) Distance from c to a D(0) D(1) P 1 7
a b
Direct : c → a=∞ a b c d e a b c d e a b c d e 5 8
Indirect: c → a → a a 0 1 ∞ 9 5 a 0 1 ∞ 9 5 a - - - - - 9 9
6 c
=0+∞ =∞ b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 2
No link, direct or through a c - 10 6
c ∞ 8 0 6 ∞ c ∞ e d
d ∞ ∞ 2 0 10 d d
e 6 ∞ ∞ ∞ 0 e e
(10) Distance from c to b
a b c d e a b c d e a b c d e 1 b 7
Direct: c → b=8 (minimum) a
a 0 1 ∞ 9 5 a 0 1 ∞ 9 5 a - - - - - 5
Indirect: c → a → b 9 8
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 6 9
= ∞+1 = ∞ c
c ∞ 8 0 6 ∞ c ∞ 8 c - - 2
10 6
d ∞ ∞ 2 0 10 d d e d
e 6 ∞ ∞ ∞ 0 e e

(11) Distance from c to d a b c d e a b c d e a b c d e 1 b 7


a
Direct: c → d=6 (minimum) a 0 1 ∞ 9 5 a 0 1 ∞ 9 5 a - - - - - 5
9 8
Indirect: c → a → d b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 6 9
c
=∞ + 9 = ∞ c ∞ 8 0 6 ∞ c ∞ 8 0 6 c - - - - 2
10 6
d ∞ ∞ 2 0 10 d d e d
e a ∞ ∞ ∞ 0 e e

(12) Distance from c to e 1 b 7


a b c d e a b c d e a b c d e a
Direct: c → e=∞ a 0 1 ∞ 9 5 a - - - - - 5
a 0 1 ∞ 9 5 9 8
Indirect: c → a → e b ∞ 0 7 9 ∞ b - - - - - 6 9
c
b ∞ 0 7 9 ∞
= ∞ + 5 =∞ c ∞ 8 0 6 ∞ c - - - - - 2
c ∞ 8 0 6 ∞ 10 6
No link, direct or through a d ∞ ∞ 2 0 10 d e d
d
e 6 ∞ ∞ ∞ 0 e e
Start vertex End vertex

Distances D(0) : Distances matrix D(1): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertex a for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex d to other vertices, direct or via vertex a , using table D(0)
(13) Distance from d to a D(0) D(1) P 1 7
Direct : d → a=∞ a b
a b c d e a b c d e a b c d e 5
Indirect: d → a → a a 0 1 ∞ 9 5 a 0 1 ∞ 9 5 a - - - - - 9 9
8
6 c
=∞+0 =∞ b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 2
No link, direct or through a c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - e 10 6
d
d ∞ ∞ 2 0 10 d ∞ d -
e ∞ ∞ ∞ ∞ 0 e e
(14) Distance from d to b a b c d e a b c d e a b c d e 1 7
a b
Direct: d → b= ∞ a 0 1 ∞ 9 5 a 0 1 ∞ 9 5 a - - - - - 5 8
Indirect: d → a →b 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 6 9
=∞ + 1 = ∞ c
c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 2
No link, direct or through a 10 6
d ∞ ∞ 2 0 10 d ∞ ∞ d - - e d
e 6 ∞ ∞ ∞ 0 e e

(15) Distance from d to c a b c d e 1 b 7


a b c d e a b c d e a
Direct: d → c=2 (minimum) a 0 1 ∞ 9 5 a 0 1 ∞ 9 5 a - - - - - 5
9 8
Indirect: d → a → c b ∞ 0 7 9 ∞ b - - - - - 6 9
b ∞ 0 7 9 ∞ c
=∞+∞=∞ c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 2
6
e 10
d ∞ ∞ 2 0 10 d ∞ ∞ 2 0 10 d - - - d
e 6 ∞ ∞ ∞ 0 e e

(16) Distance from d to e 1 b 7


a b c d e a b c d e a b c d e a
Direct: d → e=10 (minimum) a 0 1 ∞ 9 5 a - - - - - 5
a 0 1 ∞ 9 5 9 8
Indirect: d → a → e b ∞ 0 7 9 ∞ b - - - - - 6 9
c
b ∞ 0 7 9 ∞
=∞+5 =∞ c ∞ 8 0 6 ∞ c - - - - - 2
c ∞ 8 0 6 ∞ 10 6
d ∞ ∞ 2 0 10 d - - - - - e d
d ∞ ∞ 2 0 10
e 6 ∞ ∞ ∞ 0 e e
Start vertex End vertex

Distances D(0) : Distances matrix D(1): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertex a for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex e to other vertices, direct or via vertex a , using table D(0)
(17) Distance from e to a D(0) D(1) P 1 7
a b
Direct : e →a=6 a b c d e a b c d e a b c d e
5 8
Indirect: e → a →a a 0 1 ∞ 9 5 a - - - - - 9
a 0 1 ∞ 9 5 6 9
b - - - - - c
= 6+ 0 = 6 b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ 2
c ∞ 8 0 6 ∞ c - - - - - 10 6
c ∞ 8 0 6 ∞ e d
d ∞ ∞ 2 0 10 d ∞ ∞ 2 0 10 d - - - - -
e 6 ∞ ∞ ∞ 0 e 6 e -
(18) Distance from e to b a b c d e 1
a b c d e a b c d e b 7
Direct: e → b= ∞ a - - - - - a
a 0 1 ∞ 9 5 a 0 1 ∞ 9 5 5
Indirect:e → a → b b - - - - - 9 8
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ 6 9
c
= 6 + 1 = 7 (minimum) c ∞ 8 0 6 ∞ c - - - - -
c ∞ 8 0 6 ∞ 2
Minimum distance via a d - - - - - e 10 6
d ∞ ∞ 2 0 10 d ∞ ∞ 2 0 10 d
Vertex a stored in table P e - a
e 6 ∞ ∞ ∞ 0 e 6 7

(19) Distance from e to c 1 b 7


a b c d e a b c d e a b c d e a
Direct: e → c=∞ 5
a 0 1 ∞ 9 5 a 0 1 ∞ 9 5 a - - - - - 9 8
Indirect: e → a → c 6 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - c
=6+∞=∞ 2
c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 10 6
No link, direct or through a e d
d ∞ ∞ 2 0 10 d ∞ ∞ 2 0 10 d - - - - -
e 6 ∞ ∞ ∞ 0 e 6 7 ∞ e - a -

(20) Distance from e to d 1 b 7


a b c d e a b c d e a b c d e a
Direct: e → d = ∞ 5
a 0 1 ∞ 9 5 a 0 1 ∞ 9 5 a - - - - - 9 8
Indirect: e → a → d 6 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - c
= 6 + 9 = 15 (minimum) 2
c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 10 6
Minimum distance via a e d
d ∞ ∞ 2 0 10 d ∞ ∞ 2 0 10 d - - - - -
Vertex a stored into table P
e 6 ∞ ∞ ∞ 0 e 6 7 ∞ 15 0 e - a - a 0 End vertex
Start vertex

Distances D(0) : Distances matrix D(1): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertex a for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex a to other vertices, direct or via vertices a, b, using table D(1)
(21) Distance from a to b D(1) D(2) P 1 7
a b
Direct : a → b=1 a b c d e a b c d e a b c d e 5 8
Indirect: a → b → b a 0 1 ∞ 9 5 a 0 1 a - - - - - 9 9
6 c
=1 + 0 = 1 b ∞ 0 7 9 ∞ b b - - - - - 2
10 6
c ∞ 8 0 6 ∞ c c - - - - - e d
d ∞ ∞ 2 0 10 d d - - - - -
e 6 7 ∞ 15 0 e e - a - a -
(22) Distance from a to c a b c d e a b c d e a b c d e 1 7
a b
Direct: a → c= ∞ a 0 1 ∞ 9 5 a 0 1 8 a - - b - - 5 8
Indirect: a → b → c b ∞ 0 7 9 ∞ b b - - - - - 9 9
6 c
= 1 + 7 = 8 (minimum) c ∞ 8 0 6 ∞ c c - - - - - 2
Minimum distance via b 10 6
d ∞ ∞ 2 0 10 d d - - - - - e d
Vertex b stored into table P e 6 7 ∞ 15 0 e e - a - a -

(23) Distance from a to d 1 b 7


a b c d e a b c d e a b c d e a
Direct: a → d=9 (minimum) a 0 1 ∞ 9 5 5
a 0 1 8 9 a - - b - - 9 8
Indirect: a →b → d b ∞ 0 7 9 ∞ 6 9
b b - - - - - c
= 1 + 9 = 10 c ∞ 8 0 6 ∞ 2
c c - - - - - 10 6
d ∞ ∞ 2 0 10 e d
d d - - - - -
e 6 7 ∞ 15 0 e e - a - a -

(24) Distance from a to e a b c d e 1 b 7


a b c d e a b c d e a
Direct: a → e = 5 (minimum) a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 5
9 8
Indirect: a → b → e b ∞ 0 7 9 ∞ 9
b b - - - - - 6 c
=1+∞ =∞ c ∞ 8 0 6 ∞ c - - - - - 2
c 10 6
d ∞ ∞ 2 0 10 d d - - - - - e d
e 6 7 ∞ 15 0 e e - a - a -
Start vertex End vertex

Distances D(1): Shortest distances, direct, D(2): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertex a or via vertices a, b for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex b to other vertices, direct or via vertices a, b using table D(1)
(25) Distance from b to a D(1) D(2) P 1 7
a b
Direct : b → a=∞ a b c d e a b c d e a b c d e
5 8
Indirect: b →b → a a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 9 9
6 c
= 0+ ∞= ∞ b ∞ 0 7 9 ∞ b ∞ b - - - - - 2
No link, direct or through a, b c ∞ 8 0 6 ∞ c c - - - - - 10 6
e d
d ∞ ∞ 2 0 10 d d - - - - -
e 6 7 ∞ 15 0 e e - a - a -
(26) Distance from b to c 1
a b c d e a b c d e a b c d e a b 7
Direct:b → c= 7
a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: b → b → c 9 9
b ∞ 0 7 9 ∞ b ∞ 0 7 b - - - - - 6 c
=0+7=7
c ∞ 8 0 6 ∞ c c - - - - - 2
6
e 10
d ∞ ∞ 2 0 10 d d - - - - - d
e 6 7 ∞ 15 0 e e - a - a -
(27) Distance from b to d a b c d e
1 b 7
a b c d e a b c d e a
Direct: b → d=9 5
a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 9 8
Indirect: b →b → d 6 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 b - - - - - c
=0 + 9 =9 2
c ∞ 8 0 6 ∞ c c - - - - - 10 6
d ∞ ∞ 2 0 10 d - - - - - e d
d
e 6 7 ∞ 15 0 e e - a - a -

(28) Distance from b to e a b c d e a b c d e a b c d e 1 b 7


Direct: b → e=∞ a
a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: b →b → e 9 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 6 c
=0+∞ =∞ c ∞ 8 0 6 ∞ c c - - - - - 2
No link, direct or through a ,b 10 6
d ∞ ∞ 2 0 10 d d - - - - - e d
e 6 7 ∞ 15 0 e e - a - a -
Start vertex End vertex

Distances D(1): Shortest distances, direct, D(2): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertex a or via vertices a, b for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex c to other vertices, direct or via vertices a, b using table D(1)
D(1) D(2) P 1 7
(29) Distance from c to a a b
a b c d e a b c d e a b c d e
Direct : c → a=∞ 5
9 8
a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 9
Indirect: c → b → a 6 c
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 2
= 8 + ∞ =∞ 6
c ∞ 8 0 6 ∞ c ∞ c - - - - - e 10
No link, direct or through a, b d
d ∞ ∞ 2 0 10 d d - - - - -
e 6 7 ∞ 15 0 e e - a - a -

(30) Distance from c to b a b c d e a b c d e a b c d e 1 b 7


Direct: c → b=8 (minimum) a
a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: c → b → b 9 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 6 c
= 8 +0 = 8 c ∞ 8 0 6 ∞ c ∞ 8 0 c - - - - - 2
10 6
d ∞ ∞ 2 0 10 d d - - - - - e d
e 6 7 ∞ 15 0 e e - a - a -
(31) Distance from c to d 1 b 7
a b c d e a b c d e a b c d e a
Direct: c → d=6 (minimum) a 0 1 ∞ 9 5 a - - b - - 5
a 0 1 8 9 5 9 8
Indirect: c → b → d b ∞ 0 7 9 ∞ b - - - - - 6 9
b ∞ 0 7 9 ∞ c
=8 + 9 = 17 c ∞ 8 0 6 ∞ c - - - - - 2
c ∞ 8 0 6 10 6
d ∞ ∞ 2 0 10 d - - - - - e d
d
e 6 7 ∞ 15 0 e e - a - a -

(32) Distance from c to e 1 b 7


a b c d e a b c d e a b c d e a
Direct: c → e=∞ a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 5
9 8
Indirect: c → b → e 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 6 c
=8+∞ =∞ c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 2
10 6
No link, direct or through a, b d ∞ ∞ 2 0 10 d d - - - - - e d
e 6 7 ∞ 15 0 e e - a - a -
Start vertex End vertex

Distances D(1): Shortest distances, direct, D(2): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertex a or via vertices a, b for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex d to other vertices, direct or via vertices a, b using table D(1)
(33) Distance from d to a D(1) D(2) P 1 7
Direct : d → a=∞ a b
a b c d e a b c d e a b c d e 5
Indirect: d → b → a a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 9 9
8
6 c
=∞+ ∞ =∞ b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 2
No link, direct or via a, b c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - e 10 6
d
d ∞ ∞ 2 0 10 d ∞ d - - - - -
e 6 7 ∞ 15 0 e e - a - a -
(34) Distance from d to b a b c d e 1
a b c d e a b c d e b 7
Direct: d → b= ∞ a
a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 5
Indirect: d →b →b b ∞ 0 7 9 ∞ 9 8
b ∞ 0 7 9 ∞ b - - - - - 6 9
c
=∞ + 0 = ∞ c ∞ 8 0 6 ∞ c - - - - -
c ∞ 8 0 6 ∞ 2
No link, direct or via a, b d ∞ ∞ 2 0 10 e 10 6
d ∞ ∞ d - - - - - d
e 6 7 ∞ 15 0 e e - a - a -
(35) Distance from d to c 1 b 7
Direct: d → c=2 (minimum) a b c d e a b c d e a b c d e a
a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: d → b → c 9 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 6 c
=∞+7=∞ 2
c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 10 6
d ∞ ∞ 2 0 10 e d
d ∞ ∞ 2 0 d - - - - -
e 6 7 ∞ 15 0 e e - a - a -

(36) Distance from d to e 1 b 7


a b c d e a b c d e a b c d e a
Direct: d → e=10 (minimum)
a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 5
9 8
Indirect: d →b → e 6 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - c
=∞+5 =∞ 2
c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 10 6
d ∞ ∞ 2 0 10 d ∞ ∞ 2 0 10 d - - - - - e d
e 6 7 ∞ 15 0 e e - a - a -
Start vertex End vertex

Distances D(1): Shortest distances, direct, D(2): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertex a or via vertices a, b for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex e to other vertices, direct or via vertices a, b, using table D(1)
(37) Distance from e to a D(1) D(2) P 1 7
Direct : e →a=6 (minimum) a b
a b c d e a b c d e a b c d e 5
Indirect: e → b →a a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 9 9
8
6 c
= 7+ ∞ = ∞ b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 2
c ∞ 8 0 6 ∞ c - - - - - 10 6
c ∞ 8 0 6 ∞ e d
d ∞ ∞ 2 0 10 d ∞ ∞ 2 0 10 d - - - - -
e 6 7 ∞ 15 0 e 6 e - a - a -
(38) Distance from e to b a b c d e a b c d e 1
a b c d e b 7
Direct: e → b= 7 a
a 0 1 ∞ 9 5 a 0 1 8 9 5 a - - b - - 5
Indirect: e → b → b b ∞ 0 7 9 ∞ b - - - - - 9 9
8
b ∞ 0 7 9 ∞ 6 c
= 7+0 =7 c ∞ 8 0 6 ∞ c - - - - -
c ∞ 8 0 6 ∞ 2
10 6
d ∞ ∞ 2 0 10 d ∞ ∞ 2 0 10 d - - - - - e d
e 6 7 ∞ 15 0 e 6 7 e - a - a -

(39) Distance from e to c 1 b 7


a b c d e a b c d e a b c d e a
Direct: e → c=∞ a 0 1 ∞ 9 5 a - - b - - 5
a 0 1 8 9 5 9 9
8
Indirect: e → b → c b ∞ 0 7 9 ∞ b - - - - - 6 c
b ∞ 0 7 9 ∞
= 7 +7 =14 (minimum) c ∞ 8 0 6 ∞ c - - - - - 2
c ∞ 8 0 6 ∞ 10 6
Minimum distance is via b e d
d ∞ ∞ 2 0 10 d ∞ ∞ 2 0 10 d - - - - -
Vertex b stored into table P e 6 7 ∞ 15 0 e - a b a -
e 6 7 14

(40) Distance from e to d 1 b 7


a b c d e a b c d e a b c d e a
Direct: e → d=15 (minimum)
a 0 1 ∞ 9 5 a - - b - - 5 8
Indirect: e → b → d a 0 1 8 9 5 9 9
b ∞ 0 7 9 ∞ b - - - - - 6 c
= 7 + 9 = 16 b ∞ 0 7 9 ∞ 2
c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 10 6
d ∞ ∞ 2 0 10 d - - - - - e d
d ∞ ∞ 2 0 10
e 6 7 ∞ 15 0 e 6 7 14 15 0 e - a b a -
Start vertex End vertex

Distances D(1): Shortest distances, direct, D(2): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertex a or via vertices a, b for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex a to other vertices, direct or via vertices a, b, c , using table D(2)
(41) Distance from a to b D(2) D(3) P 1 7
Direct : a → b=1 (minimum) a b
a b c d e a b c d e a b c d e 5
Indirect: a → c → b a 0 1 8 9 5 a 0 1 a - - b - - 9 9
8
6 c
= 8 + 8 = 16 b ∞ 0 7 9 ∞ b b - - - - - 2
c ∞ 8 0 6 ∞ c c - - - - - 10 6
e d
d ∞ ∞ 2 0 10 d d - - - - -
e 6 7 14 15 0 e e - a b a -
(42) Distance from a to c a b c d e a b c d e a b c d e 1 b 7
Direct: a → c= 8 a 0 1 8 9 5 a 0 1 8 a - - b - - a
5 8
Indirect: a →c → c b ∞ 0 7 9 ∞ b b - - - - - 6
9 9
c c
= 8 +0 = 8 c ∞ 8 0 6 ∞ c - - - - - 2
d 10 6
d ∞ ∞ 2 0 10 d - - - - - e d
e 6 7 14 15 0 e e - a b a -

(43) Distance from a to d 1 b 7


a b c d e a b c d e a b c d e a
Direct: a → d=9 (minimum) 5
a 0 1 8 9 5 a 0 1 8 9 a - - b - - 9 8
Indirect: a →c → d 6 9
b ∞ 0 7 9 ∞ b b - - - - - c
= 8 +6 = 14 2
c ∞ 8 0 6 ∞ c c - - - - - 10 6
e d
d ∞ ∞ 2 0 10 d d - - - - -
e 6 7 14 15 0 e e - a b a -

(44) Distance from a to e 1 b 7


a b c d e a b c d e a b c d e a
Direct: a → e=5 (minimum)
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5
9 8
Indirect: a →c → e 9
b ∞ 0 7 9 ∞ b b - - - - - 6 c
= 9 + ∞= ∞ 2
c ∞ 8 0 6 ∞ c c - - - - - 10 6
d ∞ ∞ 2 0 10 d e d
d - - - - -
e 6 7 14 15 0 e e - a b a -
Start vertex End vertex

Distances D(2): Shortest distances, direct, D(3): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b or via vertices a, b, c for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex b to other vertices, direct or via vertices a, b, c , using table D(2)
(45) Distance from b to a D(2) D(3) P 1 7
a b
Direct : b → a=∞ a b c d e a b c d e a b c d e
5 8
Indirect: b → c → a a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
6 c
= 7+∞=∞ b ∞ 0 7 9 ∞ b ∞ 0 b - - - - - 2
No link, direct or via a, b, c c ∞ 8 0 6 ∞ c c - - - - - e 10 6
d d
d ∞ ∞ 2 0 10 d - - - - -
e 6 7 14 15 0 e e - a b a -
(46) Distance from b to c 1
a b c d e a b c d e a b c d e b 7
Direct: b → c= 7 a
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5
Indirect: b → c → c 9 8
b ∞ 0 7 9 ∞ b ∞ 0 7 b - - - - - 6 9
c
= 7 +0 = 7
c ∞ 8 0 6 ∞ c c - - - - - 2
10 6
d ∞ ∞ 2 0 10 d d - - - - - e d
e 6 7 14 15 0 e e - a b a -
(47) Distance from b to d a b c d e 1 b 7
a b c d e a b c d e a
Direct: b → d=9 (minimum) a 0 1 8 9 5 5
a 0 1 8 9 5 a - - b - - 9 8
Indirect: b → c → d b ∞ 0 7 9 6 9
b ∞ 0 7 9 ∞ b - - - - - c
=7 + 6 = 13 c 2
c ∞ 8 0 6 ∞ c - - - - - 10 6
d ∞ ∞ 2 0 10 d d - - - - - e d
e 6 7 14 15 0 e e - a b a -

(48) Distance from b to e a b c d e a b c d e a b c d e 1 b 7


Direct: b → e=∞ a
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: b → c → e 9 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 6 c
= 7+ ∞ = ∞ c ∞ 8 0 6 ∞ c c - - - - - 2
10 6
No link, direct or via a, b, c d ∞ ∞ 2 0 10 d d - - - - - e d
e 6 7 14 15 0 e e - a b a -
Start vertex End vertex

Distances D(2): Shortest distances, direct, D(3): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b or via vertices a, b, c for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex c to other vertices, direct or via vertices a, b, c , using table D(2)
(49) Distance from c to a D(2) D(3) P 1 7
a b
Direct : c → a=∞ a b c d e a b c d e a b c d e 5 8
Indirect: c → c → a a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
6 c
=0+∞ =∞ b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 2
No link, direct or via a, b, c c ∞ 8 0 6 ∞ c ∞ c - - - - - 10 6
e d
d ∞ ∞ 2 0 10 d d - - - - -
e 6 7 14 15 0 e e - a b a -
(50) Distance from c to b a b c d e a b c d e a b c d e 1 7
a b
Direct: c → b=8 a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: c → c → b b ∞ 0 7 9 ∞ 9
b ∞ 0 7 9 ∞ b - - - - - 6 9
c
= 0+8=8 c ∞ 8 0 6 ∞ c ∞ 8 0 c - - - - - 2
10 6
d ∞ ∞ 2 0 10 d d - - - - - e d
e 6 7 14 15 0 e e - a b a -

(51) Distance from c to d 1 b 7


a b c d e a b c d e a b c d e a
Direct: c → d=6 a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5
9 8
Indirect: c →c → d 6 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - c
=0+6=6 c ∞ 8 0 6 ∞ c ∞ 8 0 6 c - - - - - 2
6
e 10
d ∞ ∞ 2 0 10 d d - - - - - d
e 6 7 14 15 0 e e - a b a -

(52) Distance from c to e a b c d e a b c d e a b c d e 1 b 7


Direct: c → e = ∞ a
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5
Indirect: c → c → e 9 8
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 6 9
= 0 +∞ = ∞ c
c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 2
No link, direct or via a, b, c 10 6
d ∞ ∞ 2 0 10 d d - - - - - e d
e 6 7 14 15 0 e e - a b a -
Start vertex End vertex

Distances D(2): Shortest distances, direct, D(3): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b or via vertices a, b, c for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex d to other vertices, direct or via vertices a, b, c using table D(2)
(53) Distance from d to a D(2) D(3) P 1 7
a b
Direct : d → a=∞ a b c d e a b c d e a b c d e 5 8
Indirect: d → c → a a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
6 c
=2 + ∞ = ∞ b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 2
No link, direct or via a, b, c c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 10 6
e d
d ∞ ∞ 2 0 10 d ∞ d - - - - -
e 6 7 14 15 0 e e - a b a -
(54) Distance from d to b 1
a b c d e a b c d e a b c d e b 7
Direct: d → b= ∞ a
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5
Indirect: d →c →b 9 8
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 6 9
c
=2+ 8 = 10 (minimum)
c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 2
Minimum distance via c e 10 6
d ∞ ∞ 2 0 10 d ∞ 10 d - c - - - d
Vertex c stored into table P
e 6 7 14 15 0 e e - a b a -
(55) Distance from d to c 1 b 7
a b c d e a b c d e a b c d e a
Direct: d → c=2 a 0 1 8 9 5 a 0 1 8 9 5 5
a - - b - - 9 8
Indirect: d → c → c 6 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - c
=2+ 0=2 c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ 2
c - - - - - 10 6
d ∞ 10 2 0 e d
d ∞ ∞ 2 0 10 d - c - - -
e 6 7 14 15 0 e e - a b a -

(56) Distance from d to e a b c d e a b c d e a b c d e


1 b 7
Direct: d → e=10 (minimum) a
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: d →c → e 9 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 6 c
=2+ ∞ = ∞ c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 2
10 6
d ∞ ∞ 2 0 10 d ∞ 10 2 0 10 d - c - - - e d
e 6 7 14 15 0 e e - a b a -
Start vertex End vertex

Distances D(2): Shortest distances, direct, D(3): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b or via vertices a, b, c for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex e to other vertices, direct or via vertices a, b, c, using table D(2)
(57) Distance from e to a D(2) D(3) P 1 7
a b
Direct : e →a=6 (minimum) a b c d e a b c d e a b c d e 5 8
Indirect: e → c →a a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
6 c
= 14 + ∞ = ∞ b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 2
c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 10 6
e d
d ∞ ∞ 2 0 10 d ∞ 10 2 0 10 d - c - - -
e 6 7 14 15 0 e 6 e - a b a -
(58) Distance from e to b 1
a b c d e a b c d e a b c d e b 7
Direct: e → b= 7 (minimum* a
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5
Indirect: e → c → b 9 8
b ∞ 0 7 9 ∞ b ∞ 0 7 9 ∞ b - - - - - 6 9
= 14 + 2 = 16 c
c ∞ 8 0 6 ∞ c ∞ 8 0 6 ∞ c - - - - - 2
10 6
d ∞ ∞ 2 0 10 d ∞ 10 2 0 10 d - c - - - e d
e 6 7 14 15 0 e 6 7 e - a b a -
(59) Distance from e to c a b c d e 1 b 7
a b c d e a b c d e a
Direct: e → c=14 a 0 1 8 9 5 5
a 0 1 8 9 5 a - - b - - 9 8
Indirect: e → c → c b ∞ 0 7 9 ∞ 6 9
b ∞ 0 7 9 ∞ b - - - - - c
= 14 + 0 = 14 c ∞ 8 0 6 ∞ 2
c ∞ 8 0 6 ∞ c - - - - - 10 6
d ∞ ∞ 2 0 10 d ∞ 10 2 0 10 d - c - - - e d
e 6 7 14 15 0 e 6 7 14 e - a b a -

(60) Distance from e to d a b c d e 1 b 7


a b c d e a b c d e a
Direct: e → d=15 (minimum) a 0 1 8 9 5 5
a 0 1 8 9 5 a - - b - - 9 8
Indirect: e → c → d b ∞ 0 7 9 ∞ 6 9
b ∞ 0 7 9 ∞ b - - - - - c
= 14 + 6 = 20 c ∞ 8 0 6 ∞ 2
c ∞ 8 0 6 ∞ c - - - - - 10 6
d ∞ ∞ 2 0 10 d ∞ 10 2 0 10 d - c - - - e d
e 6 7 14 15 0 e 6 7 14 15 0 e - a b a -
Start vertex End vertex

Distances D(2): Shortest distances, direct, D(3): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b or via vertices a, b, c for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex a to other vertices, direct or via vertices a, b, c, d , using table D(3)
(61) Distance from a to b D(3) D(4) P 1 7
a b
Direct : a → b=1 (minimum) a b c d e a b c d e a b c d e 5
Indirect: a → d → b a 0 1 8 9 5 a 0 1 9 8
a - - b - - 6 9
= 9 + 10 = 19 b ∞ 0 7 9 ∞ b c
b - - - - - 2
c ∞ 8 0 6 ∞ c 10 6
c - - - - - e d
d ∞ 10 2 0 10 d d - c - - -
e 6 7 14 15 0 e e - a b a -
(62) Distance from a to c 1
a b c d e a b c d e a b c d e b 7
Direct: a → c= 8 (minimum) a
a 0 1 8 9 5 a 0 1 8 a - - b - - 5 8
Indirect: a → d → c b ∞ 0 7 9 ∞ b b - - - - - 6
9 9
c
= 9 + 2 = 11 c ∞ 8 0 6 ∞ c c - - - - - 2
10 6
d ∞ 10 2 0 10 d d - c - - - e d
e 6 7 14 15 0 e e - a b a -
(63) Distance from a to d 1 b 7
Direct: a → d=9 a b c d e a b c d e a b c d e a
a 0 1 8 9 5 a 0 1 8 9 5
Indirect: a →d → d a - - b - - 9 9
8
b ∞ 0 7 9 ∞ b 6 c
=9+0=9 b - - - - -
2
c ∞ 8 0 6 ∞ c c - - - - - 10 6
e d
d ∞ 10 2 0 10 d d - c - - -
e 6 7 14 15 0 e e - a b a -

(64) Distance from a to e 1 b 7


a b c d e a b c d e a b c d e a
Direct: a → e=5 (minimum) 5
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
8
Indirect: a → d → e 6 c
b ∞ 0 7 9 ∞ b b - - - - -
= 9 + 10 = 19 2
c ∞ 8 0 6 ∞ c c - - - - - 10 6
e d
d ∞ 10 2 0 10 d d - c - - -
e 6 7 14 15 0 e e - a b a - Start vertex End vertex

Distances D(3): Shortest distances, direct, D(4): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b, c or via vertices a, b, c, d for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex b to other vertices, direct or via vertices a, b, c, d , using table D(3)
(65) Distance from b to a D(3) D(4) P 1 7
a b
Direct : b → a=∞ a b c d e a b c d e a b c d e 5 8
Indirect: b → d → a a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
6 c
=9+ ∞=∞ b ∞ 0 7 9 ∞ b ∞ 0 b - - - - - 2
c ∞ 8 0 6 ∞ c c - - - - - 10 6
e d
d ∞ 10 2 0 10 d d - c - - -
e 6 7 14 15 0 e e - a b a -

(66) Distance from b to c a b c d e a b c d e a b c d e 1 b 7


Direct: b → c= 7 (minimum) a
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5
Indirect: b → d → c 9 8
b ∞ 0 7 9 ∞ b ∞ 0 7 b - - - - - 6 9
= 9 + 2 = 11 c
c ∞ 8 0 6 ∞ c c - - - - - 2
10 6
d ∞ 10 2 0 10 d d - c - - - e d
e 6 7 14 15 0 e e - a b a -

(67) Distance from b to d 1 b 7


a b c d e a b c d e a b c d e a
Direct: b → d=9 5 8
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
Indirect: b →d → d 6 c
b ∞ 0 7 9 ∞ b ∞ 0 7 9 b - - - - -
= 9 + 0= 9 2
c ∞ 8 0 6 ∞ c c - - - - - e 10 6
d ∞ 10 2 0 10 d
d d - c - - -
e 6 7 14 15 0 e e - a b a -
(68) Distance from b to e 1 7
a b c d e a b c d e a b c d e a b
Direct: b → e=∞
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: b → d → e 9 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 b - - - - d 6 c
= 9 + 10 =19 (minimum)
c ∞ 8 0 6 ∞ c 2
Minimum distance is via d c - - - - - 10 6
e d
Vertex d stored into table P d ∞ 10 2 0 10 d d - c - - -
e 6 7 14 15 0 e e - a b a - End vertex
Start vertex

Distances D(3): Shortest distances, direct, D(4): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b, c or via vertices a, b, c, d for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex c to other vertices, direct or via vertices a, b, c, d , using table D(3)
(69) Distance from c to a D(3) D(4) P 1 7
a b
Direct : c → a=∞ a b c d e a b c d e a b c d e 5 8
Indirect: c → d → a a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
6 c
=6 +∞ =∞ b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 b - - - - d 2
No link, direct or via a ,b, c, d c ∞ 8 0 6 ∞ c ∞ 10 6
c - - - - - e d
d ∞ 10 2 0 10 d d - c - - -
e 6 7 14 15 0 e e - a b a -
(70) Distance from c to b a b c d e a b c d e a b c d e 1 7
a b
Direct: c → b=8 (minimum) a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: c → d → b 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 b - - - - d 6 9
= 6 +10 = 16 c
c ∞ 8 0 6 ∞ c ∞ 8 0 c - - - - - 2
10 6
d ∞ 10 2 0 10 d d - c - - - e d
e 6 7 14 15 0 e e - a b a -

(71) Distance from c to d 1 b 7


a b c d e a b c d e a b c d e a
Direct: c → d=6 (minimum) a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
9 9
Indirect: c → d → d b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 b - - - - d 6 c
=6 + 0 = 6 c ∞ 8 0 6 ∞ c ∞ 8 0 6 c - - - - - 2
10 6
d ∞ 10 2 0 10 d e d
d - c - - -
e 6 7 14 15 0 e e - a b a -

(72) Distance from c to e 1 b 7


a b c d e a b c d e a b c d e a
Direct: c → e=∞ a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
9 9
Indirect: c → d → e b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 b - - - - d 6 c
= 6 + 10 =16( minimum) c ∞ 8 0 6 ∞ c ∞ 8 0 6 16 c - - - - d 2
10 6
Minimum distance is via d d ∞ 10 2 0 10 d e d
d - c - - -
Vertex d stored into table P e 6 7 14 15 0 e e - a b a -
Start vertex End vertex

Distances D(3): Shortest distances, direct, D(4): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b, c or via vertices a, b, c, d for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex d to other vertices, direct or via vertices a, b, c, d , using table D(3)
(73) Distance from d to a D(3) D(4) P 1 7
a b
Direct : d → a=∞ a b c d e a b c d e a b c d e 5 8
Indirect: d → d →a a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
6 c
=0+∞ =∞ b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 b - - - - d 2
No link, direct or via a, b, c, d c ∞ 8 0 6 ∞ c ∞ 8 0 6 16 c - - - - d 10 6
e d
d ∞ 10 2 0 10 d ∞ d - c - - -
e 6 7 14 15 0 e e - a b a -
(74) Distance from d to b a b c d e a b c d e a b c d e 1 7
a b
Direct: d → b=10 a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: d →d →b b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 b - - - - d 9 9
6 c
= 0 +10 = 10 c ∞ 8 0 6 ∞ c ∞ 8 0 6 16 c - - - - d 2
d ∞ 10 2 0 10 d ∞ 10 10 6
d - c - - - e d
e 6 7 14 15 0 e e - a b a -

(75) Distance from d to c 1 b 7


a b c d e a b c d e a b c d e a
Direct: d → c=2 (minimum) a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
9 9
Indirect: d → d → c b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 b - - - - d 6 c
=0+2 =2 c ∞ 8 0 6 ∞ c ∞ 8 0 6 16 c - - - - d 2
10 6
d ∞ 10 2 0 10 d ∞ 10 2 0 e d
d - c - - -
e 6 7 14 15 0 e e - a b a -

(76) Distance from d to e 1 b 7


a b c d e a b c d e a b c d e a
Direct: d → e=10 a 0 1 8 9 5 a 0 1 8 9 5 5
a - - b - - 9 8
Indirect: d →d → e b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 6 9
c
b - - - - d
= 0 + 10 = 10 c ∞ 8 0 6 ∞ c ∞ 8 0 6 16 2
c - - - - d 10 6
d ∞ 10 2 0 10 d ∞ 10 2 0 10 e d
d - c - - -
e 6 7 14 15 0 e e - a b a -
Start vertex End vertex

Distances D(3): Shortest distances, direct, D(4): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b, c or via vertices a, b, c, d for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex e to other vertices, direct or via vertices a, b, c, d , using table D(3)
(77) Distance from e to a D(3) D(4) P 1 7
a b
Direct : e →a=6 (minimum) a b c d e a b c d e a b c d e 5
Indirect: e → d →a a 0 1 8 9 5 a 0 1 8 9 5 9 8
a - - b - - 6 9
= 15+ ∞ = ∞ b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 c
b - - - - d 2
c ∞ 8 0 6 ∞ c ∞ 8 0 6 16 10 6
c - - - - d e d
d ∞ 10 2 0 10 d ∞ 10 2 0 10 d - c - - -
e 6 7 14 15 0 e 6 e - a b a -
(78) Distance from e to b a b c d e a b c d e 1 7
a b c d e a b
Direct: e → b= 7 (minimum) a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5
9 8
Indirect: e → d → b b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 9
b - - - - d 6 c
= 15 + 10 = 25 c ∞ 8 0 6 ∞ c ∞ 8 0 6 16 c - - - - d 2
6
e 10
d ∞ 10 2 0 10 d ∞ 10 2 0 10 d - c - - - d
e 6 7 14 15 0 e 6 7 e - a b a -
(79) Distance from e to c 1 b 7
a b c d e a b c d e a b c d e a
Direct: e → c=14 (minimum)
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5
9 8
Indirect: e → d → c 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 b - - - - d 6 c
= 15 + 2 = 17 2
c ∞ 8 0 6 ∞ c ∞ 8 0 6 16 c - - - - d 10 6
d ∞ 10 2 0 10 d ∞ 10 2 0 10 e d
d - c - - -
e 6 7 14 15 0 e 6 7 14 e - a b a -

(80) Distance from e to d 1 b 7


a b c d e a b c d e a b c d e a
Direct: e → d=15
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5
9 8
Indirect: e → d → d 9
b ∞ 0 7 9 ∞ b ∞ 0 7 9 19 b - - - - d 6 c
=15 + 0 = 15 2
c ∞ 8 0 6 ∞ c ∞ 8 0 6 16 c - - - - d 10 6
d ∞ 10 2 0 10 d ∞ 10 2 0 10 e d
d - c - - -
e 6 7 14 15 0 e 6 7 14 15 0 e - a b a -
Start vertex End vertex

Distances D(3): Shortest distances, direct, D(4): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b, c or via vertices a, b, c, d for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex a to other vertices, direct or via vertices a, b, c, d, e , using table D(4)
(81) Distance from a to b D(4) D(5) P 1 7
a b c d e a b
Direct : a → b=1 (minimum) a b c d e a b c d e
5
Indirect: a → e → b a 0 1 8 9 5 a 0 1 a - - b - - 9 9
8
6 c
=5 + 7 = 12 b ∞ 0 7 9 19 b b - - - - d
2
c ∞ 8 0 6 16 c c - - - - d e 10 6
d d - c - - - d
d ∞ 10 2 0 10
e 6 7 14 15 0 e e - a b a -

(82) Distance from a to c a b c d e a b c d e a b c d e 1 7


a b
Direct: a → c= 8 (minimum) a 0 1 8 9 5 a 0 1 8 a - - b - - 5 8
Indirect: a → e → c b b - - - - d 9 9
b ∞ 0 7 9 19 6 c
= 5 + 14 = 19 c ∞ 8 0 6 16 c c - - - - d 2
10 6
d ∞ 10 2 0 10 d d - c - - - e d
e 6 7 14 15 0 e e - a b a -

(83) Distance from a to d 1 b 7


a b c d e a b c d e a b c d e a
Direct: a → d=9 (minimum) a 0 1 8 9 a - - b - - 5
a 0 1 8 9 5 9 9
8
Indirect: a → e → d b b - - - - d 6 c
b ∞ 0 7 9 19
= 5 + 15 = 20 c c - - - - d 2
c ∞ 8 0 6 16 10 6
d e d
d ∞ 10 2 0 10 d - c - - -
e 6 7 14 15 0 e e - a b a -

(84) Distance from a to e a b c d e


1 b 7
a b c d e a b c d e a
Direct: a → e=5 5
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 8
Indirect: a → e → e 6 9
b ∞ 0 7 9 19 b b - - - - d c
=5+0 =5 2
c ∞ 8 0 6 16 c c - - - - d 10 6
d d - c - - - e d
d ∞ 10 2 0 10
e 6 7 14 15 0 e e - a b a -
Start vertex End vertex

Distances D(4): Shortest distances, direct, D(5): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b, c, d or via vertices a, b, c, d , e for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex b to other vertices, direct or via vertices a, b, c, d, e , using table D(4)
(85) Distance from b to a D(4) D(5) P 1 7
a b
Direct : b → a=∞ a b c d e a b c d e a b c d e
5 8
Indirect: b → e → a a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
6 c
= 19+ 6= 25 (minimum) b ∞ 0 7 9 19 b 25 b e - - - d 2
Minimum distance is via e c ∞ 8 0 6 16 c - - - - d 10 6
c e d
Vertex e stored into table P d ∞ 10 2 0 10 d d - c - - -
e 6 7 14 15 0 e e - a b a -
(86) Distance from b to c a b c d e a b c d e a b c d e 1 7
a b
Direct: b → c= 7 (minimum) a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: b →e → c 9
b ∞ 0 7 9 19 b 25 0 7 b e - - - d 6 9
=19 + 14 = 3 3 c
c ∞ 8 0 6 16 c c - - - - d 2
10 6
d ∞ 10 2 0 10 d d - c - - - e d
e 6 7 14 15 0 e e - a b a -
(87) Distance from b to d a b c d e a b c d e a b c d e 1 b 7
a
Direct: b → d=9 (minimum) a - - b - - 5
a 0 1 8 9 5 a 0 1 8 9 5 9 8
Indirect: b →e → d 6 9
b ∞ 0 7 9 19 b 25 0 7 9 b e - - - d c
= 19 + 15 = 34 2
c ∞ 8 0 6 16 c c - - - - d 10 6
e d
d ∞ 10 2 0 10 d d - c - - -
e 6 7 14 15 0 e e - a b a -

(88) Distance from b to e a b c d e 1 b 7


a b c d e a b c d e a
Direct: b → e=19 a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
9 9
Indirect: b → e → e b ∞ 0 7 9 19 b 25 0 7 9 19 b e - - - d 6 c
=19 + 0 = 19 c ∞ 8 0 6 16 c c - - - - d 2
6
e 10
d ∞ 10 2 0 10 d d - c - - - d
e 6 7 14 15 0 e e - a b a -
Start vertex End vertex

Distances D(4): Shortest distances, direct, D(5): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b, c, d or via vertices a, b, c, d , e for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex c to other vertices, direct or via vertices a, b, c, d, e , using table D(4)
(89) Distance from c to a D(4) D(5) P 1 7
a b
Direct : c → a=∞ a b c d e a b c d e a b c d e
5 8
Indirect: c → e → a a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
6 c
=16 +6 = 22 b ∞ 0 7 9 19 b 25 0 7 9 19 b e - - - d 2
Minimum distance is via e c e - - - d 10 6
c ∞ 8 0 6 16 c 22 e d
Vertex e stored into table P d ∞ 10 2 0 10 d d - c - - -
e 6 7 14 15 0 e e - a b a -
(90) Distance from c to b a b c d e a b c d e a b c d e 1 7
a b
Direct: c → b=8 (minimum) a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: c → e → b 9
b ∞ 0 7 9 19 b 25 0 7 9 19 b e - - - d 6 9
= 16 +7 = 23 c
c ∞ 8 0 6 16 c 22 8 0 c e - - - d 2
10 6
d ∞ 10 2 0 10 d d - c - - - e d
e 6 7 14 15 0 e e - a b a -
(91) Distance from c to d a b c d e
1 b 7
a b c d e a b c d e a
Direct : c → d=6 (minimum) 5
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 8
Indirect: c → e → d 6 9
b ∞ 0 7 9 19 b 25 0 7 9 19 b e - - - d c
=16 + 14 =30 2
c ∞ 8 0 6 16 c 22 8 0 6 c e - - - d 10 6
e d
d ∞ 10 2 0 10 d d - c - - -
e 6 7 14 15 0 e e - a b a -
(92) Distance from c to e 1 7
a b c d e a b c d e a b c d e a b
Direct : c → e=16
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: c → e → e 9 9
b ∞ 0 7 9 19 b 25 0 7 9 19 b e - - - d 6 c
= 16 + 0 =16
c e - - - d 2
c ∞ 8 0 6 16 c 22 8 0 6 16 10 6
e d
d ∞ 10 2 0 10 d d - c - - -
e 6 7 14 15 0 e e - a b a - Start vertex End vertex

Distances D(4): Shortest distances, direct, D(5): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b, c, d or via vertices a, b, c, d , e for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex d to other vertices, direct or via vertices a, b, c, d, e , using table D(4)
(93) Distance from d to a D(4) D(5) P 1 b 7
Direct : d → a=∞ a b c d e a b c d e a b c d e a
Indirect: d →e →a 5 8
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
= 10 +6 = 16 6 c
b ∞ 0 7 9 19 b 25 0 7 9 19 b e - - - d 2
Minimum distance is via e c ∞ 8 0 6 16 c 22 8 0 6 16 c e - - - d 10 6
e d
Vertex e stored into table P d e c - - -
d ∞ 10 2 0 10 d 16
e 6 7 14 15 0 e e - a b a -
(94) Distance from d to b a b c d e
a b c d e a b c d e 1 b 7
Direct: d → b=10 (minimum) a - - b - - a
a 0 1 8 9 5 a 0 1 8 9 5 5
Indirect: d → e →b 9 8
b ∞ 0 7 9 19 b 25 0 7 9 19 b e - - - d 6 9
=10 + 7 = 17 c
c ∞ 8 0 6 16 c 22 8 0 6 16 c e - - - d 2
10 6
d ∞ 10 2 0 10 d 16 10 d e c - - - e d
e 6 7 14 15 0 e e - a b a -
(95) Distance from d to c a b c d e
1 b 7
a b c d e a b c d e a
Direct: d → c=2 (minimum) a - - b - - 5
a 0 1 8 9 5 a 0 1 8 9 5 9 8
Indirect: d → e → c 6 9
b ∞ 0 7 9 19 b 25 0 7 9 19 b e - - - d c
= 10 + 14 = 24 2
c ∞ 8 0 6 16 c 22 8 0 6 16 c e - - - d 10 6
e d
d ∞ 10 2 0 10 d 16 10 2 0 d e c - - -
e 6 7 14 15 0 e e - a b a -
(96) Distance from d to e 1 b 7
Direct : d → e=10 a b c d e a b c d e a b c d e a
a - - b - - 5
Indirect: d → e → e a 0 1 8 9 5 a 0 1 8 9 5 9 9
8
b e - - - d 6 c
=10 + 0 = 10 b ∞ 0 7 9 19 b 25 0 7 9 19
2
c ∞ 8 0 6 16 c 22 8 0 6 16 c e - - - d e 10 6
d e c - - - d
d ∞ 10 2 0 10 d 16 10 2 0 10
e 6 7 14 15 0 e e - a b a - Start vertex End vertex

Distances D(4): Shortest distances, direct, D(5): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b, c, d or via vertices a, b, c, d , e for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
Shortest distances from vertex e to other vertices, direct or via vertices a, b, c, d, e , using table D(4)
(97) Distance from e to a D(4) D(5) P 1 7
Direct : a b
a b c d e a b c d e a b c d e
5
e →a=6 a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 9
8
6 c
Indirect: b ∞ 0 7 9 19 b 25 0 7 9 19 b e - - - d 2
e → e →a =0 + 60 = 6 c ∞ 8 0 6 16 c 22 8 0 6 16 c e - - - d e 10 6
d
d ∞ 10 2 0 10 d 16 10 2 0 10 d e c - - -
e 6 7 14 15 0 e 6 e - a b a -
(98) Distance from e to b a b c d e a b c d e a b c d e 1 7
a b
Direct : e → b= 7 a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 5 8
Indirect: e →e → b 9
b ∞ 0 7 9 19 b 25 0 7 9 19 b e - - - d 6 9
= 0+7 =7 c
c ∞ 8 0 6 16 c 22 8 0 6 16 c e - - - d 2
10 6
d ∞ 10 2 0 10 d 16 10 2 0 10 d e c - - - e d
e 6 7 14 15 0 e 6 7 e - a b a -
(99) Distance from e to c 1 b 7
a b c d e a b c d e a b c d e a
Direct : e → c=14 5
a 0 1 8 9 5 a 0 1 8 9 5 a - - b - - 9 8
Indirect: e → e → c 6 9
b ∞ 0 7 9 19 b 25 0 7 9 19 b e - - - d c
= 0+ 14 =14 2
c ∞ 8 0 6 16 c 22 8 0 6 16 c e - - - d 10 6
e d
d ∞ 10 2 0 10 d 16 10 2 0 10 d e c - - -
e 6 7 14 15 0 e 6 7 14 e - a b a -

(100) Distance from e to d 1 b 7


a b c d e a b c d e a b c d e a
Direct : e → d=15 a - - b - - 5
a 0 1 8 9 5 a 0 1 8 9 5 9 9
8
Indirect: e → e → d 6 c
b ∞ 0 7 9 19 b 25 0 7 9 19 b e - - - d
=0 + 15 = 15 2
c ∞ 8 0 6 16 c 22 8 0 6 16 c e - - - d e 10 6
d
d ∞ 10 2 0 10 d 16 10 2 0 10 d e c - - -
e 6 7 14 15 0 e 6 7 14 15 0 e - a b a - Start vertex End vertex

Distances D(4): Shortest distances, direct, D(5): Shortest distances, direct, P: Intermediate vertices Paths
Indirect or via vertices a, b, c, d or via vertices a, b, c, d , e for shortest paths Direct Indirect
Direct
Floyd-Warshall Algorithm
All Pairs Shortest Distances
The table in figure (i), generated by the Floyd Warshall algorithm, stores the shortest distances
between all pairs of vertices V={a, b, c, d, e } of the sample graph depicted in figure (ii). The
first column of the table contains the start vertices. The top row contains the end vertices of
the shortest paths. For example, the shortest distance from vertex a to vertex c is 8, whereas,
shortest distance from vertex c to a is 22.

End vertices

a b c d e
1
b 7
a 0 1 8 9 5 a
b 25 0 7 9 19 8
5
9 9
Start vertices c 22 8 0 6 16 6 2 c

d 16 10 2 0 10 6
e 10
e 6 7 14 15 0 d

(i) Table of all pairs shortest distances (ii) Sample weighted graph
Floyd-Warshall Algorithm
All Pairs Shortest Paths
The table (i), stores the information about the intermediate vertices on a shortest path between a
pair of vertices V={a, b, c, d, e } of the sample graph depicted in figure (ii). The first column
of the table contains the start vertices. The top row contains the end vertices of the shortest
paths. A dash (- ) means that there is direct shortest path between the corresponding vertices
For example, there is a direct shortest path from vertex a to vertex b. A vertex label in the table
indicates, the vertex that links the last segment of the shortest path from a given vertex. There
are ten such paths. Some pass through more than one vertex. For example, the last part of the
shortest path from a to c passes through the vertex b. The table can be used to map all other
nine shortest paths that pass through the intermediate vertices, as illustrated in the next set of
diagrams.
1
a b c d e b 7
a
a - - b - - 8
5
b e - - - d 9 9
6 2 c
c e - - - d
6
e 10
d e c - - - d
e - a b a - (ii) Sample weighted graph The highlighted
(i) Table of intermediate vertices in Path is shortest path from a to c through b
a shortest path
Floyd-Warshall Algorithm
Shortest paths through intermediate vertices
Path #2: From table P it follows that the last a b c d e
1 7
segment of the shortest path, from vertex b to a - - b - - a
b
vertex a, passes through the vertex e. Thus, b e - - - d 5
8
9
b→e→a 9
c e - - - d 6
c
Again, the shortest path from b to e passes
through d. Therefore, the shortest path is d e c - - - 10 2 6
e
b→d→e→a e - a b a - d

Path #3: The shortest path from vertex b to a b c d e 1 7


vertex e passes through the vertex d. The b
a - - b - - a
5
shortest path from b to d is direct. Thus shortest b e - - - d 9 8
9
path from b to e is 6
c
b→d→e
c e - - - d
2 6
d e c - - - e
10
d
e - a b a -

Path #4 : The shortest path from vertex c to a b c d e 1 7


vertex a passes through the vertex e. Thus, b
a - - b - - 5
a
c→e→a 9 8
b e - - - d 9
However, shortest path from c to e passes 6
c
through d. Therefore, the shortest path from c c e - - - d
2 6
10
to a, through the intermediate vertices d, e , is d e c - - - e d
c→d→e→a e - a b a -
Intermediate vertex Start vertex Mid vertex End vertex
Floyd-Warshall Algorithm
Shortest paths through intermediate vertices
Path #5: The shortest path from vertex c to a b c d e
1 7
vertex e passes through the vertex d. The a - - b - - a
b
shortest path from c to d is direct. Thus, the b e - - - d 5
8
9
shortest path from c to e is 9
c e - - - d 6
c
c→d→e
d e c - - - 10 2 6
e
e - a b a - d

Path #6: The shortest path from vertex d to a b c d e 1 7


vertex a passes through the vertex e. The b
a - - b - - a
5
shortest path from d to e is direct. Thus, the b e - - - d 9 8
9
shortest path from d to a is 6
c
d→e→a
c e - - - d
2 6
d e c - - - e
10
d
e - a b a -

Path #7: The shortest path from vertex d to a b c d e 1 7


vertex b passes through the vertex c. Again, the b
a - - b - - 5
a
shortest path from d to b is direct. Thus, the 9 8
b e - - - d 9
shortest path from d to c is 6
c
d → b →c c e - - - d
2 6
10
d e c - - - e d
e - a b a -
Intermediate vertex Start vertex Mid vertex End vertex
Floyd-Warshall Algorithm
Shortest paths through intermediate vertices
Path #8: The shortest path from vertex e to a b c d e
1 7
vertex b passes through the vertex a. The a - - b - - a
b
shortest path from e to a is direct. Thus, the b e - - - d 5
8
9
shortest path from e to b is 9
c e - - - d 6
c
e→a→b
d e c - - - 10 2 6
e
e - a b a - d

Path #9: The last segment of the shortest path a b c d e 1 7


from vertex e to vertex c passes through the b
vertex b. Thus,
a - - b - - a
5
8
e→b→c b e - - - d 6
9
9
Again, the shortest path from e to b passes c e - - - d c
2 6
through a. Thus, the shortest path from e to c is d e c - - - 10
e d
e→ a → b → c e - a b a -

Path # 10: The shortest path from vertex e to a b c d e 1 7


vertex d passes through the vertex a. The b
a - - b - - 5
a
shortest path from e to a is direct. Thus, the 9 8
b e - - - d 9
shortest path from e to d is 6
c
e → a →d c e - - - d
2 6
10
d e c - - - e d
e - a b a -
Intermediate vertex Start vertex Mid vertex End vertex
All Pairs Shortest Distances
Recurrence
Let d ij( k) be an element of D(k). In particular, dij(0) is equal to wij of weight matrix

The element dij(k) is computed by using the entries in the matrix D(k-1). If the shortest distance is
along the direct path, then dij(k)=dij(k-1) . However, if the shortest path is through an intermediate
vertex, then dij(k)=dik(k-1)+dkj(k-1).

Direct path from vi to vj


dij(k-1)
vi vj

dik(k-1) dkj(k-1)
vk
Indirect path through vertex vk

¾ It follows that the distance dij(k) satisfies the following recurrence:


dij(0) = wij
dij(k) = minimum ( dij(k-1) , dik(k-1) + dkj(k-1) )
Floyd-Warshall Algorithm
Implementation
The following code implements the algorithm. It computes the shortest paths between the
vertices of a weighted graph. The vertices are numbered 1,2, ….n. W[i,j] is the weigh
matrix. The outer-most loop identifies the intermediate vertices through which the shortest
paths pass. The indexes of the intermediate vertices are saved in table P, which stores the
highest index of vertex along the shortest path. In other words, P stores the vertex of the last
shortest sub-path to the end vertex
SHORTEST-DISTANCE(W, n)
for i ←1 to n do
for j←1 to n do
D[i,j] ←W[i,j] ► Copy weight matrix to table D
for i ←1 to n do
for j←1 to n do
P[i,j] ←0 ► Initialize table P to hold intermediate vertices
for k ←1 to n do ►Intermediate vertices, numbered 1,2,3…n, are referenced by index k.
for i ←1 to n do ►Start vertices, numbered 1,2,3…n, are referenced by index i
for j ←1 to n do ►End vertices, numbered 1,2,3…n, are referenced by index j
if D[i, k]+D[k ,j] <D[i, j] ►Shortest path from vertex i to vertex j is via vertex k
then D[i, j] ←D[i, k] + D[k, j] ►Update the shortest distance and copy to original table
P[i, j] ←k ►Store index of intermediate vertex along the shortest path in table P
return D,P

Visualization
Printing Shortest Paths
Implementation
The following code makes recursive calls to print the vertices along the shortest path
between vertices with indexes i and j. The intermediate vertices are listed using the table P

LIST-PATH( i, j ) ► Print shortest path between vertices vi and vj


if P[i,j]≠0
then LIST-PATH ( i, P[ i, j ]) ► Intermediate vertex
Print “v” + P[ i, j ] ► Print the vertex on the shortest path
LIST-PATH ( P[ i, j ], j ) ► Make recursive call to list other intermediate vertices
return
Analysis of Floyd-Warshall Algorithm
Time and Space Complexity
The code for the implementation of Floyd-Warshall algorithm consists of the following three
nested loops, each executing n times

for k ←1 to n do ► Intermediate vertices numbered 1, 2, 3,…n


for i ←1 to n do ► Start vertices numbered 1, 2, 3,…n
for j ←1 to n do ► End vertices numbered 1, 2, 3,…n
if D[i, k]+D[k ,j] <D[i, j] ►Shortest paths via vertices numbered 1,2,..,k
then D[i, j] ←D[i, k]+D[k, j]

¾ Therefore, the time complexity, of the algorithm is


T(n)=θ(n x n x n)=θ(n3)

¾ The algorithm has space complexity of S(n) =θ(n2), because in each iteration the matrices
D(0), D(1), ….D(n) the are obtained by updating and overwriting the preceding distances in
the source matrix. Further, each matrix stores n x n distances between all pairs of graph
vertices
Dynamic Programming
Limitations
The disadvantages associated with dynamic programming are:
• There is an overhead of memory requirement to store tables for the solution of
sub problems

• The formulation of sub problem structure is difficult

• The principle of optimality must hold

You might also like