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 output distances array after running Bellman-Ford on this graph from source 0?
DSA Python
edges = [(0, 1, 4), (0, 2, 5), (1, 2, -3), (2, 3, 4), (3, 1, 6)] V = 4 source = 0 def bellman_ford(edges, V, source): dist = [float('inf')] * V dist[source] = 0 for _ in range(V - 1): for u, v, w in edges: if dist[u] != float('inf') and dist[u] + w < dist[v]: dist[v] = dist[u] + w return dist result = bellman_ford(edges, V, source) print(result)
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 step by step. The negative edge from 1 to 2 reduces distance to 2 to 1. Then 2 to 3 updates distance to 5. The final distances are [0, 4, 1, 5].
🧠 Conceptual
intermediateDetecting negative weight cycles with Bellman-Ford
Which statement correctly describes how Bellman-Ford detects a negative weight cycle?
Attempts:
2 left
💡 Hint
Think about what happens if distances keep decreasing after all relaxations.
✗ Incorrect
After V-1 iterations, if any edge can still reduce a distance, it means a cycle with negative total weight exists.
🔧 Debug
advancedIdentify the error in this Bellman-Ford implementation
What error will this code produce or what is wrong with it?
DSA Python
def bellman_ford(edges, V, source): dist = [float('inf')] * V dist[source] = 0 for i in range(V): for u, v, w in edges: if dist[u] + w < dist[v]: dist[v] = dist[u] + w return dist edges = [(0, 1, 1), (1, 2, 2)] print(bellman_ford(edges, 3, 0))
Attempts:
2 left
💡 Hint
Check what happens when dist[u] is infinity and you add w to it.
✗ Incorrect
Adding a number to float('inf') is valid in Python, but the code misses a check if dist[u] is infinity before adding w, which can cause wrong comparisons or logic errors.
❓ Predict Output
advancedOutput after detecting negative cycle
What will the output be after running this Bellman-Ford code on a graph with a negative cycle?
DSA Python
edges = [(0, 1, 1), (1, 2, -1), (2, 0, -1)] V = 3 source = 0 def bellman_ford(edges, V, source): dist = [float('inf')] * V dist[source] = 0 for _ in range(V - 1): for u, v, w in edges: if dist[u] != float('inf') and dist[u] + w < dist[v]: dist[v] = dist[u] + w for u, v, w in edges: if dist[u] != float('inf') and dist[u] + w < dist[v]: return 'Negative cycle detected' return dist print(bellman_ford(edges, V, source))
Attempts:
2 left
💡 Hint
Check the final loop that detects if any edge can still be relaxed.
✗ Incorrect
The cycle 0->1->2->0 has negative total weight, so after V-1 relaxations, edges can still be relaxed, triggering the negative cycle detection.
🚀 Application
expertMinimum number of iterations to detect negative cycle
Given a graph with V vertices and a negative weight cycle reachable from the source, what is the minimum number of edge relaxation iterations Bellman-Ford needs to detect the cycle?
Attempts:
2 left
💡 Hint
Think about how Bellman-Ford uses V-1 iterations to find shortest paths and one extra iteration to detect cycles.
✗ Incorrect
Bellman-Ford relaxes edges V-1 times to find shortest paths. The V-th iteration is used to check if any edge can still be relaxed, indicating a negative cycle.
