What is the main purpose of the Bellman Ford algorithm?
It finds the shortest paths from a single source vertex to all other vertices in a weighted graph, even if some edge weights are negative.
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 edges multiple times, while Dijkstra's algorithm assumes all weights are non-negative and may fail with negative edges.
Click to reveal answer
beginner
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.
Click to reveal answer
intermediate
What does it mean if 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 infinite reduction of path cost, so shortest paths are undefined.
Click to reveal answer
beginner
How many times does Bellman Ford relax all edges in the graph?
It relaxes all edges exactly V-1 times, where V is the number of vertices, to ensure shortest paths are found.
Click to reveal answer
What is the first step in the Bellman Ford algorithm?
ARelax edges only once
BSort all edges by weight
CDetect negative weight cycles immediately
DInitialize distances from source to all vertices as infinity except source itself as zero
✗ Incorrect
Bellman Ford starts by setting the source distance to zero and all others to infinity.
How does Bellman Ford detect a negative weight cycle?
ABy running Dijkstra's algorithm first
BBy sorting edges and finding cycles
CBy checking if any edge can still be relaxed after V-1 iterations
DBy counting the number of vertices
✗ Incorrect
If after V-1 relaxations, an edge can still be relaxed, a negative cycle exists.
What is the maximum number of times edges are relaxed in Bellman Ford?
AV-1 times
BE times
CV times
DE-1 times
✗ Incorrect
Edges are relaxed V-1 times to guarantee shortest paths.
Which of these graphs is Bellman Ford best suited for?
AGraphs with negative weights but no negative cycles
BGraphs with only positive weights
CGraphs with negative cycles
DUnweighted graphs
✗ Incorrect
Bellman Ford works well with negative weights but cannot produce shortest paths if negative cycles exist.
What happens if Bellman Ford finds a negative weight cycle?
AIt returns shortest paths ignoring the cycle
BIt reports that shortest paths are undefined
CIt stops and returns partial results
DIt converts negative weights to positive
✗ Incorrect
Negative cycles mean shortest paths cannot be defined, so the algorithm reports this.
Explain how the Bellman Ford algorithm finds shortest paths in graphs with negative weights.
Think about repeated edge relaxation and cycle detection.
You got /4 concepts.
Describe how Bellman Ford detects negative weight cycles and why this is important.
Focus on the final check after all relaxations.
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: Recognize Bellman-Ford's capability
Bellman-Ford algorithm can handle negative weight edges and detect negative weight cycles, making it more versatile.
Final Answer:
It can handle graphs with negative weight edges. -> Option D
Quick Check:
Negative weights handled = A [OK]
Hint: Bellman-Ford works with negative edges; Dijkstra does not [OK]
Common Mistakes:
Thinking Bellman-Ford is always faster
Assuming it uses less memory
Believing it 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. int dist[V]; for(int i=0; i
B. int dist[V]; for(int i=0; i
C. int dist[V]; for(int i=0; i
D. int dist[V]; for(int i=0; i
Solution
Step 1: Understand distance initialization
Distances to all vertices except the source should be set to a very large number (infinity), here represented by INT_MAX, to indicate they are initially unreachable.
Step 2: Set source distance to zero
The distance to the source vertex itself must be zero because the shortest path from source to source is zero.
Final Answer:
int dist[V]; for(int i=0; i -> Option B
Quick Check:
Initialize all to max, source to 0 = C [OK]
Hint: Set all distances to INT_MAX except source = 0 [OK]
Common Mistakes:
Initializing all distances to zero
Using -1 for unreachable distances
Setting all distances to source index
3. What will be the output distances array after running Bellman-Ford on this graph with edges: edges = [(0,1,4), (0,2,5), (1,2,-3), (2,3,4)] starting from source 0?
Hint: Relax edges V-1 times to update shortest paths [OK]
Common Mistakes:
Not relaxing edges enough times
Ignoring negative edge effect
Wrong initial distances
4. Identify the error in this Bellman-Ford code snippet in C:
for (int i = 0; i < V; i++) {
for (int j = 0; j < E; j++) {
int u = edges[j].src;
int v = edges[j].dest;
int w = edges[j].weight;
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
}
}
}
medium
A. The outer loop should run V-1 times, not V times.
B. The inner loop should run V times, not E times.
C. The distance array is not initialized.
D. The weight comparison should be dist[v] + w < dist[u].
Solution
Step 1: Check loop iteration count
Bellman-Ford requires relaxing edges exactly V-1 times to find shortest paths correctly.
Step 2: Identify the incorrect loop count
The outer loop runs V times here, which is one extra iteration and unnecessary.
Final Answer:
The outer loop should run V-1 times, not V times. -> Option A
Quick Check:
Outer loop runs V-1 times = D [OK]
Hint: Relax edges exactly V-1 times for correct shortest paths [OK]
Common Mistakes:
Running outer loop V times instead of V-1
Confusing inner loop count with vertex count
Incorrect weight comparison direction
5. Given a graph with edges [(0,1,1), (1,2,-1), (2,3,-1), (3,1,-1)], what will Bellman-Ford detect when run from source 0?
hard
A. It will run infinitely without stopping.
B. It will find shortest paths without any issues.
C. It will detect a negative weight cycle.
D. It will give incorrect distances but no error.
Solution
Step 1: Analyze the cycle in the graph
The edges (1->2), (2->3), and (3->1) form a cycle with total weight -1 + -1 + -1 = -3, which is negative.