Graphs

Problem shape

Advanced
A
B
C
D
E
Core idea

Relax all edges repeatedly to handle negative weights and detect negative cycles.

Step 1 / 5
Problem shape
Current lesson

Bellman-Ford Lab

Graphs Advanced

Bellman-Ford

Welcome to the Bellman-Ford algorithm lab. Relax all edges repeatedly to handle negative weights and detect negative cycles. Use it when: Use when shortest paths may include negative edges. The time complexity is O(VE), and the space complexity is O(V). The pseudocode is: dist[start] = 0 repeat V - 1 times for each edge u -> v relax edge check one more pass for negative cycle As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(VE)

Space

O(V)

Pseudocode

dist[start] = 0
repeat V - 1 times
  for each edge u -> v
    relax edge
check one more pass for negative cycle
Open deeper structure lesson
1.0x