Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Floyd Warshall All Pairs Shortest Path in DSA Javascript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Floyd Warshall All Pairs Shortest Path
Start with distance matrix
↓
Pick intermediate node k
↓
For each pair (i, j)
↓
Check if path i->k->j is shorter
Yes/No↓
Update dist[i
↓
Repeat for all i, j
↓
Repeat for all k
↓
Final shortest paths matrix
The algorithm updates shortest paths by considering each node as an intermediate point, checking if going through it shortens paths between all pairs.
Execution Sample
DSA Javascript
const dist = [
  [0, 3, Infinity, 7],
  [8, 0, 2, Infinity],
  [5, Infinity, 0, 1],
  [2, Infinity, Infinity, 0]
];
// Floyd Warshall updates dist matrix
This code runs Floyd Warshall on a 4-node graph with initial distances, updating to shortest paths.
Execution Table
StepOperationIntermediate kPair (i,j)Old dist[i][j]New dist[i][j]Updated?Matrix State
1Initialize----No[[0,3,∞,7],[8,0,2,∞],[5,∞,0,1],[2,∞,∞,0]]
2k=0, i=0, j=00(0,0)00No[[0,3,∞,7],[8,0,2,∞],[5,∞,0,1],[2,∞,∞,0]]
3k=0, i=0, j=10(0,1)33No[[0,3,∞,7],[8,0,2,∞],[5,∞,0,1],[2,∞,∞,0]]
4k=0, i=0, j=20(0,2)∞∞No[[0,3,∞,7],[8,0,2,∞],[5,∞,0,1],[2,∞,∞,0]]
5k=0, i=0, j=30(0,3)77No[[0,3,∞,7],[8,0,2,∞],[5,∞,0,1],[2,∞,∞,0]]
6k=0, i=1, j=00(1,0)88No[[0,3,∞,7],[8,0,2,∞],[5,∞,0,1],[2,∞,∞,0]]
7k=0, i=1, j=10(1,1)00No[[0,3,∞,7],[8,0,2,∞],[5,∞,0,1],[2,∞,∞,0]]
8k=0, i=1, j=20(1,2)22No[[0,3,∞,7],[8,0,2,∞],[5,∞,0,1],[2,∞,∞,0]]
9k=0, i=1, j=30(1,3)∞15Yes[[0,3,∞,7],[8,0,2,15],[5,∞,0,1],[2,∞,∞,0]]
10k=0, i=2, j=00(2,0)55No[[0,3,∞,7],[8,0,2,15],[5,∞,0,1],[2,∞,∞,0]]
11k=0, i=2, j=10(2,1)∞8Yes[[0,3,∞,7],[8,0,2,15],[5,8,0,1],[2,∞,∞,0]]
12k=0, i=2, j=20(2,2)00No[[0,3,∞,7],[8,0,2,15],[5,8,0,1],[2,∞,∞,0]]
13k=0, i=2, j=30(2,3)11No[[0,3,∞,7],[8,0,2,15],[5,8,0,1],[2,∞,∞,0]]
14k=0, i=3, j=00(3,0)22No[[0,3,∞,7],[8,0,2,15],[5,8,0,1],[2,∞,∞,0]]
15k=0, i=3, j=10(3,1)∞5Yes[[0,3,∞,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
16k=0, i=3, j=20(3,2)∞∞No[[0,3,∞,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
17k=0, i=3, j=30(3,3)00No[[0,3,∞,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
18k=1, i=0, j=01(0,0)00No[[0,3,∞,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
19k=1, i=0, j=11(0,1)33No[[0,3,∞,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
20k=1, i=0, j=21(0,2)∞5Yes[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
21k=1, i=0, j=31(0,3)77No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
22k=1, i=1, j=01(1,0)88No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
23k=1, i=1, j=11(1,1)00No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
24k=1, i=1, j=21(1,2)22No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
25k=1, i=1, j=31(1,3)1517No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
26k=1, i=2, j=01(2,0)513No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
27k=1, i=2, j=11(2,1)88No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
28k=1, i=2, j=21(2,2)00No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
29k=1, i=2, j=31(2,3)11No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
30k=1, i=3, j=01(3,0)27No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
31k=1, i=3, j=11(3,1)55No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]]
32k=1, i=3, j=21(3,2)∞7Yes[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
33k=1, i=3, j=31(3,3)00No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
34k=2, i=0, j=02(0,0)00No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
35k=2, i=0, j=12(0,1)33No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
36k=2, i=0, j=22(0,2)55No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
37k=2, i=0, j=32(0,3)76Yes[[0,3,5,6],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
38k=2, i=1, j=02(1,0)88No[[0,3,5,6],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
39k=2, i=1, j=12(1,1)00No[[0,3,5,6],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
40k=2, i=1, j=22(1,2)22No[[0,3,5,6],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
41k=2, i=1, j=32(1,3)153Yes[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
42k=2, i=2, j=02(2,0)55No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
43k=2, i=2, j=12(2,1)88No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
44k=2, i=2, j=22(2,2)00No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
45k=2, i=2, j=32(2,3)11No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
46k=2, i=3, j=02(3,0)22No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
47k=2, i=3, j=12(3,1)55No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
48k=2, i=3, j=22(3,2)77No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
49k=2, i=3, j=32(3,3)00No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
50k=3, i=0, j=03(0,0)00No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
51k=3, i=0, j=13(0,1)33No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
52k=3, i=0, j=23(0,2)56No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
53k=3, i=0, j=33(0,3)66No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
54k=3, i=1, j=03(1,0)88No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
55k=3, i=1, j=13(1,1)00No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
56k=3, i=1, j=23(1,2)23No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
57k=3, i=1, j=33(1,3)33No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
58k=3, i=2, j=03(2,0)56No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
59k=3, i=2, j=13(2,1)88No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
60k=3, i=2, j=23(2,2)00No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
61k=3, i=2, j=33(2,3)11No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
62k=3, i=3, j=03(3,0)22No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
63k=3, i=3, j=13(3,1)55No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
64k=3, i=3, j=23(3,2)77No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
65k=3, i=3, j=33(3,3)00No[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
66End-----[[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
💡 All intermediate nodes k processed; final shortest path matrix computed
Variable Tracker
VariableStartAfter k=0After k=1After k=2After k=3Final
dist[[0,3,∞,7],[8,0,2,∞],[5,∞,0,1],[2,∞,∞,0]][[0,3,∞,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]][[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]][[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]][[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]][[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]]
Key Moments - 3 Insights
Why do we check if dist[i][k] + dist[k][j] < dist[i][j] instead of just dist[i][j]?
Because we want to see if going through the intermediate node k gives a shorter path from i to j. This is shown in execution_table rows like 9 and 11 where dist[i][j] updates after checking via k.
Why do some distances remain Infinity (∞) throughout the process?
Infinity means no path exists between those nodes. If no intermediate nodes connect them, the distance stays ∞, as seen in early rows before any updates.
Why do we update the matrix multiple times for the same pair (i,j)?
Because each intermediate node k might offer a shorter path. The matrix updates progressively, as seen in rows 20 and 37 where dist[0][2] and dist[0][3] get shorter.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 11, what is the new distance from node 2 to node 1?
A8
BInfinity
C5
D2
💡 Hint
Check the 'New dist[i][j]' column at step 11 for pair (2,1)
At which iteration does the distance from node 1 to node 3 first update to a finite value?
AStep 41
BStep 9
CStep 21
DStep 25
💡 Hint
Look for the first 'Yes' in 'Updated?' column for pair (1,3)
If the initial distance from node 0 to node 2 was 10 instead of Infinity, how would the matrix state change after k=1?
ANo change, still 5
BIt would remain 10
CIt would update to 5
DIt would update to 3
💡 Hint
Check how dist[0][2] updates at step 20 after considering k=1
Concept Snapshot
Floyd Warshall Algorithm:
- Uses dynamic programming on a distance matrix
- Iterates over all nodes as intermediate points k
- Updates dist[i][j] if dist[i][k] + dist[k][j] < dist[i][j]
- Handles negative edges but no negative cycles
- Final matrix shows shortest paths between all pairs
Full Transcript
Floyd Warshall algorithm finds shortest paths between all pairs of nodes in a graph. It starts with a distance matrix representing direct edges or infinity if no direct edge exists. Then, for each node k, it checks if using k as an intermediate node shortens the path between every pair (i,j). If yes, it updates the distance. This process repeats for all k, progressively improving the shortest paths. The final matrix shows the shortest distances between all nodes. Key points include checking if going through k is shorter, updating distances multiple times, and handling unreachable nodes with infinity.

Practice

(1/5)
1. What is the main purpose of the Floyd Warshall algorithm in graph theory?
easy
A. To detect cycles in a graph
B. To find the shortest path from a single source to all other vertices
C. To find the shortest paths between all pairs of vertices in a weighted graph
D. To sort vertices in topological order

Solution

  1. Step 1: Understand Floyd Warshall's goal

    The Floyd Warshall algorithm calculates shortest paths between every pair of vertices in a weighted graph.
  2. Step 2: Differentiate from other algorithms

    Unlike Dijkstra's algorithm which finds shortest paths from one source, Floyd Warshall finds paths for all pairs.
  3. Final Answer:

    To find the shortest paths between all pairs of vertices in a weighted graph -> Option C
  4. Quick Check:

    All pairs shortest path = Floyd Warshall [OK]
Hint: Floyd Warshall finds shortest paths for all vertex pairs [OK]
Common Mistakes:
  • Confusing with Dijkstra's single source shortest path
  • Thinking it detects cycles
  • Assuming it sorts vertices
2. Which of the following is the correct way to initialize the distance matrix dist for Floyd Warshall in JavaScript given a graph adjacency matrix graph?
easy
A. const dist = graph.map(row => row.slice());
B. const dist = graph.slice();
C. const dist = graph.map(row => row);
D. const dist = [...graph];

Solution

  1. Step 1: Understand deep copy necessity

    We need a deep copy of the 2D array so changes to dist do not affect graph.
  2. Step 2: Check each option for deep copy

    const dist = graph.map(row => row.slice()); uses map and slice to copy each row array, creating a deep copy. Others create shallow copies.
  3. Final Answer:

    const dist = graph.map(row => row.slice()); -> Option A
  4. Quick Check:

    Deep copy with map and slice = const dist = graph.map(row => row.slice()); [OK]
Hint: Use map with slice for deep copy of 2D arrays [OK]
Common Mistakes:
  • Using shallow copy causing original graph to change
  • Using spread operator which copies only top-level array
  • Using map without slice copying references
3. What will be the output distance matrix after running Floyd Warshall on the following graph adjacency matrix where Infinity means no direct edge?
const graph = [
  [0, 3, Infinity],
  [2, 0, 5],
  [Infinity, 1, 0]
];
medium
A. [[0, 3, 7], [2, 0, 5], [3, 1, 0]]
B. [[0, 3, 5], [2, 0, 5], [Infinity, 1, 0]]
C. [[0, 3, Infinity], [2, 0, 5], [3, 1, 0]]
D. [[0, 3, 8], [2, 0, 5], [3, 1, 0]]

Solution

  1. Step 1: Initialize dist matrix same as graph

    dist = [[0,3,∞],[2,0,5],[∞,1,0]]
  2. Step 2: Update dist using Floyd Warshall iterations

    Check paths through each vertex k=0,1,2:
    k=0: dist[2][0] = min(∞, dist[2][0]+dist[0][0])=∞ no change
    k=1: dist[0][2] = min(∞, dist[0][1]+dist[1][2])=3+5=8
    dist[2][0] = min(∞, dist[2][1]+dist[1][0])=1+2=3
    k=2: no shorter paths found
    Final dist = [[0,3,8],[2,0,5],[3,1,0]]
  3. Final Answer:

    [[0, 3, 8], [2, 0, 5], [3, 1, 0]] -> Option D
  4. Quick Check:

    Updated shortest paths matrix = [[0, 3, 8], [2, 0, 5], [3, 1, 0]] [OK]
Hint: Check all intermediate vertices to update shortest paths [OK]
Common Mistakes:
  • Not updating paths through intermediate vertices
  • Confusing Infinity with zero
  • Missing updates for indirect paths
4. Identify the error in this Floyd Warshall implementation snippet:
for (let k = 0; k < V; k++) {
  for (let i = 0; i < V; i++) {
    for (let j = 0; j < V; j++) {
      if (dist[i][k] + dist[k][j] < dist[i][j]) {
        dist[i][j] = dist[i][k] + dist[k][j];
      }
    }
  }
}
medium
A. Missing check if dist[i][k] or dist[k][j] is Infinity before addition
B. The loops should be reversed: i, j, then k
C. No error, the code is correct
D. The condition should use <= instead of <

Solution

  1. Step 1: Analyze addition of distances

    Adding dist[i][k] + dist[k][j] without checking if either is Infinity can cause incorrect results or overflow.
  2. Step 2: Identify missing Infinity checks

    Before adding, must ensure dist[i][k] !== Infinity and dist[k][j] !== Infinity to avoid invalid sums.
  3. Final Answer:

    Missing check if dist[i][k] or dist[k][j] is Infinity before addition -> Option A
  4. Quick Check:

    Check Infinity before sum to avoid errors [OK]
Hint: Always check for Infinity before adding distances [OK]
Common Mistakes:
  • Ignoring Infinity causing wrong updates
  • Changing loop order unnecessarily
  • Using <= instead of < causing redundant updates
5. Given a weighted directed graph with possible negative edges but no negative cycles, how does Floyd Warshall detect the presence of a negative cycle after running the algorithm?
hard
A. By checking if any dist[i][j] is Infinity
B. By checking if any diagonal element dist[i][i] becomes negative
C. By verifying if the sum of all distances is zero
D. By checking if any dist[i][j] is negative for i != j

Solution

  1. Step 1: Understand negative cycle effect on dist matrix

    Negative cycles cause the shortest path from a vertex to itself to become negative.
  2. Step 2: Check diagonal elements after Floyd Warshall

    If any dist[i][i] < 0, it means a negative cycle exists involving vertex i.
  3. Final Answer:

    By checking if any diagonal element dist[i][i] becomes negative -> Option B
  4. Quick Check:

    Negative cycle detected if dist[i][i] < 0 [OK]
Hint: Check diagonal dist[i][i] for negativity to find negative cycles [OK]
Common Mistakes:
  • Checking off-diagonal elements for negative cycles
  • Confusing Infinity with negative cycles
  • Assuming sum of distances indicates cycles
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard