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):

  1. Seed a queue with start at distance zero.
  2. Dequeue vertex v; for each outgoing edge v→w: if dist(w) > dist(v) + weight, relax and enqueue w (if not already queued).
  3. Track enqueue count per vertex — if any vertex is enqueued V times, a negative cycle is reachable → return None.
  4. 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 Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def shortestPaths[V, E : Monoid : Ordering](graph: DirectedGraph[V, E], start: V): Option[VertexTraversalResult[V, DirectedEdge[V, E]]]

Computes shortest paths from start to all reachable vertices in graph.

Computes shortest paths from start to all reachable vertices in graph.

Type parameters

E

the edge-weight type; must have Monoid and Ordering.

V

the vertex type.

Value parameters

graph

the directed graph (may contain cycles and negative weights).

start

the source vertex.

Attributes

Returns

Some(VertexTraversalResult) mapping each reachable non-source vertex to its shortest-path incoming edge, or None if a negative cycle is reachable from start.