JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 137538
  • Score
    100M100P100Q162521F
  • License MIT

A NodeJS implementation of Dijkstra's algorithm

Package Exports

  • node-dijkstra

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-dijkstra) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status Coverage Status Dependency Status Code Climate

node-dijkstra

A NodeJS (or io.js) implementation of the Dijkstra's shortest path problem.

Installation

npm install node-dijkstra --save

Usage

Basic example:

var Graph = require('node-dijkstras');

var g = new Graph();

g.addVertex('A', {B:1});
g.addVertex('B', {A:1, C:2, D: 4});
g.addVertex('C', {B:2, D:1});
g.addVertex('D', {C:1, B:4});

console.log(g.shortestPath('A', 'D')); // => ['A', 'B', 'C', 'D']

API

Graph([vertices])

Parameters:

  • verticies (optional): An object containing a vertices graph.
var g = new Graph();
// or
var g = new Graph({
  A: {B: 1, C: 2},
  B: {A: 1}
})

addVertex(name, edges)

Parameters:

  • name: name of the vertex
  • edges: object containing the connected vertices and the cost

Returns: this

var g = new Graph();

g.addVertex('A', {B:1});

// you can chain the calls
g.addVertex('B', {A:1}).addVertex('C', {A:3});

shortestPath(start, finish [, options])

Parameters:

  • start: name of the starting vertex
  • finish: name of the end vertex
  • options: optional options object

Returns:

Array containing the crossed vertices names, in order from the starting vertex to the finish vertex, by default it includes the start and finish vertices as well.

Options:

  • trim (default: false): if set to true, it won't include the starting and finish vertices in the returned path.
var Graph = require('node-dijkstras');

var g = new Graph();

g.addVertex('A', {B:1});
g.addVertex('B', {A:1, C:2, D: 4});
g.addVertex('C', {B:2, D:1});
g.addVertex('D', {C:1, B:4});

g.shortestPath('A', 'D'); // => ['A', 'B', 'C', 'D']

// or trimmed
g.shortestPath('A', 'D', {trim: true}); // => [B', 'C']

Testing

npm test