Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
The Floyd-Warshall Algorithm
is a classic dynamic programming technique used to find the shortest
paths between all pairs of vertices in a graph. Unlike Dijkstra’s (which is
single-source), Floyd-Warshall gives you a complete distance matrix — showing
the shortest distance from every vertex to every other vertex.
This algorithm can handle negative
edge weights (but not negative cycles) and works with directed or
undirected graphs. It operates by progressively updating the shortest paths
between all pairs using an intermediate vertex k, checking if the path i → k →
j is shorter than i → j.
Though powerful, its O(V³)
time complexity makes it suitable only for small to medium-sized dense
graphs.
2. Floyd-Warshall Coding (with
2 Examples)
a. Syllabus-Oriented Example
(All-Pairs Shortest Paths)
import java.util.*;
public class FloydWarshallExample {
static
final int INF = 99999;
public
static void floydWarshall(int[][] graph, int V) {
int[][] dist = new int[V][V];
//
Initialize the solution matrix
for
(int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i][j] = graph[i][j];
//
Core logic
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] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k]
+ dist[k][j];
}
}
}
//
Output shortest path matrix
for
(int i = 0; i < V; i++) {
System.out.println(Arrays.toString(dist[i]));
}
}
public
static void main(String[] args) {
int[][] graph = {
{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}
};
floydWarshall(graph, 4);
}
}
b. Real-World Example (Network
Routing Cost Matrix)
In telecommunication or cloud
networks, Floyd-Warshall helps build a routing table that shows the least-cost
path between every router in the network.
// Imagine this is a cost matrix
between servers or routers
// The Floyd-Warshall logic
remains the same — just change node labels to represent routers (R1, R2, etc.)
import java.util.*;
public class FloydWarshallNetworkRouting {
static final
int INF = 99999;
// Mapping
router names to integer indices
static
Map<String, Integer> routerIndex = Map.of(
"R1", 0, "R2", 1, "R3", 2, "R4",
3
);
static String[]
routers = {"R1", "R2", "R3", "R4"};
public static
void floydWarshall(int[][] costMatrix) {
int V =
costMatrix.length;
int[][]
dist = new int[V][V];
//
Initialize distance matrix
for (int i
= 0; i < V; i++)
for
(int j = 0; j < V; j++)
dist[i][j] = costMatrix[i][j];
//
Floyd-Warshall logic
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] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
// Display
routing cost table
System.out.println("Routing Cost Table (All-Pairs Shortest
Paths):");
System.out.print("
");
for (String
dest : routers)
System.out.print(dest + "
");
System.out.println();
for (int i
= 0; i < V; i++) {
System.out.print(routers[i] + "
");
for
(int j = 0; j < V; j++) {
if
(dist[i][j] == INF)
System.out.print("INF ");
else
System.out.printf("%3d ", dist[i][j]);
}
System.out.println();
}
}
public static
void main(String[] args) {
int[][]
networkGraph = {
{0, 5, INF, 10},
{INF, 0, 3,
INF},
{INF,
INF, 0, 1},
{INF,
INF, INF, 0}
};
floydWarshall(networkGraph);
}
}
// For practical use, apply node
mapping from router names to integer indices and back
Real-world Floyd-Warshall
implementations often include a path reconstruction matrix to also
return the actual paths, not just distances.
3. Pros of Floyd-Warshall
Algorithm
4. Cons of Floyd-Warshall
Algorithm
5. How Floyd-Warshall Is
Better Than Other Algorithms
6. Best-Case Scenarios to Use
Floyd-Warshall
7. Applications of
Floyd-Warshall Algorithm
8. Worst-Case Scenarios to Use
Floyd-Warshall
BFS (Breadth-First Search) explores a graph level-by-level
using a queue, which makes it ideal for finding the shortest path in unweighted
graphs or for level-order traversal. On the other hand, DFS (Depth-First
Search) dives deep into each branch before backtracking using recursion or a
stack, which is better suited for problems like cycle detection, topological
sorting, or solving mazes. BFS is more memory-intensive for wide graphs, while
DFS may hit stack limits in deep graphs.
Bellman-Ford is the preferred choice for graphs with negative edge weights because it relaxes all edges up to V-1 times and can detect negative weight cycles. Dijkstra's Algorithm assumes that once a node's shortest path is found, it won't change—which fails when a negative edge later offers a better path. Hence, Dijkstra produces incorrect results in graphs with negative weights.
No, BFS and DFS do not work correctly for weighted graphs
when computing the shortest path unless all edges have the same weight (in
which case BFS can be used). In weighted graphs, algorithms like Dijkstra or
Bellman-Ford should be used to account for varying edge weights.
Yes,
typically. Dijkstra’s Algorithm (especially with a min-priority queue) has a
time complexity of O((V + E) log V) using a binary heap, which is significantly
faster than Bellman-Ford’s O(V × E) in dense graphs.
However,
Dijkstra is restricted to graphs with non-negative weights, while Bellman-Ford
is more versatile in terms of weight handling but slower in execution.
Floyd-Warshall does not explicitly detect negative cycles but can indirectly do so. If any diagonal element dist[i][i] becomes negative, it indicates a negative weight cycle. In contrast, Bellman-Ford directly checks for cycle presence after the V-1 iterations, making it more transparent and suitable for single-source shortest path detection with cycle checking.
An adjacency matrix uses O(V²) space regardless of edge count, which is inefficient for sparse graphs. Adjacency lists use O(V + E) space, making them more space-efficient and faster for traversals in sparse graphs. Most graph algorithms like BFS, DFS, Dijkstra, and Bellman-Ford perform better with adjacency lists in large, sparse graphs.
Floyd-Warshall is preferred when the graph is dense and edge weights may be negative (but no negative cycles), as it provides a clean O(V³) solution for all-pairs shortest paths. Running Dijkstra’s Algorithm V times yields O(V × (V + E) log V), which may be better in sparse graphs but complex to implement compared to Floyd-Warshall’s simplicity.
No, by default, BFS or DFS starting from a single source
will only explore the connected component of that source. To process the entire
graph, especially for tasks like component counting or cycle detection, you
must run BFS or DFS on each unvisited node to ensure all disconnected
components are covered.
For real-time systems, efficiency and response time are critical. BFS or optimized Dijkstra (with Fibonacci or binary heaps) is ideal for fast queries in routing. Systems often precompute paths using Floyd-Warshall or store distance matrices. In large-scale applications like social networks, graph traversal is often distributed or approximated using heuristics like A*.
Most graph algorithms work for both, but implementation
varies. For DFS and BFS, directionality affects traversal order. For Dijkstra
and Bellman-Ford, directed edges must be processed correctly for accurate
pathfinding. Special attention must be paid in cycle detection, SCCs (Strongly
Connected Components), and topological sorting, which only apply to directed
graphs.
Please log in to access this content. You will be redirected to the login page shortly.
Login
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)