graphs-scrapyard
function largestConnectedComponent(graph) {
let maxSize = 0;
const visited = new Set();
for (const node of Object.keys(graph)) {
const size = componentSize(graph, node, visited);
maxSize = Math.max(maxSize, size);
}
return maxSize;
}
function componentSize(graph, currNode, visited) {
let count = 0;
function visit(node) {
if (visited.has(node)) return;
visited.add(node);
count += 1;
for (const nbr of graph[node]) {
visit(nbr);
}
}
visit(currNode);
return count;
}
const graph = {
"0": ["8", "1", "5"],
"1": ["0"],
"5": ["0", "8"],
"8": ["0", "5"],
"2": ["3", "4"],
"3": ["2", "4"],
"4": ["3", "2"],
};
const a = largestConnectedComponent(graph);
console.log(a);INFO