-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
179 lines (179 loc) · 5.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CycleError = exports.Vertex = void 0;
class Vertex {
constructor(name, successors) {
this.index = -1;
this.lowLink = -1;
this.onStack = false;
this.visited = false;
this.name = name;
this.successors = successors || [];
this.reset();
}
reset() {
this.index = -1;
this.lowLink = -1;
this.onStack = false;
this.visited = false;
}
}
exports.Vertex = Vertex;
class CycleError extends Error {
constructor(message, cycles) {
super(message);
this.cycles = cycles;
}
}
exports.CycleError = CycleError;
class Graph {
constructor() {
this.vertices = {};
}
add(key, descendants) {
descendants = Array.isArray(descendants) ? descendants : [descendants];
const successors = descendants.map((key) => {
if (!this.vertices[key]) {
this.vertices[key] = new Vertex(key, []);
}
return this.vertices[key];
});
if (!this.vertices[key]) {
this.vertices[key] = new Vertex(key);
}
this.vertices[key].successors = successors.concat([]).reverse();
return this;
}
reset() {
Object.keys(this.vertices).forEach((key) => {
this.vertices[key].reset();
});
}
addAndVerify(key, descendants) {
this.add(key, descendants);
const cycles = this.getCycles();
if (cycles.length) {
let message = `Detected ${cycles.length} cycle${cycles.length === 1 ? '' : 's'}:`;
message += '\n' + cycles.map((scc) => {
const names = scc.map(v => v.name);
return ` ${names.join(' -> ')} -> ${names[0]}`;
}).join('\n');
throw new CycleError(message, cycles);
}
return this;
}
dfs(key, visitor) {
this.reset();
const stack = [this.vertices[key]];
let v;
while (v = stack.pop()) {
if (v.visited) {
continue;
}
//pre-order traversal
visitor(v);
v.visited = true;
v.successors.forEach(w => stack.push(w));
}
}
getDescendants(key) {
const descendants = [];
let ignore = true;
this.dfs(key, (v) => {
if (ignore) {
//ignore the first node
ignore = false;
return;
}
descendants.push(v.name);
});
return descendants;
}
hasCycle() {
return this.getCycles().length > 0;
}
getStronglyConnectedComponents() {
const V = Object.keys(this.vertices).map((key) => {
this.vertices[key].reset();
return this.vertices[key];
});
let index = 0;
const stack = [];
const components = [];
const stronglyConnect = (v) => {
v.index = index;
v.lowLink = index;
index++;
stack.push(v);
v.onStack = true;
v.successors.forEach((w) => {
if (w.index < 0) {
stronglyConnect(w);
v.lowLink = Math.min(v.lowLink, w.lowLink);
}
else if (w.onStack) {
v.lowLink = Math.min(v.lowLink, w.index);
}
});
if (v.lowLink === v.index) {
const scc = [];
let w;
do {
w = stack.pop();
if (!w) {
break;
}
w.onStack = false;
scc.push(w);
} while (w !== v);
components.push(scc);
}
};
V.forEach(function (v) {
if (v.index < 0) {
stronglyConnect(v);
}
});
return components;
}
getCycles() {
return this.getStronglyConnectedComponents().filter((scc) => {
if (scc.length > 1) {
return true;
}
const startNode = scc[0];
return startNode && startNode.successors.some(node => node === startNode);
});
}
clone() {
const graph = new Graph();
Object.keys(this.vertices).forEach((key) => {
const v = this.vertices[key];
graph.add(v.name, v.successors.map((w) => {
return w.name;
}));
});
return graph;
}
toDot() {
const V = this.vertices;
const lines = ['digraph {'];
this.getCycles().forEach((scc, i) => {
lines.push(' subgraph cluster' + i + ' {');
lines.push(' color=red;');
lines.push(' ' + scc.map(v => v.name).join('; ') + ';');
lines.push(' }');
});
Object.keys(V).forEach((key) => {
const v = V[key];
if (v.successors.length) {
v.successors.forEach((w) => {
lines.push(` ${v.name} -> ${w.name}`);
});
}
});
lines.push('}');
return lines.join('\n') + '\n';
}
}
exports.default = Graph;