@dagrejs/dagre 1.1.5 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/dagre.cjs.js +3 -0
  2. package/{index.js → dist/dagre.cjs.js.LEGAL.txt} +2 -12
  3. package/dist/dagre.cjs.js.map +7 -0
  4. package/dist/dagre.esm.js +3 -0
  5. package/dist/dagre.esm.js.LEGAL.txt +23 -0
  6. package/dist/dagre.esm.js.map +7 -0
  7. package/dist/dagre.js +3384 -4267
  8. package/dist/dagre.js.LEGAL.txt +23 -0
  9. package/dist/dagre.js.map +7 -0
  10. package/dist/dagre.min.js +3 -801
  11. package/dist/dagre.min.js.LEGAL.txt +23 -0
  12. package/dist/dagre.min.js.map +7 -0
  13. package/package.json +18 -23
  14. package/index.d.ts +0 -147
  15. package/lib/acyclic.js +0 -67
  16. package/lib/add-border-segments.js +0 -37
  17. package/lib/coordinate-system.js +0 -70
  18. package/lib/data/list.js +0 -58
  19. package/lib/debug.js +0 -31
  20. package/lib/greedy-fas.js +0 -124
  21. package/lib/layout.js +0 -405
  22. package/lib/nesting-graph.js +0 -126
  23. package/lib/normalize.js +0 -89
  24. package/lib/order/add-subgraph-constraints.js +0 -51
  25. package/lib/order/barycenter.js +0 -26
  26. package/lib/order/build-layer-graph.js +0 -73
  27. package/lib/order/cross-count.js +0 -66
  28. package/lib/order/index.js +0 -81
  29. package/lib/order/init-order.js +0 -37
  30. package/lib/order/resolve-conflicts.js +0 -118
  31. package/lib/order/sort-subgraph.js +0 -73
  32. package/lib/order/sort.js +0 -56
  33. package/lib/parent-dummy-chains.js +0 -84
  34. package/lib/position/bk.js +0 -424
  35. package/lib/position/index.js +0 -32
  36. package/lib/rank/feasible-tree.js +0 -95
  37. package/lib/rank/index.js +0 -54
  38. package/lib/rank/network-simplex.js +0 -235
  39. package/lib/rank/util.js +0 -67
  40. package/lib/util.js +0 -331
  41. package/lib/version.js +0 -1
