Debian Codegraph @

lib/rank/network-simplex.js

raw symbols SCIP
1 "use strict";
2
3 var _ = require("../lodash");
4 var feasibleTree = require("./feasible-tree");
5 var slack = require("./util").slack;
6 var initRank = require("./util").longestPath;
7 var preorder = require("../graphlib").alg.preorder;
8 var postorder = require("../graphlib").alg.postorder;
9 var simplify = require("../util").simplify;
10
11 module.exports = networkSimplex;
12
13 // Expose some internals for testing purposes
14 networkSimplex.initLowLimValues = initLowLimValues;
15 networkSimplex.initCutValues = initCutValues;
16 networkSimplex.calcCutValue = calcCutValue;
17 networkSimplex.leaveEdge = leaveEdge;
18 networkSimplex.enterEdge = enterEdge;
19 networkSimplex.exchangeEdges = exchangeEdges;
20
21 /*
22 * The network simplex algorithm assigns ranks to each node in the input graph
23 * and iteratively improves the ranking to reduce the length of edges.
24 *
25 * Preconditions:
26 *
27 * 1. The input graph must be a DAG.
28 * 2. All nodes in the graph must have an object value.
29 * 3. All edges in the graph must have "minlen" and "weight" attributes.
30 *
31 * Postconditions:
32 *
33 * 1. All nodes in the graph will have an assigned "rank" attribute that has
34 * been optimized by the network simplex algorithm. Ranks start at 0.
35 *
36 *
37 * A rough sketch of the algorithm is as follows:
38 *
39 * 1. Assign initial ranks to each node. We use the longest path algorithm,
40 * which assigns ranks to the lowest position possible. In general this
41 * leads to very wide bottom ranks and unnecessarily long edges.
42 * 2. Construct a feasible tight tree. A tight tree is one such that all
43 * edges in the tree have no slack (difference between length of edge
44 * and minlen for the edge). This by itself greatly improves the assigned
45 * rankings by shorting edges.
46 * 3. Iteratively find edges that have negative cut values. Generally a
47 * negative cut value indicates that the edge could be removed and a new
48 * tree edge could be added to produce a more compact graph.
49 *
50 * Much of the algorithms here are derived from Gansner, et al., "A Technique
51 * for Drawing Directed Graphs." The structure of the file roughly follows the
52 * structure of the overall algorithm.
53 */
54 function networkSimplex(g) {
55 g = simplify(g);
56 initRank(g);
57 var t = feasibleTree(g);
58 initLowLimValues(t);
59 initCutValues(t, g);
60
61 var e, f;
62 while ((e = leaveEdge(t))) {
63 f = enterEdge(t, g, e);
64 exchangeEdges(t, g, e, f);
65 }
66 }
67
68 /*
69 * Initializes cut values for all edges in the tree.
70 */
71 function initCutValues(t, g) {
72 var vs = postorder(t, t.nodes());
73 vs = vs.slice(0, vs.length - 1);
74 _.forEach(vs, function(v) {
75 assignCutValue(t, g, v);
76 });
77 }
78
79 function assignCutValue(t, g, child) {
80 var childLab = t.node(child);
81 var parent = childLab.parent;
82 t.edge(child, parent).cutvalue = calcCutValue(t, g, child);
83 }
84
85 /*
86 * Given the tight tree, its graph, and a child in the graph calculate and
87 * return the cut value for the edge between the child and its parent.
88 */
89 function calcCutValue(t, g, child) {
90 var childLab = t.node(child);
91 var parent = childLab.parent;
92 // True if the child is on the tail end of the edge in the directed graph
93 var childIsTail = true;
94 // The graph's view of the tree edge we're inspecting
95 var graphEdge = g.edge(child, parent);
96 // The accumulated cut value for the edge between this node and its parent
97 var cutValue = 0;
98
99 if (!graphEdge) {
100 childIsTail = false;
101 graphEdge = g.edge(parent, child);
102 }
103
104 cutValue = graphEdge.weight;
105
106 _.forEach(g.nodeEdges(child), function(e) {
107 var isOutEdge = e.v === child,
108 other = isOutEdge ? e.w : e.v;
109
110 if (other !== parent) {
111 var pointsToHead = isOutEdge === childIsTail,
112 otherWeight = g.edge(e).weight;
113
114 cutValue += pointsToHead ? otherWeight : -otherWeight;
115 if (isTreeEdge(t, child, other)) {
116 var otherCutValue = t.edge(child, other).cutvalue;
117 cutValue += pointsToHead ? -otherCutValue : otherCutValue;
118 }
119 }
120 });
121
122 return cutValue;
123 }
124
125 function initLowLimValues(tree, root) {
126 if (arguments.length < 2) {
127 root = tree.nodes()[0];
128 }
129 dfsAssignLowLim(tree, {}, 1, root);
130 }
131
132 function dfsAssignLowLim(tree, visited, nextLim, v, parent) {
133 var low = nextLim;
134 var label = tree.node(v);
135
136 visited[v] = true;
137 _.forEach(tree.neighbors(v), function(w) {
138 if (!_.has(visited, w)) {
139 nextLim = dfsAssignLowLim(tree, visited, nextLim, w, v);
140 }
141 });
142
143 label.low = low;
144 label.lim = nextLim++;
145 if (parent) {
146 label.parent = parent;
147 } else {
148 // TODO should be able to remove this when we incrementally update low lim
149 delete label.parent;
150 }
151
152 return nextLim;
153 }
154
155 function leaveEdge(tree) {
156 return _.find(tree.edges(), function(e) {
157 return tree.edge(e).cutvalue < 0;
158 });
159 }
160
161 function enterEdge(t, g, edge) {
162 var v = edge.v;
163 var w = edge.w;
164
165 // For the rest of this function we assume that v is the tail and w is the
166 // head, so if we don't have this edge in the graph we should flip it to
167 // match the correct orientation.
168 if (!g.hasEdge(v, w)) {
169 v = edge.w;
170 w = edge.v;
171 }
172
173 var vLabel = t.node(v);
174 var wLabel = t.node(w);
175 var tailLabel = vLabel;
176 var flip = false;
177
178 // If the root is in the tail of the edge then we need to flip the logic that
179 // checks for the head and tail nodes in the candidates function below.
180 if (vLabel.lim > wLabel.lim) {
181 tailLabel = wLabel;
182 flip = true;
183 }
184
185 var candidates = _.filter(g.edges(), function(edge) {
186 return flip === isDescendant(t, t.node(edge.v), tailLabel) &&
187 flip !== isDescendant(t, t.node(edge.w), tailLabel);
188 });
189
190 return _.minBy(candidates, function(edge) { return slack(g, edge); });
191 }
192
193 function exchangeEdges(t, g, e, f) {
194 var v = e.v;
195 var w = e.w;
196 t.removeEdge(v, w);
197 t.setEdge(f.v, f.w, {});
198 initLowLimValues(t);
199 initCutValues(t, g);
200 updateRanks(t, g);
201 }
202
203 function updateRanks(t, g) {
204 var root = _.find(t.nodes(), function(v) { return !g.node(v).parent; });
205 var vs = preorder(t, root);
206 vs = vs.slice(1);
207 _.forEach(vs, function(v) {
208 var parent = t.node(v).parent,
209 edge = g.edge(v, parent),
210 flipped = false;
211
212 if (!edge) {
213 edge = g.edge(parent, v);
214 flipped = true;
215 }
216
217 g.node(v).rank = g.node(parent).rank + (flipped ? edge.minlen : -edge.minlen);
218 });
219 }
220
221 /*
222 * Returns true if the edge is in the tree.
223 */
224 function isTreeEdge(tree, u, v) {
225 return tree.hasEdge(u, v);
226 }
227
228 /*
229 * Returns true if the specified node is descendant of the root node per the
230 * assigned low and lim attributes in the tree.
231 */
232 function isDescendant(tree, vLabel, rootLabel) {
233 return rootLabel.low <= vLabel.lim && vLabel.lim <= rootLabel.lim;
234 }