Skip to content

Graph iterator Bellman-Ford ​

Single-source shortest paths for finite weights of either sign. One step relaxes the outgoing arcs of one reached vertex. The walk makes at most |V| - 1 passes and then checks whether any reached arc can still improve.

Read full: Boost Bellman–Ford

Import ​

ts
import {
  DirectedGraph,
  EnumGraphTraversalType,
  createGraphIterator,
  shortestPath,
} from "@apexds/core";

API reference ​

API: /api/algorithms/graph/iterator-bellman-ford

When the path is ready ​

getPath throws until the walk finishes. shortestPath therefore drains every step, including when the start and the goal are the same vertex. After a successful finish, getPath(from, from) is [from], and getPath(from, to) is the recorded path. An existing vertex that was never reached throws IllegalStateException.

The run copies vertices and weighted arcs before the first step. The vertex array is a new array, so a graph that later mutates the array returned by vertices() does not change this run. Editing the graph later does not change it either. The next initIterator sees the edits. A failed init leaves the previous path in place.

Negative cycles ​

A negative cycle that can be reached from the start fails the whole run with IllegalStateException, even if the requested goal sits on another branch. A cycle in an unreachable component does not. A negative loop on the start fails too, so from === to does not return [from]. The same error is raised when a reached vertex has a negative loop whose subtraction disappears in IEEE-754, such as weight 1e16 followed by a loop of -1. It is also raised for a longer cycle whose own weight is negative, even when a distance along that cycle is not exact. A → B (0), B → A (-1) is that cycle when the path enters through S → A or S → B with weight 1e16 or -1e16. A vertex may be undefined, and it remains a parent. An undirected negative edge is stored in both directions and is a negative cycle. A cycle whose weight is zero does not improve a distance, so the search succeeds.

A distance is kept only when left + right is finite and exact. An inexact sum that does not close a negative cycle throws IllegalArgumentException, and hasNext, current, next, and getPath keep throwing it until the next initIterator. The negative cycle has priority over that precision error. The proof adds reachable weights exactly, so an entry of -1e16 still leaves the cycle weight at -1.

After either error, a new initIterator is allowed.

Example ​

ts
const graph = new DirectedGraph<string>();
graph
  .addVertex("S")
  .addVertex("A")
  .addVertex("T")
  .addEdge("S", "T", 2)
  .addEdge("S", "A", 5)
  .addEdge("A", "T", -10);

shortestPath({
  graph,
  from: "S",
  to: "T",
  traversalType: EnumGraphTraversalType.BELLMAN_FORD,
});
// ["S", "A", "T"], cost -5

const iterator = createGraphIterator(
  graph,
  EnumGraphTraversalType.BELLMAN_FORD,
);
iterator.initIterator("S");
while (iterator.hasNext()) {
  iterator.next();
}
iterator.getPath("S", "T");

Preparation and the worst walk are O(V + V·E). The snapshot uses O(V + E) memory. A sum of two finite costs that overflows throws IllegalArgumentException instead of being treated as unreachable. A failed sum looks for a reachable negative cycle once, adding those weights exactly. Ancestor lists are not walked on every arc.

MIT Licensed