Floyd Warshall Algorithm – Floyd Warshall Algorithm in Data Structure – Floyd Warshall Algorithm in Graph Theory

Floyd-Warshall Algorithm

The Floyd-Warshall Algorithm is an efficient way to find the shortest paths between all pairs of nodes in a weighted graph. It works for both directed and undirected graphs and can handle negative edge weights (but not negative cycles).

Key Features:

  1. All-Pairs Shortest Paths:
    Unlike Dijkstra or Bellman-Ford (which find shortest paths from a single source), Floyd-Warshall finds the shortest paths between every pair of vertices.
  2. Dynamic Programming Approach:
    It progressively updates the shortest path between pairs of vertices by considering intermediate nodes.
  3. Handles Negative Weights:
    It works with negative edge weights, but if there’s a negative weight cycle, the algorithm can detect it.

Time Complexity:

  • O(V³), where V is the number of vertices. This makes it efficient for dense graphs or when we need all-pairs shortest paths.

Algorithm Steps:

  1. Initialization:
    • Create a distance matrix dist[][] where dist[i][j] represents the shortest distance from node i to node j.
    • If there’s no direct edge between i and j, set dist[i][j] = ∞ (infinity).
    • For self-loops (distance from a node to itself), set dist[i][i] = 0.
  2. Iterative Update:
    For every node k (considered as an intermediate node):
    • Update the shortest distance between nodes i and j by checking if the path i → k → j is shorter than the current i → j.
    Formula: dist[i][j] = min⁡(dist[i][j], dist[i][k]+dist[k][j])
  3. Repeat for all nodes k, until no more updates can be made.

Example:

1. Initial Distance Matrix:

2. Update the Matrix Using Intermediate Nodes:

Using A as intermediate: No updates (since A doesn’t reduce any paths).

For each pair (i, j), we check if the path through A is shorter.

Checking all pairs:

From (i)To (j)Current dist[i][j]Via A (dist[i][A] + dist[A][j])Update?
AA00 + 0 = 0No change
AB30 + 3 = 3No change
AC100 + 10 = 10No change
AD0 + ∞ = ∞No change
BA∞ + 0 = ∞No change
BB0∞ + 3 = ∞No change
BC1∞ + 10 = ∞No change
BD∞ + ∞ = ∞No change
CA∞ + 0 = ∞No change
CB∞ + 3 = ∞No change
CC0∞ + 10 = ∞No change
CD2∞ + ∞ = ∞No change
DA∞ + 0 = ∞No change
DB-4∞ + 3 = ∞No change
DC∞ + 10 = ∞No change
DD0∞ + ∞ = ∞No change

Using B as intermediate:

For each pair (i, j), we check if the path through B is shorter.

Checking all pairs:

From (i)To (j)Current dist[i][j]Via B (dist[i][B] + dist[B][j])Update?
AA03 + ∞ = ∞No change
AB33 + 0 = 3No change
AC103 + 1 = 4Update (10 → 4)
AD3 + ∞ = ∞No change
BA0 + ∞ = ∞No change
BB00 + 0 = 0No change
BC10 + 1 = 1No change
BD0 + ∞ = ∞No change
CA∞ + ∞ = ∞No change
CB∞ + 0 = ∞No change
CC0∞ + 1 = ∞No change
CD2∞ + ∞ = ∞No change
DA-4 + ∞ = ∞No change
DB-4-4 + 0 = -4No change
DC-4 + 1 = -3Update (∞ → -3)
DD0-4 + ∞ = ∞No change

Updated Distance Matrix After Using B as Intermediate

From (i) → To (j)ABCD
A034
B01
C02
D-4-30

Using C as intermediate:

For each pair (i, j), we check if the path through C is shorter.

Checking all pairs:

Pair (i → j)Current DistanceVia C (dist[i][C] + dist[C][j])Updated?
A → A04+∞=∞4 + ∞ = ∞4+∞=∞❌ No update
A → B34+∞=∞4 + ∞ = ∞4+∞=∞❌ No update
A → C44+0=44 + 0 = 44+0=4❌ No update
A → D4+2=64 + 2 = 64+2=6Update to 6
B → A1+∞=∞1 + ∞ = ∞1+∞=∞❌ No update
B → B01+∞=∞1 + ∞ = ∞1+∞=∞❌ No update
B → C11+0=11 + 0 = 11+0=1❌ No update
B → D1+2=31 + 2 = 31+2=3Update to 3
C → A0+∞=∞0 + ∞ = ∞0+∞=∞❌ No update
C → B0+∞=∞0 + ∞ = ∞0+∞=∞❌ No update
C → C00+0=00 + 0 = 00+0=0❌ No update
C → D20+2=20 + 2 = 20+2=2❌ No update
D → A−3+∞=∞-3 + ∞ = ∞−3+∞=∞❌ No update
D → B-4−3+∞=∞-3 + ∞ = ∞−3+∞=∞❌ No update
D → C-3−3+0=−3-3 + 0 = -3−3+0=−3❌ No update
D → D0−3+2=−1-3 + 2 = -1−3+2=−1❌ No update

Updated Distance Matrix After Using C as Intermediate

ABCD
A0346
B013
C02
D-4-30

Using D as intermediate:

For each pair (i, j), we check if the path through D is shorter.

Checking all pairs:

Pair (i → j)Current DistanceVia D (dist[i][D] + dist[D][j])Updated?
A → A06+∞=∞6 + ∞ = ∞6+∞=∞❌ No update
A → B36+(−4)=26 + (-4) = 26+(−4)=2Update to 2
A → C46+(−3)=36 + (-3) = 36+(−3)=3Update to 3
A → D66+0=66 + 0 = 66+0=6❌ No update
B → A3+∞=∞3 + ∞ = ∞3+∞=∞❌ No update
B → B03+(−4)=−13 + (-4) = -13+(−4)=−1Update to -1
B → C13+(−3)=03 + (-3) = 03+(−3)=0Update to 0
B → D33+0=33 + 0 = 33+0=3❌ No update
C → A2+∞=∞2 + ∞ = ∞2+∞=∞❌ No update
C → B2+(−4)=−22 + (-4) = -22+(−4)=−2Update to -2
C → C02+(−3)=−12 + (-3) = -12+(−3)=−1Update to -1
C → D22+0=22 + 0 = 22+0=2❌ No update
D → A0+∞=∞0 + ∞ = ∞0+∞=∞❌ No update
D → B-40+(−4)=−40 + (-4) = -40+(−4)=−4❌ No update
D → C-30+(−3)=−30 + (-3) = -30+(−3)=−3❌ No update
D → D00+0=00 + 0 = 00+0=0❌ No update

Updated Distance Matrix After Using D as an Intermediate

ABCD
A0236
B-103
C-2-12
D-4-30

3. Final Distance Matrix (All-Pairs Shortest Paths):

Detecting Negative Cycles:

To check if a negative weight cycle exists in the graph, we need to look at the diagonal elements of the final distance matrix.

  • If dist[i][i] < 0 for any node i, it means there’s a negative weight cycle.

Checking Diagonal Elements:

  • dist[A][A] = 0
  • dist[B][B] = -1
  • dist[C][C] = -1
  • dist[D][D] = 0

The graph contains a negative weight cycle because dist[B][B] < 0 and dist[C][C] < 0.

Negative weight cycle: B -> C -> D -> B

Note:

For V = 4 (A, B, C, D):

  • There are 4×4=16 (i, j) pairs.
  • We check all 16 pairs for each intermediate node (A, B, C, D).
  • This results in 4×16=64 checks.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top