Last active
December 26, 2015 12:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// let's say you have graph 'graph'. | |
// How to traverse all links? | |
traverse(graph) | |
.links() | |
.forEach(function(link){ | |
// link has 'to' and 'from' properties | |
}); | |
// How to get all links from node with 'startNodeId'? | |
traverse(graph) | |
.links() | |
.from(startNodeId) | |
.forEach(function(link){ | |
// link has 'to' and 'from' properties | |
}); | |
// How to get all links to node with 'toNodeId'? | |
traverse(graph) | |
.links() | |
.to(toNodeId) | |
.forEach(function(link){ | |
// link has 'to' and 'from' properties | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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