mirror of
https://github.com/anvaka/ngraph.path.git
synced 2025-12-08 19:55:59 +00:00
ngraph.path
Path finding in a graph
usage
Basic usage
This is a basic example, which finds a path between arbitrary two nodes in arbitrary graph
let path = require('ngraph.path');
let pathFinder = path.aStar(graph);
// now we can find a path between two nodes:
let fromNodeId = 40;
let toNodeId = 42;
let foundPath = pathFinder.find(fromNodeId, toNodeId);
// foundPath is array of nodes in the graph
Example above works for any graph, and it's equivalent to unweighted Dijkstra's algorithm.
Weighted graph
Let's say we have the following graph:
let createGraph = require('ngraph.graph');
let graph = createGraph();
graph.addLink('a', 'b', {weight: 10});
graph.addLink('a', 'c', {weight: 10});
graph.addLink('c', 'd', {weight: 5});
graph.addLink('b', 'd', {weight: 10});
license
MIT
Languages
JavaScript
100%