Skip to content

Instantly share code, notes, and snippets.

@devinivy
Last active November 2, 2015 12:32
Show Gist options
  • Save devinivy/a21b879d23d02aebd971 to your computer and use it in GitHub Desktop.
Save devinivy/a21b879d23d02aebd971 to your computer and use it in GitHub Desktop.
module.exports = function (graph, root) {
var traversal = graph.traverse();
var edges = [];
var stack = [root];
var current = null;
while (stack.length) {
traversal.hop(stack.pop());
current = traversal.currentVertex();
var v;
for (var i = 0; i < current.to.length; i++) {
v = current.to[i];
if (!traversal.visits(v)) {
stack.push(v);
edges.push([current.id, v]);
}
}
}
return graph.subgraph({
vertices: traversal.sequence,
edges: edges
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment