Debian Codegraph @

lib/nesting-graph.js

raw symbols SCIP
1 var _ = require("./lodash");
2 var util = require("./util");
3
4 module.exports = {
5 run: run,
6 cleanup: cleanup
7 };
8
9 /*
10 * A nesting graph creates dummy nodes for the tops and bottoms of subgraphs,
11 * adds appropriate edges to ensure that all cluster nodes are placed between
12 * these boundries, and ensures that the graph is connected.
13 *
14 * In addition we ensure, through the use of the minlen property, that nodes
15 * and subgraph border nodes to not end up on the same rank.
16 *
17 * Preconditions:
18 *
19 * 1. Input graph is a DAG
20 * 2. Nodes in the input graph has a minlen attribute
21 *
22 * Postconditions:
23 *
24 * 1. Input graph is connected.
25 * 2. Dummy nodes are added for the tops and bottoms of subgraphs.
26 * 3. The minlen attribute for nodes is adjusted to ensure nodes do not
27 * get placed on the same rank as subgraph border nodes.
28 *
29 * The nesting graph idea comes from Sander, "Layout of Compound Directed
30 * Graphs."
31 */
32 function run(g) {
33 var root = util.addDummyNode(g, "root", {}, "_root");
34 var depths = treeDepths(g);
35 var height = _.max(_.values(depths)) - 1; // Note: depths is an Object not an array
36 var nodeSep = 2 * height + 1;
37
38 g.graph().nestingRoot = root;
39
40 // Multiply minlen by nodeSep to align nodes on non-border ranks.
41 _.forEach(g.edges(), function(e) { g.edge(e).minlen *= nodeSep; });
42
43 // Calculate a weight that is sufficient to keep subgraphs vertically compact
44 var weight = sumWeights(g) + 1;
45
46 // Create border nodes and link them up
47 _.forEach(g.children(), function(child) {
48 dfs(g, root, nodeSep, weight, height, depths, child);
49 });
50
51 // Save the multiplier for node layers for later removal of empty border
52 // layers.
53 g.graph().nodeRankFactor = nodeSep;
54 }
55
56 function dfs(g, root, nodeSep, weight, height, depths, v) {
57 var children = g.children(v);
58 if (!children.length) {
59 if (v !== root) {
60 g.setEdge(root, v, { weight: 0, minlen: nodeSep });
61 }
62 return;
63 }
64
65 var top = util.addBorderNode(g, "_bt");
66 var bottom = util.addBorderNode(g, "_bb");
67 var label = g.node(v);
68
69 g.setParent(top, v);
70 label.borderTop = top;
71 g.setParent(bottom, v);
72 label.borderBottom = bottom;
73
74 _.forEach(children, function(child) {
75 dfs(g, root, nodeSep, weight, height, depths, child);
76
77 var childNode = g.node(child);
78 var childTop = childNode.borderTop ? childNode.borderTop : child;
79 var childBottom = childNode.borderBottom ? childNode.borderBottom : child;
80 var thisWeight = childNode.borderTop ? weight : 2 * weight;
81 var minlen = childTop !== childBottom ? 1 : height - depths[v] + 1;
82
83 g.setEdge(top, childTop, {
84 weight: thisWeight,
85 minlen: minlen,
86 nestingEdge: true
87 });
88
89 g.setEdge(childBottom, bottom, {
90 weight: thisWeight,
91 minlen: minlen,
92 nestingEdge: true
93 });
94 });
95
96 if (!g.parent(v)) {
97 g.setEdge(root, top, { weight: 0, minlen: height + depths[v] });
98 }
99 }
100
101 function treeDepths(g) {
102 var depths = {};
103 function dfs(v, depth) {
104 var children = g.children(v);
105 if (children && children.length) {
106 _.forEach(children, function(child) {
107 dfs(child, depth + 1);
108 });
109 }
110 depths[v] = depth;
111 }
112 _.forEach(g.children(), function(v) { dfs(v, 1); });
113 return depths;
114 }
115
116 function sumWeights(g) {
117 return _.reduce(g.edges(), function(acc, e) {
118 return acc + g.edge(e).weight;
119 }, 0);
120 }
121
122 function cleanup(g) {
123 var graphLabel = g.graph();
124 g.removeNode(graphLabel.nestingRoot);
125 delete graphLabel.nestingRoot;
126 _.forEach(g.edges(), function(e) {
127 var edge = g.edge(e);
128 if (edge.nestingEdge) {
129 g.removeEdge(e);
130 }
131 });
132 }