Understand Floyd Warshall Algorithm Problem

Problem Name: Floyd Warshall Algorithm
Problem Description:

Floyd‑Warshall Algorithm

The Floyd‑Warshall algorithm is a dynamic programming approach used to find the shortest paths between all pairs of vertices in a weighted graph. It works for graphs with both positive and negative edge weights (provided there are no negative weight cycles) and has a time complexity of  (O(V3) \ (O(V^3)


How It Works

The algorithm maintains a 2D array dist where:

  • ( dist[i][j] ) represents the shortest distance from vertex (i) to vertex (j).

Initially, dist[i][j] is set to the weight of the edge from (i) to (j) if such an edge exists; otherwise, it is set to infinity (often denoted by \infty) . The diagonal elements are zero since the distance from any vertex to itself is zero.

For each vertex (k) (considered as an intermediate vertex), the algorithm updates the distance between every pair of vertices (i) and (j) using the recurrence relation:

dist[i][j]=min(dist[i][j],  dist[i][k]+dist[k][j])\text{dist}[i][j] = \min \Big( \text{dist}[i][j],\; \text{dist}[i][k] + \text{dist}[k][j] \Big)

Example

Consider a graph with 4 vertices (0, 1, 2, 3) and the following initial distance matrix:

Initial dist:

[051003010]\begin{bmatrix} 0 & 5 & \infty & 10 \\ \infty & 0 & 3 & \infty \\ \infty & \infty & 0 & 1 \\ \infty & \infty & \infty & 0 \\ \end{bmatrix}

There is an edge from 0 to 1 with weight 5.
There is an edge from 0 to 3 with weight 10.
There is an edge from 1 to 2 with weight 3.
There is an edge from 2 to 3 with weight 1.


Step-by-Step Updates

For (k = 0):
Consider paths going through vertex 0.
No update occurs because vertex 0 is the source for its outgoing edges.


For (k = 1):
Consider paths via vertex 1.

  • Update:
    Update (dist[0][2]\text{dist}[0][2]) using the path 012  0 \rightarrow 1 \rightarrow 2\ :
    dist[0][2]=min(,5+3)=8.\text{dist}[0][2] = \min(\infty,\, 5 + 3) = 8.
  • Updated Matrix:
[0581003010]\begin{bmatrix} 0 & 5 & 8 & 10 \\ \infty & 0 & 3 & \infty \\ \infty & \infty & 0 & 1 \\ \infty & \infty & \infty & 0 \\ \end{bmatrix}

For (k = 2):
Consider paths via vertex 2.

  • Update:
    Update ( \text{dist}[0][3] ) using the path (0 \rightarrow 1 \rightarrow 2 \rightarrow 3):
    dist[0][3]=min(10,8+1)=9.\text{dist}[0][3] = \min(10,\, 8 + 1) = 9.
  • Updated Matrix:
[058903010]\begin{bmatrix} 0 & 5 & 8 & 9 \\ \infty & 0 & 3 & \infty \\ \infty & \infty & 0 & 1 \\ \infty & \infty & \infty & 0 \\ \end{bmatrix}

For (k = 3):
Consider paths via vertex 3.
No further improvements are possible.


Final Result

The final shortest path distance matrix is:

dist=[058903010]\text{dist} = \begin{bmatrix} 0 & 5 & 8 & 9 \\ \infty & 0 & 3 & \infty \\ \infty & \infty & 0 & 1 \\ \infty & \infty & \infty & 0 \\ \end{bmatrix}

This matrix tells us, for example, that the shortest distance from vertex 0 to vertex 3 is 9.


Summary

The Floyd‑Warshall algorithm efficiently computes the shortest paths between every pair of vertices by iteratively considering each vertex as an intermediate point. It is particularly useful for dense graphs or when multiple shortest path queries are needed.

Category:
  • Graphs
Programming Language:
  • Java
Reference Link:

https://drawtocode.vercel.app/problems/floyd-warshall-algorithm

Online IDE

Scroll down for output
Java
Output:

Loading component...

Loading component...

Tracking code (View only. In case you want to track the code, click this button):
Main Function:

INPUT: dist = { {0, 5, INF, 10}, {INF, 0, 3, INF}, {INF, INF, 0, 1}, {INF, INF, INF, 0} };

OUTPUT: { { 0 5 8 9 } { INF 0 3 4 } { INF INF 0 1 } { INF INF INF 0 } }

public static void main(String[] args) {

int V = 4;

int[][] dist = { {0, 5, INF, 10}, {INF, 0, 3, INF}, {INF, INF, 0, 1}, {INF, INF, INF, 0} };

for (int k = 0; k < V; k++) {

for (int i = 0; i < V; i++) {

for (int j = 0; j < V; j++) {

if (dist[i][k] != INF && dist[k][j] != INF && dist[i][k] + dist[k][j] < dist[i][j]) {

dist[i][j] = dist[i][k] + dist[k][j];

}//If End

}//Loop End

}//Loop End

}//Loop End

System.out.println("Shortest distances between every pair of vertices:");

for (int i = 0; i < V; i++) {

for (int j = 0; j < V; j++) {

if (dist[i][j] == INF){

System.out.print("INF ");

}//If End

else {

System.out.print(dist[i][j] + " ");

}//Else End

}//Loop End

System.out.println();

}//Loop End

}//function end

Helper Function:

Helper Function is not defined.

Utility Functions and Global variables:

static final int INF = 99999;