Debian Codegraph @

lib/order/resolve-conflicts.js

raw symbols SCIP
1 "use strict";
2
3 var _ = require("../lodash");
4
5 module.exports = resolveConflicts;
6
7 /*
8 * Given a list of entries of the form {v, barycenter, weight} and a
9 * constraint graph this function will resolve any conflicts between the
10 * constraint graph and the barycenters for the entries. If the barycenters for
11 * an entry would violate a constraint in the constraint graph then we coalesce
12 * the nodes in the conflict into a new node that respects the contraint and
13 * aggregates barycenter and weight information.
14 *
15 * This implementation is based on the description in Forster, "A Fast and
16 * Simple Hueristic for Constrained Two-Level Crossing Reduction," thought it
17 * differs in some specific details.
18 *
19 * Pre-conditions:
20 *
21 * 1. Each entry has the form {v, barycenter, weight}, or if the node has
22 * no barycenter, then {v}.
23 *
24 * Returns:
25 *
26 * A new list of entries of the form {vs, i, barycenter, weight}. The list
27 * `vs` may either be a singleton or it may be an aggregation of nodes
28 * ordered such that they do not violate constraints from the constraint
29 * graph. The property `i` is the lowest original index of any of the
30 * elements in `vs`.
31 */
32 function resolveConflicts(entries, cg) {
33 var mappedEntries = {};
34 _.forEach(entries, function(entry, i) {
35 var tmp = mappedEntries[entry.v] = {
36 indegree: 0,
37 "in": [],
38 out: [],
39 vs: [entry.v],
40 i: i
41 };
42 if (!_.isUndefined(entry.barycenter)) {
43 tmp.barycenter = entry.barycenter;
44 tmp.weight = entry.weight;
45 }
46 });
47
48 _.forEach(cg.edges(), function(e) {
49 var entryV = mappedEntries[e.v];
50 var entryW = mappedEntries[e.w];
51 if (!_.isUndefined(entryV) && !_.isUndefined(entryW)) {
52 entryW.indegree++;
53 entryV.out.push(mappedEntries[e.w]);
54 }
55 });
56
57 var sourceSet = _.filter(mappedEntries, function(entry) {
58 return !entry.indegree;
59 });
60
61 return doResolveConflicts(sourceSet);
62 }
63
64 function doResolveConflicts(sourceSet) {
65 var entries = [];
66
67 function handleIn(vEntry) {
68 return function(uEntry) {
69 if (uEntry.merged) {
70 return;
71 }
72 if (_.isUndefined(uEntry.barycenter) ||
73 _.isUndefined(vEntry.barycenter) ||
74 uEntry.barycenter >= vEntry.barycenter) {
75 mergeEntries(vEntry, uEntry);
76 }
77 };
78 }
79
80 function handleOut(vEntry) {
81 return function(wEntry) {
82 wEntry["in"].push(vEntry);
83 if (--wEntry.indegree === 0) {
84 sourceSet.push(wEntry);
85 }
86 };
87 }
88
89 while (sourceSet.length) {
90 var entry = sourceSet.pop();
91 entries.push(entry);
92 _.forEach(entry["in"].reverse(), handleIn(entry));
93 _.forEach(entry.out, handleOut(entry));
94 }
95
96 return _.map(_.filter(entries, function(entry) { return !entry.merged; }),
97 function(entry) {
98 return _.pick(entry, ["vs", "i", "barycenter", "weight"]);
99 });
100
101 }
102
103 function mergeEntries(target, source) {
104 var sum = 0;
105 var weight = 0;
106
107 if (target.weight) {
108 sum += target.barycenter * target.weight;
109 weight += target.weight;
110 }
111
112 if (source.weight) {
113 sum += source.barycenter * source.weight;
114 weight += source.weight;
115 }
116
117 target.vs = source.vs.concat(target.vs);
118 target.barycenter = sum / weight;
119 target.weight = weight;
120 target.i = Math.min(source.i, target.i);
121 source.merged = true;
122 }