npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graph-route-finder

v1.1.0

Published

Find all/least-cost routes in weighted directed graph with given limitations

Downloads

179

Readme

graph-route-finder

Build Status Coverage Status

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

Init graph

const Graph = require('graph-route-finder');

const graph = new Graph({
      A: { B: 1, C: 4, D: 10 },
      B: { E: 3 },
      C: { D: 4, F: 2 },
      D: { E: 1 },
      E: { B: 3, A: 2 },
      F: { D: 1 },
    }); 

console.log(graph.nodes)
// {
//   A: { B: 1, C: 4, D: 10 },
//   B: { E: 3 },
//   C: { D: 4, F: 2 },
//   D: { E: 1 },
//   E: { B: 3, A: 2 },
//   F: { D: 1 },
// }

Refresh graph

graph.refresh(nodes)

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

graph.refresh({ J: { A: 10 } });

console.log(graph.nodes)
// { J: { A: 10 } }

Get all nodes that start from the given node

graph.from(nodeName)

console.log(graph.from('A'));
// { B: 1, C: 4, D: 10 }

Get all nodes that direct to the given node

graph.to(nodeName)

console.log(graph.to('B'))
// { A: { B: 1 }, E: { B: 3 } }

Add a new route or update a existent route

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

By default, cost will be 1 if not provided.

graph.set('A', 'J', 12);
console.log(graph.nodes);
// {
//   A: { B: 1, C: 4, D: 10, J: 12 },
//   B: { E: 3 },
//   C: { D: 4, F: 2 },
//   D: { E: 1 },
//   E: { B: 3, A: 2 },
//   F: { D: 1 },
// }

graph.set('A', 'B', 12);
console.log(graph.nodes);
// {
//   A: { B: 12, C: 4, D: 10, J: 12 },
//   B: { E: 3 },
//   C: { D: 4, F: 2 },
//   D: { E: 1 },
//   E: { B: 3, A: 2 },
//   F: { D: 1 },
// }

Remove a existent route

graph.remove(startNode, endNode)

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

graph.remove('A', 'B');
console.log(graph.nodes);
// {
//   A: { C: 4, D: 10 },
//   B: { E: 3 },
//   C: { D: 4, F: 2 },
//   D: { E: 1 },
//   E: { B: 3, A: 2 },
//   F: { D: 1 },
// }

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.

graph.calculate(['E', 'A', 'C', 'F'])
// 8

graph.calculate(['A', 'D', 'F']);
// Error { message: 'No​ ​Such​ ​Route }

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.
const routes = graph.findRoutes('E', 'D', { stopLimit: 4 });
// [
//   {
//     nodes: [ 'E', 'A', 'C', 'F', 'D' ],
//     paths: { 'E-A': 1, 'A-C': 1, 'C-F': 1, 'F-D': 1 },
//     cost: 9
//   },
//   {
//     nodes: [ 'E', 'A', 'C', 'D' ],
//     paths: { 'E-A': 1, 'A-C': 1, 'C-D': 1 },
//     cost: 10
//   },
//   { 
//     nodes: [ 'E', 'A', 'D' ], 
//     paths: { 'E-A': 1, 'A-D': 1 }, 
//     cost: 12 
//   },
//   {
//     nodes: [ 'E', 'B', 'E', 'A', 'D' ],
//     paths: { 'E-B': 1, 'B-E': 1, 'E-A': 1, 'A-D': 1 },
//     cost: 18
//   }
// ]

const routes = graph.findRoutes('E', 'E', { costLimit: 10 });
// [
//   { 
//     nodes: [ 'E', 'B', 'E' ], 
//     paths: { 'E-B': 1, 'B-E': 1 }, 
//     cost: 6 },
//   {
//     nodes: [ 'E', 'A', 'B', 'E' ],
//     paths: { 'E-A': 1, 'A-B': 1, 'B-E': 1 },
//     cost: 6
//   },
//   {
//     nodes: [ 'E', 'A', 'C', 'F', 'D', 'E' ],
//     paths: { 'E-A': 1, 'A-C': 1, 'C-F': 1, 'F-D': 1, 'D-E': 1 },
//     cost: 10
//   }
// ]

const routes = graph.findRoutes('E', 'E', { routeLimit: 1 });
// [
//   { nodes: [ 'E', 'B', 'E' ], paths: { 'E-B': 1, 'B-E': 1 }, cost: 6 }
// ]

How to test

yarn test

License