What is the main purpose of the Floyd Warshall algorithm?
It finds the shortest paths between all pairs of vertices in a weighted graph, even if some edges have negative weights, as long as there are 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 vertex i to vertex j considering intermediate vertices up to a certain index k.
Click to reveal answer
intermediate
Why does Floyd Warshall algorithm use three nested loops?
Because it iterates over all pairs of vertices (i, j) and considers each vertex k as an intermediate point to update the shortest path between i and j.
Click to reveal answer
advanced
How does Floyd Warshall detect a negative weight cycle?
If any diagonal element dist[i][i] becomes negative after running the algorithm, it indicates a negative weight cycle reachable from vertex i.
Click to reveal answer
What type of graphs can Floyd Warshall algorithm handle?
AGraphs with negative cycles
BOnly graphs with positive edges
COnly unweighted graphs
DGraphs with negative edges but no negative cycles
✗ Incorrect
Floyd Warshall can handle graphs with negative edges as long as there are no negative weight cycles.
What does the Floyd Warshall algorithm update in each iteration?
ANumber of edges in the graph
BOnly shortest path from source to destination
CShortest path distances between all pairs of vertices
DGraph adjacency list
✗ Incorrect
It updates the shortest distances between every pair of vertices considering intermediate vertices.
Which data structure is primarily used to store distances in Floyd Warshall?
ALinked list
B2D matrix
CQueue
DStack
✗ Incorrect
A 2D matrix stores shortest distances between all pairs of vertices.
What does a negative value in dist[i][i] after the algorithm finishes indicate?
ANegative weight cycle reachable from vertex i
BNo path from i to i
CGraph is disconnected
DAlgorithm failed
✗ Incorrect
A negative diagonal entry means a negative cycle exists involving vertex i.
What is the main difference between Floyd Warshall and Dijkstra's algorithm?
AFloyd Warshall finds shortest paths between all pairs; Dijkstra finds shortest path from one source
BDijkstra works with negative edges; Floyd Warshall does not
CFloyd Warshall uses a queue; Dijkstra uses a stack
DBoth algorithms are identical
✗ Incorrect
Floyd Warshall computes shortest paths for all pairs, while Dijkstra computes from a single source.
Explain how the Floyd Warshall algorithm updates the shortest path matrix step-by-step.
Think about considering each vertex as an intermediate point.
You got /4 concepts.
Describe how Floyd Warshall detects negative weight cycles and why this is important.
Focus on the meaning of dist[i][i] after algorithm completion.
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 shortest paths between all pairs of vertices in a weighted graph
B. To find the minimum spanning tree of a graph
C. To detect cycles in a directed graph
D. To perform depth-first search traversal
Solution
Step 1: Understand Floyd Warshall algorithm goal
The Floyd Warshall algorithm is designed to find shortest paths between every pair of vertices in a weighted graph, including negative weights but no negative cycles.
Step 2: Compare with other graph algorithms
Minimum spanning tree algorithms find a tree connecting all vertices with minimum total edge weight, cycle detection algorithms find cycles, and DFS is a traversal method. These are different from Floyd Warshall's goal.
Final Answer:
To find 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 Floyd Warshall with minimum spanning tree algorithms
Thinking it only finds shortest path from one source
Assuming it detects cycles instead of paths
2. Which of the following is the correct way to initialize the distance matrix dist for Floyd Warshall in C++ for a graph with n vertices and adjacency matrix graph?
easy
A. for (int i = 0; i < n; i++) dist[i] = graph[i];
B. for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = graph[i][j];
C. dist = graph;
D. dist = new int[n][n];
Solution
Step 1: Understand distance matrix initialization
We need to copy each element from the adjacency matrix graph to dist element-wise because dist is a 2D array and direct assignment is not allowed in C++.
Step 2: Analyze each option
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = graph[i][j]; uses nested loops to copy each element correctly. for (int i = 0; i < n; i++) dist[i] = graph[i]; tries to assign a row pointer which is invalid for 2D arrays. dist = graph; tries direct assignment which is invalid for arrays. dist = new int[n][n]; only allocates memory but does not initialize values.
Final Answer:
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = graph[i][j]; -> Option B
Quick Check:
Element-wise copy needed = for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = graph[i][j]; [OK]
Hint: Use nested loops to copy 2D arrays element-wise [OK]
Common Mistakes:
Trying to assign 2D arrays directly
Copying only row pointers instead of elements
Not initializing distance matrix before algorithm
3. What will be the output distance matrix after running Floyd Warshall on the following graph with 3 vertices and adjacency matrix: