# layered-graph

> Multigraph data structure that collapses to a simple graph

Latest version **1.2.0** (published 2022-09-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install layered-graph
pnpm add layered-graph
yarn add layered-graph
bun add layered-graph
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2022-09-01 |
| First published | 2018-07-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 11.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | Dominic Tarr |
| Maintainers | arj03, chereseeriepa, happy0, staltz, pfrazee, mixmix, cel, christianbundy, dominictarr |

## Links

- npm: https://www.npmjs.com/package/layered-graph
- Repository: https://github.com/ssbc/layered-graph
- Issues: https://github.com/ssbc/layered-graph/issues
- npm.io page: https://npm.io/package/layered-graph

## Dependencies (4)

- [pull-cont](https://npm.io/package/pull-cont.md) ^0.1.1
- [pull-notify](https://npm.io/package/pull-notify.md) ^0.1.1
- [pull-stream](https://npm.io/package/pull-stream.md) ^3.6.9
- [dynamic-dijkstra](https://npm.io/package/dynamic-dijkstra.md) ^1.0.0

## Recent versions

- 1.2.0 (latest) — 2022-09-01
- 1.1.3 — 2019-01-02
- 1.1.2 — 2018-12-04
- 1.1.1 — 2018-10-10
- 1.1.0 — 2018-09-26
- 1.0.0 — 2018-07-26

## README

# 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](https://github.com/dominictarr/dynamic-dijkstra) 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?) => {<id>: <hops>}

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

---
_Source: https://npm.io/package/layered-graph · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
