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 edges
What is the shortest distance array output after running Bellman-Ford on the given graph from source node 0?
DSA C++
/* Graph edges: (u, v, w) */ // (0, 1, 4), (0, 2, 5), (1, 2, -3), (2, 3, 4), (3, 1, 6) #include <iostream> #include <vector> #include <limits> using namespace std; struct Edge { int u, v, w; }; int main() { int V = 4; int E = 5; vector<Edge> edges = {{0,1,4},{0,2,5},{1,2,-3},{2,3,4},{3,1,6}}; vector<int> dist(V, numeric_limits<int>::max()); dist[0] = 0; for (int i = 1; i <= V - 1; i++) { for (auto &e : edges) { if (dist[e.u] != numeric_limits<int>::max() && dist[e.u] + e.w < dist[e.v]) { dist[e.v] = dist[e.u] + e.w; } } } cout << "Distances: "; for (int d : dist) cout << d << " "; cout << endl; return 0; }
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 dist[0] = 0 and others infinity. After first iteration, dist[1] = 4, dist[2] = 5. Then relaxing edge (1,2,-3) updates dist[2] to 1 (4 + -3). Next, dist[3] becomes 5 (1 + 4). No further improvements after V-1 iterations. So final distances are [0, 4, 1, 5].
🧠 Conceptual
intermediateDetecting negative weight cycles with Bellman-Ford
After running Bellman-Ford for V-1 iterations, what is the next step to detect if a negative weight cycle exists in the graph?
Attempts:
2 left
💡 Hint
Negative cycles allow distances to keep decreasing after V-1 relaxations.
✗ Incorrect
If after V-1 iterations, any edge can still relax a distance, it means a negative cycle exists because distances should have stabilized otherwise.
🔧 Debug
advancedIdentify the bug in this Bellman-Ford implementation snippet
What is the bug in the following code snippet that causes incorrect shortest path calculation?
DSA C++
for (int i = 0; i < V; i++) { for (auto &e : edges) { if (dist[e.u] + e.w < dist[e.v]) { dist[e.v] = dist[e.u] + e.w; } } }
Attempts:
2 left
💡 Hint
Bellman-Ford requires exactly V-1 iterations to guarantee shortest paths.
✗ Incorrect
The code runs the relaxation loop V times instead of V-1 times. Bellman-Ford requires exactly V-1 iterations to find the shortest paths (in terms of simple paths). An extra iteration is typically used only for detecting negative cycles.
❓ Predict Output
advancedOutput after detecting negative cycle
What will the program output after running Bellman-Ford on this graph with edges: (0,1,1), (1,2,-1), (2,0,-1) from source 0?
DSA C++
#include <iostream> #include <vector> #include <limits> using namespace std; struct Edge { int u, v, w; }; int main() { int V = 3; int E = 3; vector<Edge> edges = {{0,1,1},{1,2,-1},{2,0,-1}}; vector<int> dist(V, numeric_limits<int>::max()); dist[0] = 0; for (int i = 1; i <= V - 1; i++) { for (auto &e : edges) { if (dist[e.u] != numeric_limits<int>::max() && dist[e.u] + e.w < dist[e.v]) { dist[e.v] = dist[e.u] + e.w; } } } bool negative_cycle = false; for (auto &e : edges) { if (dist[e.u] != numeric_limits<int>::max() && dist[e.u] + e.w < dist[e.v]) { negative_cycle = true; break; } } if (negative_cycle) cout << "Negative cycle detected" << endl; else { cout << "Distances: "; for (int d : dist) cout << d << " "; cout << endl; } return 0; }
Attempts:
2 left
💡 Hint
Check if any edge can still relax after V-1 iterations.
✗ Incorrect
The cycle 0->1->2->0 has total weight 1 + (-1) + (-1) = -1, which is negative. After V-1 iterations, distances can still be improved, so the program detects a negative cycle and prints the message.
🧠 Conceptual
expertWhy Bellman-Ford is preferred over Dijkstra for graphs with negative weights
Which of the following best explains why Bellman-Ford algorithm is used instead of Dijkstra's algorithm when graphs have negative edge weights?
Attempts:
2 left
💡 Hint
Think about how negative weights affect shortest path calculations.
✗ Incorrect
Dijkstra's algorithm assumes all edge weights are non-negative to guarantee correctness. It fails or gives wrong results if negative weights exist. Bellman-Ford handles negative weights and can detect negative cycles.
