BellmanFord Module
Bellman-Ford algorithm for single-source shortest paths with support for negative edge weights.
The Bellman-Ford algorithm finds shortest paths from a source node to all other nodes, even when edges have negative weights. It can also detect negative cycles reachable from the source, which make shortest paths undefined.
Algorithm
Algorithm |
Function |
Complexity |
Best For |
|---|---|---|---|
Bellman-Ford |
bellmanFord |
O(VE) |
Negative weights, cycle detection |
SPFA (Queue-optimized) |
implicitBellmanFord |
O(E) average |
Sparse graphs with few negative edges |
Key Concepts
- Relaxation: Repeatedly improve distance estimates (V-1 passes)
- Negative Cycle: Cycle with total negative weight (no shortest path exists)
- Shortest Path Tree: Tree of shortest paths from source to all nodes
Why V-1 Relaxation Passes?
In a graph with V nodes, any shortest path has at most V-1 edges. Each pass of Bellman-Ford relaxes all edges, propagating shortest path information one hop further each time.
Comparison with Dijkstra
Feature |
Bellman-Ford |
Dijkstra |
|---|---|---|
Negative weights |
✓ Yes |
✗ No |
Negative cycle detection |
✓ Yes |
✗ N/A |
Time complexity |
O(VE) |
O((V+E) log V) |
Data structure |
Simple loops |
Priority queue |
Use Cases
- Currency arbitrage: Detecting negative cycles in exchange rates
- Financial modeling: Cost calculations with credits/penalties
- Chemical reactions: Energy changes with positive and negative values
- Constraint solving: Difference constraints systems
History
Published independently by Richard Bellman (1958) and Lester Ford Jr. (1956). The algorithm is a classic example of dynamic programming.
Types
| Type | Description |
|
Result type for Bellman-Ford algorithm. |
|
|
Result type for implicit Bellman-Ford algorithm. |
Functions and values
| Function or value |
Description
|
Full Usage:
bellmanFord zero add compare start goal graph
Parameters:
'e
add : 'e -> 'e -> 'e
compare : 'e -> 'e -> int
start : NodeId
goal : NodeId
graph : Graph<'n, 'e>
Returns: BellmanFordResult<'e>
|
Finds shortest path with support for negative edge weights using Bellman-Ford. Time Complexity: O(VE)
|
|
Finds shortest path with float weights, handling negative edges.
|
|
Finds shortest path with integer weights, handling negative edges.
|
Full Usage:
implicitBellmanFord zero add compare successors isGoal start
Parameters:
'cost
add : 'cost -> 'cost -> 'cost
compare : 'cost -> 'cost -> int
successors : 'state -> ('state * 'cost) list
isGoal : 'state -> bool
start : 'state
Returns: ImplicitBellmanFordResult<'cost>
|
Finds shortest path in implicit graphs with support for negative edge weights. Uses SPFA (Shortest Path Faster Algorithm) internally.
|
Full Usage:
implicitBellmanFordBy zero add compare successors keyFn isGoal start
Parameters:
'cost
add : 'cost -> 'cost -> 'cost
compare : 'cost -> 'cost -> int
successors : 'state -> ('state * 'cost) list
keyFn : 'state -> 'key
isGoal : 'state -> bool
start : 'state
Returns: ImplicitBellmanFordResult<'cost>
|
Like
|