const jsnx = require('jsnetworkx');
function check(h, d) {
for (let i of h.nodes()) {
if (h.degree(i) <= d) {
return true;
}
}
return false;
}
function findNodes(h, it) {
let set1 = [];
for (let i of h.nodes()) {
if (h.degree(i) <= it) {
set1.push(i);
}
}
return set1;
}
const g = new jsnx.Graph();
g.addEdgesFrom([
[1, 2], [1, 9], [3, 13], [4, 6],
[5, 6], [5, 7], [5, 8], [5, 9],
[5, 10], [5, 11], [5, 12], [10, 12],
[10, 13], [11, 14], [12, 14],
[12, 15], [13, 14], [13, 15],
[13, 17], [14, 15], [15, 16]
]);
const h = g.copy();
let it = 1;
let tmp = [];
let buckets = [];
while (true) {
let flag = check(h, it);
if (flag === false) {
it += 1;
if (tmp.length > 0) {
buckets.push([...tmp]);
tmp = [];
}
}
if (flag === true) {
let nodeSet = findNodes(h, it);
for (let each of nodeSet) {
h.removeNode(each);
tmp.push(each);
}
}
if (h.numberOfNodes() === 0 && tmp.length > 0) {
buckets.push([...tmp]);
break;
}
}
const formattedOutput = buckets.map(bucket => "[" + bucket.join(", ") + "]");
console.log("[" + formattedOutput.join(", ") + "]");