What is the main goal of the Floyd Warshall algorithm?
To find the shortest paths between all pairs of vertices in a weighted graph, including graphs with negative edge weights but no negative cycles.
Click to reveal answer
intermediate
What is the time complexity of the Floyd Warshall algorithm?
The time complexity is O(V³), where V is the number of vertices in the graph.
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 vertex i to vertex j considering intermediate vertices up to a certain index k.
Click to reveal answer
advanced
Why can Floyd Warshall handle negative edge weights but not negative cycles?
Because it updates shortest paths by checking all intermediate vertices, but if a negative cycle exists, distances can decrease indefinitely, making shortest paths undefined.
Click to reveal answer
beginner
What is the initial setup of the distance matrix in Floyd Warshall?
Set dist[i][j] to the edge weight if there is an edge from i to j, 0 if i equals j, and Infinity if there is no direct edge.
Click to reveal answer
What does Floyd Warshall algorithm compute?
AShortest paths between all pairs of vertices
BShortest path from a single source to all vertices
CMinimum spanning tree
DTopological sort
✗ Incorrect
Floyd Warshall finds shortest paths between every pair of vertices.
What is the time complexity of Floyd Warshall algorithm?
AO(E log V)
BO(V²)
CO(V + E)
DO(V³)
✗ Incorrect
Floyd Warshall runs in cubic time relative to the number of vertices.
Which type of graphs can Floyd Warshall handle that Dijkstra's algorithm cannot?
AGraphs with only positive weights
BGraphs with negative edge weights
CGraphs with cycles
DUnweighted graphs
✗ Incorrect
Floyd Warshall can handle negative edge weights as long as there are no negative cycles.
What does dist[i][i] equal at the start of Floyd Warshall?
AEdge weight from i to i
BInfinity
C0
D-1
✗ Incorrect
Distance from a vertex to itself is zero initially.
If a negative cycle exists in the graph, what happens to Floyd Warshall's output?
ADistances can become infinitely small, making results invalid
BIt correctly finds shortest paths
CIt detects the cycle and stops
DIt ignores negative cycles
✗ Incorrect
Negative cycles cause distances to decrease indefinitely, so shortest paths are undefined.
Explain how Floyd Warshall algorithm updates the distance matrix to find shortest paths.
Think about checking all possible paths through each vertex step by step.
You got /3 concepts.
Describe the limitations of Floyd Warshall algorithm and when it should not be used.
Consider graph size and presence of negative cycles.
You got /3 concepts.
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
Step 1: Understand Floyd Warshall's goal
The Floyd Warshall algorithm calculates shortest paths between every pair of vertices in a weighted graph.
Step 2: Differentiate from other algorithms
Unlike Dijkstra's algorithm which finds shortest paths from one source, Floyd Warshall finds paths for all pairs.
Final Answer:
To find the shortest paths between all pairs of vertices in a weighted graph -> Option C
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
Step 1: Understand deep copy necessity
We need a deep copy of the 2D array so changes to dist do not affect graph.
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.
Final Answer:
const dist = graph.map(row => row.slice()); -> Option A
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?
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
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.
Step 2: Identify missing Infinity checks
Before adding, must ensure dist[i][k] !== Infinity and dist[k][j] !== Infinity to avoid invalid sums.
Final Answer:
Missing check if dist[i][k] or dist[k][j] is Infinity before addition -> Option A
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
Step 1: Understand negative cycle effect on dist matrix
Negative cycles cause the shortest path from a vertex to itself to become negative.
Step 2: Check diagonal elements after Floyd Warshall
If any dist[i][i] < 0, it means a negative cycle exists involving vertex i.
Final Answer:
By checking if any diagonal element dist[i][i] becomes negative -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.