Compute shortest distances from a source vertex to all vertices, allowing negative edge weights.
When to Use
- Graph has negative edge weights.
- Need to detect whether a reachable negative cycle exists.
Idea
- Initialize
dist(s)=0, all others to . - Repeat edge relaxation for all edges exactly times.
- Do one extra pass:
- If any distance still improves, a reachable negative cycle exists.
Relaxation
For each directed edge :
Complexity
- Time:
- Space: (plus optional parent array)
Output
- Distance array
dist - Optional predecessor array
parentfor path reconstruction - Negative-cycle detection result