Floyd’s Algorithm
(shortest-path problem)
Section 8.2
Shortest-path Problem
► Given:
►A
set of objects (called vertices) and
► A set of distances between objects (called edges)
► Find:
► The shortest path from a designated starting point
► The shortest path between any pair of vertices
Shortest-path Problem
► Given:
►A
set of objects (called vertices) and
► A set of distances between objects (called edges)
► Find:
► The shortest path from a designated starting point
► The shortest path between any pair of vertices
► Floyd’s algorithm solves this problem
Shortest-path Problem
► Given:
►A
set of objects (called vertices) and
► A set of distances between objects (called edges)
► Find:
► The shortest path from a designated starting point
► BTW, Dijkstra’s algorithm solves this one
► The shortest path between any pair of vertices
Floyd’s Algorithm
► Dynamic
Programming
Algorithm
► It uses an N X N
matrix
► N is the # of vertices
► BTW, Pink Floyd’s The
Wall is great movie to
watch if you want to…
Floyd’s Algorithm
► Recall
matrix
a graph can be represented using an N X N
8
A
B
3
1
4
1
D
2
C
1
8
E
A
B
C
D
E
A
B
C
D
E
0
8
3
1
8
0
4
2
3
4
0
1
1
1
1
0
8
2
1
8
0
Floyd’s Algorithm
► Dynamic
Programming
Solves one step of the problem
Stores it, so it doesn’t have to be recomputed
Uses stored step to solve the next step
Avoids re-computing progressive steps
► Many Algorithms (Brute Force, even Divide-and
Conquer) re-compute the same information over
and over again.
Floyd’s Algorithm
► Dynamic
Programming
Uses extra memory to store steps (or subproblems)
Avoids re-computation
However, the extra memory can be
expensive.
Floyd’s Algorithm
►
Famous Example of Dynamic Programming
Optimal sequence alignment algorithm
►Most famous version is called the Smith-Waterman
Algorithm
Align two sequences of length N
►Previous algorithm O(2N) comparisons O(N) memory
►Smith-Waterman Algorithm O(N2) comparisons O(N2)
memory.
In this case the trade-off was worth it.
Floyd’s Algorithm
► Back
to the shortest-path problem.
8
A
B
3
1
4
1
D
2
C
1
8
E
A
B
C
D
E
A
B
C
D
E
0
8
3
1
8
0
4
2
3
4
0
1
1
1
1
0
8
2
1
8
0
Floyd’s Algorithm
►
►
First step, where can we go without
using an intermediate vertex
Mk, where k the set of intermediate
vertices
8
A
1
4
1
D
2
C
1
8
E
B
C
D
E
A
0
8
3
1
B
8
0
4
2
C
3
4
0
1
1
D
1
1
0
8
E
2
8
0
1
M{}
B
3
A
A
B
C
D
E
A
0
8
3
1
B
8
0
4
2
C
3
4
0
1
1
D
1
1
0
8
E
2
1
8
0
Floyd’s Algorithm
►
Second step, where can we go if we
use A as an intermediate vertices
8
A
1
4
1
D
2
C
1
8
E
B
C
D
E
A
0
8
3
1
B
8
0
4
2
C
3
4
0
1
1
D
1
1
0
8
E
2
8
0
1
M{A}
B
3
A
A
B
C
D
E
A
0
8
3
1
B
8
0
4
9
2
C
3
4
0
1
1
D
1
9
1
0
8
E
2
1
8
0
Floyd’s Algorithm
►
Third step, where can we go if we use
A and B as intermediate vertices
8
A
1
4
1
D
2
C
1
8
E
B
C
D
E
A
0
8
3
1
B
8
0
4
2
C
3
4
0
1
1
D
1
1
0
8
E
2
8
0
1
M{A,B}
B
3
A
A
B
C
D
E
A
0
8
3
1
10
B
8
0
4
9
2
C
3
4
0
1
1
D
1
9
1
0
8
E
10
2
1
8
0
Floyd’s Algorithm
►
►
Fourth step, where can we go if we use
A, B, and C as intermediate vertices
You tell me!
8
A
1
4
1
D
2
C
1
8
E
B
C
D
E
A
0
8
3
1
B
8
0
4
2
C
3
4
0
1
1
D
1
1
0
8
E
2
8
0
1
M{A,B,C}
B
3
A
A
B
C
D
E
A
0
8
3
1
10
B
8
0
4
9
2
C
3
4
0
1
1
D
1
9
1
0
8
E
10
2
1
8
0
Floyd’s Algorithm
►
Fourth step, where can we go if we use
A, B, and C as intermediate vertices
8
A
1
4
1
D
2
C
1
8
E
B
C
D
E
A
0
8
3
1
B
8
0
4
2
C
3
4
0
1
1
D
1
1
0
8
E
2
8
0
1
M{A,B,C}
B
3
A
A
B
C
D
E
A
0
7
3
1
4
B
7
0
4
5
2
C
3
4
0
1
1
D
1
5
1
0
2
E
4
2
1
2
0
Floyd’s Algorithm
►
►
Fifth step, where can we go if we use
A, B, C, and D as intermediate vertices
You tell me!
8
A
1
4
1
D
2
C
1
8
E
B
C
D
E
A
0
8
3
1
B
8
0
4
2
C
3
4
0
1
1
D
1
1
0
8
E
2
8
0
1
M{A,B,C,D}
B
3
A
A
B
C
D
E
A
0
7
2
1
4
B
7
0
4
5
2
C
2
4
0
1
1
D
1
5
1
0
2
E
4
2
1
2
0
Floyd’s Algorithm
►
►
Fifth step, where can we go if we use
A, B, C, and D as intermediate vertices
OK, here is the answer.
8
A
1
4
1
D
2
C
1
8
E
B
C
D
E
A
0
8
3
1
B
8
0
4
2
C
3
4
0
1
1
D
1
1
0
8
E
2
8
0
1
M{A,B,C,D}
B
3
A
A
B
C
D
E
A
0
6
2
1
3
B
6
0
4
5
2
C
2
4
0
1
1
D
1
5
1
0
2
E
3
2
1
2
0
Floyd’s Algorithm
►
►
Final step, where can we go if we use
all the vertices as intermediates.
You tell me!
8
A
1
4
1
D
2
C
1
8
E
B
C
D
E
A
0
8
3
1
B
8
0
4
2
C
3
4
0
1
1
D
1
1
0
8
E
2
8
0
1
M{A,B,C,D,E}
B
3
A
A
B
C
D
E
A
0
6
2
1
3
B
6
0
4
5
2
C
2
4
0
1
1
D
1
5
1
0
2
E
3
2
1
2
0
Floyd’s Algorithm
►
►
Final step, where can we go if we use
all the vertices as intermediates.
You tell me!
8
A
1
4
1
D
2
C
1
8
E
B
C
D
E
A
0
8
3
1
B
8
0
4
2
C
3
4
0
1
1
D
1
1
0
8
E
2
8
0
1
M{A,B,C,D,E}
B
3
A
A
B
C
D
E
A
0
5
2
1
3
B
5
0
3
5
2
C
2
3
0
1
1
D
1
5
1
0
2
E
3
2
1
2
0
Floyd’s Algorithm
► So
how do we actually compute (update)
the matrix?
► First of all…
► Given N vertices how many steps are there?
► i.e., how many times must the matrix be
updated?
Floyd’s Algorithm
► for
(int k = 0; k < N; k++)
Update the matrix, i.e. compute M{k}
► Next…
► How
would you actually update each matrix
cell?
► i.e., how would you iterate through the
matrix?
Floyd’s Algorithm
8
A
B
3
1
4
1
D
2
C
1
8
E
A
B
C
D
E
A
0
8
3
1
B
8
0
4
2
C
3
4
0
1
1
D
1
1
0
8
E
2
1
8
0
Allowing no hops…M[D][E] = 8
What if we allow a hop through C?
M[D][C] = 1 and M[C][E] = 1
If we allow a hop through C, what can we compare to see if
M[D][E] needs to be updated?
Floyd’s Algorithm
► for
(int k = 0; k < N; k++)
for (int i = 0; i < N; i++)
►for (int j = 0; j < N; j++)
update M{k}[i][j]
► What
do we have to compare to see if M{k}[i][j]
needs to be updated?
Floyd’s Algorithm
► for
(int k = 0; k < N; k++)
for (int i = 0; i < N; i++)
►for (int j = 0; j < N; j++)
if (M[i][k]+M[k][j] < M[i][j]) then
► How
do we do the update?
Floyd’s Algorithm
► for
(int k = 0; k < N; k++)
for (int i = 0; i < N; i++)
►for (int j = 0; j < N; j++)
if (M[i][k]+M[k][j] < M[i][j]) then
►M[i][j] = M[i][k]+M[k][j]
© Copyright 2026 Paperzz