Skip to content
forked from timoxley/graphs

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

License

Notifications You must be signed in to change notification settings

Saleslogix/graphs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.add(a)
graph.add(b)
graph.link(a, b)
graph.traverse(function(from, to) {
  console.log(from.name, 'linked to', to.name)
})
// => a linked to b

Examples

Adding Nodes

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

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

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

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

Linking Nodes

graph.link(a, b)

Linking will also add nodes to the graph.

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

Unlinking Nodes

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

Deleting Nodes

  • Also removes any links (but not linked 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.
  • Will not follow cycles.
graph.traverse(startNode, function(from, to) {
  console.log('from: %s', from)
  console.log('to: %s', to)
})

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 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)
  console.log('linkedFrom: %s', linkedFrom)
})

Before/After/Guard Hooks

Makes it easy to embed custom logic into your graph.

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

graph.after('add', function(a,b) {
  // execute after add
})

graph.guard('add', function(node) {
  // prevent add from running if return falsey
})

See these libraries for usage information:

License

MIT

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%