2020-12-20 09:48:49 +03:30
|
|
|
'use strict';
|
|
|
|
|
2012-02-24 13:42:36 +01:00
|
|
|
/**
|
|
|
|
* general topological sort
|
|
|
|
* from https://gist.github.com/1232505
|
|
|
|
* @author SHIN Suzuki (shinout310@gmail.com)
|
|
|
|
* @param Array<Array> edges : list of edges. each edge forms Array<ID,ID> e.g. [12 , 3]
|
|
|
|
*
|
|
|
|
* @returns Array : topological sorted list of IDs
|
|
|
|
**/
|
|
|
|
|
2020-12-20 09:48:49 +03:30
|
|
|
const tsort = (edges) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
const nodes = {}; // hash: stringified id of the node => { id: id, afters: lisf of ids }
|
|
|
|
const sorted = []; // sorted list of IDs ( returned value )
|
|
|
|
const visited = {}; // hash: id of already visited node => true
|
2012-02-24 13:42:36 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const Node = function (id) {
|
2012-02-24 13:42:36 +01:00
|
|
|
this.id = id;
|
|
|
|
this.afters = [];
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2012-02-24 13:42:36 +01:00
|
|
|
|
|
|
|
// 1. build data structures
|
2020-11-23 13:24:19 -05:00
|
|
|
edges.forEach((v) => {
|
|
|
|
const from = v[0]; const
|
|
|
|
to = v[1];
|
2012-02-24 13:42:36 +01:00
|
|
|
if (!nodes[from]) nodes[from] = new Node(from);
|
2020-11-23 13:24:19 -05:00
|
|
|
if (!nodes[to]) nodes[to] = new Node(to);
|
2012-02-24 13:42:36 +01:00
|
|
|
nodes[from].afters.push(to);
|
|
|
|
});
|
|
|
|
|
2020-12-20 09:48:49 +03:30
|
|
|
const visit = (idstr, ancestors) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
const node = nodes[idstr];
|
|
|
|
const id = node.id;
|
2012-02-24 13:42:36 +01:00
|
|
|
|
|
|
|
// if already exists, do nothing
|
|
|
|
if (visited[idstr]) return;
|
|
|
|
|
|
|
|
if (!Array.isArray(ancestors)) ancestors = [];
|
|
|
|
|
|
|
|
ancestors.push(id);
|
|
|
|
|
|
|
|
visited[idstr] = true;
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
node.afters.forEach((afterID) => {
|
2020-12-20 09:48:49 +03:30
|
|
|
// if already in ancestors, a closed chain exists.
|
|
|
|
if (ancestors.indexOf(afterID) >= 0) throw new Error(`closed chain : ${afterID} is in ${id}`);
|
2012-02-24 13:42:36 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
visit(afterID.toString(), ancestors.map((v) => v)); // recursive call
|
2012-02-24 13:42:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
sorted.unshift(id);
|
2020-12-20 09:48:49 +03:30
|
|
|
};
|
|
|
|
|
|
|
|
// 2. topological sort
|
|
|
|
Object.keys(nodes).forEach(visit);
|
2012-02-24 13:42:36 +01:00
|
|
|
|
|
|
|
return sorted;
|
2021-01-29 01:14:03 -05:00
|
|
|
};
|
2012-02-24 13:42:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TEST
|
|
|
|
**/
|
2020-12-20 09:48:49 +03:30
|
|
|
const tsortTest = () => {
|
2012-02-24 13:42:36 +01:00
|
|
|
// example 1: success
|
2020-11-23 13:24:19 -05:00
|
|
|
let edges = [
|
2012-02-24 13:42:36 +01:00
|
|
|
[1, 2],
|
|
|
|
[1, 3],
|
|
|
|
[2, 4],
|
2020-11-23 13:24:19 -05:00
|
|
|
[3, 4],
|
2012-02-24 13:42:36 +01:00
|
|
|
];
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
let sorted = tsort(edges);
|
2012-02-24 13:42:36 +01:00
|
|
|
console.log(sorted);
|
|
|
|
|
|
|
|
// example 2: failure ( A > B > C > A )
|
|
|
|
edges = [
|
|
|
|
['A', 'B'],
|
|
|
|
['B', 'C'],
|
2020-11-23 13:24:19 -05:00
|
|
|
['C', 'A'],
|
2012-02-24 13:42:36 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
sorted = tsort(edges);
|
2021-02-22 08:26:35 +00:00
|
|
|
console.log('succeeded', sorted);
|
2020-11-23 13:24:19 -05:00
|
|
|
} catch (e) {
|
2012-02-24 13:42:36 +01:00
|
|
|
console.log(e.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// example 3: generate random edges
|
2020-12-20 09:48:49 +03:30
|
|
|
const max = 100;
|
|
|
|
const iteration = 30;
|
|
|
|
const randomInt = (max) => Math.floor(Math.random() * max) + 1;
|
2012-02-24 13:42:36 +01:00
|
|
|
|
2020-12-20 09:48:49 +03:30
|
|
|
edges = (() => {
|
|
|
|
const ret = [];
|
|
|
|
let i = 0;
|
2020-11-23 13:24:19 -05:00
|
|
|
while (i++ < iteration) ret.push([randomInt(max), randomInt(max)]);
|
2012-02-24 13:42:36 +01:00
|
|
|
return ret;
|
|
|
|
})();
|
|
|
|
|
|
|
|
try {
|
|
|
|
sorted = tsort(edges);
|
2020-11-23 13:24:19 -05:00
|
|
|
console.log('succeeded', sorted);
|
|
|
|
} catch (e) {
|
|
|
|
console.log('failed', e.message);
|
2012-02-24 13:42:36 +01:00
|
|
|
}
|
2020-12-20 09:48:49 +03:30
|
|
|
};
|
2012-02-24 13:42:36 +01:00
|
|
|
|
|
|
|
// for node.js
|
2020-11-23 13:24:19 -05:00
|
|
|
if (typeof exports === 'object' && exports === this) {
|
2012-02-24 13:42:36 +01:00
|
|
|
module.exports = tsort;
|
|
|
|
if (process.argv[1] === __filename) tsortTest();
|
|
|
|
}
|