node-dijkstra
Fast JavaScript implementation of the Dijkstra's shortest path problem for NodeJS
Installation
Since version 2 this plugin uses some ES6 features. You can run the latest version on NodeJS v4.0.0
or newer
npm install node-dijkstra --save
v4.0.0
NodeJS prior On versions of NodeJS prior v4.0.0
, although less performant, it's safe to use the version 1.1.3
that you can install as follows:
npm install node-dijkstra@1.1.3 --save
You can then refer to the v1.1.3
documentation
Usage
Basic example:
const Graph = const route = routerouterouteroute route // => [ 'A', 'B', 'C', 'D' ]
API
Graph([nodes])
Parameters
Object|Map nodes
optional: Initial nodes graph.
A nodes graph must follow this structure:
{
node: {
neighbor: cost Number
}
}
'A': 'B': 1 'B': 'A': 1 'C': 2 'D': 4
Example
const route = // or with pre-populated graphconst route = 'A': 'B': 1 'B': 'A': 1 'C': 2 'D': 4
It's possible to pass the constructor a deep Map
. This allows using numbers as keys for the nodes.
const graph = const a = a const b = bbb graphgraph; const route = graph
Graph#addNode(name, edges)
Add a node to the nodes graph
Parameters
String name
: name of the nodeObject|Map edges
: object orMap
containing the name of the neighboring nodes and their cost
Returns
Returns this
allowing chained calls.
const route = route // chaining is possibleroute; // passing a Map directly is possibleconst c = c route;
Graph#removeNode(name)
Removes a node and all its references from the graph
Parameters
String name
: name of the node to remove
Returns
Returns this
allowing chained calls.
const route = a: b: 3 c: 10 b: a: 5 c: 2 c: b: 1 ; route;// => The graph now is:// {// a: { b: 3 },// b: { a: 5 },// }
Graph#path(start, goal [, options])
Parameters
String start
: Name of the starting nodeString goal
: Name of out goal nodeObject options
optional: Addittional options:Boolean trim
, defaultfalse
: If set to true, the result won't include the start and goal nodesBoolean reverse
, defaultfalse
: If set to true, the result will be in reverse order, from goal to startBoolean cost
, defaultfalse
: If set to true, an object will be returned with the following keys:Array path
: Computed path (subject to other options)Number cost
: Total cost for the found path
Array avoid
, default[]
: Nodes to be avoided
Returns
If options.cost
is false
(default behaviour) an Array
will be returned, containing the name of the crossed nodes. By default it will be ordered from start to goal, and those nodes will also be included. This behaviour can be changes with options.trim
and options.reverse
(see above)
If options.cost
is true
, an Object
with keys path
and cost
will be returned. path
follows the same rules as above and cost
is the total cost of the found route between nodes.
When to route can be found, the path will be set to null
.
const Graph = const route = routerouterouteroute route // => ['A', 'B', 'C', 'D'] // trimmedroute // => [B', 'C'] // reversedroute // => ['D', 'C', 'B', 'A'] // include the costroute// => {// path: [ 'A', 'B', 'C', 'D' ],// cost: 4// }
v1
Upgrading from - The
v2
release in not compatible with NodeJS prior to the version 4.0 - The method
Graph#shortestPath
has been deprecated, useGraph#path
instead - The method
Graph#addVertex
has been deprecated, useGraph#addNode
instead
Testing
npm test