2.0.0 • Published 6 years ago

# graphs-tob v2.0.0

4
MIT
Repository
github
Last release
6 years ago

## graphs

An intuitive data structure for graphs, implemented using ES6 data structures.

``````var Graph = require('graphs')
var graph = new Graph()
var a = {name: 'a'}
var b = {name: 'b'}
graph.traverse(function(from, to) {
})
// => a linked to b``````

## Examples

``````var graph = new Graph()
var a = {name: 'a'}

graph.has(a) // => true
graph.size // => 1

var b = {name: 'b'}
graph.has(b) // => false
graph.size // => 2

graph.has(b) // => true``````

``graph.link(a, b)``

### Getting Links to a Node

`.to` and `.from` return ES6 Sets of connected nodes.

``````graph.link(a, b)

graph.from(a) // Set of nodes connected from a
graph.from(a).size // => 1
graph.from(a).has(b) // => true

graph.to(b) // Set of nodes connected to b
graph.to(b).has(a) // => true
graph.from(b).size // => 0``````

``````graph.unlink(a, b)
graph.from(a).size // => 0``````

### Deleting Nodes

``graph.delete(b)``

### Iterating over all Nodes

• `.forEach` will even include entirely unlinked nodes.
``````graph.forEach(function(node) {
console.log('node: %s', node.name)
})``````

### 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` and `to` parameters.
• If startNode is not linked to anything, callback will not fire.
``````graph.traverse(startNode, function(from, to) {
console.log('from: %s', from)
console.log('to: %s', to)
})``````

`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 if `visit` didn't follow a link to the current node (e.g. on the first iteration).
``````graph.visit(startNode, function(node, linkedFrom) {
console.log('node: %s', node)
})``````

## Before/After/Guard Hooks

Makes it easy to embed custom logic into your graph.

``````graph.before('add', function(a,b) {
})