Floyd Warshall Report
Floyd Warshall Report
1. Introduction to Topic
The Floyd-Warshall algorithm is a well-known graph algorithm used to solve the All-Pairs Shortest
Path (APSP) problem in weighted graphs. Unlike single-source shortest path (SSSP) algorithms such
as Dijkstra’s algorithm and Bellman-Ford algorithm, which compute the shortest paths from a
single source vertex, Floyd-Warshall computes the shortest distances between every pair of
vertices in the graph. The algorithm follows a dynamic programming approach, iteratively refining
a distance matrix to determine the shortest possible paths.
The Floyd-Warshall algorithm is widely used in various fields, including network routing, urban
transportation systems, and computational biology, where analyzing shortest paths between
multiple points is crucial. It is particularly effective for dense graphs, where most vertices are
connected by edges. However, its O(N³) time complexity makes it computationally expensive for
large and sparse graphs, leading to the development of optimized versions that reduce unnecessary
computations. Despite its limitations, the simplicity and systematic approach of Floyd-Warshall
make it a preferred choice for APSP problems in many applications.
1. Comprehensive Path Calculation: It computes shortest paths between all pairs of vertices,
making it suitable for applications requiring complete path analysis.
2. Handles Negative Edge Weights: Unlike Dijkstra’s algorithm, Floyd-Warshall can process
graphs with negative-weight edges, provided there are no negative cycles.
4. Applicability in Dense Graphs: Floyd-Warshall performs well in dense graphs, where most
vertices are interconnected, ensuring efficient shortest path calculations.
5. Wide Range of Applications: It is commonly used in network routing, traffic optimization,
and scientific computing, where determining the shortest paths between multiple points is
essential.
Despite its O(N³) complexity, the Floyd-Warshall algorithm remains a foundational approach for
shortest path computations and serves as a benchmark for evaluating improvements in APSP
algorithms.
}
k
We use the notation D k to represent the matrix corresponding to d ⅈj, which stores the shortest path
distances at the kth iteration. The recurrence relation essentially determines the order in which the
relaxation operation is performed.
Like other shortest path algorithms, the main operation of this recurrence relation is the relaxation
step:
if (d ik + d kj < d ij ) then d ij ←d ik + d kj
Step 2 – Create a new matrix M₁ from M, keeping the first row and first column unchanged. For
remaining values, update M₁[i, j] as:
If M[i, j] > M[i, k] + M[k, j], set M₁[i, j] = M[i, k] + M[k, j].
Step 3 – Repeat Step 2 for all vertices, updating matrices with different pivot values until the final
matrix is achieved.
Step 4 – The final adjacency matrix contains the shortest paths between all pairs of vertices.
Pseudocode:
//Problem Description: Find the shortest paths between all pairs of vertices in
a //weighted, directed graph (with possible negative weights but no negative
cycles).
//Input: A |N| × |N| adjacency matrix M representing edge weights (with ∞ for no
direct edge).
Algorithm FloydWarshall(M, N)
1. for k ← 1 to N do
2. for i ← 1 to N do
3. for j ← 1 to N do
6. end if
7. end for
8. end for
9. end for
10. Return M
4. Operation of Pseudocode
The algorithm takes two inputs:
2. M[1..N, 1..N]: An adjacency matrix where M[i, j] represents the initial weight of the edge
from vertex i to vertex j. If no direct edge exists, M[i, j] is typically infinity (∞), and M[i, i] = 0
(distance from a vertex to itself).
Line-by-Line Operation
Line 1: for k = 1 to N do
k represents the intermediate vertex being considered. The algorithm checks whether using k
as a "middle point" can shorten paths between any pair of vertices.
Line 2: for i = 1 to N do
This middle loop iterates over all possible source vertices i (from 1 to N).
Line 3: for j = 1 to N do
This inner loop iterates over all possible destination vertices j (from 1 to N).
Together, the i and j loops ensure every pair of vertices (i, j) is checked.
M[i, k] + M[k, j]: The distance of a potential path from i to j via k (i.e., i → k → j).
If the path via k is shorter (M[i, j] > M[i, k] + M[k, j]), it means a better route exists.
Special case: If M[i, j] = ∞ (no path) and M[i, k] + M[k, j] is finite, a new path is discovered.
If the condition is true, update M[i, j] with the shorter distance found by going through k.
This "relaxes" the path from i to j, replacing the old distance with the new, shorter one.
Line 6: end if
The algorithm finishes when all k values (1 to ,N) have been considered.
The above pseudo code explains how the algorithm iteratively updates the shortest path between
all pairs of vertices in a given graph using an adjacency matrix. It considers each vertex as an
intermediate point and checks if a path through this vertex offers a shorter distance between any
two vertices. If a shorter path is found, the matrix is updated accordingly. The process repeats for
all intermediate vertices, updating the matrix until it holds the shortest distances between all pairs.
The algorithm efficiently finds these paths by refining distances through dynamic programming.
5. Numerical Example
Step 1: Define the Graph
Imagine a graph with 4 vertices labelled 1, 2, 3, and 4. The initial adjacency matrix A represents the
weights of direct edges between vertices. If there’s no direct edge, we use infinity (∞). For
simplicity, assume the following weighted directed graph:
6. The initial adjacency matrix A (with 0s on the diagonal, as the distance from a vertex to
itself is 0) looks like this:
A=
1 2 3 4
1 0 4 10 ∞
2 ∞ 0 3 ∞
3 ∞ ∞ 0 2
4 ∞ 5 ∞ 0
3. And so on.
The pseudocode iterates over k (intermediate vertex), i (source vertex), and j (destination vertex).
For each combination, it checks if the path i → k → j is shorter than the current
A=
1 2 3 4
1 0 4 10 ∞
2 ∞ 0 3 ∞
3 ∞ ∞ 0 2
4 ∞ 5 ∞ 0
1. Check all pairs (i, j) to see if going through vertex 1 improves the path.
3. For i = 2, j = 3:
6. For i = 4, j = 2:
8. 5 < ∞, no update.
9. Check all other pairs similarly; no updates occur because A[i, 1] or A[1, j] is often ∞.
Matrix after k = 1:
A=
1 2 3 4
1 0 4 10 ∞
2 ∞ 0 3 ∞
3 ∞ ∞ 0 2
4 ∞ 5 ∞ 0
3. For i = 1, j = 3:
6. For i = 4, j = 3:
9. Other pairs: No further updates (e.g., A[1, 4] = ∞ > A[1, 2] + A[2, 4] = 4 + ∞ = ∞).
Matrix after k = 2:
A=
1 2 3 4
1 0 4 7 ∞
2 ∞ 0 3 ∞
3 ∞ ∞ 0 2
4 ∞ 5 8 0
3. For i = 1, j = 4:
6. For i = 2, j = 4:
9. For i = 4, j = 4:
Matrix after k = 3:
A=
1 2 3 4
1 0 4 7 9
2 ∞ 0 3 5
3 ∞ ∞ 0 2
4 ∞ 5 8 0
3. For i = 1, j = 2:
6. For i = 2, j = 2:
9. For i = 3, j = 2:
12. For i = 3, j = 3:
A=
1 2 3 4
1 0 4 7 9
2 ∞ 0 3 5
3 ∞ 7 0 2
4 ∞ 5 8 0
The final matrix A gives the shortest path distances between all pairs of vertices:
This example demonstrates how Floyd-Warshall systematically builds the shortest paths, even
discovering indirect routes (like 1 → 2 → 3 → 4) that weren’t obvious in the initial matrix.
6. Conclusion
The Floyd-Warshall algorithm is a powerful solution for APSP problems, particularly in dense
graphs where all-pairs shortest path computation is necessary. However, for sparse graphs,
algorithms like Dijkstra’s or Bellman-Ford may be more efficient. Despite its cubic time
complexity, it remains widely used due to its simplicity and ability to handle both positive and
negative edge weights (except negative weight cycles).
2. Space Complexity: The algorithm uses a distance matrix of size V × V, so the space
complexity is O(V²).
2. Flight and Transportation Networks – Helps in finding the shortest travel paths
between multiple cities or locations.
3. Social Network Analysis – Determines the shortest connections between users in social
media graphs.
4. Graph Analysis in AI – Used in robotics and AI for path planning and decision-making.
5. Traffic and Urban Planning – Assists in optimizing traffic flow by computing shortest
paths in road networks.