What is the main purpose of the Floyd Warshall algorithm?
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
beginner
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 node i to node j considering intermediate nodes up to a certain point in the algorithm.
Click to reveal answer
intermediate
Why can Floyd Warshall handle negative edge weights but not negative cycles?
Because it updates shortest paths by checking intermediate nodes, but if a negative cycle exists, the shortest path can decrease indefinitely, making the problem unsolvable.
Click to reveal answer
intermediate
What is the key idea behind the Floyd Warshall algorithm's approach to finding shortest paths?
It uses dynamic programming by gradually allowing paths through intermediate nodes, updating shortest distances step-by-step for all pairs.
Click to reveal answer
What does the Floyd Warshall algorithm compute?
AMinimum spanning tree
BShortest path from a single source to all nodes
CShortest paths between all pairs of nodes
DTopological sort of nodes
✗ Incorrect
Floyd Warshall finds shortest paths between every pair of nodes in the graph.
Which of these is a requirement for Floyd Warshall to work correctly?
ANo negative weight cycles
BGraph must be a tree
CNo negative weight edges
DGraph must be directed acyclic
✗ Incorrect
Floyd Warshall can handle negative edges but not negative cycles.
What is the main data structure used in Floyd Warshall to store distances?
AAdjacency list
BDistance matrix
CQueue
DStack
✗ Incorrect
A matrix stores shortest distances between all pairs of nodes.
What is the time complexity of Floyd Warshall for a graph with V vertices?
AO(V²)
BO(E + V)
CO(V log V)
DO(V³)
✗ Incorrect
The algorithm uses three nested loops over vertices, resulting in O(V³) time.
Which step is repeated in Floyd Warshall to update shortest paths?
ACheck if going through an intermediate node improves path
BRelax edges from a single source
CRemove edges with negative weights
DSort nodes by distance
✗ Incorrect
The algorithm checks if using an intermediate node k improves the path from i to j.
Explain how the Floyd Warshall algorithm updates the shortest path distances using intermediate nodes.
Think about how the algorithm tries paths through each node as a middle step.
You got /4 concepts.
Describe the limitations of the Floyd Warshall algorithm and when it should not be used.
Consider graph size and presence of negative cycles.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the Floyd Warshall algorithm in graph theory?
easy
A. To find the shortest paths between all pairs of vertices in a weighted graph
B. To find the minimum spanning tree of a graph
C. To perform a depth-first search traversal
D. To detect cycles in a graph
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: Compare with other graph algorithms
Minimum spanning tree, DFS, and cycle detection serve different purposes than Floyd Warshall.
Final Answer:
To find the shortest paths between all pairs of vertices in a weighted graph -> Option A
Quick Check:
Floyd Warshall = all pairs shortest paths [OK]
Hint: Floyd Warshall finds shortest paths between all vertex pairs [OK]
Common Mistakes:
Confusing with minimum spanning tree algorithms
Thinking it finds shortest path from one source only
Mixing with traversal algorithms like DFS or BFS
2. Which of the following is the correct way to initialize the distance matrix dist for Floyd Warshall in Python for a graph with n vertices?
easy
A. dist = [[0] * n] * n
B. dist = [[float('inf')] * n for _ in range(n)]
C. dist = [float('inf')] * n * n
D. dist = [0 for _ in range(n)]
Solution
Step 1: Understand matrix initialization
We need a 2D list with size n x n, each element set to infinity initially.
Step 2: Check each option
dist = [[float('inf')] * n for _ in range(n)] correctly creates a new list for each row with float('inf'). dist = [float('inf')] * n * n creates a 1D list, not 2D. dist = [[0] * n] * n creates shallow copies causing aliasing. dist = [0 for _ in range(n)] creates 1D list.
Final Answer:
dist = [[float('inf')] * n for _ in range(n)] -> Option B
Quick Check:
2D list with inf values = dist = [[float('inf')] * n for _ in range(n)] [OK]
Hint: Use list comprehension for 2D matrix initialization [OK]
Common Mistakes:
Using multiplication for 2D lists causing shared references
Creating 1D list instead of 2D
Initializing with zeros instead of infinity
3. What will be the output distance matrix after running Floyd Warshall on this graph represented by adjacency matrix?
Hint: Update dist with min of current and via intermediate vertex [OK]
Common Mistakes:
Not updating distances through intermediate vertices
Confusing indices in nested loops
Leaving float('inf') where shorter paths exist
4. Consider this snippet of Floyd Warshall implementation in Python:
for k in range(n):
for i in range(n):
for j in range(n):
if dist[i][k] + dist[k][j] < dist[i][j]:
dist[i][j] = dist[i][k] + dist[k][j]
What is the main bug if dist[i][k] or dist[k][j] is float('inf')?
medium
A. It causes incorrect updates because adding infinity is not handled
B. It causes incorrect addition leading to overflow errors
C. No bug, the code works fine with infinity values
D. It causes syntax errors due to float('inf') usage
Solution
Step 1: Understand addition with infinity in Python
float('inf') + any finite number = float('inf'), and float('inf') + float('inf') = float('inf').
Step 2: Check the condition
The condition if dist[i][k] + dist[k][j] < dist[i][j] will be False whenever the sum is inf, because inf is not less than any finite or inf value. Thus, no incorrect updates occur; invalid paths are correctly skipped.
Final Answer:
No bug, the code works fine with infinity values -> Option C
Quick Check:
inf + x = inf, inf < y always False for path updates [OK]
Hint: Python's float('inf') handles additions and comparisons correctly for FW [OK]
Common Mistakes:
Believing inf causes incorrect updates
Confusing with integer overflow issues
Thinking syntax error with inf
5. Given a graph with negative edge weights but no negative cycles, how does Floyd Warshall algorithm handle these edges to find shortest paths?
hard
A. It requires a separate algorithm to preprocess negative edges before running
B. It fails because it cannot handle negative weights at all
C. It ignores negative edges and only uses positive weights
D. It updates distances by considering intermediate vertices, correctly handling negative edges without cycles
Solution
Step 1: Understand Floyd Warshall with negative edges
The algorithm can handle negative edges by updating distances through intermediate vertices, as long as no negative cycles exist.
Step 2: Confirm no negative cycle condition
If negative cycles exist, shortest paths are undefined. Without them, Floyd Warshall converges to correct shortest paths.
Final Answer:
It updates distances by considering intermediate vertices, correctly handling negative edges without cycles -> Option D
Quick Check:
Negative edges allowed if no negative cycles [OK]
Hint: Floyd Warshall handles negative edges if no negative cycles exist [OK]
Common Mistakes:
Thinking Floyd Warshall cannot handle any negative edges
Confusing negative edges with negative cycles
Assuming preprocessing is always needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.