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
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 and iojs
npm install node-dijkstra --saveNodeJS prior v4.0.0
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 --saveYou can then refer to the v1.1.3 documentation
Usage
Basic example:
const Graph = require('node-dijkstras')
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' ]API
Graph([nodes])
Parameters
Object nodesoptional: 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 = new Graph()
// or with pre-populated graph
const route = new Graph({
'A': { 'B': 1 },
'B': { 'A': 1, 'C': 2, 'D': 4 }
})Graph#addNode(name, edges)
Add a node to the nodes graph
Parameters
String name: name of the nodeObject edges: object containing the name of the neighboring nodes and their cost
Returns
Returns this allowing chained calls.
const route = new Graph()
route.addNode('A', { B: 1 })
// chaining is possible
route.addNode('B', { A: 1 }).addNode('C', { A: 3 });Graph#addVertex(name, edges)
Deprecated: Use Graph#addNode instead
Graph#path(start, goal [, options])
Parameters
String start: Name of the starting nodeString goal: Name of out goal nodeObject optionsoptional: Addittional options:Boolean trim, deafultfalse: 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
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 = require('node-dijkstras')
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']
// trimmed
route.path('A', 'D', { trim: true }) // => [B', 'C']
// reversed
route.path('A', 'D', { reverse: true }) // => ['D', 'C', 'B', 'A']
// include the cost
route.path('A', 'D', { cost: true })
// => {
// path: [ 'A', 'B', 'C', 'D' ],
// cost: 4
// }Graph#shortestPath(origin, destination [, options])
Deprecated: Use Graph#path instead
Upgrading from v1
- The
v2release in not compatible with NodeJS - The method
Graph#shortestPathhas been deprecated, useGraph#pathinstead - The method
Graph#addVertexhas been deprecated, useGraph#addNodeinstead
Testing
npm test