Skip to content

eles.bellmanFord(options?)

eles.bellmanFord — Collection > Algorithms > Search and shortest paths. Cytoscape.js signatures, parameters and return contract.

eles.bellmanFord — Collection › Algorithms › Search and shortest paths

Preview · Collection › Algorithms › Search and shortest paths

Read in the complete API reference · All API members

// Compute single-source shortest paths with Bellman–Ford
eles.bellmanFord(options?: BellmanFordOptions): BellmanFordResult

Compute single-source shortest paths with Bellman–Ford. Unlike dijkstra(), this algorithm supports negative edge weights and reports reachable negative-weight cycles. Undirected, every edge is both arcs, so a negative edge reachable from the root is itself a negative cycle .

Parameters
NameDescription
options: BellmanFordOptions?

{ root, weight, directed, findNegativeWeightCycles }. The root must belong to this collection; weight(edge) defaults to 1 and directed: true follows outgoing edges only. Set findNegativeWeightCycles: false to stop after detecting a cycle without constructing cycle collections.

Fields of options
NameDescription
root: Collection | null?
weight: WeightFn?
directed: boolean?
findNegativeWeightCycles: boolean?
Returns BellmanFordResult

distance and path accessors, hasNegativeWeightCycle, and negativeWeightCycles (collections describing detected cycles); unreachable nodes have infinite distance and an empty path

Example

const result = eles.bellmanFord({ root: cy.getElementById('a'), weight: edge => edge.data('weight') });
console.log(result.distanceTo(cy.getElementById('c'))); // 5

Example graph and handle setup

Throws

if root is missing or does not contain a node