@dagrejs/dagre 0.8.0 → 1.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.
@@ -1,2916 +0,0 @@
1
- !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.dagre=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2
- /*
3
- Copyright (c) 2012-2014 Chris Pettitt
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
22
- */
23
-
24
- module.exports = {
25
- graphlib: require("./lib/graphlib"),
26
-
27
- layout: require("./lib/layout"),
28
- debug: require("./lib/debug"),
29
- util: {
30
- time: require("./lib/util").time,
31
- notime: require("./lib/util").notime
32
- },
33
- version: require("./lib/version")
34
- };
35
-
36
- },{"./lib/debug":6,"./lib/graphlib":7,"./lib/layout":9,"./lib/util":29,"./lib/version":30}],2:[function(require,module,exports){
37
- "use strict";
38
-
39
- var _ = require("./lodash"),
40
- greedyFAS = require("./greedy-fas");
41
-
42
- module.exports = {
43
- run: run,
44
- undo: undo
45
- };
46
-
47
- function run(g) {
48
- var fas = (g.graph().acyclicer === "greedy"
49
- ? greedyFAS(g, weightFn(g))
50
- : dfsFAS(g));
51
- _.forEach(fas, function(e) {
52
- var label = g.edge(e);
53
- g.removeEdge(e);
54
- label.forwardName = e.name;
55
- label.reversed = true;
56
- g.setEdge(e.w, e.v, label, _.uniqueId("rev"));
57
- });
58
-
59
- function weightFn(g) {
60
- return function(e) {
61
- return g.edge(e).weight;
62
- };
63
- }
64
- }
65
-
66
- function dfsFAS(g) {
67
- var fas = [],
68
- stack = {},
69
- visited = {};
70
-
71
- function dfs(v) {
72
- if (_.has(visited, v)) {
73
- return;
74
- }
75
- visited[v] = true;
76
- stack[v] = true;
77
- _.forEach(g.outEdges(v), function(e) {
78
- if (_.has(stack, e.w)) {
79
- fas.push(e);
80
- } else {
81
- dfs(e.w);
82
- }
83
- });
84
- delete stack[v];
85
- }
86
-
87
- _.forEach(g.nodes(), dfs);
88
- return fas;
89
- }
90
-
91
- function undo(g) {
92
- _.forEach(g.edges(), function(e) {
93
- var label = g.edge(e);
94
- if (label.reversed) {
95
- g.removeEdge(e);
96
-
97
- var forwardName = label.forwardName;
98
- delete label.reversed;
99
- delete label.forwardName;
100
- g.setEdge(e.w, e.v, label, forwardName);
101
- }
102
- });
103
- }
104
-
105
- },{"./greedy-fas":8,"./lodash":10}],3:[function(require,module,exports){
106
- var _ = require("./lodash"),
107
- util = require("./util");
108
-
109
- module.exports = addBorderSegments;
110
-
111
- function addBorderSegments(g) {
112
- function dfs(v) {
113
- var children = g.children(v),
114
- node = g.node(v);
115
- if (children.length) {
116
- _.forEach(children, dfs);
117
- }
118
-
119
- if (_.has(node, "minRank")) {
120
- node.borderLeft = [];
121
- node.borderRight = [];
122
- for (var rank = node.minRank, maxRank = node.maxRank + 1;
123
- rank < maxRank;
124
- ++rank) {
125
- addBorderNode(g, "borderLeft", "_bl", v, node, rank);
126
- addBorderNode(g, "borderRight", "_br", v, node, rank);
127
- }
128
- }
129
- }
130
-
131
- _.forEach(g.children(), dfs);
132
- }
133
-
134
- function addBorderNode(g, prop, prefix, sg, sgNode, rank) {
135
- var label = { width: 0, height: 0, rank: rank, borderType: prop },
136
- prev = sgNode[prop][rank - 1],
137
- curr = util.addDummyNode(g, "border", label, prefix);
138
- sgNode[prop][rank] = curr;
139
- g.setParent(curr, sg);
140
- if (prev) {
141
- g.setEdge(prev, curr, { weight: 1 });
142
- }
143
- }
144
-
145
- },{"./lodash":10,"./util":29}],4:[function(require,module,exports){
146
- "use strict";
147
-
148
- var _ = require("./lodash");
149
-
150
- module.exports = {
151
- adjust: adjust,
152
- undo: undo
153
- };
154
-
155
- function adjust(g) {
156
- var rankDir = g.graph().rankdir.toLowerCase();
157
- if (rankDir === "lr" || rankDir === "rl") {
158
- swapWidthHeight(g);
159
- }
160
- }
161
-
162
- function undo(g) {
163
- var rankDir = g.graph().rankdir.toLowerCase();
164
- if (rankDir === "bt" || rankDir === "rl") {
165
- reverseY(g);
166
- }
167
-
168
- if (rankDir === "lr" || rankDir === "rl") {
169
- swapXY(g);
170
- swapWidthHeight(g);
171
- }
172
- }
173
-
174
- function swapWidthHeight(g) {
175
- _.forEach(g.nodes(), function(v) { swapWidthHeightOne(g.node(v)); });
176
- _.forEach(g.edges(), function(e) { swapWidthHeightOne(g.edge(e)); });
177
- }
178
-
179
- function swapWidthHeightOne(attrs) {
180
- var w = attrs.width;
181
- attrs.width = attrs.height;
182
- attrs.height = w;
183
- }
184
-
185
- function reverseY(g) {
186
- _.forEach(g.nodes(), function(v) { reverseYOne(g.node(v)); });
187
-
188
- _.forEach(g.edges(), function(e) {
189
- var edge = g.edge(e);
190
- _.forEach(edge.points, reverseYOne);
191
- if (_.has(edge, "y")) {
192
- reverseYOne(edge);
193
- }
194
- });
195
- }
196
-
197
- function reverseYOne(attrs) {
198
- attrs.y = -attrs.y;
199
- }
200
-
201
- function swapXY(g) {
202
- _.forEach(g.nodes(), function(v) { swapXYOne(g.node(v)); });
203
-
204
- _.forEach(g.edges(), function(e) {
205
- var edge = g.edge(e);
206
- _.forEach(edge.points, swapXYOne);
207
- if (_.has(edge, "x")) {
208
- swapXYOne(edge);
209
- }
210
- });
211
- }
212
-
213
- function swapXYOne(attrs) {
214
- var x = attrs.x;
215
- attrs.x = attrs.y;
216
- attrs.y = x;
217
- }
218
-
219
- },{"./lodash":10}],5:[function(require,module,exports){
220
- /*
221
- * Simple doubly linked list implementation derived from Cormen, et al.,
222
- * "Introduction to Algorithms".
223
- */
224
-
225
- module.exports = List;
226
-
227
- function List() {
228
- var sentinel = {};
229
- sentinel._next = sentinel._prev = sentinel;
230
- this._sentinel = sentinel;
231
- }
232
-
233
- List.prototype.dequeue = function() {
234
- var sentinel = this._sentinel,
235
- entry = sentinel._prev;
236
- if (entry !== sentinel) {
237
- unlink(entry);
238
- return entry;
239
- }
240
- };
241
-
242
- List.prototype.enqueue = function(entry) {
243
- var sentinel = this._sentinel;
244
- if (entry._prev && entry._next) {
245
- unlink(entry);
246
- }
247
- entry._next = sentinel._next;
248
- sentinel._next._prev = entry;
249
- sentinel._next = entry;
250
- entry._prev = sentinel;
251
- };
252
-
253
- List.prototype.toString = function() {
254
- var strs = [],
255
- sentinel = this._sentinel,
256
- curr = sentinel._prev;
257
- while (curr !== sentinel) {
258
- strs.push(JSON.stringify(curr, filterOutLinks));
259
- curr = curr._prev;
260
- }
261
- return "[" + strs.join(", ") + "]";
262
- };
263
-
264
- function unlink(entry) {
265
- entry._prev._next = entry._next;
266
- entry._next._prev = entry._prev;
267
- delete entry._next;
268
- delete entry._prev;
269
- }
270
-
271
- function filterOutLinks(k, v) {
272
- if (k !== "_next" && k !== "_prev") {
273
- return v;
274
- }
275
- }
276
-
277
- },{}],6:[function(require,module,exports){
278
- var _ = require("./lodash"),
279
- util = require("./util"),
280
- Graph = require("./graphlib").Graph;
281
-
282
- module.exports = {
283
- debugOrdering: debugOrdering
284
- };
285
-
286
- /* istanbul ignore next */
287
- function debugOrdering(g) {
288
- var layerMatrix = util.buildLayerMatrix(g);
289
-
290
- var h = new Graph({ compound: true, multigraph: true }).setGraph({});
291
-
292
- _.forEach(g.nodes(), function(v) {
293
- h.setNode(v, { label: v });
294
- h.setParent(v, "layer" + g.node(v).rank);
295
- });
296
-
297
- _.forEach(g.edges(), function(e) {
298
- h.setEdge(e.v, e.w, {}, e.name);
299
- });
300
-
301
- _.forEach(layerMatrix, function(layer, i) {
302
- var layerV = "layer" + i;
303
- h.setNode(layerV, { rank: "same" });
304
- _.reduce(layer, function(u, v) {
305
- h.setEdge(u, v, { style: "invis" });
306
- return v;
307
- });
308
- });
309
-
310
- return h;
311
- }
312
-
313
- },{"./graphlib":7,"./lodash":10,"./util":29}],7:[function(require,module,exports){
314
- /* global window */
315
-
316
- var graphlib;
317
-
318
- if (typeof require === "function") {
319
- try {
320
- graphlib = require("@dagrejs/graphlib");
321
- } catch (e) {}
322
- }
323
-
324
- if (!graphlib) {
325
- graphlib = window.graphlib;
326
- }
327
-
328
- module.exports = graphlib;
329
-
330
- },{"@dagrejs/graphlib":undefined}],8:[function(require,module,exports){
331
- var _ = require("./lodash"),
332
- Graph = require("./graphlib").Graph,
333
- List = require("./data/list");
334
-
335
- /*
336
- * A greedy heuristic for finding a feedback arc set for a graph. A feedback
337
- * arc set is a set of edges that can be removed to make a graph acyclic.
338
- * The algorithm comes from: P. Eades, X. Lin, and W. F. Smyth, "A fast and
339
- * effective heuristic for the feedback arc set problem." This implementation
340
- * adjusts that from the paper to allow for weighted edges.
341
- */
342
- module.exports = greedyFAS;
343
-
344
- var DEFAULT_WEIGHT_FN = _.constant(1);
345
-
346
- function greedyFAS(g, weightFn) {
347
- if (g.nodeCount() <= 1) {
348
- return [];
349
- }
350
- var state = buildState(g, weightFn || DEFAULT_WEIGHT_FN);
351
- var results = doGreedyFAS(state.graph, state.buckets, state.zeroIdx);
352
-
353
- // Expand multi-edges
354
- return _.flatten(_.map(results, function(e) {
355
- return g.outEdges(e.v, e.w);
356
- }), true);
357
- }
358
-
359
- function doGreedyFAS(g, buckets, zeroIdx) {
360
- var results = [],
361
- sources = buckets[buckets.length - 1],
362
- sinks = buckets[0];
363
-
364
- var entry;
365
- while (g.nodeCount()) {
366
- while ((entry = sinks.dequeue())) { removeNode(g, buckets, zeroIdx, entry); }
367
- while ((entry = sources.dequeue())) { removeNode(g, buckets, zeroIdx, entry); }
368
- if (g.nodeCount()) {
369
- for (var i = buckets.length - 2; i > 0; --i) {
370
- entry = buckets[i].dequeue();
371
- if (entry) {
372
- results = results.concat(removeNode(g, buckets, zeroIdx, entry, true));
373
- break;
374
- }
375
- }
376
- }
377
- }
378
-
379
- return results;
380
- }
381
-
382
- function removeNode(g, buckets, zeroIdx, entry, collectPredecessors) {
383
- var results = collectPredecessors ? [] : undefined;
384
-
385
- _.forEach(g.inEdges(entry.v), function(edge) {
386
- var weight = g.edge(edge),
387
- uEntry = g.node(edge.v);
388
-
389
- if (collectPredecessors) {
390
- results.push({ v: edge.v, w: edge.w });
391
- }
392
-
393
- uEntry.out -= weight;
394
- assignBucket(buckets, zeroIdx, uEntry);
395
- });
396
-
397
- _.forEach(g.outEdges(entry.v), function(edge) {
398
- var weight = g.edge(edge),
399
- w = edge.w,
400
- wEntry = g.node(w);
401
- wEntry["in"] -= weight;
402
- assignBucket(buckets, zeroIdx, wEntry);
403
- });
404
-
405
- g.removeNode(entry.v);
406
-
407
- return results;
408
- }
409
-
410
- function buildState(g, weightFn) {
411
- var fasGraph = new Graph(),
412
- maxIn = 0,
413
- maxOut = 0;
414
-
415
- _.forEach(g.nodes(), function(v) {
416
- fasGraph.setNode(v, { v: v, "in": 0, out: 0 });
417
- });
418
-
419
- // Aggregate weights on nodes, but also sum the weights across multi-edges
420
- // into a single edge for the fasGraph.
421
- _.forEach(g.edges(), function(e) {
422
- var prevWeight = fasGraph.edge(e.v, e.w) || 0,
423
- weight = weightFn(e),
424
- edgeWeight = prevWeight + weight;
425
- fasGraph.setEdge(e.v, e.w, edgeWeight);
426
- maxOut = Math.max(maxOut, fasGraph.node(e.v).out += weight);
427
- maxIn = Math.max(maxIn, fasGraph.node(e.w)["in"] += weight);
428
- });
429
-
430
- var buckets = _.range(maxOut + maxIn + 3).map(function() { return new List(); });
431
- var zeroIdx = maxIn + 1;
432
-
433
- _.forEach(fasGraph.nodes(), function(v) {
434
- assignBucket(buckets, zeroIdx, fasGraph.node(v));
435
- });
436
-
437
- return { graph: fasGraph, buckets: buckets, zeroIdx: zeroIdx };
438
- }
439
-
440
- function assignBucket(buckets, zeroIdx, entry) {
441
- if (!entry.out) {
442
- buckets[0].enqueue(entry);
443
- } else if (!entry["in"]) {
444
- buckets[buckets.length - 1].enqueue(entry);
445
- } else {
446
- buckets[entry.out - entry["in"] + zeroIdx].enqueue(entry);
447
- }
448
- }
449
-
450
- },{"./data/list":5,"./graphlib":7,"./lodash":10}],9:[function(require,module,exports){
451
- "use strict";
452
-
453
- var _ = require("./lodash"),
454
- acyclic = require("./acyclic"),
455
- normalize = require("./normalize"),
456
- rank = require("./rank"),
457
- normalizeRanks = require("./util").normalizeRanks,
458
- parentDummyChains = require("./parent-dummy-chains"),
459
- removeEmptyRanks = require("./util").removeEmptyRanks,
460
- nestingGraph = require("./nesting-graph"),
461
- addBorderSegments = require("./add-border-segments"),
462
- coordinateSystem = require("./coordinate-system"),
463
- order = require("./order"),
464
- position = require("./position"),
465
- util = require("./util"),
466
- Graph = require("./graphlib").Graph;
467
-
468
- module.exports = layout;
469
-
470
- function layout(g, opts) {
471
- var time = opts && opts.debugTiming ? util.time : util.notime;
472
- time("layout", function() {
473
- var layoutGraph = time(" buildLayoutGraph",
474
- function() { return buildLayoutGraph(g); });
475
- time(" runLayout", function() { runLayout(layoutGraph, time); });
476
- time(" updateInputGraph", function() { updateInputGraph(g, layoutGraph); });
477
- });
478
- }
479
-
480
- function runLayout(g, time) {
481
- time(" makeSpaceForEdgeLabels", function() { makeSpaceForEdgeLabels(g); });
482
- time(" removeSelfEdges", function() { removeSelfEdges(g); });
483
- time(" acyclic", function() { acyclic.run(g); });
484
- time(" nestingGraph.run", function() { nestingGraph.run(g); });
485
- time(" rank", function() { rank(util.asNonCompoundGraph(g)); });
486
- time(" injectEdgeLabelProxies", function() { injectEdgeLabelProxies(g); });
487
- time(" removeEmptyRanks", function() { removeEmptyRanks(g); });
488
- time(" nestingGraph.cleanup", function() { nestingGraph.cleanup(g); });
489
- time(" normalizeRanks", function() { normalizeRanks(g); });
490
- time(" assignRankMinMax", function() { assignRankMinMax(g); });
491
- time(" removeEdgeLabelProxies", function() { removeEdgeLabelProxies(g); });
492
- time(" normalize.run", function() { normalize.run(g); });
493
- time(" parentDummyChains", function() { parentDummyChains(g); });
494
- time(" addBorderSegments", function() { addBorderSegments(g); });
495
- time(" order", function() { order(g); });
496
- time(" insertSelfEdges", function() { insertSelfEdges(g); });
497
- time(" adjustCoordinateSystem", function() { coordinateSystem.adjust(g); });
498
- time(" position", function() { position(g); });
499
- time(" positionSelfEdges", function() { positionSelfEdges(g); });
500
- time(" removeBorderNodes", function() { removeBorderNodes(g); });
501
- time(" normalize.undo", function() { normalize.undo(g); });
502
- time(" fixupEdgeLabelCoords", function() { fixupEdgeLabelCoords(g); });
503
- time(" undoCoordinateSystem", function() { coordinateSystem.undo(g); });
504
- time(" translateGraph", function() { translateGraph(g); });
505
- time(" assignNodeIntersects", function() { assignNodeIntersects(g); });
506
- time(" reversePoints", function() { reversePointsForReversedEdges(g); });
507
- time(" acyclic.undo", function() { acyclic.undo(g); });
508
- }
509
-
510
- /*
511
- * Copies final layout information from the layout graph back to the input
512
- * graph. This process only copies whitelisted attributes from the layout graph
513
- * to the input graph, so it serves as a good place to determine what
514
- * attributes can influence layout.
515
- */
516
- function updateInputGraph(inputGraph, layoutGraph) {
517
- _.forEach(inputGraph.nodes(), function(v) {
518
- var inputLabel = inputGraph.node(v),
519
- layoutLabel = layoutGraph.node(v);
520
-
521
- if (inputLabel) {
522
- inputLabel.x = layoutLabel.x;
523
- inputLabel.y = layoutLabel.y;
524
-
525
- if (layoutGraph.children(v).length) {
526
- inputLabel.width = layoutLabel.width;
527
- inputLabel.height = layoutLabel.height;
528
- }
529
- }
530
- });
531
-
532
- _.forEach(inputGraph.edges(), function(e) {
533
- var inputLabel = inputGraph.edge(e),
534
- layoutLabel = layoutGraph.edge(e);
535
-
536
- inputLabel.points = layoutLabel.points;
537
- if (_.has(layoutLabel, "x")) {
538
- inputLabel.x = layoutLabel.x;
539
- inputLabel.y = layoutLabel.y;
540
- }
541
- });
542
-
543
- inputGraph.graph().width = layoutGraph.graph().width;
544
- inputGraph.graph().height = layoutGraph.graph().height;
545
- }
546
-
547
- var graphNumAttrs = ["nodesep", "edgesep", "ranksep", "marginx", "marginy"],
548
- graphDefaults = { ranksep: 50, edgesep: 20, nodesep: 50, rankdir: "tb" },
549
- graphAttrs = ["acyclicer", "ranker", "rankdir", "align"],
550
- nodeNumAttrs = ["width", "height"],
551
- nodeDefaults = { width: 0, height: 0 },
552
- edgeNumAttrs = ["minlen", "weight", "width", "height", "labeloffset"],
553
- edgeDefaults = {
554
- minlen: 1, weight: 1, width: 0, height: 0,
555
- labeloffset: 10, labelpos: "r"
556
- },
557
- edgeAttrs = ["labelpos"];
558
-
559
- /*
560
- * Constructs a new graph from the input graph, which can be used for layout.
561
- * This process copies only whitelisted attributes from the input graph to the
562
- * layout graph. Thus this function serves as a good place to determine what
563
- * attributes can influence layout.
564
- */
565
- function buildLayoutGraph(inputGraph) {
566
- var g = new Graph({ multigraph: true, compound: true }),
567
- graph = canonicalize(inputGraph.graph());
568
-
569
- g.setGraph(_.merge({},
570
- graphDefaults,
571
- selectNumberAttrs(graph, graphNumAttrs),
572
- _.pick(graph, graphAttrs)));
573
-
574
- _.forEach(inputGraph.nodes(), function(v) {
575
- var node = canonicalize(inputGraph.node(v));
576
- g.setNode(v, _.defaults(selectNumberAttrs(node, nodeNumAttrs), nodeDefaults));
577
- g.setParent(v, inputGraph.parent(v));
578
- });
579
-
580
- _.forEach(inputGraph.edges(), function(e) {
581
- var edge = canonicalize(inputGraph.edge(e));
582
- g.setEdge(e, _.merge({},
583
- edgeDefaults,
584
- selectNumberAttrs(edge, edgeNumAttrs),
585
- _.pick(edge, edgeAttrs)));
586
- });
587
-
588
- return g;
589
- }
590
-
591
- /*
592
- * This idea comes from the Gansner paper: to account for edge labels in our
593
- * layout we split each rank in half by doubling minlen and halving ranksep.
594
- * Then we can place labels at these mid-points between nodes.
595
- *
596
- * We also add some minimal padding to the width to push the label for the edge
597
- * away from the edge itself a bit.
598
- */
599
- function makeSpaceForEdgeLabels(g) {
600
- var graph = g.graph();
601
- graph.ranksep /= 2;
602
- _.forEach(g.edges(), function(e) {
603
- var edge = g.edge(e);
604
- edge.minlen *= 2;
605
- if (edge.labelpos.toLowerCase() !== "c") {
606
- if (graph.rankdir === "TB" || graph.rankdir === "BT") {
607
- edge.width += edge.labeloffset;
608
- } else {
609
- edge.height += edge.labeloffset;
610
- }
611
- }
612
- });
613
- }
614
-
615
- /*
616
- * Creates temporary dummy nodes that capture the rank in which each edge's
617
- * label is going to, if it has one of non-zero width and height. We do this
618
- * so that we can safely remove empty ranks while preserving balance for the
619
- * label's position.
620
- */
621
- function injectEdgeLabelProxies(g) {
622
- _.forEach(g.edges(), function(e) {
623
- var edge = g.edge(e);
624
- if (edge.width && edge.height) {
625
- var v = g.node(e.v),
626
- w = g.node(e.w),
627
- label = { rank: (w.rank - v.rank) / 2 + v.rank, e: e };
628
- util.addDummyNode(g, "edge-proxy", label, "_ep");
629
- }
630
- });
631
- }
632
-
633
- function assignRankMinMax(g) {
634
- var maxRank = 0;
635
- _.forEach(g.nodes(), function(v) {
636
- var node = g.node(v);
637
- if (node.borderTop) {
638
- node.minRank = g.node(node.borderTop).rank;
639
- node.maxRank = g.node(node.borderBottom).rank;
640
- maxRank = _.max(maxRank, node.maxRank);
641
- }
642
- });
643
- g.graph().maxRank = maxRank;
644
- }
645
-
646
- function removeEdgeLabelProxies(g) {
647
- _.forEach(g.nodes(), function(v) {
648
- var node = g.node(v);
649
- if (node.dummy === "edge-proxy") {
650
- g.edge(node.e).labelRank = node.rank;
651
- g.removeNode(v);
652
- }
653
- });
654
- }
655
-
656
- function translateGraph(g) {
657
- var minX = Number.POSITIVE_INFINITY,
658
- maxX = 0,
659
- minY = Number.POSITIVE_INFINITY,
660
- maxY = 0,
661
- graphLabel = g.graph(),
662
- marginX = graphLabel.marginx || 0,
663
- marginY = graphLabel.marginy || 0;
664
-
665
- function getExtremes(attrs) {
666
- var x = attrs.x,
667
- y = attrs.y,
668
- w = attrs.width,
669
- h = attrs.height;
670
- minX = Math.min(minX, x - w / 2);
671
- maxX = Math.max(maxX, x + w / 2);
672
- minY = Math.min(minY, y - h / 2);
673
- maxY = Math.max(maxY, y + h / 2);
674
- }
675
-
676
- _.forEach(g.nodes(), function(v) { getExtremes(g.node(v)); });
677
- _.forEach(g.edges(), function(e) {
678
- var edge = g.edge(e);
679
- if (_.has(edge, "x")) {
680
- getExtremes(edge);
681
- }
682
- });
683
-
684
- minX -= marginX;
685
- minY -= marginY;
686
-
687
- _.forEach(g.nodes(), function(v) {
688
- var node = g.node(v);
689
- node.x -= minX;
690
- node.y -= minY;
691
- });
692
-
693
- _.forEach(g.edges(), function(e) {
694
- var edge = g.edge(e);
695
- _.forEach(edge.points, function(p) {
696
- p.x -= minX;
697
- p.y -= minY;
698
- });
699
- if (_.has(edge, "x")) { edge.x -= minX; }
700
- if (_.has(edge, "y")) { edge.y -= minY; }
701
- });
702
-
703
- graphLabel.width = maxX - minX + marginX;
704
- graphLabel.height = maxY - minY + marginY;
705
- }
706
-
707
- function assignNodeIntersects(g) {
708
- _.forEach(g.edges(), function(e) {
709
- var edge = g.edge(e),
710
- nodeV = g.node(e.v),
711
- nodeW = g.node(e.w),
712
- p1, p2;
713
- if (!edge.points) {
714
- edge.points = [];
715
- p1 = nodeW;
716
- p2 = nodeV;
717
- } else {
718
- p1 = edge.points[0];
719
- p2 = edge.points[edge.points.length - 1];
720
- }
721
- edge.points.unshift(util.intersectRect(nodeV, p1));
722
- edge.points.push(util.intersectRect(nodeW, p2));
723
- });
724
- }
725
-
726
- function fixupEdgeLabelCoords(g) {
727
- _.forEach(g.edges(), function(e) {
728
- var edge = g.edge(e);
729
- if (_.has(edge, "x")) {
730
- if (edge.labelpos === "l" || edge.labelpos === "r") {
731
- edge.width -= edge.labeloffset;
732
- }
733
- switch (edge.labelpos) {
734
- case "l": edge.x -= edge.width / 2 + edge.labeloffset; break;
735
- case "r": edge.x += edge.width / 2 + edge.labeloffset; break;
736
- }
737
- }
738
- });
739
- }
740
-
741
- function reversePointsForReversedEdges(g) {
742
- _.forEach(g.edges(), function(e) {
743
- var edge = g.edge(e);
744
- if (edge.reversed) {
745
- edge.points.reverse();
746
- }
747
- });
748
- }
749
-
750
- function removeBorderNodes(g) {
751
- _.forEach(g.nodes(), function(v) {
752
- if (g.children(v).length) {
753
- var node = g.node(v),
754
- t = g.node(node.borderTop),
755
- b = g.node(node.borderBottom),
756
- l = g.node(_.last(node.borderLeft)),
757
- r = g.node(_.last(node.borderRight));
758
-
759
- node.width = Math.abs(r.x - l.x);
760
- node.height = Math.abs(b.y - t.y);
761
- node.x = l.x + node.width / 2;
762
- node.y = t.y + node.height / 2;
763
- }
764
- });
765
-
766
- _.forEach(g.nodes(), function(v) {
767
- if (g.node(v).dummy === "border") {
768
- g.removeNode(v);
769
- }
770
- });
771
- }
772
-
773
- function removeSelfEdges(g) {
774
- _.forEach(g.edges(), function(e) {
775
- if (e.v === e.w) {
776
- var node = g.node(e.v);
777
- if (!node.selfEdges) {
778
- node.selfEdges = [];
779
- }
780
- node.selfEdges.push({ e: e, label: g.edge(e) });
781
- g.removeEdge(e);
782
- }
783
- });
784
- }
785
-
786
- function insertSelfEdges(g) {
787
- var layers = util.buildLayerMatrix(g);
788
- _.forEach(layers, function(layer) {
789
- var orderShift = 0;
790
- _.forEach(layer, function(v, i) {
791
- var node = g.node(v);
792
- node.order = i + orderShift;
793
- _.forEach(node.selfEdges, function(selfEdge) {
794
- util.addDummyNode(g, "selfedge", {
795
- width: selfEdge.label.width,
796
- height: selfEdge.label.height,
797
- rank: node.rank,
798
- order: i + (++orderShift),
799
- e: selfEdge.e,
800
- label: selfEdge.label
801
- }, "_se");
802
- });
803
- delete node.selfEdges;
804
- });
805
- });
806
- }
807
-
808
- function positionSelfEdges(g) {
809
- _.forEach(g.nodes(), function(v) {
810
- var node = g.node(v);
811
- if (node.dummy === "selfedge") {
812
- var selfNode = g.node(node.e.v),
813
- x = selfNode.x + selfNode.width / 2,
814
- y = selfNode.y,
815
- dx = node.x - x,
816
- dy = selfNode.height / 2;
817
- g.setEdge(node.e, node.label);
818
- g.removeNode(v);
819
- node.label.points = [
820
- { x: x + 2 * dx / 3, y: y - dy },
821
- { x: x + 5 * dx / 6, y: y - dy },
822
- { x: x + dx , y: y },
823
- { x: x + 5 * dx / 6, y: y + dy },
824
- { x: x + 2 * dx / 3, y: y + dy }
825
- ];
826
- node.label.x = node.x;
827
- node.label.y = node.y;
828
- }
829
- });
830
- }
831
-
832
- function selectNumberAttrs(obj, attrs) {
833
- return _.mapValues(_.pick(obj, attrs), Number);
834
- }
835
-
836
- function canonicalize(attrs) {
837
- var newAttrs = {};
838
- _.forEach(attrs, function(v, k) {
839
- newAttrs[k.toLowerCase()] = v;
840
- });
841
- return newAttrs;
842
- }
843
-
844
- },{"./acyclic":2,"./add-border-segments":3,"./coordinate-system":4,"./graphlib":7,"./lodash":10,"./nesting-graph":11,"./normalize":12,"./order":17,"./parent-dummy-chains":22,"./position":24,"./rank":26,"./util":29}],10:[function(require,module,exports){
845
- /* global window */
846
-
847
- var lodash;
848
-
849
- if (typeof require === "function") {
850
- try {
851
- lodash = require("lodash");
852
- } catch (e) {}
853
- }
854
-
855
- if (!lodash) {
856
- lodash = window._;
857
- }
858
-
859
- module.exports = lodash;
860
-
861
- },{"lodash":undefined}],11:[function(require,module,exports){
862
- var _ = require("./lodash"),
863
- util = require("./util");
864
-
865
- module.exports = {
866
- run: run,
867
- cleanup: cleanup
868
- };
869
-
870
- /*
871
- * A nesting graph creates dummy nodes for the tops and bottoms of subgraphs,
872
- * adds appropriate edges to ensure that all cluster nodes are placed between
873
- * these boundries, and ensures that the graph is connected.
874
- *
875
- * In addition we ensure, through the use of the minlen property, that nodes
876
- * and subgraph border nodes to not end up on the same rank.
877
- *
878
- * Preconditions:
879
- *
880
- * 1. Input graph is a DAG
881
- * 2. Nodes in the input graph has a minlen attribute
882
- *
883
- * Postconditions:
884
- *
885
- * 1. Input graph is connected.
886
- * 2. Dummy nodes are added for the tops and bottoms of subgraphs.
887
- * 3. The minlen attribute for nodes is adjusted to ensure nodes do not
888
- * get placed on the same rank as subgraph border nodes.
889
- *
890
- * The nesting graph idea comes from Sander, "Layout of Compound Directed
891
- * Graphs."
892
- */
893
- function run(g) {
894
- var root = util.addDummyNode(g, "root", {}, "_root");
895
- var depths = treeDepths(g);
896
- var height = _.max(_.values(depths)) - 1; // Note: depths is an Object not an array
897
- var nodeSep = 2 * height + 1;
898
-
899
- g.graph().nestingRoot = root;
900
-
901
- // Multiply minlen by nodeSep to align nodes on non-border ranks.
902
- _.forEach(g.edges(), function(e) { g.edge(e).minlen *= nodeSep; });
903
-
904
- // Calculate a weight that is sufficient to keep subgraphs vertically compact
905
- var weight = sumWeights(g) + 1;
906
-
907
- // Create border nodes and link them up
908
- _.forEach(g.children(), function(child) {
909
- dfs(g, root, nodeSep, weight, height, depths, child);
910
- });
911
-
912
- // Save the multiplier for node layers for later removal of empty border
913
- // layers.
914
- g.graph().nodeRankFactor = nodeSep;
915
- }
916
-
917
- function dfs(g, root, nodeSep, weight, height, depths, v) {
918
- var children = g.children(v);
919
- if (!children.length) {
920
- if (v !== root) {
921
- g.setEdge(root, v, { weight: 0, minlen: nodeSep });
922
- }
923
- return;
924
- }
925
-
926
- var top = util.addBorderNode(g, "_bt"),
927
- bottom = util.addBorderNode(g, "_bb"),
928
- label = g.node(v);
929
-
930
- g.setParent(top, v);
931
- label.borderTop = top;
932
- g.setParent(bottom, v);
933
- label.borderBottom = bottom;
934
-
935
- _.forEach(children, function(child) {
936
- dfs(g, root, nodeSep, weight, height, depths, child);
937
-
938
- var childNode = g.node(child),
939
- childTop = childNode.borderTop ? childNode.borderTop : child,
940
- childBottom = childNode.borderBottom ? childNode.borderBottom : child,
941
- thisWeight = childNode.borderTop ? weight : 2 * weight,
942
- minlen = childTop !== childBottom ? 1 : height - depths[v] + 1;
943
-
944
- g.setEdge(top, childTop, {
945
- weight: thisWeight,
946
- minlen: minlen,
947
- nestingEdge: true
948
- });
949
-
950
- g.setEdge(childBottom, bottom, {
951
- weight: thisWeight,
952
- minlen: minlen,
953
- nestingEdge: true
954
- });
955
- });
956
-
957
- if (!g.parent(v)) {
958
- g.setEdge(root, top, { weight: 0, minlen: height + depths[v] });
959
- }
960
- }
961
-
962
- function treeDepths(g) {
963
- var depths = {};
964
- function dfs(v, depth) {
965
- var children = g.children(v);
966
- if (children && children.length) {
967
- _.forEach(children, function(child) {
968
- dfs(child, depth + 1);
969
- });
970
- }
971
- depths[v] = depth;
972
- }
973
- _.forEach(g.children(), function(v) { dfs(v, 1); });
974
- return depths;
975
- }
976
-
977
- function sumWeights(g) {
978
- return _.reduce(g.edges(), function(acc, e) {
979
- return acc + g.edge(e).weight;
980
- }, 0);
981
- }
982
-
983
- function cleanup(g) {
984
- var graphLabel = g.graph();
985
- g.removeNode(graphLabel.nestingRoot);
986
- delete graphLabel.nestingRoot;
987
- _.forEach(g.edges(), function(e) {
988
- var edge = g.edge(e);
989
- if (edge.nestingEdge) {
990
- g.removeEdge(e);
991
- }
992
- });
993
- }
994
-
995
- },{"./lodash":10,"./util":29}],12:[function(require,module,exports){
996
- "use strict";
997
-
998
- var _ = require("./lodash"),
999
- util = require("./util");
1000
-
1001
- module.exports = {
1002
- run: run,
1003
- undo: undo
1004
- };
1005
-
1006
- /*
1007
- * Breaks any long edges in the graph into short segments that span 1 layer
1008
- * each. This operation is undoable with the denormalize function.
1009
- *
1010
- * Pre-conditions:
1011
- *
1012
- * 1. The input graph is a DAG.
1013
- * 2. Each node in the graph has a "rank" property.
1014
- *
1015
- * Post-condition:
1016
- *
1017
- * 1. All edges in the graph have a length of 1.
1018
- * 2. Dummy nodes are added where edges have been split into segments.
1019
- * 3. The graph is augmented with a "dummyChains" attribute which contains
1020
- * the first dummy in each chain of dummy nodes produced.
1021
- */
1022
- function run(g) {
1023
- g.graph().dummyChains = [];
1024
- _.forEach(g.edges(), function(edge) { normalizeEdge(g, edge); });
1025
- }
1026
-
1027
- function normalizeEdge(g, e) {
1028
- var v = e.v,
1029
- vRank = g.node(v).rank,
1030
- w = e.w,
1031
- wRank = g.node(w).rank,
1032
- name = e.name,
1033
- edgeLabel = g.edge(e),
1034
- labelRank = edgeLabel.labelRank;
1035
-
1036
- if (wRank === vRank + 1) return;
1037
-
1038
- g.removeEdge(e);
1039
-
1040
- var dummy, attrs, i;
1041
- for (i = 0, ++vRank; vRank < wRank; ++i, ++vRank) {
1042
- edgeLabel.points = [];
1043
- attrs = {
1044
- width: 0, height: 0,
1045
- edgeLabel: edgeLabel, edgeObj: e,
1046
- rank: vRank
1047
- };
1048
- dummy = util.addDummyNode(g, "edge", attrs, "_d");
1049
- if (vRank === labelRank) {
1050
- attrs.width = edgeLabel.width;
1051
- attrs.height = edgeLabel.height;
1052
- attrs.dummy = "edge-label";
1053
- attrs.labelpos = edgeLabel.labelpos;
1054
- }
1055
- g.setEdge(v, dummy, { weight: edgeLabel.weight }, name);
1056
- if (i === 0) {
1057
- g.graph().dummyChains.push(dummy);
1058
- }
1059
- v = dummy;
1060
- }
1061
-
1062
- g.setEdge(v, w, { weight: edgeLabel.weight }, name);
1063
- }
1064
-
1065
- function undo(g) {
1066
- _.forEach(g.graph().dummyChains, function(v) {
1067
- var node = g.node(v),
1068
- origLabel = node.edgeLabel,
1069
- w;
1070
- g.setEdge(node.edgeObj, origLabel);
1071
- while (node.dummy) {
1072
- w = g.successors(v)[0];
1073
- g.removeNode(v);
1074
- origLabel.points.push({ x: node.x, y: node.y });
1075
- if (node.dummy === "edge-label") {
1076
- origLabel.x = node.x;
1077
- origLabel.y = node.y;
1078
- origLabel.width = node.width;
1079
- origLabel.height = node.height;
1080
- }
1081
- v = w;
1082
- node = g.node(v);
1083
- }
1084
- });
1085
- }
1086
-
1087
- },{"./lodash":10,"./util":29}],13:[function(require,module,exports){
1088
- var _ = require("../lodash");
1089
-
1090
- module.exports = addSubgraphConstraints;
1091
-
1092
- function addSubgraphConstraints(g, cg, vs) {
1093
- var prev = {},
1094
- rootPrev;
1095
-
1096
- _.forEach(vs, function(v) {
1097
- var child = g.parent(v),
1098
- parent,
1099
- prevChild;
1100
- while (child) {
1101
- parent = g.parent(child);
1102
- if (parent) {
1103
- prevChild = prev[parent];
1104
- prev[parent] = child;
1105
- } else {
1106
- prevChild = rootPrev;
1107
- rootPrev = child;
1108
- }
1109
- if (prevChild && prevChild !== child) {
1110
- cg.setEdge(prevChild, child);
1111
- return;
1112
- }
1113
- child = parent;
1114
- }
1115
- });
1116
-
1117
- /*
1118
- function dfs(v) {
1119
- var children = v ? g.children(v) : g.children();
1120
- if (children.length) {
1121
- var min = Number.POSITIVE_INFINITY,
1122
- subgraphs = [];
1123
- _.each(children, function(child) {
1124
- var childMin = dfs(child);
1125
- if (g.children(child).length) {
1126
- subgraphs.push({ v: child, order: childMin });
1127
- }
1128
- min = Math.min(min, childMin);
1129
- });
1130
- _.reduce(_.sortBy(subgraphs, "order"), function(prev, curr) {
1131
- cg.setEdge(prev.v, curr.v);
1132
- return curr;
1133
- });
1134
- return min;
1135
- }
1136
- return g.node(v).order;
1137
- }
1138
- dfs(undefined);
1139
- */
1140
- }
1141
-
1142
- },{"../lodash":10}],14:[function(require,module,exports){
1143
- var _ = require("../lodash");
1144
-
1145
- module.exports = barycenter;
1146
-
1147
- function barycenter(g, movable) {
1148
- return _.map(movable, function(v) {
1149
- var inV = g.inEdges(v);
1150
- if (!inV.length) {
1151
- return { v: v };
1152
- } else {
1153
- var result = _.reduce(inV, function(acc, e) {
1154
- var edge = g.edge(e),
1155
- nodeU = g.node(e.v);
1156
- return {
1157
- sum: acc.sum + (edge.weight * nodeU.order),
1158
- weight: acc.weight + edge.weight
1159
- };
1160
- }, { sum: 0, weight: 0 });
1161
-
1162
- return {
1163
- v: v,
1164
- barycenter: result.sum / result.weight,
1165
- weight: result.weight
1166
- };
1167
- }
1168
- });
1169
- }
1170
-
1171
-
1172
- },{"../lodash":10}],15:[function(require,module,exports){
1173
- var _ = require("../lodash"),
1174
- Graph = require("../graphlib").Graph;
1175
-
1176
- module.exports = buildLayerGraph;
1177
-
1178
- /*
1179
- * Constructs a graph that can be used to sort a layer of nodes. The graph will
1180
- * contain all base and subgraph nodes from the request layer in their original
1181
- * hierarchy and any edges that are incident on these nodes and are of the type
1182
- * requested by the "relationship" parameter.
1183
- *
1184
- * Nodes from the requested rank that do not have parents are assigned a root
1185
- * node in the output graph, which is set in the root graph attribute. This
1186
- * makes it easy to walk the hierarchy of movable nodes during ordering.
1187
- *
1188
- * Pre-conditions:
1189
- *
1190
- * 1. Input graph is a DAG
1191
- * 2. Base nodes in the input graph have a rank attribute
1192
- * 3. Subgraph nodes in the input graph has minRank and maxRank attributes
1193
- * 4. Edges have an assigned weight
1194
- *
1195
- * Post-conditions:
1196
- *
1197
- * 1. Output graph has all nodes in the movable rank with preserved
1198
- * hierarchy.
1199
- * 2. Root nodes in the movable layer are made children of the node
1200
- * indicated by the root attribute of the graph.
1201
- * 3. Non-movable nodes incident on movable nodes, selected by the
1202
- * relationship parameter, are included in the graph (without hierarchy).
1203
- * 4. Edges incident on movable nodes, selected by the relationship
1204
- * parameter, are added to the output graph.
1205
- * 5. The weights for copied edges are aggregated as need, since the output
1206
- * graph is not a multi-graph.
1207
- */
1208
- function buildLayerGraph(g, rank, relationship) {
1209
- var root = createRootNode(g),
1210
- result = new Graph({ compound: true }).setGraph({ root: root })
1211
- .setDefaultNodeLabel(function(v) { return g.node(v); });
1212
-
1213
- _.forEach(g.nodes(), function(v) {
1214
- var node = g.node(v),
1215
- parent = g.parent(v);
1216
-
1217
- if (node.rank === rank || node.minRank <= rank && rank <= node.maxRank) {
1218
- result.setNode(v);
1219
- result.setParent(v, parent || root);
1220
-
1221
- // This assumes we have only short edges!
1222
- _.forEach(g[relationship](v), function(e) {
1223
- var u = e.v === v ? e.w : e.v,
1224
- edge = result.edge(u, v),
1225
- weight = !_.isUndefined(edge) ? edge.weight : 0;
1226
- result.setEdge(u, v, { weight: g.edge(e).weight + weight });
1227
- });
1228
-
1229
- if (_.has(node, "minRank")) {
1230
- result.setNode(v, {
1231
- borderLeft: node.borderLeft[rank],
1232
- borderRight: node.borderRight[rank]
1233
- });
1234
- }
1235
- }
1236
- });
1237
-
1238
- return result;
1239
- }
1240
-
1241
- function createRootNode(g) {
1242
- var v;
1243
- while (g.hasNode((v = _.uniqueId("_root"))));
1244
- return v;
1245
- }
1246
-
1247
- },{"../graphlib":7,"../lodash":10}],16:[function(require,module,exports){
1248
- "use strict";
1249
-
1250
- var _ = require("../lodash");
1251
-
1252
- module.exports = crossCount;
1253
-
1254
- /*
1255
- * A function that takes a layering (an array of layers, each with an array of
1256
- * ordererd nodes) and a graph and returns a weighted crossing count.
1257
- *
1258
- * Pre-conditions:
1259
- *
1260
- * 1. Input graph must be simple (not a multigraph), directed, and include
1261
- * only simple edges.
1262
- * 2. Edges in the input graph must have assigned weights.
1263
- *
1264
- * Post-conditions:
1265
- *
1266
- * 1. The graph and layering matrix are left unchanged.
1267
- *
1268
- * This algorithm is derived from Barth, et al., "Bilayer Cross Counting."
1269
- */
1270
- function crossCount(g, layering) {
1271
- var cc = 0;
1272
- for (var i = 1; i < layering.length; ++i) {
1273
- cc += twoLayerCrossCount(g, layering[i-1], layering[i]);
1274
- }
1275
- return cc;
1276
- }
1277
-
1278
- function twoLayerCrossCount(g, northLayer, southLayer) {
1279
- // Sort all of the edges between the north and south layers by their position
1280
- // in the north layer and then the south. Map these edges to the position of
1281
- // their head in the south layer.
1282
- var southPos = _.zipObject(southLayer,
1283
- _.map(southLayer, function (v, i) { return i; }));
1284
- var southEntries = _.flatten(_.map(northLayer, function(v) {
1285
- return _.chain(g.outEdges(v))
1286
- .map(function(e) {
1287
- return { pos: southPos[e.w], weight: g.edge(e).weight };
1288
- })
1289
- .sortBy("pos")
1290
- .value();
1291
- }), true);
1292
-
1293
- // Build the accumulator tree
1294
- var firstIndex = 1;
1295
- while (firstIndex < southLayer.length) firstIndex <<= 1;
1296
- var treeSize = 2 * firstIndex - 1;
1297
- firstIndex -= 1;
1298
- var tree = _.map(new Array(treeSize), function() { return 0; });
1299
-
1300
- // Calculate the weighted crossings
1301
- var cc = 0;
1302
- _.forEach(southEntries.forEach(function(entry) {
1303
- var index = entry.pos + firstIndex;
1304
- tree[index] += entry.weight;
1305
- var weightSum = 0;
1306
- while (index > 0) {
1307
- if (index % 2) {
1308
- weightSum += tree[index + 1];
1309
- }
1310
- index = (index - 1) >> 1;
1311
- tree[index] += entry.weight;
1312
- }
1313
- cc += entry.weight * weightSum;
1314
- }));
1315
-
1316
- return cc;
1317
- }
1318
-
1319
- },{"../lodash":10}],17:[function(require,module,exports){
1320
- "use strict";
1321
-
1322
- var _ = require("../lodash"),
1323
- initOrder = require("./init-order"),
1324
- crossCount = require("./cross-count"),
1325
- sortSubgraph = require("./sort-subgraph"),
1326
- buildLayerGraph = require("./build-layer-graph"),
1327
- addSubgraphConstraints = require("./add-subgraph-constraints"),
1328
- Graph = require("../graphlib").Graph,
1329
- util = require("../util");
1330
-
1331
- module.exports = order;
1332
-
1333
- /*
1334
- * Applies heuristics to minimize edge crossings in the graph and sets the best
1335
- * order solution as an order attribute on each node.
1336
- *
1337
- * Pre-conditions:
1338
- *
1339
- * 1. Graph must be DAG
1340
- * 2. Graph nodes must be objects with a "rank" attribute
1341
- * 3. Graph edges must have the "weight" attribute
1342
- *
1343
- * Post-conditions:
1344
- *
1345
- * 1. Graph nodes will have an "order" attribute based on the results of the
1346
- * algorithm.
1347
- */
1348
- function order(g) {
1349
- var maxRank = util.maxRank(g),
1350
- downLayerGraphs = buildLayerGraphs(g, _.range(1, maxRank + 1), "inEdges"),
1351
- upLayerGraphs = buildLayerGraphs(g, _.range(maxRank - 1, -1, -1), "outEdges");
1352
-
1353
- var layering = initOrder(g);
1354
- assignOrder(g, layering);
1355
-
1356
- var bestCC = Number.POSITIVE_INFINITY,
1357
- best;
1358
-
1359
- for (var i = 0, lastBest = 0; lastBest < 4; ++i, ++lastBest) {
1360
- sweepLayerGraphs(i % 2 ? downLayerGraphs : upLayerGraphs, i % 4 >= 2);
1361
-
1362
- layering = util.buildLayerMatrix(g);
1363
- var cc = crossCount(g, layering);
1364
- if (cc < bestCC) {
1365
- lastBest = 0;
1366
- best = _.cloneDeep(layering);
1367
- bestCC = cc;
1368
- }
1369
- }
1370
-
1371
- assignOrder(g, best);
1372
- }
1373
-
1374
- function buildLayerGraphs(g, ranks, relationship) {
1375
- return _.map(ranks, function(rank) {
1376
- return buildLayerGraph(g, rank, relationship);
1377
- });
1378
- }
1379
-
1380
- function sweepLayerGraphs(layerGraphs, biasRight) {
1381
- var cg = new Graph();
1382
- _.forEach(layerGraphs, function(lg) {
1383
- var root = lg.graph().root;
1384
- var sorted = sortSubgraph(lg, root, cg, biasRight);
1385
- _.forEach(sorted.vs, function(v, i) {
1386
- lg.node(v).order = i;
1387
- });
1388
- addSubgraphConstraints(lg, cg, sorted.vs);
1389
- });
1390
- }
1391
-
1392
- function assignOrder(g, layering) {
1393
- _.forEach(layering, function(layer) {
1394
- _.forEach(layer, function(v, i) {
1395
- g.node(v).order = i;
1396
- });
1397
- });
1398
- }
1399
-
1400
- },{"../graphlib":7,"../lodash":10,"../util":29,"./add-subgraph-constraints":13,"./build-layer-graph":15,"./cross-count":16,"./init-order":18,"./sort-subgraph":20}],18:[function(require,module,exports){
1401
- "use strict";
1402
-
1403
- var _ = require("../lodash");
1404
-
1405
- module.exports = initOrder;
1406
-
1407
- /*
1408
- * Assigns an initial order value for each node by performing a DFS search
1409
- * starting from nodes in the first rank. Nodes are assigned an order in their
1410
- * rank as they are first visited.
1411
- *
1412
- * This approach comes from Gansner, et al., "A Technique for Drawing Directed
1413
- * Graphs."
1414
- *
1415
- * Returns a layering matrix with an array per layer and each layer sorted by
1416
- * the order of its nodes.
1417
- */
1418
- function initOrder(g) {
1419
- var visited = {},
1420
- simpleNodes = _.filter(g.nodes(), function(v) {
1421
- return !g.children(v).length;
1422
- }),
1423
- maxRank = _.max(_.map(simpleNodes, function(v) { return g.node(v).rank; })),
1424
- layers = _.map(_.range(maxRank + 1), function() { return []; });
1425
-
1426
- function dfs(v) {
1427
- if (_.has(visited, v)) return;
1428
- visited[v] = true;
1429
- var node = g.node(v);
1430
- layers[node.rank].push(v);
1431
- _.forEach(g.successors(v), dfs);
1432
- }
1433
-
1434
- var orderedVs = _.sortBy(simpleNodes, function(v) { return g.node(v).rank; });
1435
- _.forEach(orderedVs, dfs);
1436
-
1437
- return layers;
1438
- }
1439
-
1440
- },{"../lodash":10}],19:[function(require,module,exports){
1441
- "use strict";
1442
-
1443
- var _ = require("../lodash");
1444
-
1445
- module.exports = resolveConflicts;
1446
-
1447
- /*
1448
- * Given a list of entries of the form {v, barycenter, weight} and a
1449
- * constraint graph this function will resolve any conflicts between the
1450
- * constraint graph and the barycenters for the entries. If the barycenters for
1451
- * an entry would violate a constraint in the constraint graph then we coalesce
1452
- * the nodes in the conflict into a new node that respects the contraint and
1453
- * aggregates barycenter and weight information.
1454
- *
1455
- * This implementation is based on the description in Forster, "A Fast and
1456
- * Simple Hueristic for Constrained Two-Level Crossing Reduction," thought it
1457
- * differs in some specific details.
1458
- *
1459
- * Pre-conditions:
1460
- *
1461
- * 1. Each entry has the form {v, barycenter, weight}, or if the node has
1462
- * no barycenter, then {v}.
1463
- *
1464
- * Returns:
1465
- *
1466
- * A new list of entries of the form {vs, i, barycenter, weight}. The list
1467
- * `vs` may either be a singleton or it may be an aggregation of nodes
1468
- * ordered such that they do not violate constraints from the constraint
1469
- * graph. The property `i` is the lowest original index of any of the
1470
- * elements in `vs`.
1471
- */
1472
- function resolveConflicts(entries, cg) {
1473
- var mappedEntries = {};
1474
- _.forEach(entries, function(entry, i) {
1475
- var tmp = mappedEntries[entry.v] = {
1476
- indegree: 0,
1477
- "in": [],
1478
- out: [],
1479
- vs: [entry.v],
1480
- i: i
1481
- };
1482
- if (!_.isUndefined(entry.barycenter)) {
1483
- tmp.barycenter = entry.barycenter;
1484
- tmp.weight = entry.weight;
1485
- }
1486
- });
1487
-
1488
- _.forEach(cg.edges(), function(e) {
1489
- var entryV = mappedEntries[e.v],
1490
- entryW = mappedEntries[e.w];
1491
- if (!_.isUndefined(entryV) && !_.isUndefined(entryW)) {
1492
- entryW.indegree++;
1493
- entryV.out.push(mappedEntries[e.w]);
1494
- }
1495
- });
1496
-
1497
- var sourceSet = _.filter(mappedEntries, function(entry) {
1498
- return !entry.indegree;
1499
- });
1500
-
1501
- return doResolveConflicts(sourceSet);
1502
- }
1503
-
1504
- function doResolveConflicts(sourceSet) {
1505
- var entries = [];
1506
-
1507
- function handleIn(vEntry) {
1508
- return function(uEntry) {
1509
- if (uEntry.merged) {
1510
- return;
1511
- }
1512
- if (_.isUndefined(uEntry.barycenter) ||
1513
- _.isUndefined(vEntry.barycenter) ||
1514
- uEntry.barycenter >= vEntry.barycenter) {
1515
- mergeEntries(vEntry, uEntry);
1516
- }
1517
- };
1518
- }
1519
-
1520
- function handleOut(vEntry) {
1521
- return function(wEntry) {
1522
- wEntry["in"].push(vEntry);
1523
- if (--wEntry.indegree === 0) {
1524
- sourceSet.push(wEntry);
1525
- }
1526
- };
1527
- }
1528
-
1529
- while (sourceSet.length) {
1530
- var entry = sourceSet.pop();
1531
- entries.push(entry);
1532
- _.forEach(entry["in"].reverse(), handleIn(entry));
1533
- _.forEach(entry.out, handleOut(entry));
1534
- }
1535
-
1536
- return _.chain(entries)
1537
- .filter(function(entry) { return !entry.merged; })
1538
- .map(function(entry) {
1539
- return _.pick(entry, ["vs", "i", "barycenter", "weight"]);
1540
- })
1541
- .value();
1542
- }
1543
-
1544
- function mergeEntries(target, source) {
1545
- var sum = 0,
1546
- weight = 0;
1547
-
1548
- if (target.weight) {
1549
- sum += target.barycenter * target.weight;
1550
- weight += target.weight;
1551
- }
1552
-
1553
- if (source.weight) {
1554
- sum += source.barycenter * source.weight;
1555
- weight += source.weight;
1556
- }
1557
-
1558
- target.vs = source.vs.concat(target.vs);
1559
- target.barycenter = sum / weight;
1560
- target.weight = weight;
1561
- target.i = Math.min(source.i, target.i);
1562
- source.merged = true;
1563
- }
1564
-
1565
- },{"../lodash":10}],20:[function(require,module,exports){
1566
- var _ = require("../lodash"),
1567
- barycenter = require("./barycenter"),
1568
- resolveConflicts = require("./resolve-conflicts"),
1569
- sort = require("./sort");
1570
-
1571
- module.exports = sortSubgraph;
1572
-
1573
- function sortSubgraph(g, v, cg, biasRight) {
1574
- var movable = g.children(v),
1575
- node = g.node(v),
1576
- bl = node ? node.borderLeft : undefined,
1577
- br = node ? node.borderRight: undefined,
1578
- subgraphs = {};
1579
-
1580
- if (bl) {
1581
- movable = _.filter(movable, function(w) {
1582
- return w !== bl && w !== br;
1583
- });
1584
- }
1585
-
1586
- var barycenters = barycenter(g, movable);
1587
- _.forEach(barycenters, function(entry) {
1588
- if (g.children(entry.v).length) {
1589
- var subgraphResult = sortSubgraph(g, entry.v, cg, biasRight);
1590
- subgraphs[entry.v] = subgraphResult;
1591
- if (_.has(subgraphResult, "barycenter")) {
1592
- mergeBarycenters(entry, subgraphResult);
1593
- }
1594
- }
1595
- });
1596
-
1597
- var entries = resolveConflicts(barycenters, cg);
1598
- expandSubgraphs(entries, subgraphs);
1599
-
1600
- var result = sort(entries, biasRight);
1601
-
1602
- if (bl) {
1603
- result.vs = _.flatten([bl, result.vs, br], true);
1604
- if (g.predecessors(bl).length) {
1605
- var blPred = g.node(g.predecessors(bl)[0]),
1606
- brPred = g.node(g.predecessors(br)[0]);
1607
- if (!_.has(result, "barycenter")) {
1608
- result.barycenter = 0;
1609
- result.weight = 0;
1610
- }
1611
- result.barycenter = (result.barycenter * result.weight +
1612
- blPred.order + brPred.order) / (result.weight + 2);
1613
- result.weight += 2;
1614
- }
1615
- }
1616
-
1617
- return result;
1618
- }
1619
-
1620
- function expandSubgraphs(entries, subgraphs) {
1621
- _.forEach(entries, function(entry) {
1622
- entry.vs = _.flatten(entry.vs.map(function(v) {
1623
- if (subgraphs[v]) {
1624
- return subgraphs[v].vs;
1625
- }
1626
- return v;
1627
- }), true);
1628
- });
1629
- }
1630
-
1631
- function mergeBarycenters(target, other) {
1632
- if (!_.isUndefined(target.barycenter)) {
1633
- target.barycenter = (target.barycenter * target.weight +
1634
- other.barycenter * other.weight) /
1635
- (target.weight + other.weight);
1636
- target.weight += other.weight;
1637
- } else {
1638
- target.barycenter = other.barycenter;
1639
- target.weight = other.weight;
1640
- }
1641
- }
1642
-
1643
- },{"../lodash":10,"./barycenter":14,"./resolve-conflicts":19,"./sort":21}],21:[function(require,module,exports){
1644
- var _ = require("../lodash"),
1645
- util = require("../util");
1646
-
1647
- module.exports = sort;
1648
-
1649
- function sort(entries, biasRight) {
1650
- var parts = util.partition(entries, function(entry) {
1651
- return _.has(entry, "barycenter");
1652
- });
1653
- var sortable = parts.lhs,
1654
- unsortable = _.sortBy(parts.rhs, function(entry) { return -entry.i; }),
1655
- vs = [],
1656
- sum = 0,
1657
- weight = 0,
1658
- vsIndex = 0;
1659
-
1660
- sortable.sort(compareWithBias(!!biasRight));
1661
-
1662
- vsIndex = consumeUnsortable(vs, unsortable, vsIndex);
1663
-
1664
- _.forEach(sortable, function (entry) {
1665
- vsIndex += entry.vs.length;
1666
- vs.push(entry.vs);
1667
- sum += entry.barycenter * entry.weight;
1668
- weight += entry.weight;
1669
- vsIndex = consumeUnsortable(vs, unsortable, vsIndex);
1670
- });
1671
-
1672
- var result = { vs: _.flatten(vs, true) };
1673
- if (weight) {
1674
- result.barycenter = sum / weight;
1675
- result.weight = weight;
1676
- }
1677
- return result;
1678
- }
1679
-
1680
- function consumeUnsortable(vs, unsortable, index) {
1681
- var last;
1682
- while (unsortable.length && (last = _.last(unsortable)).i <= index) {
1683
- unsortable.pop();
1684
- vs.push(last.vs);
1685
- index++;
1686
- }
1687
- return index;
1688
- }
1689
-
1690
- function compareWithBias(bias) {
1691
- return function(entryV, entryW) {
1692
- if (entryV.barycenter < entryW.barycenter) {
1693
- return -1;
1694
- } else if (entryV.barycenter > entryW.barycenter) {
1695
- return 1;
1696
- }
1697
-
1698
- return !bias ? entryV.i - entryW.i : entryW.i - entryV.i;
1699
- };
1700
- }
1701
-
1702
- },{"../lodash":10,"../util":29}],22:[function(require,module,exports){
1703
- var _ = require("./lodash");
1704
-
1705
- module.exports = parentDummyChains;
1706
-
1707
- function parentDummyChains(g) {
1708
- var postorderNums = postorder(g);
1709
-
1710
- _.forEach(g.graph().dummyChains, function(v) {
1711
- var node = g.node(v),
1712
- edgeObj = node.edgeObj,
1713
- pathData = findPath(g, postorderNums, edgeObj.v, edgeObj.w),
1714
- path = pathData.path,
1715
- lca = pathData.lca,
1716
- pathIdx = 0,
1717
- pathV = path[pathIdx],
1718
- ascending = true;
1719
-
1720
- while (v !== edgeObj.w) {
1721
- node = g.node(v);
1722
-
1723
- if (ascending) {
1724
- while ((pathV = path[pathIdx]) !== lca &&
1725
- g.node(pathV).maxRank < node.rank) {
1726
- pathIdx++;
1727
- }
1728
-
1729
- if (pathV === lca) {
1730
- ascending = false;
1731
- }
1732
- }
1733
-
1734
- if (!ascending) {
1735
- while (pathIdx < path.length - 1 &&
1736
- g.node(pathV = path[pathIdx + 1]).minRank <= node.rank) {
1737
- pathIdx++;
1738
- }
1739
- pathV = path[pathIdx];
1740
- }
1741
-
1742
- g.setParent(v, pathV);
1743
- v = g.successors(v)[0];
1744
- }
1745
- });
1746
- }
1747
-
1748
- // Find a path from v to w through the lowest common ancestor (LCA). Return the
1749
- // full path and the LCA.
1750
- function findPath(g, postorderNums, v, w) {
1751
- var vPath = [],
1752
- wPath = [],
1753
- low = Math.min(postorderNums[v].low, postorderNums[w].low),
1754
- lim = Math.max(postorderNums[v].lim, postorderNums[w].lim),
1755
- parent,
1756
- lca;
1757
-
1758
- // Traverse up from v to find the LCA
1759
- parent = v;
1760
- do {
1761
- parent = g.parent(parent);
1762
- vPath.push(parent);
1763
- } while (parent &&
1764
- (postorderNums[parent].low > low || lim > postorderNums[parent].lim));
1765
- lca = parent;
1766
-
1767
- // Traverse from w to LCA
1768
- parent = w;
1769
- while ((parent = g.parent(parent)) !== lca) {
1770
- wPath.push(parent);
1771
- }
1772
-
1773
- return { path: vPath.concat(wPath.reverse()), lca: lca };
1774
- }
1775
-
1776
- function postorder(g) {
1777
- var result = {},
1778
- lim = 0;
1779
-
1780
- function dfs(v) {
1781
- var low = lim;
1782
- _.forEach(g.children(v), dfs);
1783
- result[v] = { low: low, lim: lim++ };
1784
- }
1785
- _.forEach(g.children(), dfs);
1786
-
1787
- return result;
1788
- }
1789
-
1790
- },{"./lodash":10}],23:[function(require,module,exports){
1791
- "use strict";
1792
-
1793
- var _ = require("../lodash"),
1794
- Graph = require("../graphlib").Graph,
1795
- util = require("../util");
1796
-
1797
- /*
1798
- * This module provides coordinate assignment based on Brandes and Köpf, "Fast
1799
- * and Simple Horizontal Coordinate Assignment."
1800
- */
1801
-
1802
- module.exports = {
1803
- positionX: positionX,
1804
- findType1Conflicts: findType1Conflicts,
1805
- findType2Conflicts: findType2Conflicts,
1806
- addConflict: addConflict,
1807
- hasConflict: hasConflict,
1808
- verticalAlignment: verticalAlignment,
1809
- horizontalCompaction: horizontalCompaction,
1810
- alignCoordinates: alignCoordinates,
1811
- findSmallestWidthAlignment: findSmallestWidthAlignment,
1812
- balance: balance
1813
- };
1814
-
1815
- /*
1816
- * Marks all edges in the graph with a type-1 conflict with the "type1Conflict"
1817
- * property. A type-1 conflict is one where a non-inner segment crosses an
1818
- * inner segment. An inner segment is an edge with both incident nodes marked
1819
- * with the "dummy" property.
1820
- *
1821
- * This algorithm scans layer by layer, starting with the second, for type-1
1822
- * conflicts between the current layer and the previous layer. For each layer
1823
- * it scans the nodes from left to right until it reaches one that is incident
1824
- * on an inner segment. It then scans predecessors to determine if they have
1825
- * edges that cross that inner segment. At the end a final scan is done for all
1826
- * nodes on the current rank to see if they cross the last visited inner
1827
- * segment.
1828
- *
1829
- * This algorithm (safely) assumes that a dummy node will only be incident on a
1830
- * single node in the layers being scanned.
1831
- */
1832
- function findType1Conflicts(g, layering) {
1833
- var conflicts = {};
1834
-
1835
- function visitLayer(prevLayer, layer) {
1836
- var
1837
- // last visited node in the previous layer that is incident on an inner
1838
- // segment.
1839
- k0 = 0,
1840
- // Tracks the last node in this layer scanned for crossings with a type-1
1841
- // segment.
1842
- scanPos = 0,
1843
- prevLayerLength = prevLayer.length,
1844
- lastNode = _.last(layer);
1845
-
1846
- _.forEach(layer, function(v, i) {
1847
- var w = findOtherInnerSegmentNode(g, v),
1848
- k1 = w ? g.node(w).order : prevLayerLength;
1849
-
1850
- if (w || v === lastNode) {
1851
- _.forEach(layer.slice(scanPos, i +1), function(scanNode) {
1852
- _.forEach(g.predecessors(scanNode), function(u) {
1853
- var uLabel = g.node(u),
1854
- uPos = uLabel.order;
1855
- if ((uPos < k0 || k1 < uPos) &&
1856
- !(uLabel.dummy && g.node(scanNode).dummy)) {
1857
- addConflict(conflicts, u, scanNode);
1858
- }
1859
- });
1860
- });
1861
- scanPos = i + 1;
1862
- k0 = k1;
1863
- }
1864
- });
1865
-
1866
- return layer;
1867
- }
1868
-
1869
- _.reduce(layering, visitLayer);
1870
- return conflicts;
1871
- }
1872
-
1873
- function findType2Conflicts(g, layering) {
1874
- var conflicts = {};
1875
-
1876
- function scan(south, southPos, southEnd, prevNorthBorder, nextNorthBorder) {
1877
- var v;
1878
- _.forEach(_.range(southPos, southEnd), function(i) {
1879
- v = south[i];
1880
- if (g.node(v).dummy) {
1881
- _.forEach(g.predecessors(v), function(u) {
1882
- var uNode = g.node(u);
1883
- if (uNode.dummy &&
1884
- (uNode.order < prevNorthBorder || uNode.order > nextNorthBorder)) {
1885
- addConflict(conflicts, u, v);
1886
- }
1887
- });
1888
- }
1889
- });
1890
- }
1891
-
1892
-
1893
- function visitLayer(north, south) {
1894
- var prevNorthPos = -1,
1895
- nextNorthPos,
1896
- southPos = 0;
1897
-
1898
- _.forEach(south, function(v, southLookahead) {
1899
- if (g.node(v).dummy === "border") {
1900
- var predecessors = g.predecessors(v);
1901
- if (predecessors.length) {
1902
- nextNorthPos = g.node(predecessors[0]).order;
1903
- scan(south, southPos, southLookahead, prevNorthPos, nextNorthPos);
1904
- southPos = southLookahead;
1905
- prevNorthPos = nextNorthPos;
1906
- }
1907
- }
1908
- scan(south, southPos, south.length, nextNorthPos, north.length);
1909
- });
1910
-
1911
- return south;
1912
- }
1913
-
1914
- _.reduce(layering, visitLayer);
1915
- return conflicts;
1916
- }
1917
-
1918
- function findOtherInnerSegmentNode(g, v) {
1919
- if (g.node(v).dummy) {
1920
- return _.find(g.predecessors(v), function(u) {
1921
- return g.node(u).dummy;
1922
- });
1923
- }
1924
- }
1925
-
1926
- function addConflict(conflicts, v, w) {
1927
- if (v > w) {
1928
- var tmp = v;
1929
- v = w;
1930
- w = tmp;
1931
- }
1932
-
1933
- var conflictsV = conflicts[v];
1934
- if (!conflictsV) {
1935
- conflicts[v] = conflictsV = {};
1936
- }
1937
- conflictsV[w] = true;
1938
- }
1939
-
1940
- function hasConflict(conflicts, v, w) {
1941
- if (v > w) {
1942
- var tmp = v;
1943
- v = w;
1944
- w = tmp;
1945
- }
1946
- return _.has(conflicts[v], w);
1947
- }
1948
-
1949
- /*
1950
- * Try to align nodes into vertical "blocks" where possible. This algorithm
1951
- * attempts to align a node with one of its median neighbors. If the edge
1952
- * connecting a neighbor is a type-1 conflict then we ignore that possibility.
1953
- * If a previous node has already formed a block with a node after the node
1954
- * we're trying to form a block with, we also ignore that possibility - our
1955
- * blocks would be split in that scenario.
1956
- */
1957
- function verticalAlignment(g, layering, conflicts, neighborFn) {
1958
- var root = {},
1959
- align = {},
1960
- pos = {};
1961
-
1962
- // We cache the position here based on the layering because the graph and
1963
- // layering may be out of sync. The layering matrix is manipulated to
1964
- // generate different extreme alignments.
1965
- _.forEach(layering, function(layer) {
1966
- _.forEach(layer, function(v, order) {
1967
- root[v] = v;
1968
- align[v] = v;
1969
- pos[v] = order;
1970
- });
1971
- });
1972
-
1973
- _.forEach(layering, function(layer) {
1974
- var prevIdx = -1;
1975
- _.forEach(layer, function(v) {
1976
- var ws = neighborFn(v);
1977
- if (ws.length) {
1978
- ws = _.sortBy(ws, function(w) { return pos[w]; });
1979
- var mp = (ws.length - 1) / 2;
1980
- for (var i = Math.floor(mp), il = Math.ceil(mp); i <= il; ++i) {
1981
- var w = ws[i];
1982
- if (align[v] === v &&
1983
- prevIdx < pos[w] &&
1984
- !hasConflict(conflicts, v, w)) {
1985
- align[w] = v;
1986
- align[v] = root[v] = root[w];
1987
- prevIdx = pos[w];
1988
- }
1989
- }
1990
- }
1991
- });
1992
- });
1993
-
1994
- return { root: root, align: align };
1995
- }
1996
-
1997
- function horizontalCompaction(g, layering, root, align, reverseSep) {
1998
- // This portion of the algorithm differs from BK due to a number of problems.
1999
- // Instead of their algorithm we construct a new block graph and do two
2000
- // sweeps. The first sweep places blocks with the smallest possible
2001
- // coordinates. The second sweep removes unused space by moving blocks to the
2002
- // greatest coordinates without violating separation.
2003
- var xs = {},
2004
- blockG = buildBlockGraph(g, layering, root, reverseSep);
2005
-
2006
- // First pass, assign smallest coordinates via DFS
2007
- var visited = {};
2008
- function pass1(v) {
2009
- if (!_.has(visited, v)) {
2010
- visited[v] = true;
2011
- xs[v] = _.reduce(blockG.inEdges(v), function(max, e) {
2012
- pass1(e.v);
2013
- return Math.max(max, xs[e.v] + blockG.edge(e));
2014
- }, 0);
2015
- }
2016
- }
2017
- _.forEach(blockG.nodes(), pass1);
2018
-
2019
- var borderType = reverseSep ? "borderLeft" : "borderRight";
2020
- function pass2(v) {
2021
- if (visited[v] !== 2) {
2022
- visited[v]++;
2023
- var node = g.node(v);
2024
- var min = _.reduce(blockG.outEdges(v), function(min, e) {
2025
- pass2(e.w);
2026
- return Math.min(min, xs[e.w] - blockG.edge(e));
2027
- }, Number.POSITIVE_INFINITY);
2028
- if (min !== Number.POSITIVE_INFINITY && node.borderType !== borderType) {
2029
- xs[v] = Math.max(xs[v], min);
2030
- }
2031
- }
2032
- }
2033
- _.forEach(blockG.nodes(), pass2);
2034
-
2035
- // Assign x coordinates to all nodes
2036
- _.forEach(align, function(v) {
2037
- xs[v] = xs[root[v]];
2038
- });
2039
-
2040
- return xs;
2041
- }
2042
-
2043
-
2044
- function buildBlockGraph(g, layering, root, reverseSep) {
2045
- var blockGraph = new Graph(),
2046
- graphLabel = g.graph(),
2047
- sepFn = sep(graphLabel.nodesep, graphLabel.edgesep, reverseSep);
2048
-
2049
- _.forEach(layering, function(layer) {
2050
- var u;
2051
- _.forEach(layer, function(v) {
2052
- var vRoot = root[v];
2053
- blockGraph.setNode(vRoot);
2054
- if (u) {
2055
- var uRoot = root[u],
2056
- prevMax = blockGraph.edge(uRoot, vRoot);
2057
- blockGraph.setEdge(uRoot, vRoot, Math.max(sepFn(g, v, u), prevMax || 0));
2058
- }
2059
- u = v;
2060
- });
2061
- });
2062
-
2063
- return blockGraph;
2064
- }
2065
-
2066
- /*
2067
- * Returns the alignment that has the smallest width of the given alignments.
2068
- */
2069
- function findSmallestWidthAlignment(g, xss) {
2070
- return _.minBy(_.values(xss), function (xs) {
2071
- var max = Number.NEGATIVE_INFINITY;
2072
- var min = Number.POSITIVE_INFINITY;
2073
-
2074
- _.forIn(xs, function (x, v) {
2075
- var halfWidth = width(g, v) / 2;
2076
-
2077
- max = Math.max(x + halfWidth, max);
2078
- min = Math.min(x - halfWidth, min);
2079
- });
2080
-
2081
- return max - min;
2082
- });
2083
- }
2084
-
2085
- /*
2086
- * Align the coordinates of each of the layout alignments such that
2087
- * left-biased alignments have their minimum coordinate at the same point as
2088
- * the minimum coordinate of the smallest width alignment and right-biased
2089
- * alignments have their maximum coordinate at the same point as the maximum
2090
- * coordinate of the smallest width alignment.
2091
- */
2092
- function alignCoordinates(xss, alignTo) {
2093
- var alignToVals = _.values(alignTo),
2094
- alignToMin = _.min(alignToVals),
2095
- alignToMax = _.max(alignToVals);
2096
-
2097
- _.forEach(["u", "d"], function(vert) {
2098
- _.forEach(["l", "r"], function(horiz) {
2099
- var alignment = vert + horiz,
2100
- xs = xss[alignment],
2101
- delta;
2102
- if (xs === alignTo) return;
2103
-
2104
- var xsVals = _.values(xs);
2105
- delta = horiz === "l" ? alignToMin - _.min(xsVals) : alignToMax - _.max(xsVals);
2106
-
2107
- if (delta) {
2108
- xss[alignment] = _.mapValues(xs, function(x) { return x + delta; });
2109
- }
2110
- });
2111
- });
2112
- }
2113
-
2114
- function balance(xss, align) {
2115
- return _.mapValues(xss.ul, function(ignore, v) {
2116
- if (align) {
2117
- return xss[align.toLowerCase()][v];
2118
- } else {
2119
- var xs = _.sortBy(_.map(xss, v));
2120
- return (xs[1] + xs[2]) / 2;
2121
- }
2122
- });
2123
- }
2124
-
2125
- function positionX(g) {
2126
- var layering = util.buildLayerMatrix(g),
2127
- conflicts = _.merge(findType1Conflicts(g, layering),
2128
- findType2Conflicts(g, layering));
2129
-
2130
- var xss = {},
2131
- adjustedLayering;
2132
- _.forEach(["u", "d"], function(vert) {
2133
- adjustedLayering = vert === "u" ? layering : _.values(layering).reverse();
2134
- _.forEach(["l", "r"], function(horiz) {
2135
- if (horiz === "r") {
2136
- adjustedLayering = _.map(adjustedLayering, function(inner) {
2137
- return _.values(inner).reverse();
2138
- });
2139
- }
2140
-
2141
- var neighborFn = _.bind(vert === "u" ? g.predecessors : g.successors, g);
2142
- var align = verticalAlignment(g, adjustedLayering, conflicts, neighborFn);
2143
- var xs = horizontalCompaction(g, adjustedLayering,
2144
- align.root, align.align,
2145
- horiz === "r");
2146
- if (horiz === "r") {
2147
- xs = _.mapValues(xs, function(x) { return -x; });
2148
- }
2149
- xss[vert + horiz] = xs;
2150
- });
2151
- });
2152
-
2153
- var smallestWidth = findSmallestWidthAlignment(g, xss);
2154
- alignCoordinates(xss, smallestWidth);
2155
- return balance(xss, g.graph().align);
2156
- }
2157
-
2158
- function sep(nodeSep, edgeSep, reverseSep) {
2159
- return function(g, v, w) {
2160
- var vLabel = g.node(v),
2161
- wLabel = g.node(w),
2162
- sum = 0,
2163
- delta;
2164
-
2165
- sum += vLabel.width / 2;
2166
- if (_.has(vLabel, "labelpos")) {
2167
- switch (vLabel.labelpos.toLowerCase()) {
2168
- case "l": delta = -vLabel.width / 2; break;
2169
- case "r": delta = vLabel.width / 2; break;
2170
- }
2171
- }
2172
- if (delta) {
2173
- sum += reverseSep ? delta : -delta;
2174
- }
2175
- delta = 0;
2176
-
2177
- sum += (vLabel.dummy ? edgeSep : nodeSep) / 2;
2178
- sum += (wLabel.dummy ? edgeSep : nodeSep) / 2;
2179
-
2180
- sum += wLabel.width / 2;
2181
- if (_.has(wLabel, "labelpos")) {
2182
- switch (wLabel.labelpos.toLowerCase()) {
2183
- case "l": delta = wLabel.width / 2; break;
2184
- case "r": delta = -wLabel.width / 2; break;
2185
- }
2186
- }
2187
- if (delta) {
2188
- sum += reverseSep ? delta : -delta;
2189
- }
2190
- delta = 0;
2191
-
2192
- return sum;
2193
- };
2194
- }
2195
-
2196
- function width(g, v) {
2197
- return g.node(v).width;
2198
- }
2199
-
2200
- },{"../graphlib":7,"../lodash":10,"../util":29}],24:[function(require,module,exports){
2201
- "use strict";
2202
-
2203
- var _ = require("../lodash"),
2204
- util = require("../util"),
2205
- positionX = require("./bk").positionX;
2206
-
2207
- module.exports = position;
2208
-
2209
- function position(g) {
2210
- g = util.asNonCompoundGraph(g);
2211
-
2212
- positionY(g);
2213
- _.forEach(positionX(g), function(x, v) {
2214
- g.node(v).x = x;
2215
- });
2216
- }
2217
-
2218
- function positionY(g) {
2219
- var layering = util.buildLayerMatrix(g),
2220
- rankSep = g.graph().ranksep,
2221
- prevY = 0;
2222
- _.forEach(layering, function(layer) {
2223
- var maxHeight = _.max(_.map(layer, function(v) { return g.node(v).height; }));
2224
- _.forEach(layer, function(v) {
2225
- g.node(v).y = prevY + maxHeight / 2;
2226
- });
2227
- prevY += maxHeight + rankSep;
2228
- });
2229
- }
2230
-
2231
-
2232
- },{"../lodash":10,"../util":29,"./bk":23}],25:[function(require,module,exports){
2233
- "use strict";
2234
-
2235
- var _ = require("../lodash"),
2236
- Graph = require("../graphlib").Graph,
2237
- slack = require("./util").slack;
2238
-
2239
- module.exports = feasibleTree;
2240
-
2241
- /*
2242
- * Constructs a spanning tree with tight edges and adjusted the input node's
2243
- * ranks to achieve this. A tight edge is one that is has a length that matches
2244
- * its "minlen" attribute.
2245
- *
2246
- * The basic structure for this function is derived from Gansner, et al., "A
2247
- * Technique for Drawing Directed Graphs."
2248
- *
2249
- * Pre-conditions:
2250
- *
2251
- * 1. Graph must be a DAG.
2252
- * 2. Graph must be connected.
2253
- * 3. Graph must have at least one node.
2254
- * 5. Graph nodes must have been previously assigned a "rank" property that
2255
- * respects the "minlen" property of incident edges.
2256
- * 6. Graph edges must have a "minlen" property.
2257
- *
2258
- * Post-conditions:
2259
- *
2260
- * - Graph nodes will have their rank adjusted to ensure that all edges are
2261
- * tight.
2262
- *
2263
- * Returns a tree (undirected graph) that is constructed using only "tight"
2264
- * edges.
2265
- */
2266
- function feasibleTree(g) {
2267
- var t = new Graph({ directed: false });
2268
-
2269
- // Choose arbitrary node from which to start our tree
2270
- var start = g.nodes()[0],
2271
- size = g.nodeCount();
2272
- t.setNode(start, {});
2273
-
2274
- var edge, delta;
2275
- while (tightTree(t, g) < size) {
2276
- edge = findMinSlackEdge(t, g);
2277
- delta = t.hasNode(edge.v) ? slack(g, edge) : -slack(g, edge);
2278
- shiftRanks(t, g, delta);
2279
- }
2280
-
2281
- return t;
2282
- }
2283
-
2284
- /*
2285
- * Finds a maximal tree of tight edges and returns the number of nodes in the
2286
- * tree.
2287
- */
2288
- function tightTree(t, g) {
2289
- function dfs(v) {
2290
- _.forEach(g.nodeEdges(v), function(e) {
2291
- var edgeV = e.v,
2292
- w = (v === edgeV) ? e.w : edgeV;
2293
- if (!t.hasNode(w) && !slack(g, e)) {
2294
- t.setNode(w, {});
2295
- t.setEdge(v, w, {});
2296
- dfs(w);
2297
- }
2298
- });
2299
- }
2300
-
2301
- _.forEach(t.nodes(), dfs);
2302
- return t.nodeCount();
2303
- }
2304
-
2305
- /*
2306
- * Finds the edge with the smallest slack that is incident on tree and returns
2307
- * it.
2308
- */
2309
- function findMinSlackEdge(t, g) {
2310
- return _.minBy(g.edges(), function(e) {
2311
- if (t.hasNode(e.v) !== t.hasNode(e.w)) {
2312
- return slack(g, e);
2313
- }
2314
- });
2315
- }
2316
-
2317
- function shiftRanks(t, g, delta) {
2318
- _.forEach(t.nodes(), function(v) {
2319
- g.node(v).rank += delta;
2320
- });
2321
- }
2322
-
2323
- },{"../graphlib":7,"../lodash":10,"./util":28}],26:[function(require,module,exports){
2324
- "use strict";
2325
-
2326
- var rankUtil = require("./util"),
2327
- longestPath = rankUtil.longestPath,
2328
- feasibleTree = require("./feasible-tree"),
2329
- networkSimplex = require("./network-simplex");
2330
-
2331
- module.exports = rank;
2332
-
2333
- /*
2334
- * Assigns a rank to each node in the input graph that respects the "minlen"
2335
- * constraint specified on edges between nodes.
2336
- *
2337
- * This basic structure is derived from Gansner, et al., "A Technique for
2338
- * Drawing Directed Graphs."
2339
- *
2340
- * Pre-conditions:
2341
- *
2342
- * 1. Graph must be a connected DAG
2343
- * 2. Graph nodes must be objects
2344
- * 3. Graph edges must have "weight" and "minlen" attributes
2345
- *
2346
- * Post-conditions:
2347
- *
2348
- * 1. Graph nodes will have a "rank" attribute based on the results of the
2349
- * algorithm. Ranks can start at any index (including negative), we'll
2350
- * fix them up later.
2351
- */
2352
- function rank(g) {
2353
- switch(g.graph().ranker) {
2354
- case "network-simplex": networkSimplexRanker(g); break;
2355
- case "tight-tree": tightTreeRanker(g); break;
2356
- case "longest-path": longestPathRanker(g); break;
2357
- default: networkSimplexRanker(g);
2358
- }
2359
- }
2360
-
2361
- // A fast and simple ranker, but results are far from optimal.
2362
- var longestPathRanker = longestPath;
2363
-
2364
- function tightTreeRanker(g) {
2365
- longestPath(g);
2366
- feasibleTree(g);
2367
- }
2368
-
2369
- function networkSimplexRanker(g) {
2370
- networkSimplex(g);
2371
- }
2372
-
2373
- },{"./feasible-tree":25,"./network-simplex":27,"./util":28}],27:[function(require,module,exports){
2374
- "use strict";
2375
-
2376
- var _ = require("../lodash"),
2377
- feasibleTree = require("./feasible-tree"),
2378
- slack = require("./util").slack,
2379
- initRank = require("./util").longestPath,
2380
- preorder = require("../graphlib").alg.preorder,
2381
- postorder = require("../graphlib").alg.postorder,
2382
- simplify = require("../util").simplify;
2383
-
2384
- module.exports = networkSimplex;
2385
-
2386
- // Expose some internals for testing purposes
2387
- networkSimplex.initLowLimValues = initLowLimValues;
2388
- networkSimplex.initCutValues = initCutValues;
2389
- networkSimplex.calcCutValue = calcCutValue;
2390
- networkSimplex.leaveEdge = leaveEdge;
2391
- networkSimplex.enterEdge = enterEdge;
2392
- networkSimplex.exchangeEdges = exchangeEdges;
2393
-
2394
- /*
2395
- * The network simplex algorithm assigns ranks to each node in the input graph
2396
- * and iteratively improves the ranking to reduce the length of edges.
2397
- *
2398
- * Preconditions:
2399
- *
2400
- * 1. The input graph must be a DAG.
2401
- * 2. All nodes in the graph must have an object value.
2402
- * 3. All edges in the graph must have "minlen" and "weight" attributes.
2403
- *
2404
- * Postconditions:
2405
- *
2406
- * 1. All nodes in the graph will have an assigned "rank" attribute that has
2407
- * been optimized by the network simplex algorithm. Ranks start at 0.
2408
- *
2409
- *
2410
- * A rough sketch of the algorithm is as follows:
2411
- *
2412
- * 1. Assign initial ranks to each node. We use the longest path algorithm,
2413
- * which assigns ranks to the lowest position possible. In general this
2414
- * leads to very wide bottom ranks and unnecessarily long edges.
2415
- * 2. Construct a feasible tight tree. A tight tree is one such that all
2416
- * edges in the tree have no slack (difference between length of edge
2417
- * and minlen for the edge). This by itself greatly improves the assigned
2418
- * rankings by shorting edges.
2419
- * 3. Iteratively find edges that have negative cut values. Generally a
2420
- * negative cut value indicates that the edge could be removed and a new
2421
- * tree edge could be added to produce a more compact graph.
2422
- *
2423
- * Much of the algorithms here are derived from Gansner, et al., "A Technique
2424
- * for Drawing Directed Graphs." The structure of the file roughly follows the
2425
- * structure of the overall algorithm.
2426
- */
2427
- function networkSimplex(g) {
2428
- g = simplify(g);
2429
- initRank(g);
2430
- var t = feasibleTree(g);
2431
- initLowLimValues(t);
2432
- initCutValues(t, g);
2433
-
2434
- var e, f;
2435
- while ((e = leaveEdge(t))) {
2436
- f = enterEdge(t, g, e);
2437
- exchangeEdges(t, g, e, f);
2438
- }
2439
- }
2440
-
2441
- /*
2442
- * Initializes cut values for all edges in the tree.
2443
- */
2444
- function initCutValues(t, g) {
2445
- var vs = postorder(t, t.nodes());
2446
- vs = vs.slice(0, vs.length - 1);
2447
- _.forEach(vs, function(v) {
2448
- assignCutValue(t, g, v);
2449
- });
2450
- }
2451
-
2452
- function assignCutValue(t, g, child) {
2453
- var childLab = t.node(child),
2454
- parent = childLab.parent;
2455
- t.edge(child, parent).cutvalue = calcCutValue(t, g, child);
2456
- }
2457
-
2458
- /*
2459
- * Given the tight tree, its graph, and a child in the graph calculate and
2460
- * return the cut value for the edge between the child and its parent.
2461
- */
2462
- function calcCutValue(t, g, child) {
2463
- var childLab = t.node(child),
2464
- parent = childLab.parent,
2465
- // True if the child is on the tail end of the edge in the directed graph
2466
- childIsTail = true,
2467
- // The graph's view of the tree edge we're inspecting
2468
- graphEdge = g.edge(child, parent),
2469
- // The accumulated cut value for the edge between this node and its parent
2470
- cutValue = 0;
2471
-
2472
- if (!graphEdge) {
2473
- childIsTail = false;
2474
- graphEdge = g.edge(parent, child);
2475
- }
2476
-
2477
- cutValue = graphEdge.weight;
2478
-
2479
- _.forEach(g.nodeEdges(child), function(e) {
2480
- var isOutEdge = e.v === child,
2481
- other = isOutEdge ? e.w : e.v;
2482
-
2483
- if (other !== parent) {
2484
- var pointsToHead = isOutEdge === childIsTail,
2485
- otherWeight = g.edge(e).weight;
2486
-
2487
- cutValue += pointsToHead ? otherWeight : -otherWeight;
2488
- if (isTreeEdge(t, child, other)) {
2489
- var otherCutValue = t.edge(child, other).cutvalue;
2490
- cutValue += pointsToHead ? -otherCutValue : otherCutValue;
2491
- }
2492
- }
2493
- });
2494
-
2495
- return cutValue;
2496
- }
2497
-
2498
- function initLowLimValues(tree, root) {
2499
- if (arguments.length < 2) {
2500
- root = tree.nodes()[0];
2501
- }
2502
- dfsAssignLowLim(tree, {}, 1, root);
2503
- }
2504
-
2505
- function dfsAssignLowLim(tree, visited, nextLim, v, parent) {
2506
- var low = nextLim,
2507
- label = tree.node(v);
2508
-
2509
- visited[v] = true;
2510
- _.forEach(tree.neighbors(v), function(w) {
2511
- if (!_.has(visited, w)) {
2512
- nextLim = dfsAssignLowLim(tree, visited, nextLim, w, v);
2513
- }
2514
- });
2515
-
2516
- label.low = low;
2517
- label.lim = nextLim++;
2518
- if (parent) {
2519
- label.parent = parent;
2520
- } else {
2521
- // TODO should be able to remove this when we incrementally update low lim
2522
- delete label.parent;
2523
- }
2524
-
2525
- return nextLim;
2526
- }
2527
-
2528
- function leaveEdge(tree) {
2529
- return _.find(tree.edges(), function(e) {
2530
- return tree.edge(e).cutvalue < 0;
2531
- });
2532
- }
2533
-
2534
- function enterEdge(t, g, edge) {
2535
- var v = edge.v,
2536
- w = edge.w;
2537
-
2538
- // For the rest of this function we assume that v is the tail and w is the
2539
- // head, so if we don't have this edge in the graph we should flip it to
2540
- // match the correct orientation.
2541
- if (!g.hasEdge(v, w)) {
2542
- v = edge.w;
2543
- w = edge.v;
2544
- }
2545
-
2546
- var vLabel = t.node(v),
2547
- wLabel = t.node(w),
2548
- tailLabel = vLabel,
2549
- flip = false;
2550
-
2551
- // If the root is in the tail of the edge then we need to flip the logic that
2552
- // checks for the head and tail nodes in the candidates function below.
2553
- if (vLabel.lim > wLabel.lim) {
2554
- tailLabel = wLabel;
2555
- flip = true;
2556
- }
2557
-
2558
- var candidates = _.filter(g.edges(), function(edge) {
2559
- return flip === isDescendant(t, t.node(edge.v), tailLabel) &&
2560
- flip !== isDescendant(t, t.node(edge.w), tailLabel);
2561
- });
2562
-
2563
- return _.minBy(candidates, function(edge) { return slack(g, edge); });
2564
- }
2565
-
2566
- function exchangeEdges(t, g, e, f) {
2567
- var v = e.v,
2568
- w = e.w;
2569
- t.removeEdge(v, w);
2570
- t.setEdge(f.v, f.w, {});
2571
- initLowLimValues(t);
2572
- initCutValues(t, g);
2573
- updateRanks(t, g);
2574
- }
2575
-
2576
- function updateRanks(t, g) {
2577
- var root = _.find(t.nodes(), function(v) { return !g.node(v).parent; }),
2578
- vs = preorder(t, root);
2579
- vs = vs.slice(1);
2580
- _.forEach(vs, function(v) {
2581
- var parent = t.node(v).parent,
2582
- edge = g.edge(v, parent),
2583
- flipped = false;
2584
-
2585
- if (!edge) {
2586
- edge = g.edge(parent, v);
2587
- flipped = true;
2588
- }
2589
-
2590
- g.node(v).rank = g.node(parent).rank + (flipped ? edge.minlen : -edge.minlen);
2591
- });
2592
- }
2593
-
2594
- /*
2595
- * Returns true if the edge is in the tree.
2596
- */
2597
- function isTreeEdge(tree, u, v) {
2598
- return tree.hasEdge(u, v);
2599
- }
2600
-
2601
- /*
2602
- * Returns true if the specified node is descendant of the root node per the
2603
- * assigned low and lim attributes in the tree.
2604
- */
2605
- function isDescendant(tree, vLabel, rootLabel) {
2606
- return rootLabel.low <= vLabel.lim && vLabel.lim <= rootLabel.lim;
2607
- }
2608
-
2609
- },{"../graphlib":7,"../lodash":10,"../util":29,"./feasible-tree":25,"./util":28}],28:[function(require,module,exports){
2610
- "use strict";
2611
-
2612
- var _ = require("../lodash");
2613
-
2614
- module.exports = {
2615
- longestPath: longestPath,
2616
- slack: slack
2617
- };
2618
-
2619
- /*
2620
- * Initializes ranks for the input graph using the longest path algorithm. This
2621
- * algorithm scales well and is fast in practice, it yields rather poor
2622
- * solutions. Nodes are pushed to the lowest layer possible, leaving the bottom
2623
- * ranks wide and leaving edges longer than necessary. However, due to its
2624
- * speed, this algorithm is good for getting an initial ranking that can be fed
2625
- * into other algorithms.
2626
- *
2627
- * This algorithm does not normalize layers because it will be used by other
2628
- * algorithms in most cases. If using this algorithm directly, be sure to
2629
- * run normalize at the end.
2630
- *
2631
- * Pre-conditions:
2632
- *
2633
- * 1. Input graph is a DAG.
2634
- * 2. Input graph node labels can be assigned properties.
2635
- *
2636
- * Post-conditions:
2637
- *
2638
- * 1. Each node will be assign an (unnormalized) "rank" property.
2639
- */
2640
- function longestPath(g) {
2641
- var visited = {};
2642
-
2643
- function dfs(v) {
2644
- var label = g.node(v);
2645
- if (_.has(visited, v)) {
2646
- return label.rank;
2647
- }
2648
- visited[v] = true;
2649
-
2650
- var rank = _.minBy(_.map(g.outEdges(v), function(e) {
2651
- return dfs(e.w) - g.edge(e).minlen;
2652
- }));
2653
-
2654
- if (rank === Number.POSITIVE_INFINITY || // return value of _.map([]) for Lodash 3
2655
- rank === undefined || // return value of _.map([]) for Lodash 4
2656
- rank === null) { // return value of _.map([null])
2657
- rank = 0;
2658
- }
2659
-
2660
- return (label.rank = rank);
2661
- }
2662
-
2663
- _.forEach(g.sources(), dfs);
2664
- }
2665
-
2666
- /*
2667
- * Returns the amount of slack for the given edge. The slack is defined as the
2668
- * difference between the length of the edge and its minimum length.
2669
- */
2670
- function slack(g, e) {
2671
- return g.node(e.w).rank - g.node(e.v).rank - g.edge(e).minlen;
2672
- }
2673
-
2674
- },{"../lodash":10}],29:[function(require,module,exports){
2675
- "use strict";
2676
-
2677
- var _ = require("./lodash"),
2678
- Graph = require("./graphlib").Graph;
2679
-
2680
- module.exports = {
2681
- addDummyNode: addDummyNode,
2682
- simplify: simplify,
2683
- asNonCompoundGraph: asNonCompoundGraph,
2684
- successorWeights: successorWeights,
2685
- predecessorWeights: predecessorWeights,
2686
- intersectRect: intersectRect,
2687
- buildLayerMatrix: buildLayerMatrix,
2688
- normalizeRanks: normalizeRanks,
2689
- removeEmptyRanks: removeEmptyRanks,
2690
- addBorderNode: addBorderNode,
2691
- maxRank: maxRank,
2692
- partition: partition,
2693
- time: time,
2694
- notime: notime
2695
- };
2696
-
2697
- /*
2698
- * Adds a dummy node to the graph and return v.
2699
- */
2700
- function addDummyNode(g, type, attrs, name) {
2701
- var v;
2702
- do {
2703
- v = _.uniqueId(name);
2704
- } while (g.hasNode(v));
2705
-
2706
- attrs.dummy = type;
2707
- g.setNode(v, attrs);
2708
- return v;
2709
- }
2710
-
2711
- /*
2712
- * Returns a new graph with only simple edges. Handles aggregation of data
2713
- * associated with multi-edges.
2714
- */
2715
- function simplify(g) {
2716
- var simplified = new Graph().setGraph(g.graph());
2717
- _.forEach(g.nodes(), function(v) { simplified.setNode(v, g.node(v)); });
2718
- _.forEach(g.edges(), function(e) {
2719
- var simpleLabel = simplified.edge(e.v, e.w) || { weight: 0, minlen: 1 },
2720
- label = g.edge(e);
2721
- simplified.setEdge(e.v, e.w, {
2722
- weight: simpleLabel.weight + label.weight,
2723
- minlen: Math.max(simpleLabel.minlen, label.minlen)
2724
- });
2725
- });
2726
- return simplified;
2727
- }
2728
-
2729
- function asNonCompoundGraph(g) {
2730
- var simplified = new Graph({ multigraph: g.isMultigraph() }).setGraph(g.graph());
2731
- _.forEach(g.nodes(), function(v) {
2732
- if (!g.children(v).length) {
2733
- simplified.setNode(v, g.node(v));
2734
- }
2735
- });
2736
- _.forEach(g.edges(), function(e) {
2737
- simplified.setEdge(e, g.edge(e));
2738
- });
2739
- return simplified;
2740
- }
2741
-
2742
- function successorWeights(g) {
2743
- var weightMap = _.map(g.nodes(), function(v) {
2744
- var sucs = {};
2745
- _.forEach(g.outEdges(v), function(e) {
2746
- sucs[e.w] = (sucs[e.w] || 0) + g.edge(e).weight;
2747
- });
2748
- return sucs;
2749
- });
2750
- return _.zipObject(g.nodes(), weightMap);
2751
- }
2752
-
2753
- function predecessorWeights(g) {
2754
- var weightMap = _.map(g.nodes(), function(v) {
2755
- var preds = {};
2756
- _.forEach(g.inEdges(v), function(e) {
2757
- preds[e.v] = (preds[e.v] || 0) + g.edge(e).weight;
2758
- });
2759
- return preds;
2760
- });
2761
- return _.zipObject(g.nodes(), weightMap);
2762
- }
2763
-
2764
- /*
2765
- * Finds where a line starting at point ({x, y}) would intersect a rectangle
2766
- * ({x, y, width, height}) if it were pointing at the rectangle's center.
2767
- */
2768
- function intersectRect(rect, point) {
2769
- var x = rect.x;
2770
- var y = rect.y;
2771
-
2772
- // Rectangle intersection algorithm from:
2773
- // http://math.stackexchange.com/questions/108113/find-edge-between-two-boxes
2774
- var dx = point.x - x;
2775
- var dy = point.y - y;
2776
- var w = rect.width / 2;
2777
- var h = rect.height / 2;
2778
-
2779
- if (!dx && !dy) {
2780
- throw new Error("Not possible to find intersection inside of the rectangle");
2781
- }
2782
-
2783
- var sx, sy;
2784
- if (Math.abs(dy) * w > Math.abs(dx) * h) {
2785
- // Intersection is top or bottom of rect.
2786
- if (dy < 0) {
2787
- h = -h;
2788
- }
2789
- sx = h * dx / dy;
2790
- sy = h;
2791
- } else {
2792
- // Intersection is left or right of rect.
2793
- if (dx < 0) {
2794
- w = -w;
2795
- }
2796
- sx = w;
2797
- sy = w * dy / dx;
2798
- }
2799
-
2800
- return { x: x + sx, y: y + sy };
2801
- }
2802
-
2803
- /*
2804
- * Given a DAG with each node assigned "rank" and "order" properties, this
2805
- * function will produce a matrix with the ids of each node.
2806
- */
2807
- function buildLayerMatrix(g) {
2808
- var layering = _.map(_.range(maxRank(g) + 1), function() { return []; });
2809
- _.forEach(g.nodes(), function(v) {
2810
- var node = g.node(v),
2811
- rank = node.rank;
2812
- if (!_.isUndefined(rank)) {
2813
- layering[rank][node.order] = v;
2814
- }
2815
- });
2816
- return layering;
2817
- }
2818
-
2819
- /*
2820
- * Adjusts the ranks for all nodes in the graph such that all nodes v have
2821
- * rank(v) >= 0 and at least one node w has rank(w) = 0.
2822
- */
2823
- function normalizeRanks(g) {
2824
- var min = _.minBy(_.map(g.nodes(), function(v) { return g.node(v).rank; }));
2825
- _.forEach(g.nodes(), function(v) {
2826
- var node = g.node(v);
2827
- if (_.has(node, "rank")) {
2828
- node.rank -= min;
2829
- }
2830
- });
2831
- }
2832
-
2833
- function removeEmptyRanks(g) {
2834
- // Ranks may not start at 0, so we need to offset them
2835
- var offset = _.minBy(_.map(g.nodes(), function(v) { return g.node(v).rank; }));
2836
-
2837
- var layers = [];
2838
- _.forEach(g.nodes(), function(v) {
2839
- var rank = g.node(v).rank - offset;
2840
- if (!layers[rank]) {
2841
- layers[rank] = [];
2842
- }
2843
- layers[rank].push(v);
2844
- });
2845
-
2846
- var delta = 0,
2847
- nodeRankFactor = g.graph().nodeRankFactor;
2848
- _.forEach(layers, function(vs, i) {
2849
- if (_.isUndefined(vs) && i % nodeRankFactor !== 0) {
2850
- --delta;
2851
- } else if (delta) {
2852
- _.forEach(vs, function(v) { g.node(v).rank += delta; });
2853
- }
2854
- });
2855
- }
2856
-
2857
- function addBorderNode(g, prefix, rank, order) {
2858
- var node = {
2859
- width: 0,
2860
- height: 0
2861
- };
2862
- if (arguments.length >= 4) {
2863
- node.rank = rank;
2864
- node.order = order;
2865
- }
2866
- return addDummyNode(g, "border", node, prefix);
2867
- }
2868
-
2869
- function maxRank(g) {
2870
- return _.max(_.map(g.nodes(), function(v) {
2871
- var rank = g.node(v).rank;
2872
- if (!_.isUndefined(rank)) {
2873
- return rank;
2874
- }
2875
- }));
2876
- }
2877
-
2878
- /*
2879
- * Partition a collection into two groups: `lhs` and `rhs`. If the supplied
2880
- * function returns true for an entry it goes into `lhs`. Otherwise it goes
2881
- * into `rhs.
2882
- */
2883
- function partition(collection, fn) {
2884
- var result = { lhs: [], rhs: [] };
2885
- _.forEach(collection, function(value) {
2886
- if (fn(value)) {
2887
- result.lhs.push(value);
2888
- } else {
2889
- result.rhs.push(value);
2890
- }
2891
- });
2892
- return result;
2893
- }
2894
-
2895
- /*
2896
- * Returns a new function that wraps `fn` with a timer. The wrapper logs the
2897
- * time it takes to execute the function.
2898
- */
2899
- function time(name, fn) {
2900
- var start = _.now();
2901
- try {
2902
- return fn();
2903
- } finally {
2904
- console.log(name + " time: " + (_.now() - start) + "ms");
2905
- }
2906
- }
2907
-
2908
- function notime(name, fn) {
2909
- return fn();
2910
- }
2911
-
2912
- },{"./graphlib":7,"./lodash":10}],30:[function(require,module,exports){
2913
- module.exports = "0.8.0";
2914
-
2915
- },{}]},{},[1])(1)
2916
- });