Debian Codegraph @

lib/parent-dummy-chains.js

raw symbols SCIP
1 var _ = require("./lodash");
2
3 module.exports = parentDummyChains;
4
5 function parentDummyChains(g) {
6 var postorderNums = postorder(g);
7
8 _.forEach(g.graph().dummyChains, function(v) {
9 var node = g.node(v);
10 var edgeObj = node.edgeObj;
11 var pathData = findPath(g, postorderNums, edgeObj.v, edgeObj.w);
12 var path = pathData.path;
13 var lca = pathData.lca;
14 var pathIdx = 0;
15 var pathV = path[pathIdx];
16 var ascending = true;
17
18 while (v !== edgeObj.w) {
19 node = g.node(v);
20
21 if (ascending) {
22 while ((pathV = path[pathIdx]) !== lca &&
23 g.node(pathV).maxRank < node.rank) {
24 pathIdx++;
25 }
26
27 if (pathV === lca) {
28 ascending = false;
29 }
30 }
31
32 if (!ascending) {
33 while (pathIdx < path.length - 1 &&
34 g.node(pathV = path[pathIdx + 1]).minRank <= node.rank) {
35 pathIdx++;
36 }
37 pathV = path[pathIdx];
38 }
39
40 g.setParent(v, pathV);
41 v = g.successors(v)[0];
42 }
43 });
44 }
45
46 // Find a path from v to w through the lowest common ancestor (LCA). Return the
47 // full path and the LCA.
48 function findPath(g, postorderNums, v, w) {
49 var vPath = [];
50 var wPath = [];
51 var low = Math.min(postorderNums[v].low, postorderNums[w].low);
52 var lim = Math.max(postorderNums[v].lim, postorderNums[w].lim);
53 var parent;
54 var lca;
55
56 // Traverse up from v to find the LCA
57 parent = v;
58 do {
59 parent = g.parent(parent);
60 vPath.push(parent);
61 } while (parent &&
62 (postorderNums[parent].low > low || lim > postorderNums[parent].lim));
63 lca = parent;
64
65 // Traverse from w to LCA
66 parent = w;
67 while ((parent = g.parent(parent)) !== lca) {
68 wPath.push(parent);
69 }
70
71 return { path: vPath.concat(wPath.reverse()), lca: lca };
72 }
73
74 function postorder(g) {
75 var result = {};
76 var lim = 0;
77
78 function dfs(v) {
79 var low = lim;
80 _.forEach(g.children(v), dfs);
81 result[v] = { low: low, lim: lim++ };
82 }
83 _.forEach(g.children(), dfs);
84
85 return result;
86 }