Fast TypeScript implementation of Dijkstra's shortest path algorithm for NodeJS
Requires Node.js 16.0.0 or newer.
npm install node-dijkstraimport { Graph } from 'node-dijkstra';
const route = new Graph();
route.addNode('A', { B: 1 });
route.addNode('B', { A: 1, C: 2, D: 4 });
route.addNode('C', { B: 2, D: 1 });
route.addNode('D', { C: 1, B: 4 });
route.path('A', 'D'); // => ['A', 'B', 'C', 'D']const { Graph } = require('node-dijkstra');
const route = new Graph();
route.addNode('A', { B: 1 });
route.addNode('B', { A: 1, C: 2, D: 4 });
route.addNode('C', { B: 2, D: 1 });
route.addNode('D', { C: 1, B: 4 });
route.path('A', 'D'); // => ['A', 'B', 'C', 'D']type NodeKey = string | number;
type EdgeWeight = number;
type GraphData = Record<string, Record<string, EdgeWeight>>;
interface PathOptions {
trim?: boolean;
reverse?: boolean;
cost?: boolean;
avoid?: NodeKey[];
}
interface PathResult {
path: NodeKey[] | null;
cost: EdgeWeight;
}Creates a new Graph instance with optional initial data.
Adds a node with its neighbors and edge weights.
Removes a node and all references to it.
Finds the shortest path between two nodes. Returns PathResult when options.cost is true.
MIT