What problem does the Bellman Ford algorithm solve?
It finds the shortest paths from a single source vertex to all other vertices in a 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 handle negative weight edges by relaxing all edges multiple times, while Dijkstra's algorithm assumes all edge weights are non-negative and may fail with negative weights.
Click to reveal answer
beginner
What is the main step repeated in the Bellman Ford algorithm?
Relaxing all edges in the graph repeatedly (|V| - 1) times, where |V| is the number of vertices.
Click to reveal answer
intermediate
How does Bellman Ford detect a negative weight cycle?
After |V| - 1 relaxations, it performs one more relaxation step. If any distance can still be reduced, a negative weight cycle exists.
Click to reveal answer
beginner
What is the time complexity of the Bellman Ford algorithm?
O(|V| * |E|), where |V| is the number of vertices and |E| is the number of edges.
Click to reveal answer
What does the Bellman Ford algorithm do if it finds a negative weight cycle?
AReports that no shortest path exists due to the cycle
BIgnores the cycle and continues
CStops and returns the current distances
DConverts negative weights to positive
✗ Incorrect
Bellman Ford detects negative weight cycles and reports that shortest paths are undefined because the cycle can reduce path cost indefinitely.
How many times does Bellman Ford relax all edges in the worst case?
A|V| - 1 times
B|V| times
C|E| times
DOnce
✗ Incorrect
Bellman Ford relaxes all edges |V| - 1 times to ensure shortest paths are found.
Which of these algorithms can handle graphs with negative edge weights?
ADijkstra's algorithm
BPrim's algorithm
CBellman Ford algorithm
DKruskal's algorithm
✗ Incorrect
Only Bellman Ford can handle negative edge weights safely.
What is the main data structure used to store distances in Bellman Ford?
AStack
BArray or map of distances
CQueue
DLinked list
✗ Incorrect
Distances from the source to each vertex are stored in an array or map.
If no negative weight cycles exist, what does Bellman Ford guarantee?
ANo paths found
BLongest paths from source to all vertices
COnly paths with positive weights
DShortest paths from source to all vertices
✗ Incorrect
Bellman Ford guarantees shortest paths if no negative weight cycles exist.
Explain how the Bellman Ford algorithm detects negative weight cycles in a graph.
Think about what happens if distances keep decreasing after expected relaxations.
You got /3 concepts.
Describe the main steps of the Bellman Ford algorithm for finding shortest paths.
Focus on initialization, relaxation, and cycle detection.
You got /3 concepts.
Practice
(1/5)
1. What is the main advantage of the Bellman-Ford algorithm compared to Dijkstra's algorithm?
easy
A. It runs faster on all graphs.
B. It uses less memory.
C. It can handle graphs with negative weight edges.
D. It only works on directed acyclic graphs.
Solution
Step 1: Understand Dijkstra's limitation
Dijkstra's algorithm cannot handle negative weight edges because it assumes once a node is visited with the shortest path, it won't be updated again.
Step 2: Bellman-Ford's capability
Bellman-Ford algorithm relaxes edges multiple times and can detect negative weight edges, allowing it to work correctly even with negative weights.
Final Answer:
It can handle graphs with negative weight edges. -> Option C
Quick Check:
Negative weights handled = B [OK]
Hint: Bellman-Ford works with negative edges, Dijkstra doesn't [OK]
Common Mistakes:
Thinking Bellman-Ford is always faster
Believing it uses less memory than Dijkstra
Assuming it only works on acyclic graphs
2. Which of the following is the correct way to initialize the distance array in the Bellman-Ford algorithm in Go?
easy
A. dist := make([]int, V); for i := range dist { dist[i] = math.MaxInt32 }
B. dist := make([]int, V); for i := range dist { dist[i] = 0 }
C. dist := make([]int, V); for i := range dist { dist[i] = -1 }
D. dist := make([]int, V); for i := range dist { dist[i] = 1 }
Solution
Step 1: Understand distance initialization
Distances to all vertices except the source should be set to a very large number (infinity) to represent unknown distance.
Step 2: Use math.MaxInt32 for infinity
In Go, math.MaxInt32 is used to represent a large integer value for initialization.
Final Answer:
dist := make([]int, V); for i := range dist { dist[i] = math.MaxInt32 } -> Option A
Quick Check:
Initialize distances to max int = A [OK]
Hint: Initialize distances to max int except source [OK]
Common Mistakes:
Initializing distances to zero
Using negative values for distances
Not initializing distances at all
3. What will be the output distances array after running Bellman-Ford on this graph with edges: edges = []Edge{{0,1,4}, {0,2,5}, {1,2,-3}, {2,3,4}} starting from source 0?
medium
A. [0, 4, 5, 9]
B. [0, 4, 1, 5]
C. [0, 4, 1, 9]
D. [0, 4, -3, 4]
Solution
Step 1: Initialize distances
dist = [0, ∞, ∞, ∞]
Step 2: Relax edges repeatedly
After first relaxation: dist[1]=4, dist[2]=5 After second relaxation: dist[2]=min(5,4 + (-3))=1 After third relaxation: dist[3]=dist[2] + 4 = 1 + 4 = 5 After further relaxations no changes occur.
Final Answer:
[0, 4, 1, 5] -> Option B
Quick Check:
Relax edges stepwise = A [OK]
Hint: Relax edges V-1 times to update distances [OK]
Common Mistakes:
Not updating dist[2] after negative edge relaxation
Adding weights incorrectly
Confusing edge directions
4. Identify the bug in this Go code snippet for Bellman-Ford algorithm:
for i := 0; i < V; i++ {
for _, e := range edges {
if dist[e.src] + e.weight < dist[e.dest] {
dist[e.dest] = dist[e.src] + e.weight
}
}
}
medium
A. Loop should run V-1 times, not V times.
B. The condition should use <= instead of <.
C. dist array is not initialized.
D. Edges should be relaxed only once.
Solution
Step 1: Check loop iteration count
Bellman-Ford requires relaxing edges exactly V-1 times to find shortest paths correctly.
Step 2: Identify off-by-one error
The loop runs V times, which is one extra iteration and may cause incorrect behavior or unnecessary work.
Final Answer:
Loop should run V-1 times, not V times. -> Option A
Quick Check:
Relax edges V-1 times = A [OK]
Hint: Relax edges exactly V-1 times [OK]
Common Mistakes:
Running loop V times instead of V-1
Using wrong comparison operator
Not initializing dist array
5. Given a graph with edges: edges = []Edge{{0,1,1}, {1,2,-1}, {2,3,-1}, {3,1,-1}} and source vertex 0, what does Bellman-Ford detect after running?
hard
A. No negative cycle detected; shortest paths found.
B. Distances remain infinite for all vertices except source.
C. Algorithm fails due to zero-weight edges.
D. Negative weight cycle detected involving vertices 1, 2, 3.
Solution
Step 1: Analyze edges for cycles
The edges form a cycle 1 -> 2 -> 3 -> 1 with total weight -3 (negative cycle).
Step 2: Bellman-Ford cycle detection
After V-1 relaxations, one more relaxation detects if any distance can still be reduced, indicating a negative cycle.