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 vertex 0?
DSA Java
class Edge { int src, dest, weight; Edge(int s, int d, int w) { src = s; dest = d; weight = w; } } public class BellmanFord { static int V = 4; static int E = 5; static Edge[] edges = new Edge[E]; public static void main(String[] args) { edges[0] = new Edge(0, 1, 1); edges[1] = new Edge(0, 2, 4); edges[2] = new Edge(1, 2, -3); edges[3] = new Edge(1, 3, 2); edges[4] = new Edge(3, 2, 5); int[] dist = new int[V]; for (int i = 0; i < V; i++) dist[i] = Integer.MAX_VALUE; dist[0] = 0; for (int i = 1; i <= V - 1; 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] != Integer.MAX_VALUE && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; } } } System.out.print("[" + dist[0]); for (int i = 1; i < V; i++) { System.out.print(", " + dist[i]); } System.out.println("]"); } }
Attempts:
2 left
💡 Hint
Remember Bellman-Ford relaxes edges V-1 times and updates distances if a shorter path is found.
✗ Incorrect
The algorithm updates distances by relaxing edges. The negative edge from 1 to 2 reduces distance to 2 to -2 after iterations.
❓ Predict Output
intermediateDetecting negative weight cycle output
What will the program print when detecting a negative weight cycle in the graph?
DSA Java
class Edge { int src, dest, weight; Edge(int s, int d, int w) { src = s; dest = d; weight = w; } } public class BellmanFordCycle { static int V = 3; static int E = 3; static Edge[] edges = new Edge[E]; public static void main(String[] args) { edges[0] = new Edge(0, 1, 1); edges[1] = new Edge(1, 2, -1); edges[2] = new Edge(2, 0, -1); int[] dist = new int[V]; for (int i = 0; i < V; i++) dist[i] = Integer.MAX_VALUE; dist[0] = 0; for (int i = 1; i <= V - 1; 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] != Integer.MAX_VALUE && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; } } } boolean hasNegativeCycle = false; 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] != Integer.MAX_VALUE && dist[u] + w < dist[v]) { hasNegativeCycle = true; break; } } if (hasNegativeCycle) { System.out.println("Negative cycle detected"); } else { System.out.println("No negative cycle"); } } }
Attempts:
2 left
💡 Hint
Check if any edge can still relax after V-1 iterations.
✗ Incorrect
The cycle 0->1->2->0 has total negative weight, so the algorithm detects a negative cycle.
🔧 Debug
advancedIdentify the bug causing incorrect shortest path
Why does the following Bellman-Ford implementation produce incorrect shortest paths when negative edges exist?
DSA Java
int V = 3; int E = 3; Edge[] edges = new Edge[E]; // edges initialization omitted for brevity int[] dist = new int[V]; for (int i = 0; i < V; i++) dist[i] = Integer.MAX_VALUE; dist[0] = 0; for (int i = 0; i < V; i++) { // Loop runs V times instead of V-1 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] != Integer.MAX_VALUE && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; } } }
Attempts:
2 left
💡 Hint
Bellman-Ford requires exactly V-1 iterations to guarantee shortest paths without negative cycles.
✗ Incorrect
Running the loop V times can cause incorrect updates or false detection of negative cycles.
🚀 Application
advancedChoosing Bellman-Ford over Dijkstra
Which scenario best justifies using Bellman-Ford algorithm instead of Dijkstra's algorithm?
Attempts:
2 left
💡 Hint
Dijkstra fails with negative weights; Bellman-Ford handles them safely.
✗ Incorrect
Bellman-Ford works correctly with negative weights and detects negative cycles, unlike Dijkstra.
🧠 Conceptual
expertEffect of negative weight cycles on shortest path results
What is the effect of a negative weight cycle reachable from the source vertex on the shortest path results computed by Bellman-Ford?
Attempts:
2 left
💡 Hint
Think about what happens if you keep going around a negative cycle.
✗ Incorrect
Negative cycles allow infinite reduction of path cost, so shortest paths are not well-defined.
