Debian Codegraph @

lib/order/init-order.js

raw symbols SCIP
1 "use strict";
2
3 var _ = require("../lodash");
4
5 module.exports = initOrder;
6
7 /*
8 * Assigns an initial order value for each node by performing a DFS search
9 * starting from nodes in the first rank. Nodes are assigned an order in their
10 * rank as they are first visited.
11 *
12 * This approach comes from Gansner, et al., "A Technique for Drawing Directed
13 * Graphs."
14 *
15 * Returns a layering matrix with an array per layer and each layer sorted by
16 * the order of its nodes.
17 */
18 function initOrder(g) {
19 var visited = {};
20 var simpleNodes = _.filter(g.nodes(), function(v) {
21 return !g.children(v).length;
22 });
23 var maxRank = _.max(_.map(simpleNodes, function(v) { return g.node(v).rank; }));
24 var layers = _.map(_.range(maxRank + 1), function() { return []; });
25
26 function dfs(v) {
27 if (_.has(visited, v)) return;
28 visited[v] = true;
29 var node = g.node(v);
30 layers[node.rank].push(v);
31 _.forEach(g.successors(v), dfs);
32 }
33
34 var orderedVs = _.sortBy(simpleNodes, function(v) { return g.node(v).rank; });
35 _.forEach(orderedVs, dfs);
36
37 return layers;
38 }