A minor re-write of albertorestifo/node-dijkstra with a more limited API.
yarn add smart-parks/dijkstra
Basic example:
const Graph = require('dijkstra')
const route = new Graph()
route.connect('A', 'B', 1)
route.connect('B', 'A', 1)
route.connect('B', 'C', 2)
route.connect('B', 'D', 4)
route.connect('C', 'B', 2)
route.connect('C', 'D', 1)
route.connect('D', 'C', 1)
route.connect('D', 'B', 4)
route.path('A', 'D') // => [ 'A', 'B', 'C', 'D' ]
Object
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
}
}
const route = new Graph()
// or with pre-populated graph
const route = new Graph({
'A': { 'B': 1 },
'B': { 'A': 1, 'C': 2, 'D': 4 }
})
Adds a connection to the graph.
const a = { name: 'Billy' } // any value
const b = { name: 'Sam' } // any value
const route = new Graph()
route.connect(a, b, 100)
Removes a node and all its references from the graph
node
: name of the node to remove
start
: starting nodegoal
: 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
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
.