네트워크
function solution(n, computers) {
const graph = {};
computers.forEach((edges, idx) => {
graph[idx + 1] = [];
edges.forEach((vertex, index) => {
if (index != idx && vertex == 1) {
graph[idx + 1].push(index + 1);
}
});
});
let count = 0;
const stack = [];
const visitedList = [];
const visitedHash = {};
for (const node in graph) {
if (!visitedHash.hasOwnProperty(node)) {
stack.push(node);
while (stack.length) {
const popped = stack.pop();
visitedList.push(parseInt(popped));
visitedHash[popped] = true;
const neighbors = graph[popped];
if (!neighbors.length) {
break;
}
for (const vertex of neighbors) {
if (visitedHash.hasOwnProperty(vertex)) {
continue;
}
stack.push(vertex);
}
}
count++;
}
}
return count;
}