Calculate the weights between each vertex node and help you find the fastest route.
import { TrafficGraph, TrafficTraversal } from 'traffic-traversal'
const graph = TrafficGraph.Create()
graph.to('start', {
b: 10,
c: 20
}).to('b', { goal: 5 }).to('c', { goal: 5 })
const traversal = TrafficTraversal.Create(graph.state)
traversal.routes('start', 'goal') // ['start', 'b', 'goal']
traversal.traffic('start', 'goal') // 15
traversal.traffic('goal', 'start') // Infinity
traversal.reachable('start', 'goal') // true
traversal.reachable('goal', 'start') // false
traversal.depth('start', 'goal') // 2
traversal.depth('goal', 'start') // Infinity
traversal.distance('start', 'goal') // 2
traversal.distance('goal', 'start') // 2
traversal.edges('start') // ['b', 'c', 'goal']
traversal.edges('start', 1) // ['b', 'c']
Create a new graph instance. You can generate from existing data using data
parameters.
Returns to an array in the form that can serialize the graph information of the current instance.
The current status of the instance is exported to an immutable object.
Returns all the vertices listed in the current instance in an array.
Currently copied instance and returns to a new instance.
Create a single direction weight route. It is possible to traverse the source
to dest
, but vice versa is impossible. If you had the same vertex before, the value is overwritten.
You can specify relative values. If you fill in the prior character =
, -=
, *=
, /=
, The target value is calculated based on the current value of the property.
Set the weight route that leads to both directions between the two vertices. 'a' vertex and 'b' vertex can traverse to each other.
For example, graph.both('a', { b: 1 })
is same as graph.to('a', { b: 1 }).to('b', { a: 1 })
You can specify relative values. If you fill in the prior character =
, -=
, *=
, /=
, The target value is calculated based on the current value of the property.
Set the weight between all vertices passed by parameters.
For example, graph.all({ a: 1, b: 2, c: 3 })
is same as graph.to('a', { b: 2, c: 3 }).to('b', { a: 1, c: 3 }).to('c', { a: 1, b: 2 })
You can specify relative values. If you fill in the prior character =
, -=
, *=
, /=
, The target value is calculated based on the current value of the property.
Delete the single direction weight route created by the to
method.
Delete the bidirectional weight route created by the both
method.
Delete certain vertices. All weight routes connected to the vertex are deleted.
It returns whether the instance has a vertex.
It returns whether all the vertices exist in that instance. Returns false
if any of the vertices are missing.
Invert all weights in an instance. For example, when A to B has a 2
weight, it will be -2
.
It's useful for switching the shortest to longest routes or minimum to maximum traffic in a graph.
Create an instance that is responsible for the route and utility functions of the graph instance. It takes a graph.state
instance as a parameter.
Finds the route with the lowest weight between two vertices and returns it as an array.
Returns a list of vertices adjacent to that vertex as an array. You can set a depth limit using the depth
parameter.
Returns whether the target vertex can be reached from the starting vertex.
Returns the sum of the least weighted routes from the starting vertex to the target vertex. If unreachable, returns Infinity
.
Returns the shortest distance from the starting vertex to the target vertex. This is similar to the distance
method, but takes direction into account. If unreachable, returns Infinity
.
Returns the shortest distance between two vertices. This is similar to the depth
method, but does not take direction into account. If unreachable, returns Infinity
.
npm i traffic-traversal
<script type="module">
import { TrafficGraph, TrafficTraversal } from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/esm/index.min.js'
</script>
MIT LICENSE