How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
🧠 Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
📖 Select Passive to activate
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
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 ) . 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:
Consider a graph with 4 vertices (0, 1, 2, 3) and the following initial distance matrix:
Initial dist:
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.
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.
For (k = 2):
Consider paths via vertex 2.
For (k = 3):
Consider paths via vertex 3.
No further improvements are possible.
The final shortest path distance matrix is:
This matrix tells us, for example, that the shortest distance from vertex 0 to vertex 3 is 9.
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.
— Written by Saurabh Patil • B.Tech CSE • Software Developer🎯 Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
🔒 Locked steps cannot be moved. Only unlocked steps are draggable.
🔊 Enable sound for swap feedback and completion effects.