Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@vila91/graph

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vila91/graph

Directed and Undirected graph implementation based on the following node structure

  • 2.0.1
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Directed and Undirected graph implementation based on the following node structure. Nodes can be added using at construction or by using the .set(x, y, cost) method with cost defaulting to 1.


	let nodes = {x: {y: 2}, y: {z: 3}},
		graph = Directed(nodes).set(z, x)
	console.log(graph)
	//	Directed {x: {y: 2}, y: {z: 3}, z: {x: 1}}

The .routes(x, y, limit) method returns the shortest routes from x to y, same-cost routes are sorted by number of nodes ascending, limit defaults to Infinity, for performance purposes it is recommended to set it to exactly the number needed.


	graph.set("B", "C").set("C", "D").set("B", "D").set("A", "D", 3)
	console.log(graph.routes("A", "D"))
	/*	[
			{cost: 2, nodes: ["A", "B", "D"]}, 
			{cost: 3, nodes: ["A", "D"]}, 
			{cost: 3, nodes: ["A", "B", "C", "D"]}
		]	*/
		
	console.log(graph.routes("A", "D", 1))
	//	[{cost: 2, nodes: ["A", "B", "D"]}]
	

The .route(x, ...y) method returns the cheapest route going from x through all ...y nodes. It is based on calls to .routes with limit = 1.

	
	let route = graph.route("A", "B", "D")
	console.log(route)
	//	{cost: 2, nodes: ["A", "B", "D"]}
	
	

Keywords

FAQs

Package last updated on 04 Apr 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc