graphs
An intuitive data structure for graphs, implemented using ES6 data structures.
var Graph = var graph = var a = name: 'a'var b = name: 'b'graphgraphgraphlinka bgraph// => a linked to b
Examples
Adding Nodes
var graph = var a = name: 'a' graphgraph // => truegraphsize // => 1 var b = name: 'b'graph // => falsegraphsize // => 2 graphgraph // => true
Linking Nodes
graphlinka b
Linking will also add nodes to the graph.
Getting Links to a Node
.to
and .from
return ES6 Sets of connected nodes.
graphlinka b graph // Set of nodes connected from agraphsize // => 1graph // => true graph // Set of nodes connected to bgraph // => truegraphsize // => 0
Unlinking Nodes
graphgraphsize // => 0
Deleting Nodes
- Also removes any links (but not linked nodes).
graph
Iterating over all Nodes
.forEach
will even include entirely unlinked nodes.
graph
Traversing the Graph
graph.traverse
will traverse all links from the specified node.
- Arguments to the callback are
from, to
- Starts at a node and follows links.
- May visit a node multiple times (depending on how many times it's linked to).
- The callback will always fire with valid
from
andto
parameters. - If startNode is not linked to anything, callback will not fire.
- Will not follow cycles.
graph
Visiting Linked Nodes
graph.visit
will visit each node that can be reached from the specified node, once.
- Arguments to the callback are
to, from
- Will follow links but will not visit any node more than once.
- The
from
argument may not be set ifvisit
didn't follow a link to the current node (e.g. on the first iteration).
graph
Before/After/Guard Hooks
Makes it easy to embed custom logic into your graph.
graph graph graph
See these libraries for usage information:
License
MIT