What problem does the Bellman Ford algorithm solve?
It finds the shortest path from a single source vertex to all other vertices in a weighted graph, even if some edges have negative weights.
Click to reveal answer
beginner
Why can Dijkstra's algorithm fail with negative edge weights?
Because it assumes that once a vertex's shortest path is found, it cannot be improved. Negative weights can reduce path costs later, breaking this assumption.
Click to reveal answer
intermediate
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 in the graph.
Click to reveal answer
intermediate
What does Bellman Ford do to detect negative weight cycles?
After |V| - 1 relaxations, it checks all edges one more time. If any edge can still be relaxed, it means 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 is the main advantage of Bellman Ford over Dijkstra's algorithm?
AIt only works on unweighted graphs
BIt works with graphs that have negative edge weights
CIt uses less memory
DIt is faster for all graphs
✗ Incorrect
Bellman Ford can handle negative edge weights, unlike Dijkstra's algorithm.
How many times does Bellman Ford relax edges in the worst case?
A|V| times
B|E| times
C|V| - 1 times
DOnce
✗ Incorrect
Bellman Ford relaxes all edges |V| - 1 times to find shortest paths.
What does it mean if Bellman Ford can still relax an edge after |V| - 1 iterations?
AThere is a negative weight cycle
BGraph is disconnected
CAll shortest paths are found
DGraph has no edges
✗ Incorrect
Further relaxation indicates a negative weight cycle in the graph.
Which data structure is primarily used to represent the graph in Bellman Ford?
AEdge list
BAdjacency list
CAdjacency matrix
DStack
✗ Incorrect
Bellman Ford typically uses an edge list to relax edges efficiently.
What is the time complexity of Bellman Ford algorithm?
AO(|V| + |E|)
BO(|E|^2)
CO(|V|^2)
DO(|V| * |E|)
✗ Incorrect
Bellman Ford runs in O(|V| * |E|) time.
Explain how Bellman Ford algorithm detects negative weight cycles.
Think about what happens if distances keep improving after expected iterations.
You got /4 concepts.
Describe the step-by-step process of the Bellman Ford algorithm for finding shortest paths.
Start from setting initial distances and repeat edge relaxation.
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 uses less memory.
B. It runs faster on all graphs.
C. It can handle graphs with negative weight edges.
D. It only works on directed acyclic graphs.
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 C
Quick Check:
Negative weights handled = A [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
Assuming it only works on acyclic graphs
2. Which of the following is the correct TypeScript syntax to initialize the distance array for Bellman-Ford with infinity except the source node set to 0?
easy
A. const dist = new Array(n).fill(Infinity); dist[source] = 0;
B. const dist = Array(n).fill(0); dist[source] = Infinity;
C. const dist = new Array(n).fill(0); dist[source] = 0;
D. const dist = new Array(n).fill(-Infinity); dist[source] = 0;
Solution
Step 1: Initialize distances with Infinity
We start with all distances as Infinity to represent unknown paths, except the source which is 0.
Step 2: Correct syntax for array initialization
Using new Array(n).fill(Infinity) creates an array of size n filled with Infinity, then setting dist[source] = 0 sets the source distance correctly.
Final Answer:
const dist = new Array(n).fill(Infinity); dist[source] = 0; -> Option A
Quick Check:
Initialize with Infinity except source 0 = C [OK]
Hint: Fill array with Infinity, then set source to 0 [OK]
Common Mistakes:
Filling with 0 instead of Infinity
Setting source distance to Infinity
Using -Infinity which is incorrect
3. Consider the following TypeScript code snippet implementing one iteration of edge relaxation in Bellman-Ford:
for (const [u, v, w] of edges) {
if (dist[u] !== Infinity && dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
}
}
console.log(dist);
If edges = [[0,1,4],[1,2,-2],[0,2,5]] and initial dist = [0, Infinity, Infinity], what will be the output after this iteration?