BellmanFord
com.phasmidsoftware.gryphon.traverse.BellmanFord
object BellmanFord
Computes shortest paths in a weighted directed graph using the Bellman-Ford-Moore algorithm (queue-based Bellman-Ford).
Unlike Dijkstra, this algorithm correctly handles negative edge weights. Unlike AcyclicShortestPaths, it works on graphs with cycles. It detects negative cycles reachable from the source and returns None in that case.
Algorithm (Bellman-Ford-Moore):
- Seed a queue with
startat distance zero. - Dequeue vertex
v; for each outgoing edge v→w: if dist(w) > dist(v) + weight, relax and enqueue w (if not already queued). - Track enqueue count per vertex — if any vertex is enqueued V times, a negative cycle is reachable → return None.
- Terminate when queue is empty → return Some(result).
Time complexity: O(VE) worst case, much faster in practice due to early termination.
The edge-weight type E requires Monoid (zero + combine) and Ordering (comparison) — consistent with AcyclicShortestPaths.
Attributes
- Graph
-
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
BellmanFord.type
Members list
In this article