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 index k.
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, 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; set dist[i][i] to 0 for all i; set dist[i][j] to infinity if no direct edge exists.
Click to reveal answer
What does Floyd Warshall algorithm compute?
AMinimum spanning tree
BShortest path from a single source to all vertices
CShortest paths between all pairs of vertices
DTopological sort
✗ Incorrect
Floyd Warshall computes shortest paths between every pair of vertices.
What is the time complexity of Floyd Warshall algorithm for a graph with V vertices?
AO(V²)
BO(V³)
CO(E log V)
DO(V + E)
✗ Incorrect
Floyd Warshall uses three nested loops over vertices, resulting in O(V³) time.
Which type of graphs can Floyd Warshall handle that Dijkstra's algorithm cannot?
AGraphs with negative edge weights
BGraphs with cycles
CGraphs with only positive weights
DGraphs without edges
✗ Incorrect
Floyd Warshall can handle negative edge weights as long as there are no negative cycles.
What does the Floyd Warshall algorithm use to update shortest paths?
ARelaxation of edges
BDepth-first search
CGreedy selection of edges
DDynamic programming with intermediate nodes
✗ Incorrect
It uses dynamic programming considering intermediate nodes to update shortest paths.
What is the initial value of dist[i][i] in Floyd Warshall algorithm?
A0
BEdge weight from i to i
CInfinity
D-1
✗ Incorrect
Distance from a node to itself is always 0.
Explain how the Floyd Warshall algorithm updates the shortest path matrix step-by-step.
Think about how the algorithm tries all possible intermediate nodes.
You got /4 concepts.
Describe the differences between Floyd Warshall and Dijkstra's algorithm in terms of use cases and limitations.
Consider the type of graphs and the problem each algorithm solves.
You got /4 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 minimum spanning tree of a graph
C. To find the shortest paths between all pairs of vertices in a weighted graph
D. To perform a depth-first search traversal
Solution
Step 1: Understand Floyd Warshall's goal
The algorithm computes shortest paths between every pair of vertices in a weighted graph.
Step 2: Compare with other graph algorithms
Minimum spanning tree, cycle detection, and DFS serve different purposes than Floyd Warshall.
Final Answer:
To find the shortest paths between all pairs of vertices in a weighted graph -> Option C
Quick Check:
Floyd Warshall = All pairs shortest paths [OK]
Hint: Remember: Floyd Warshall finds shortest paths for all vertex pairs [OK]
Common Mistakes:
Confusing with Dijkstra's single-source shortest path
Thinking it finds minimum spanning tree
Assuming it detects cycles
2. Which of the following is the correct way to initialize the distance matrix dist for Floyd Warshall algorithm in C for a graph with V vertices?
easy
A. for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) dist[i][j] = INF;
B. for (int i = 0; i < V; i++) dist[i] = 0;
C. for (int i = 0; i < V; i++) dist[i][i] = INF;
D. for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) dist[i][j] = (i == j) ? 0 : INF;
Solution
Step 1: Understand initialization requirements
Distance from a vertex to itself is 0; others are set to infinity (INF) initially.
Step 2: Check each option
for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) dist[i][j] = (i == j) ? 0 : INF; correctly sets 0 for same vertices and INF for others; others do not initialize properly.
Final Answer:
for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) dist[i][j] = (i == j) ? 0 : INF; -> Option D