0% found this document useful (0 votes)
15 views

Floyd Warshall Report

The Floyd-Warshall algorithm is a dynamic programming approach used to find the shortest paths between all pairs of vertices in a weighted graph, capable of handling negative edge weights but not negative cycles. It operates with a time complexity of O(N³) and is particularly effective for dense graphs, making it suitable for applications in network routing and transportation systems. The algorithm iteratively updates a distance matrix based on a recurrence relation, refining the shortest path calculations through multiple iterations over the graph's vertices.

Uploaded by

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

Floyd Warshall Report

The Floyd-Warshall algorithm is a dynamic programming approach used to find the shortest paths between all pairs of vertices in a weighted graph, capable of handling negative edge weights but not negative cycles. It operates with a time complexity of O(N³) and is particularly effective for dense graphs, making it suitable for applications in network routing and transportation systems. The algorithm iteratively updates a distance matrix based on a recurrence relation, refining the shortest path calculations through multiple iterations over the graph's vertices.

Uploaded by

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

The Floyd-Warshall All Pairs Shortest Paths Algorithm

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.

2. Purpose and Motivation


The Floyd-Warshall algorithm is widely used because of its simplicity, versatility, and ability to
handle all-pairs shortest path computations efficiently. Unlike other shortest path algorithms that
require multiple executions (such as running Dijkstra’s algorithm for each vertex), Floyd-Warshall
solves APSP in a single execution, making it well-suited for scenarios where multiple shortest paths
are needed simultaneously.

Key reasons for using this algorithm include:

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.

3. Simple Matrix-Based Implementation: The algorithm uses a straightforward matrix


structure, making it easy to implement and adapt for parallel computing and high-
performance systems.

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.

3. Algorithm and Recurrence Relation of Floyd-Warshall

The Floyd-Warshall algorithm The Floyd-Warshall algorithm is based on a recurrence relation


that updates the shortest path distances iteratively. Let w ij represent the initial edge weights in the
k
adjacency matrix of the graph. Define d ⅈjas the shortest path weight from vertex i to vertex j,
0
considering only the intermediate vertices from the set {1, 2, ..., k}. Initially, d ⅈj corresponds to w ij.

The recurrence relation for the Floyd-Warshall algorithm is as follows:


k
d ⅈj = { w ij, if k = 0
(k−1) (k−1) (k−1)
min (d ⅈj , d ⅈk + d kj ), if k > 0

}
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

Algorithm: Floyd-Warshall for All-Pairs Shortest Paths


Step 1 – Construct an adjacency matrix M with edge costs. If no path exists between two vertices,
set the value as ∞.

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].

 Otherwise, keep M[i, j] unchanged.

 Here, k = 1 (first vertex as the pivot).

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).

// Output: The shortest path distance matrix M.

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

4. if M[i, j] > M[i, k] + M[k, j] then

5. M[i, j] ← M[i, k] + M[k, j]

6. end if

7. end for

8. end for

9. end for

10. Return M

4. Operation of Pseudocode
The algorithm takes two inputs:

1. N: The number of vertices in the graph.

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

This is the outer loop, iterating over each vertex k from 1 to N.

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).

i is the starting point of the paths being evaluated.

Line 3: for j = 1 to N do

This inner loop iterates over all possible destination vertices j (from 1 to N).

j is the endpoint of the paths being evaluated.

Together, the i and j loops ensure every pair of vertices (i, j) is checked.

Line 4: if M[i, j] > M[i, k] + M[k, j] then

M[i, j]: The current known distance from i to j.

M[i, k] + M[k, j]: The distance of a potential path from i to j via k (i.e., i → k → j).

The algorithm compares these two values:

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.

Line 5: M[i, j] ← M[i, k] + M[k, j]

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

Closes the conditional block. If no improvement is found, M[i, j] remains unchanged.

Lines 7-9: Closing the Loops

The inner j loop ends, moving to the next destination.

The middle i loop ends, moving to the next source.

The outer k loop ends, moving to the next intermediate vertex.


Line 10: end procedure

The algorithm finishes when all k values (1 to ,N) have been considered.

Returns the final shortest path matrix M.

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:

1. Vertex 1 to Vertex 2: weight 4

2. Vertex 1 to Vertex 3: weight 10

3. Vertex 2 to Vertex 3: weight 3

4. Vertex 3 to Vertex 4: weight 2

5. Vertex 4 to Vertex 2: weight 5

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

1. A[1, 2] = 4 means the direct edge from 1 to 2 has weight 4.

2. A[1, 4] = ∞ means no direct edge from 1 to 4.

3. And so on.

Step 2: Apply the Floyd-Warshall Algorithm

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[i, j], and updates A[i, j] if true.

Initial Matrix (Before Algorithm Starts):

A=

