UNPKG

907 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.CycleException = exports.topsort = void 0;
4const each = require("lodash.foreach");
5const size = require("lodash.size");
6function topsort(g) {
7 const visited = {};
8 const stack = {};
9 const results = [];
10 function visit(node) {
11 if (node in stack) {
12 throw new CycleException();
13 }
14 if (!(node in visited)) {
15 stack[node] = true;
16 visited[node] = true;
17 each(g.predecessors(node), visit);
18 delete stack[node];
19 results.push(node);
20 }
21 }
22 each(g.sinks(), visit);
23 if (size(visited) !== g.nodeCount()) {
24 throw new CycleException();
25 }
26 return results;
27}
28exports.topsort = topsort;
29class CycleException extends Error {
30}
31exports.CycleException = CycleException;
32//# sourceMappingURL=topsort.js.map
\No newline at end of file