@snyk/dep-graph
Version:
Snyk dependency graph library
43 lines • 1.31 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.dfs = void 0;
const each = require("lodash.foreach");
/*
* A helper that preforms a pre- or post-order traversal on the input graph
* and returns the nodes in the order they were visited. If the graph is
* undirected then this algorithm will navigate using neighbors. If the graph
* is directed then this algorithm will navigate using successors.
*
* Order must be one of "pre" or "post".
*/
function dfs(g, vs, order) {
if (!Array.isArray(vs)) {
vs = [vs];
}
const navigation = (g.isDirected() ? g.successors : g.neighbors).bind(g);
const acc = [];
const visited = {};
each(vs, (v) => {
if (!g.hasNode(v)) {
throw new Error('Graph does not have node: ' + v);
}
doDfs(g, v, order === 'post', visited, navigation, acc);
});
return acc;
}
exports.dfs = dfs;
function doDfs(g, v, postorder, visited, navigation, acc) {
if (!(v in visited)) {
visited[v] = true;
if (!postorder) {
acc.push(v);
}
each(navigation(v), function (w) {
doDfs(g, w, postorder, visited, navigation, acc);
});
if (postorder) {
acc.push(v);
}
}
}
//# sourceMappingURL=dfs.js.map
;