What problem does the Floyd Warshall algorithm solve?
It finds the shortest paths between all pairs of nodes in a weighted graph, including graphs with negative edge weights but no negative cycles.
Click to reveal answer
intermediate
What is the main idea behind the Floyd Warshall algorithm?
It uses dynamic programming to update shortest paths by considering each node as an intermediate point, improving paths step-by-step until all shortest paths are found.
Click to reveal answer
intermediate
In Floyd Warshall, what does the matrix entry dist[i][j] represent during the algorithm?
It represents the shortest known distance from node i to node j considering only intermediate nodes from the set {1, 2, ..., k} at step k.
Click to reveal answer
beginner
What is the time complexity of the Floyd Warshall algorithm?
The time complexity is O(n³), where n is the number of nodes in the graph, because it uses three nested loops over all nodes.
Click to reveal answer
advanced
Can Floyd Warshall detect negative weight cycles? If yes, how?
Yes, by checking if any dist[i][i] becomes negative after the algorithm finishes, indicating a negative cycle reachable from node i.
Click to reveal answer
What does Floyd Warshall algorithm compute?
AShortest paths between all pairs of nodes
BShortest path from a single source to all nodes
CMinimum spanning tree
DTopological sort
✗ Incorrect
Floyd Warshall finds shortest paths between every pair of nodes.
Which of these is true about Floyd Warshall algorithm?
AIt can handle negative edges but no negative cycles
BIt works only on graphs without negative edges
CIt finds shortest paths using BFS
DIt uses a priority queue
✗ Incorrect
Floyd Warshall can handle negative edges but fails if negative cycles exist.
What is the main data structure used in Floyd Warshall?
AAdjacency list
BQueue
CStack
DDistance matrix
✗ Incorrect
Floyd Warshall uses a matrix to store shortest distances between nodes.
What does a negative value in dist[i][i] after Floyd Warshall indicate?
ANo path exists
BGraph is disconnected
CNegative weight cycle reachable from node i
DAlgorithm failed
✗ Incorrect
Negative dist[i][i] means a negative cycle involving node i.
What is the time complexity of Floyd Warshall algorithm?
AO(n log n)
BO(n³)
CO(n²)
DO(m + n)
✗ Incorrect
Floyd Warshall uses three nested loops over n nodes, so O(n³).
Explain how Floyd Warshall algorithm updates the shortest path matrix step-by-step.
Think about dynamic programming and intermediate nodes.
You got /4 concepts.
Describe how Floyd Warshall algorithm can detect negative weight cycles in a graph.
Focus on the meaning of dist[i][i] after algorithm finishes.
You got /3 concepts.
Practice
(1/5)
1. What does Floyd-Warshall compute that single-source shortest path algorithms like Dijkstra do not?
easy
A. Shortest path from one source to all vertices
B. Shortest paths between ALL pairs of vertices in one run
C. Minimum spanning tree of the graph
D. Topological order of vertices
Solution
Step 1: Single-source vs all-pairs
Dijkstra/Bellman-Ford compute shortest paths from ONE source to all others. To get all-pairs with Dijkstra, run it V times = O(V(E + V log V)).
Step 2: Floyd-Warshall
Floyd-Warshall computes shortest paths between every pair (i,j) in a single O(V³) pass. Simpler to implement for dense graphs.
2. What is the Floyd-Warshall recurrence? dist[i][j] after considering vertex k as intermediate is:
medium
A. dist[i][j] = dist[i][k] + dist[k][j]
B. dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
C. dist[i][j] = max(dist[i][j], dist[i][k] + dist[k][j])
D. dist[i][j] = dist[i][k] * dist[k][j]
Solution
Step 1: Core idea
For each intermediate vertex k: is the path i→k→j shorter than the current best i→j?
Step 2: Recurrence
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]). Keep the minimum of: current best OR going through k.
Final Answer:
min(dist[i][j], dist[i][k] + dist[k][j]) -> Option B
Quick Check:
Always take the minimum - relaxation. [OK]
Hint: Floyd-Warshall: dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]). Relax via each intermediate k. [OK]
Common Mistakes:
Using + without min (overwrites current best)
Using max instead of min
Multiplying instead of adding edge weights
3. In TypeScript, how do you initialize dist[i][i] and dist[i][j] (no direct edge) for Floyd-Warshall?
medium
A. dist[i][i] = 0, dist[i][j] = -1
B. dist[i][i] = 0, dist[i][j] = Infinity
C. dist[i][i] = 1, dist[i][j] = 0
D. dist[i][i] = Infinity, dist[i][j] = Infinity
Solution
Step 1: Self-distance
dist[i][i] = 0 - zero cost to stay at the same vertex.
Step 2: No edge
dist[i][j] = Infinity when no direct edge exists - represents unreachable initially. In TypeScript: Infinity works correctly with Math.min().
Final Answer:
dist[i][i]=0, no edge=Infinity -> Option B
Quick Check:
Self=0, no edge=Infinity. Infinity + x = Infinity in JS - works with min(). [OK]
Hint: Init: self=0, no edge=Infinity. TypeScript Infinity works correctly with Math.min(). [OK]
Common Mistakes:
Using -1 for no edge (breaks min comparison)
Setting self-distance to Infinity
Using 0 for no edge (treats as zero-weight edge)
4. What is the correct loop order for Floyd-Warshall in TypeScript?
hard
A. for i → for j → for k
B. for k → for i → for j
C. for i → for k → for j
D. for j → for k → for i
Solution
Step 1: Why k is outermost
k is the intermediate vertex being 'unlocked' at each outer iteration. When we process intermediate k, all pairs i→j must be relaxed using k.
Step 2: Correctness
If k is innermost, we might use a later-unlocked vertex when relaxing - violating the DP ordering. k must be outermost so all (i,j) pairs are fully relaxed before moving to k+1.
Final Answer:
k outermost, then i, then j. -> Option B
Quick Check:
Floyd-Warshall loop order: k→i→j. k must be outermost. [OK]
Hint: Loop order: k (intermediate) → i (source) → j (dest). k must be outermost - critical for correctness. [OK]
Common Mistakes:
Putting i outermost (most common mistake)
Putting j outermost
Any order other than k→i→j gives wrong results
5. How do you detect a negative cycle using Floyd-Warshall after completion?
hard
A. Check if any dist[i][j] === -Infinity
B. Check if any dist[i][i] < 0 - a vertex with negative self-distance indicates a negative cycle
C. Check if the total sum of all dist values is negative
D. Run the algorithm a second time and compare results
Solution
Step 1: Negative cycle effect
If a negative cycle exists reachable from i back to i, the shortest path dist[i][i] will become negative (going around the cycle reduces cost below 0).
Step 2: Detection
After the algorithm completes, check: for any i, if dist[i][i] < 0, a negative cycle exists involving i.
Final Answer:
Check dist[i][i] < 0 for any i. -> Option B
Quick Check:
Negative cycle: dist[i][i] < 0 after Floyd-Warshall. [OK]