What is the main purpose of the Bellman Ford algorithm?
The Bellman Ford algorithm finds the shortest paths from a single source vertex to all other vertices in a weighted graph, even if some edges have negative weights.
Click to reveal answer
intermediate
How does Bellman Ford handle negative weight edges differently from Dijkstra's algorithm?
Bellman Ford can correctly process graphs with negative weight edges by relaxing all edges multiple times, while Dijkstra's algorithm assumes all weights are non-negative and can give wrong results if negative weights exist.
Click to reveal answer
intermediate
What does it mean when Bellman Ford detects a negative weight cycle?
It means there is a cycle in the graph where the total sum of edge weights is negative, causing the shortest path to be undefined because you can keep going around the cycle to reduce the path cost infinitely.
Click to reveal answer
beginner
In Bellman Ford, how many times do we relax all edges in the graph?
We relax all edges exactly (V - 1) times, where V is the number of vertices in the graph. This ensures the shortest paths are found if no negative cycles exist.
Click to reveal answer
intermediate
What is the time complexity of the Bellman Ford algorithm?
The time complexity is O(V * E), where V is the number of vertices and E is the number of edges, because it relaxes all edges (V - 1) times.
Click to reveal answer
What does the Bellman Ford algorithm detect besides shortest paths?
ANegative weight cycles
BMaximum flow
CMinimum spanning tree
DStrongly connected components
✗ Incorrect
Bellman Ford can detect negative weight cycles by checking if any edge can still be relaxed after (V - 1) iterations.
How many times are edges relaxed in Bellman Ford for a graph with V vertices?
AV times
BV - 1 times
CE times
DE - 1 times
✗ Incorrect
Edges are relaxed (V - 1) times to ensure shortest paths are found.
Which type of graph edge weights can Bellman Ford handle that Dijkstra cannot?
ANegative weights
BPositive weights only
CZero weights only
DNon-integer weights
✗ Incorrect
Bellman Ford works correctly with negative weight edges, unlike Dijkstra.
What happens if Bellman Ford finds an edge that can still be relaxed after (V - 1) iterations?
AThe graph has no edges
BThe graph is disconnected
CThe algorithm finished successfully
DThe graph has a negative weight cycle
✗ Incorrect
Relaxing an edge after (V - 1) iterations means a negative weight cycle exists.
What is the worst-case time complexity of Bellman Ford?
AO(V + E)
BO(V^2)
CO(V * E)
DO(E^2)
✗ Incorrect
Bellman Ford runs in O(V * E) time because it relaxes all edges (V - 1) times.
Explain how the Bellman Ford algorithm finds shortest paths in graphs with negative weights.
Think about how edges are relaxed and how cycles affect shortest paths.
You got /4 concepts.
Describe the significance of detecting a negative weight cycle in Bellman Ford.
Why does a negative cycle break shortest path calculations?
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of the Bellman Ford algorithm compared to Dijkstra's algorithm?
easy
A. It only works on directed acyclic graphs.
B. It runs faster on all graphs.
C. It uses less memory.
D. It can handle graphs with negative weight edges.
Solution
Step 1: Understand the limitation of Dijkstra's algorithm
Dijkstra's algorithm cannot correctly handle graphs with negative weight edges because it assumes that once a node's shortest path is found, it cannot be improved.
Step 2: Identify Bellman Ford's capability
Bellman Ford algorithm can handle negative weight edges and detect negative weight cycles, making it more versatile for such graphs.
Final Answer:
It can handle graphs with negative weight edges. -> Option D
Quick Check:
Negative weights handled = D [OK]
Hint: Bellman Ford works with negative edges, Dijkstra does not [OK]
Common Mistakes:
Thinking Bellman Ford is always faster than Dijkstra
Believing Bellman Ford cannot detect negative cycles
Assuming Bellman Ford only works on acyclic graphs
2. Which of the following is the correct way to initialize the distance array in Bellman Ford algorithm in C++?
easy
A. vector<int> dist(V, 0); dist[src] = INT_MAX;
B. vector<int> dist(V, INT_MAX); dist[src] = 0;
C. vector<int> dist(V, -1); dist[src] = 0;
D. vector<int> dist(V, INT_MIN); dist[src] = 0;
Solution
Step 1: Understand distance initialization
Distances to all vertices except the source should be set to a very large number (infinity equivalent), here INT_MAX, to represent unknown distance.
Step 2: Set source distance to zero
The source vertex distance is zero because the distance from source to itself is zero.
Final Answer:
vector<int> dist(V, INT_MAX); dist[src] = 0; -> Option B
Quick Check:
Initialize with INT_MAX and source zero = B [OK]
Hint: Initialize all distances to INT_MAX except source zero [OK]
Common Mistakes:
Setting all distances to zero
Using INT_MIN which is incorrect for distances
Not setting source distance to zero
3. Consider the following graph edges and weights: