# graph-route-finder

1.1.0 • Public • Published

# graph-route-finder

Find all/least-cost routes in weighted directed graph with given limitations. The idea of this lib is to find all possible routes, including the least-cost one, in a weighted directed graph which is commonly used in delivery services. With a bunch of limitations, it can be used in many scenarios. For example, find all possible routes from A to B, while total cost should be less than 20, whole stops should be less than 4.

## How to use

### Refresh graph

#### graph.refresh(nodes)

Which will remove previous nodes and add provided graph nodes to totally refresh graph instance.

### Add a new route or update a existent route

#### graph.set(startNode, endNode [, cost])

By default, cost will be 1 if not provided.

### Remove a existent route

#### graph.remove(startNode, endNode)

Trying to remove a non-existent route will result in no change in graph.

### Calculate cost for given route(a array of nodes)

#### graph.calculate([nodeName1, nodeName2, ...])

This function will go through each node to the next from given node array to calculate cost for the whole route, if a path is not found in the route, `No​ ​Such​ ​Route` error will be returned.

### Find routes for given start node and end node

#### graph.findRoutes(startNode, endNode [, options ])

This function will return a list of all possible routes from start node to end node. The list will be sorted by cost ASC.

• Options:
• `routeLimit`: Infinity. How many routes to return, by giving `routeLimit:1`, it will return only one route which is also the least-cost route.
• `stopLimit`: Infinity. The maximum stops the routes can reach.
• `costLimit`: Infinity. The maximum cost the routes can reach.
• `pathReuseLimit`: 1. Experiment How many time the same route can be reused.

## Package Sidebar

### Install

`npm i graph-route-finder`

### Repository

github.com/CCharlieLi/graph-route-finder

12

1.1.0

MIT

12.5 kB

8