What is the main purpose of the Floyd Warshall algorithm?
It finds 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
beginner
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 processed so far.
Click to reveal answer
intermediate
Why does Floyd Warshall use three nested loops?
Because it iterates over all pairs of vertices (i, j) and tries to improve the path by checking an intermediate vertex k, updating dist[i][j] if a shorter path is found.
Click to reveal answer
intermediate
What is the time complexity of the Floyd Warshall algorithm?
O(V³), where V is the number of vertices in the graph, due to the three nested loops over all vertices.
Click to reveal answer
intermediate
How does Floyd Warshall handle graphs with negative edge weights?
It correctly computes shortest paths even with negative edges, as long as there are no negative weight cycles reachable from any vertex.
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.
Which of these is a requirement for Floyd Warshall to work correctly?
ANo negative weight cycles
BGraph must be acyclic
CGraph must be undirected
DAll edges must have positive weights
✗ Incorrect
Floyd Warshall works with negative edges but not with negative weight cycles.
What is the initial value of dist[i][j] if there is no direct edge between i and j?
A0
BInfinity (a very large number)
C-1
D1
✗ Incorrect
Distances are set to infinity if no direct edge exists initially.
How many nested loops does Floyd Warshall use?
A1
B2
C3
D4
✗ Incorrect
Three nested loops iterate over k, i, and j vertices.
What does the variable 'k' represent in Floyd Warshall?
ACurrent source vertex
BCurrent destination vertex
CNumber of edges
DIntermediate vertex being considered
✗ Incorrect
'k' is the intermediate vertex used to check if paths can be improved.
Explain how Floyd Warshall algorithm updates the shortest path matrix step-by-step.
Think about checking if going through an intermediate vertex shortens the path.
You got /4 concepts.
Describe the conditions under which Floyd Warshall algorithm can handle negative edge weights and when it fails.
Consider what happens if you can keep reducing path length indefinitely.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the Floyd Warshall algorithm in graph theory?
easy
A. To perform a depth-first search traversal
B. To find the minimum spanning tree of a graph
C. To detect cycles in a graph
D. To find the shortest paths between all pairs of vertices in a weighted 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, including negative weights but no negative cycles.
Step 2: Compare with other algorithms
Minimum spanning tree algorithms find a tree connecting all vertices with minimum total weight, cycle detection finds loops, and DFS is a traversal method, none of which match Floyd Warshall's purpose.
Final Answer:
To find the shortest paths between all pairs of vertices in a weighted graph -> Option D
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 only finds shortest path from one source
Assuming it works with negative cycles
2. Which of the following is the correct way to initialize the distance matrix dist for Floyd Warshall in C# when given an adjacency matrix graph of size V?
easy
A. for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dist[i, j] = graph[i, j]; } }
B. for (int i = 0; i < V; i++) { dist[i, i] = 0; } for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i != j) dist[i, j] = int.MaxValue; } }
C. for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dist[i, j] = 0; } }
D. for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dist[i, j] = -1; } }"
Solution
Step 1: Understand initialization for Floyd Warshall
The distance matrix dist should start as a copy of the adjacency matrix graph because it holds initial edge weights or infinity where no edge exists.
Step 2: Check each option
for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dist[i, j] = graph[i, j]; } } copies graph to dist, which is correct. for (int i = 0; i < V; i++) { dist[i, i] = 0; } for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i != j) dist[i, j] = int.MaxValue; } } sets 0 for diagonal and max value elsewhere but does not copy actual edges. Options C and D incorrectly set all distances to 0 or -1.
Final Answer:
for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dist[i, j] = graph[i, j]; } } -> Option A
Quick Check:
Initialize dist as graph copy [OK]
Hint: Copy graph matrix to dist for Floyd Warshall start [OK]
Common Mistakes:
Setting all distances to zero
Not copying actual edge weights
Using -1 instead of infinity for no edge
3. Consider the following C# code snippet implementing Floyd Warshall on a graph with 3 vertices and adjacency matrix:
int V = 3;
int[,] graph = {
{0, 5, int.MaxValue},
{50, 0, 15},
{30, int.MaxValue, 0}
};
int[,] dist = new int[V, V];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i, j] = graph[i, j];
for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i, k] != int.MaxValue && dist[k, j] != int.MaxValue && dist[i, k] + dist[k, j] < dist[i, j]) {
dist[i, j] = dist[i, k] + dist[k, j];
}
}
}
}
// Print dist matrix
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
Console.Write(dist[i, j] == int.MaxValue ? "INF " : dist[i, j] + " ");
}
Console.WriteLine();
}
What is the printed output?
medium
A. 0 5 20
50 0 15
30 INF 0
B. 0 5 20
45 0 15
30 35 0
C. 0 5 INF
50 0 15
30 INF 0
D. 0 5 15
50 0 15
30 35 0
Solution
Step 1: Initialize dist matrix
dist starts as:
0 5 INF
50 0 15
30 INF 0
Step 2: Apply Floyd Warshall updates
Check paths through each vertex k:
- k=0: Update dist[1,2] via 1->0->2: 50 + INF no update; dist[2,1] via 2->0->1: 30 + 5 = 35 < INF, update dist[2,1] = 35
- k=1: Update dist[0,2] via 0->1->2: 5 + 15 = 20 < INF, update dist[0,2] = 20
- k=2: No further improvements
Final dist:
0 5 20
45 0 15
30 35 0