Skip to content

Instantly share code, notes, and snippets.

@anvaka
Last active December 26, 2015 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anvaka/7149637 to your computer and use it in GitHub Desktop.
Save anvaka/7149637 to your computer and use it in GitHub Desktop.
Trying to come up with API for graph traversal. API delays calculation of actual traversal as much as it can. That is, you do not actually starting graph traversal, until you call forEach() method.
// How to get all nodes?
traverse(graph)
.nodes()
.forEach(function(node) {
// node is here
});
// how to get all neighbors of startNodeId?
var grandChildren = traverse(graph)
.nodes()
.neighbors(startNodeId)
.forEach(function(node) {
// node is here
});
// from()/to()/neighborsOf() can also accept traversers.
// E.g. how to get all neighbors of node's neighbors?
var graphNodes = traverse(graph).nodes();
var children = graphNodes.neighbors(startNodeId);
var grandChildren = graphNodes.neighbors(children);
// how to get all links between startNodeId and grandchildren?
traverse(graph)
.links()
.from(children)
.to(grandChildren)
.forEach(function(link) {
// get all links from children to grand children of startNode
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment