Skip to content

API reference

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

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

Find the shortest path between nodes using non-negative edge weights.

Preview · Collection › Algorithms › Search and shortest paths

Read in the complete API reference · All API members

#eles.dijkstra(options)

// Compute single-source shortest paths from a root over this collection's subgraph using Dijkstra's algorithm
eles.dijkstra(options: DijkstraOptions): DijkstraResult

Compute single-source shortest paths from a root over this collection's subgraph using Dijkstra's algorithm. The caller is responsible for using non-negative edge weights; the library does not validate the weight function's mathematical properties.

Parameters
NameDescription
options: DijkstraOptions

{ root, weight, directed }. The root must be a node in this collection. weight(edge) returns a number and defaults to 1; directed: true follows outgoing edges only.

Fields of options
NameDescription
root: Collection | null?

Starting node in this collection; required at runtime.

weight: WeightFn?

Non-negative edge cost; defaults to 1 for each edge.

directed: boolean?

Follow outgoing edges only when true; defaults to false.

Returns DijkstraResult

{ distanceTo, pathTo }; an unreachable node has infinite distance and its pathTo() collection contains that node alone. A node outside this subgraph has undefined distance and an empty path.

Example

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

Example graph and handle setup

Throws

if root is missing or is not a node in this collection

#eles.dijkstra(root, weight?, directed?)

// Compute shortest paths with positional arguments
eles.dijkstra(root: Collection, weight?: WeightFn, directed?: boolean): DijkstraResult

Compute shortest paths with positional arguments. Use non-negative weights.

Parameters
NameDescription
root: Collection

a node in this collection

weight: WeightFn?

the edge cost function; defaults to a cost of 1

directed: boolean?

true to follow outgoing edges only; defaults to false

Returns DijkstraResult

shortest-path distanceTo() and pathTo() functions; unreachable nodes have infinite distance, nodes outside this subgraph have undefined distance

Example

const result = eles.dijkstra(cy.getElementById('a'), edge => edge.data('weight'), true);
console.log(result.distanceTo(cy.getElementById('c'))); // 5

Example graph and handle setup

Throws

if root is missing or is not a node in this collection