@@ -1,235 +0,0 @@
1
- "use strict";
2
-
3
- var feasibleTree = require("./feasible-tree");
4
- var slack = require("./util").slack;
5
- var initRank = require("./util").longestPath;
6
- var preorder = require("@dagrejs/graphlib").alg.preorder;
7
- var postorder = require("@dagrejs/graphlib").alg.postorder;
8
- var simplify = require("../util").simplify;
9
-
10
- module.exports = networkSimplex;
11
-
12
- // Expose some internals for testing purposes
13
- networkSimplex.initLowLimValues = initLowLimValues;
14
- networkSimplex.initCutValues = initCutValues;
15
- networkSimplex.calcCutValue = calcCutValue;
16
- networkSimplex.leaveEdge = leaveEdge;
17
- networkSimplex.enterEdge = enterEdge;
18
- networkSimplex.exchangeEdges = exchangeEdges;
19
-
20
- /*
21
- * The network simplex algorithm assigns ranks to each node in the input graph
22
- * and iteratively improves the ranking to reduce the length of edges.
23
- *
24
- * Preconditions:
25
- *
26
- * 1. The input graph must be a DAG.
27
- * 2. All nodes in the graph must have an object value.
28
- * 3. All edges in the graph must have "minlen" and "weight" attributes.
29
- *
30
- * Postconditions:
31
- *
32
- * 1. All nodes in the graph will have an assigned "rank" attribute that has
33
- * been optimized by the network simplex algorithm. Ranks start at 0.
34
- *
35
- *
36
- * A rough sketch of the algorithm is as follows:
37
- *
38
- * 1. Assign initial ranks to each node. We use the longest path algorithm,
39
- * which assigns ranks to the lowest position possible. In general this
40
- * leads to very wide bottom ranks and unnecessarily long edges.
41
- * 2. Construct a feasible tight tree. A tight tree is one such that all
42
- * edges in the tree have no slack (difference between length of edge
43
- * and minlen for the edge). This by itself greatly improves the assigned
44
- * rankings by shorting edges.
45
- * 3. Iteratively find edges that have negative cut values. Generally a
46
- * negative cut value indicates that the edge could be removed and a new
47
- * tree edge could be added to produce a more compact graph.
48
- *
49
- * Much of the algorithms here are derived from Gansner, et al., "A Technique
50
- * for Drawing Directed Graphs." The structure of the file roughly follows the
51
- * structure of the overall algorithm.
52
- */
53
- function networkSimplex(g) {
54
- g = simplify(g);
55
- initRank(g);
56
- var t = feasibleTree(g);
57
- initLowLimValues(t);
58
- initCutValues(t, g);
59
-
60
- var e, f;
61
- while ((e = leaveEdge(t))) {
62
- f = enterEdge(t, g, e);
63
- exchangeEdges(t, g, e, f);
64
- }
65
- }
66
-
67
- /*
68
- * Initializes cut values for all edges in the tree.
69
- */
70
- function initCutValues(t, g) {
71
- var vs = postorder(t, t.nodes());
72
- vs = vs.slice(0, vs.length - 1);
73
- vs.forEach(v => assignCutValue(t, g, v));
74
- }
75
-
76
- function assignCutValue(t, g, child) {
77
- var childLab = t.node(child);
78
- var parent = childLab.parent;
79
- t.edge(child, parent).cutvalue = calcCutValue(t, g, child);
80
- }
81
-
82
- /*
83
- * Given the tight tree, its graph, and a child in the graph calculate and
84
- * return the cut value for the edge between the child and its parent.
85
- */
86
- function calcCutValue(t, g, child) {
87
- var childLab = t.node(child);
88
- var parent = childLab.parent;
89
- // True if the child is on the tail end of the edge in the directed graph
90
- var childIsTail = true;
91
- // The graph's view of the tree edge we're inspecting
92
- var graphEdge = g.edge(child, parent);
93
- // The accumulated cut value for the edge between this node and its parent
94
- var cutValue = 0;
95
-
96
- if (!graphEdge) {
97
- childIsTail = false;
98
- graphEdge = g.edge(parent, child);
99
- }
100
-
101
- cutValue = graphEdge.weight;
102
-
103
- g.nodeEdges(child).forEach(e => {
104
- var isOutEdge = e.v === child,
105
- other = isOutEdge ? e.w : e.v;
106
-
107
- if (other !== parent) {
108
- var pointsToHead = isOutEdge === childIsTail,
109
- otherWeight = g.edge(e).weight;
110
-
111
- cutValue += pointsToHead ? otherWeight : -otherWeight;
112
- if (isTreeEdge(t, child, other)) {
113
- var otherCutValue = t.edge(child, other).cutvalue;
114
- cutValue += pointsToHead ? -otherCutValue : otherCutValue;
115
- }
116
- }
117
- });
118
-
119
- return cutValue;
120
- }
121
-
122
- function initLowLimValues(tree, root) {
123
- if (arguments.length < 2) {
124
- root = tree.nodes()[0];
125
- }
126
- dfsAssignLowLim(tree, {}, 1, root);
127
- }
128
-
129
- function dfsAssignLowLim(tree, visited, nextLim, v, parent) {
130
- var low = nextLim;
131
- var label = tree.node(v);
132
-
133
- visited[v] = true;
134
- tree.neighbors(v).forEach(w => {
135
- if (!Object.hasOwn(visited, w)) {
136
- nextLim = dfsAssignLowLim(tree, visited, nextLim, w, v);
137
- }
138
- });
139
-
140
- label.low = low;
141
- label.lim = nextLim++;
142
- if (parent) {
143
- label.parent = parent;
144
- } else {
145
- // TODO should be able to remove this when we incrementally update low lim
146
- delete label.parent;
147
- }
148
-
149
- return nextLim;
150
- }
151
-
152
- function leaveEdge(tree) {
153
- return tree.edges().find(e => tree.edge(e).cutvalue < 0);
154
- }
155
-
156
- function enterEdge(t, g, edge) {
157
- var v = edge.v;
158
- var w = edge.w;
159
-
160
- // For the rest of this function we assume that v is the tail and w is the
161
- // head, so if we don't have this edge in the graph we should flip it to
162
- // match the correct orientation.
163
- if (!g.hasEdge(v, w)) {
164
- v = edge.w;
165
- w = edge.v;
166
- }
167
-
168
- var vLabel = t.node(v);
169
- var wLabel = t.node(w);
170
- var tailLabel = vLabel;
171
- var flip = false;
172
-
173
- // If the root is in the tail of the edge then we need to flip the logic that
174
- // checks for the head and tail nodes in the candidates function below.
175
- if (vLabel.lim > wLabel.lim) {
176
- tailLabel = wLabel;
177
- flip = true;
178
- }
179
-
180
- var candidates = g.edges().filter(edge => {
181
- return flip === isDescendant(t, t.node(edge.v), tailLabel) &&
182
- flip !== isDescendant(t, t.node(edge.w), tailLabel);
183
- });
184
-
185
- return candidates.reduce((acc, edge) => {
186
- if (slack(g, edge) < slack(g, acc)) {
187
- return edge;
188
- }
189
-
190
- return acc;
191
- });
192
- }
193
-
194
- function exchangeEdges(t, g, e, f) {
195
- var v = e.v;
196
- var w = e.w;
197
- t.removeEdge(v, w);
198
- t.setEdge(f.v, f.w, {});
199
- initLowLimValues(t);
200
- initCutValues(t, g);
201
- updateRanks(t, g);
202
- }
203
-
204
- function updateRanks(t, g) {
205
- var root = t.nodes().find(v => !g.node(v).parent);
206
- var vs = preorder(t, root);
207
- vs = vs.slice(1);
208
- vs.forEach(v => {
209
- var parent = t.node(v).parent,
210
- edge = g.edge(v, parent),
211
- flipped = false;
212
-
213
- if (!edge) {
214
- edge = g.edge(parent, v);
215
- flipped = true;
216
- }
217
-
218
- g.node(v).rank = g.node(parent).rank + (flipped ? edge.minlen : -edge.minlen);
219
- });
220
- }
221
-
222
- /*
223
- * Returns true if the edge is in the tree.
224
- */
225
- function isTreeEdge(tree, u, v) {
226
- return tree.hasEdge(u, v);
227
- }
228
-
229
- /*
230
- * Returns true if the specified node is descendant of the root node per the
231
- * assigned low and lim attributes in the tree.
232
- */
233
- function isDescendant(tree, vLabel, rootLabel) {
234
- return rootLabel.low <= vLabel.lim && vLabel.lim <= rootLabel.lim;
235
- }
package/lib/rank/util.js DELETED
@@ -1,67 +0,0 @@
1
- "use strict";
2
-
3
- const { applyWithChunking } = require("../util");
4
-
5
- module.exports = {
6
- longestPath: longestPath,
7
- slack: slack
8
- };
9
-
10
- /*
11
- * Initializes ranks for the input graph using the longest path algorithm. This
12
- * algorithm scales well and is fast in practice, it yields rather poor
13
- * solutions. Nodes are pushed to the lowest layer possible, leaving the bottom
14
- * ranks wide and leaving edges longer than necessary. However, due to its
15
- * speed, this algorithm is good for getting an initial ranking that can be fed
16
- * into other algorithms.
17
- *
18
- * This algorithm does not normalize layers because it will be used by other
19
- * algorithms in most cases. If using this algorithm directly, be sure to
20
- * run normalize at the end.
21
- *
22
- * Pre-conditions:
23
- *
24
- * 1. Input graph is a DAG.
25
- * 2. Input graph node labels can be assigned properties.
26
- *
27
- * Post-conditions:
28
- *
29
- * 1. Each node will be assign an (unnormalized) "rank" property.
30
- */
31
- function longestPath(g) {
32
- var visited = {};
33
-
34
- function dfs(v) {
35
- var label = g.node(v);
36
- if (Object.hasOwn(visited, v)) {
37
- return label.rank;
38
- }
39
- visited[v] = true;
40
-
41
- let outEdgesMinLens = g.outEdges(v).map(e => {
42
- if (e == null) {
43
- return Number.POSITIVE_INFINITY;
44
- }
45
-
46
- return dfs(e.w) - g.edge(e).minlen;
47
- });
48
-
49
- var rank = applyWithChunking(Math.min, outEdgesMinLens);
50
-
51
- if (rank === Number.POSITIVE_INFINITY) {
52
- rank = 0;
53
- }
54
-
55
- return (label.rank = rank);
56
- }
57
-
58
- g.sources().forEach(dfs);
59
- }
60
-
61
- /*
62
- * Returns the amount of slack for the given edge. The slack is defined as the
63
- * difference between the length of the edge and its minimum length.
64
- */
65
- function slack(g, e) {
66
- return g.node(e.w).rank - g.node(e.v).rank - g.edge(e).minlen;
67
- }
package/lib/util.js DELETED
@@ -1,331 +0,0 @@
1
- /* eslint "no-console": off */
2
-
3
- "use strict";
4
-
5
- let Graph = require("@dagrejs/graphlib").Graph;
6
-
7
- module.exports = {
8
- addBorderNode,
9
- addDummyNode,
10
- applyWithChunking,
11
- asNonCompoundGraph,
12
- buildLayerMatrix,
13
- intersectRect,
14
- mapValues,
15
- maxRank,
16
- normalizeRanks,
17
- notime,
18
- partition,
19
- pick,
20
- predecessorWeights,
21
- range,
22
- removeEmptyRanks,
23
- simplify,
24
- successorWeights,
25
- time,
26
- uniqueId,
27
- zipObject,
28
- };
29
-
30
- /*
31
- * Adds a dummy node to the graph and return v.
32
- */
33
- function addDummyNode(g, type, attrs, name) {
34
- var v = name;
35
- while (g.hasNode(v)) {
36
- v = uniqueId(name);
37
- }
38
-
39
- attrs.dummy = type;
40
- g.setNode(v, attrs);
41
- return v;
42
- }
43
-
44
- /*
45
- * Returns a new graph with only simple edges. Handles aggregation of data
46
- * associated with multi-edges.
47
- */
48
- function simplify(g) {
49
- let simplified = new Graph().setGraph(g.graph());
50
- g.nodes().forEach(v => simplified.setNode(v, g.node(v)));
51
- g.edges().forEach(e => {
52
- let simpleLabel = simplified.edge(e.v, e.w) || { weight: 0, minlen: 1 };
53
- let label = g.edge(e);
54
- simplified.setEdge(e.v, e.w, {
55
- weight: simpleLabel.weight + label.weight,
56
- minlen: Math.max(simpleLabel.minlen, label.minlen)
57
- });
58
- });
59
- return simplified;
60
- }
61
-
62
- function asNonCompoundGraph(g) {
63
- let simplified = new Graph({ multigraph: g.isMultigraph() }).setGraph(g.graph());
64
- g.nodes().forEach(v => {
65
- if (!g.children(v).length) {
66
- simplified.setNode(v, g.node(v));
67
- }
68
- });
69
- g.edges().forEach(e => {
70
- simplified.setEdge(e, g.edge(e));
71
- });
72
- return simplified;
73
- }
74
-
75
- function successorWeights(g) {
76
- let weightMap = g.nodes().map(v => {
77
- let sucs = {};
78
- g.outEdges(v).forEach(e => {
79
- sucs[e.w] = (sucs[e.w] || 0) + g.edge(e).weight;
80
- });
81
- return sucs;
82
- });
83
- return zipObject(g.nodes(), weightMap);
84
- }
85
-
86
- function predecessorWeights(g) {
87
- let weightMap = g.nodes().map(v => {
88
- let preds = {};
89
- g.inEdges(v).forEach(e => {
90
- preds[e.v] = (preds[e.v] || 0) + g.edge(e).weight;
91
- });
92
- return preds;
93
- });
94
- return zipObject(g.nodes(), weightMap);
95
- }
96
-
97
- /*
98
- * Finds where a line starting at point ({x, y}) would intersect a rectangle
99
- * ({x, y, width, height}) if it were pointing at the rectangle's center.
100
- */
101
- function intersectRect(rect, point) {
102
- let x = rect.x;
103
- let y = rect.y;
104
-
105
- // Rectangle intersection algorithm from:
106
- // http://math.stackexchange.com/questions/108113/find-edge-between-two-boxes
107
- let dx = point.x - x;
108
- let dy = point.y - y;
109
- let w = rect.width / 2;
110
- let h = rect.height / 2;
111
-
112
- if (!dx && !dy) {
113
- throw new Error("Not possible to find intersection inside of the rectangle");
114
- }
115
-
116
- let sx, sy;
117
- if (Math.abs(dy) * w > Math.abs(dx) * h) {
118
- // Intersection is top or bottom of rect.
119
- if (dy < 0) {
120
- h = -h;
121
- }
122
- sx = h * dx / dy;
123
- sy = h;
124
- } else {
125
- // Intersection is left or right of rect.
126
- if (dx < 0) {
127
- w = -w;
128
- }
129
- sx = w;
130
- sy = w * dy / dx;
131
- }
132
-
133
- return { x: x + sx, y: y + sy };
134
- }
135
-
136
- /*
137
- * Given a DAG with each node assigned "rank" and "order" properties, this
138
- * function will produce a matrix with the ids of each node.
139
- */
140
- function buildLayerMatrix(g) {
141
- let layering = range(maxRank(g) + 1).map(() => []);
142
- g.nodes().forEach(v => {
143
- let node = g.node(v);
144
- let rank = node.rank;
145
- if (rank !== undefined) {
146
- layering[rank][node.order] = v;
147
- }
148
- });
149
- return layering;
150
- }
151
-
152
- /*
153
- * Adjusts the ranks for all nodes in the graph such that all nodes v have
154
- * rank(v) >= 0 and at least one node w has rank(w) = 0.
155
- */
156
- function normalizeRanks(g) {
157
- let nodeRanks = g.nodes().map(v => {
158
- let rank = g.node(v).rank;
159
- if (rank === undefined) {
160
- return Number.MAX_VALUE;
161
- }
162
-
163
- return rank;
164
- });
165
- let min = applyWithChunking(Math.min, nodeRanks);
166
- g.nodes().forEach(v => {
167
- let node = g.node(v);
168
- if (Object.hasOwn(node, "rank")) {
169
- node.rank -= min;
170
- }
171
- });
172
- }
173
-
174
- function removeEmptyRanks(g) {
175
- // Ranks may not start at 0, so we need to offset them
176
- let nodeRanks = g.nodes().map(v => g.node(v).rank);
177
- let offset = applyWithChunking(Math.min, nodeRanks);
178
-
179
- let layers = [];
180
- g.nodes().forEach(v => {
181
- let rank = g.node(v).rank - offset;
182
- if (!layers[rank]) {
183
- layers[rank] = [];
184
- }
185
- layers[rank].push(v);
186
- });
187
-
188
- let delta = 0;
189
- let nodeRankFactor = g.graph().nodeRankFactor;
190
- Array.from(layers).forEach((vs, i) => {
191
- if (vs === undefined && i % nodeRankFactor !== 0) {
192
- --delta;
193
- } else if (vs !== undefined && delta) {
194
- vs.forEach(v => g.node(v).rank += delta);
195
- }
196
- });
197
- }
198
-
199
- function addBorderNode(g, prefix, rank, order) {
200
- let node = {
201
- width: 0,
202
- height: 0
203
- };
204
- if (arguments.length >= 4) {
205
- node.rank = rank;
206
- node.order = order;
207
- }
208
- return addDummyNode(g, "border", node, prefix);
209
- }
210
-
211
- function splitToChunks(array, chunkSize = CHUNKING_THRESHOLD) {
212
- const chunks = [];
213
- for (let i = 0; i < array.length; i += chunkSize) {
214
- const chunk = array.slice(i, i + chunkSize);
215
- chunks.push(chunk);
216
- }
217
- return chunks;
218
- }
219
-
220
- const CHUNKING_THRESHOLD = 65535;
221
-
222
- function applyWithChunking(fn, argsArray) {
223
- if(argsArray.length > CHUNKING_THRESHOLD) {
224
- const chunks = splitToChunks(argsArray);
225
- return fn.apply(null, chunks.map(chunk => fn.apply(null, chunk)));
226
- } else {
227
- return fn.apply(null, argsArray);
228
- }
229
- }
230
-
231
- function maxRank(g) {
232
- const nodes = g.nodes();
233
- const nodeRanks = nodes.map(v => {
234
- let rank = g.node(v).rank;
235
- if (rank === undefined) {
236
- return Number.MIN_VALUE;
237
- }
238
- return rank;
239
- });
240
-
241
- return applyWithChunking(Math.max, nodeRanks);
242
- }
243
-
244
- /*
245
- * Partition a collection into two groups: `lhs` and `rhs`. If the supplied
246
- * function returns true for an entry it goes into `lhs`. Otherwise it goes
247
- * into `rhs.
248
- */
249
- function partition(collection, fn) {
250
- let result = { lhs: [], rhs: [] };
251
- collection.forEach(value => {
252
- if (fn(value)) {
253
- result.lhs.push(value);
254
- } else {
255
- result.rhs.push(value);
256
- }
257
- });
258
- return result;
259
- }
260
-
261
- /*
262
- * Returns a new function that wraps `fn` with a timer. The wrapper logs the
263
- * time it takes to execute the function.
264
- */
265
- function time(name, fn) {
266
- let start = Date.now();
267
- try {
268
- return fn();
269
- } finally {
270
- console.log(name + " time: " + (Date.now() - start) + "ms");
271
- }
272
- }
273
-
274
- function notime(name, fn) {
275
- return fn();
276
- }
277
-
278
- let idCounter = 0;
279
- function uniqueId(prefix) {
280
- var id = ++idCounter;
281
- return prefix + ("" + id);
282
- }
283
-
284
- function range(start, limit, step = 1) {
285
- if (limit == null) {
286
- limit = start;
287
- start = 0;
288
- }
289
-
290
- let endCon = (i) => i < limit;
291
- if (step < 0) {
292
- endCon = (i) => limit < i;
293
- }
294
-
295
- const range = [];
296
- for (let i = start; endCon(i); i += step) {
297
- range.push(i);
298
- }
299
-
300
- return range;
301
- }
302
-
303
- function pick(source, keys) {
304
- const dest = {};
305
- for (const key of keys) {
306
- if (source[key] !== undefined) {
307
- dest[key] = source[key];
308
- }
309
- }
310
-
311
- return dest;
312
- }
313
-
314
- function mapValues(obj, funcOrProp) {
315
- let func = funcOrProp;
316
- if (typeof funcOrProp === 'string') {
317
- func = (val) => val[funcOrProp];
318
- }
319
-
320
- return Object.entries(obj).reduce((acc, [k, v]) => {
321
- acc[k] = func(v, k);
322
- return acc;
323
- }, {});
324
- }
325
-
326
- function zipObject(props, values) {
327
- return props.reduce((acc, key, i) => {
328
- acc[key] = values[i];
329
- return acc;
330
- }, {});
331
- }
package/lib/version.js DELETED
@@ -1 +0,0 @@
1
- module.exports = "1.1.5";