Skip to content

eles.aStar(options?)

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

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

Preview · Collection › Algorithms › Search and shortest paths

Read in the complete API reference · All API members

// Find an A path between two nodes in this collection's subgraph
eles.aStar(options?: AStarOptions): AStarResult

Find an A* path between two nodes in this collection's subgraph. The heuristic defaults to zero, reducing the search to Dijkstra's ordering. The search closes nodes without reopening them; shortest-path guarantees require non-negative costs and a consistent heuristic. The library does not validate those mathematical properties.

Parameters
NameDescription
options: AStarOptions?

{ root, goal, weight, heuristic, directed }. Root and goal must belong to this collection; weight(edge) defaults to 1, heuristic(node) defaults to 0, and directed: true follows outgoing edges only.

Fields of options
NameDescription
root: Collection | null?
goal: Collection | null?
weight: WeightFn?
heuristic: AStarHeuristicFn?
directed: boolean?
Returns AStarResult

{ found, distance, path, steps }; when no path exists, found is false and distance and path are undefined

Example

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

Example graph and handle setup

Throws

if root or goal is missing or outside this collection