1 2 3 4
1 0 4 10 ∞
2 ∞ 0 3 ∞
3 ∞ ∞ 0 2
4 ∞ 5 ∞ 0

Iteration 1: k = 1 (Intermediate Vertex = 1)

1. Check all pairs (i, j) to see if going through vertex 1 improves the path.

2. Formula: if A[i, j] > A[i, 1] + A[1, j], update A[i, j].

3. For i = 2, j = 3:

4. A[2, 3] = 3, A[2, 1] + A[1, 3] = ∞ + 10 = ∞.


5. 3 < ∞, no update.

6. For i = 4, j = 2:

7. A[4, 2] = 5, A[4, 1] + A[1, 2] = ∞ + 4 = ∞.

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

(No changes, as vertex 1 doesn’t yet help shorten paths.)

Iteration 2: k = 2 (Intermediate Vertex = 2)

1. Check if going through vertex 2 improves any paths.

2. Formula: if A[i, j] > A[i, 2] + A[2, j].

3. For i = 1, j = 3:

4. A[1, 3] = 10, A[1, 2] + A[2, 3] = 4 + 3 = 7.

5. 10 > 7, update A[1, 3] = 7 (path: 1 → 2 → 3).

6. For i = 4, j = 3:

7. A[4, 3] = ∞, A[4, 2] + A[2, 3] = 5 + 3 = 8.

8. ∞ > 8, update A[4, 3] = 8 (path: 4 → 2 → 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

Iteration 3: k = 3 (Intermediate Vertex = 3)

1. Check if going through vertex 3 improves paths.

2. Formula: if A[i, j] > A[i, 3] + A[3, j].

3. For i = 1, j = 4:

4. A[1, 4] = ∞, A[1, 3] + A[3, 4] = 7 + 2 = 9.

5. ∞ > 9, update A[1, 4] = 9 (path: 1 → 2 → 3 → 4).

6. For i = 2, j = 4:

7. A[2, 4] = ∞, A[2, 3] + A[3, 4] = 3 + 2 = 5.

8. ∞ > 5, update A[2, 4] = 5 (path: 2 → 3 → 4).

9. For i = 4, j = 4:

10. A[4, 4] = 0, A[4, 3] + A[3, 4] = 8 + 2 = 10.

11. 0 < 10, no update.

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

Iteration 4: k = 4 (Intermediate Vertex = 4)

1. Check if going through vertex 4 improves paths.


2. Formula: if A[i, j] > A[i, 4] + A[4, j].

3. For i = 1, j = 2:

4. A[1, 2] = 4, A[1, 4] + A[4, 2] = 9 + 5 = 14.

5. 4 < 14, no update.

6. For i = 2, j = 2:

7. A[2, 2] = 0, A[2, 4] + A[4, 2] = 5 + 5 = 10.

8. 0 < 10, no update.

9. For i = 3, j = 2:

10. A[3, 2] = ∞, A[3, 4] + A[4, 2] = 2 + 5 = 7.

11. ∞ > 7, update A[3, 2] = 7 (path: 3 → 4 → 2).

12. For i = 3, j = 3:

13. A[3, 3] = 0, A[3, 4] + A[4, 3] = 2 + 8 = 10.

14. 0 < 10, no update.

Final Matrix after k = 4:

A=

1 2 3 4
1 0 4 7 9
2 ∞ 0 3 5
3 ∞ 7 0 2
4 ∞ 5 8 0

Step 3: Interpret the Final Matrix

The final matrix A gives the shortest path distances between all pairs of vertices:

1. A[1, 2] = 4: Shortest path from 1 to 2 is 4 (direct edge).

2. A[1, 3] = 7: Shortest path from 1 to 3 is 7 (via 1 → 2 → 3).

3. A[1, 4] = 9: Shortest path from 1 to 4 is 9 (via 1 → 2 → 3 → 4).


4. A[2, 4] = 5: Shortest path from 2 to 4 is 5 (via 2 → 3 → 4).

5. A[3, 2] = 7: Shortest path from 3 to 2 is 7 (via 3 → 4 → 2).

6. A[4, 3] = 8: Shortest path from 4 to 3 is 8 (via 4 → 2 → 3).

7. ∞ indicates no path exists (e.g., no way to get from 2 to 1).

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).

Time and Space Complexity

1. Time Complexity: O(V³), where (V) is the number of vertices.

The algorithm has three nested loops:

Outer loop for selecting an intermediate vertex (k) → runs n times.

Middle loop for selecting a source vertex (i) → runs n times.

Inner loop for selecting a destination vertex (j) → runs n times.

O(n) X O(n) X O(n) = O(n³)

2. Space Complexity: The algorithm uses a distance matrix of size V × V, so the space
complexity is O(V²).

Applications of Floyd-Warshall Algorithm

1. Network Routing – Used in computer networks (e.g., Internet routing protocols) to


determine the shortest paths between routers.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy