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

layered-graph

Package Overview
Dependencies
Maintainers
9
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

layered-graph

Multigraph data structure that collapses to a simple graph

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
38
increased by8.57%
Maintainers
9
Weekly downloads
 
Created
Source

layered-graph

Compose a graph out of multiple sublayers, and in particular, expose a dynamically updating shortest paths calculation.

Later added layers override earlier layers.

API: LayeredGraph({start, max}) => layers

start is a node id that is the "root" of the graph. hops are calculated from this node. max is a float that is the maximum path length to include in the hops calculation.

layers.createLayer (name) => add(g) || add(from, to, value)

create a layer in this graph. returns an add function. The add function should be called with an initial graph, and then new edges. Each layer must be initialized. add({}) is a valid initialization, which is adding an empty graph. add(a, b, 1) would be adding a single edge with weight 1 between a and b.

layers.getGraph() => g

returns the current layered graph merged into one layer. the graph is just a two level js object {}, structure {<id_a>:{<id_b>: <weight>},...}

layers.getHops(opts?) => {: }

return a hops map, of each peer id, to their hop length from start (passed to constructor) If opts is provided, it accepts the following fields: reverse: return hops to start instead of from start. start: calculate hops from/to a different node. max: set a different max distance. If the max is smaller than the default passed to the constructor, the output will be fastest, because it will just copy the cached value, but skip nodes at a greater distance than max.

layers.hopStream() => Source

returns a pull-stream source, where each message is a hops object (as returned by getHops) the first item will be the current state, and any subsequent objects will be diffs to that object, created by edges being added or removed in some layer in real time.

layers.onReady (fn)

call fn back once all layers have been initialized, or immediately if they are already initialized.

layers.onEdge (fn(from, to, value))

call fn when an edge is added or removed from the graph.

layers.reset()

Clear the state held by this instance, basically going back to how things were when you called LayeredGraph({start, max}).

License

MIT

FAQs

Package last updated on 01 Sep 2022

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