class Graph {
constructor(nodes, edges = []) {
if (!nodes) {
this.V = [...new Set(edges.reduce((a, b) => [...a, ...b], []))];
} else {
this.V = nodes;
}
this.E = [...edges];
}
addEdge(edge) {
if (!this.V.includes(edge[0])) this.V.push(edge[0]);
if (!this.V.includes(edge[1])) this.V.push(edge[1]);
this.E.push(edge);
}
getNodeDegrees() {
const counter = new Map(this.V.map((v, i) => [v, 0]));
this.E.forEach(([u, v]) => {
counter[u] += 1;
counter[v] += 1;
});
return counter
}
getMaximumDegree() {
const nodeDegrees = this.getNodeDegrees();
return Math.max(...Array.from(nodeDegrees.values));
}
getNodesByDegree(k) {
const nodeDegrees = this.getNodeDegrees();
return new Set(this.V.filter((v) => nodeDegrees.get(v) === k));
}
}