Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
graph-route-finder
Advanced tools
Find all/least-cost routes in weighted directed graph with given limitations
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.
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 },
// }
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 } }
console.log(graph.from('A'));
// { B: 1, C: 4, D: 10 }
console.log(graph.to('B'))
// { A: { B: 1 }, E: { B: 3 } }
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 },
// }
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 },
// }
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 }
This function will return a list of all possible routes from start node to end node. The list will be sorted by cost ASC.
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 }
// ]
yarn test
FAQs
Find all/least-cost routes in weighted directed graph with given limitations
We found that graph-route-finder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.