Challenge - 5 Problems
Bellman-Ford Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Bellman-Ford on a graph with negative weights
What is the output distances array after running Bellman-Ford algorithm on the given graph from source node 0?
DSA Javascript
const edges = [ [0, 1, 4], [0, 2, 5], [1, 2, -3], [2, 3, 4], [3, 1, 6] ]; const V = 4; const source = 0; function bellmanFord(V, edges, source) { const dist = Array(V).fill(Infinity); dist[source] = 0; for (let i = 0; i < V - 1; i++) { for (const [u, v, w] of edges) { if (dist[u] !== Infinity && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; } } } return dist; } console.log(bellmanFord(V, edges, source));
Attempts:
2 left
💡 Hint
Remember Bellman-Ford relaxes edges V-1 times and updates distances if a shorter path is found.
✗ Incorrect
The algorithm starts with distance 0 at source 0. It relaxes edges and finds that node 2 can be reached via node 1 with a shorter path (4 + -3 = 1). Node 3 is reached from node 2 with distance 1 + 4 = 5. Node 1 remains at distance 4. So final distances are [0, 4, 1, 5].
🧠 Conceptual
intermediateDetecting negative weight cycles with Bellman-Ford
After running Bellman-Ford algorithm, how can you detect if the graph contains a negative weight cycle?
Attempts:
2 left
💡 Hint
Bellman-Ford runs V-1 times to relax edges. Extra relaxation means cycle.
✗ Incorrect
After V-1 iterations, all shortest paths should be found. If any edge can still reduce a distance, it means a cycle with negative total weight exists that can be traversed indefinitely to reduce path cost.
🔧 Debug
advancedIdentify the bug in this Bellman-Ford implementation
What error or issue will this code produce when run on a graph with negative weights?
DSA Javascript
function bellmanFord(V, edges, source) {
const dist = Array(V).fill(Infinity);
dist[source] = 0;
for (let i = 0; i < V; i++) { // Loop runs V times instead of V-1
for (const [u, v, w] of edges) {
if (dist[u] !== Infinity && dist[u] + w < dist[v]) { // Added missing check if dist[u] is Infinity
dist[v] = dist[u] + w;
}
}
}
return dist;
}Attempts:
2 left
💡 Hint
Observe the loop condition (V vs V-1) and the relaxation if statement.
✗ Incorrect
The loop runs V times instead of V-1, performing one extra relaxation iteration. In graphs without negative weight cycles, no further updates occur after V-1 iterations, so the shortest paths remain correct. The missing dist[u] !== Infinity check does not cause runtime errors or incorrect updates in JavaScript, since Infinity + w evaluates to Infinity and comparisons behave as expected.
🚀 Application
advancedUsing Bellman-Ford to find shortest paths in a weighted directed graph
Given a weighted directed graph with possible negative edges but no negative cycles, which of the following is the correct output of Bellman-Ford from source 0?
DSA Javascript
const edges = [ [0, 1, 1], [1, 2, -1], [2, 3, -1], [3, 4, -1], [4, 1, 3] ]; const V = 5; const source = 0; function bellmanFord(V, edges, source) { const dist = Array(V).fill(Infinity); dist[source] = 0; for (let i = 0; i < V - 1; i++) { for (const [u, v, w] of edges) { if (dist[u] !== Infinity && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; } } } return dist; } console.log(bellmanFord(V, edges, source));
Attempts:
2 left
💡 Hint
Relax edges multiple times to propagate negative weights correctly.
✗ Incorrect
The path from 0 to 1 costs 1, then 1 to 2 costs -1 (total 0), 2 to 3 costs -1 (total -1), 3 to 4 costs -1 (total -2). The edge 4 to 1 with weight 3 does not create a negative cycle and does not reduce distances further.
🧠 Conceptual
expertWhy Bellman-Ford is preferred over Dijkstra for graphs with negative weights
Which statement best explains why Bellman-Ford algorithm is used instead of Dijkstra's algorithm on graphs with negative edge weights?
Attempts:
2 left
💡 Hint
Think about how each algorithm treats edge weights and updates distances.
✗ Incorrect
Dijkstra's algorithm assumes all edge weights are non-negative to guarantee that once a node's shortest distance is found, it won't be updated. Negative weights break this assumption, causing Dijkstra to produce incorrect results. Bellman-Ford relaxes edges multiple times and can handle negative weights correctly.
