From https://structy.net/problems/largest-component
Write a function, largestComponent, that takes in the adjacency list of an undirected graph. The function should return the size of the largest connected component in the graph.
The problem is this code works for all test cases except the first one. And i do not want to solve this with the recursive DFS method i want to use the stack or queue DFS method.
The main function
const largestComponent = (graph) => {
//init the largest count variable to 0
let largest=0;
// use a global set data structure to keep track of visited nodes
let visited = new Set();
//loop through graph object and send each node to DFS function
for (node in graph) {
//if node has been visited then skip to next node
if(visited.has(String(node))) continue;
let currentCount = DFS(graph, node, visited);
// get largest count per iteration
largest = Math.max(largest, currentCount);
}
// return result
return largest;
}
The depth first search function
const DFS = (graph, node, visited) => {
// initiate count for neighbors of current node but also count current node so init to 1
let currentCount = 1;
// populate stack with node
let stack = [node];
//loop for as long as stack isn't empty
while(!!stack.length){
// get current node from stack
let current = stack.pop();
// mark node as visited
visited.add(String(current));
// loop through neighbors of node
for( neighbor of graph[current]){
// if neighbors have been visited already then skip them
if(visited.has(String(neighbor))){
continue;
} else {
// add neighbor to count
currentCount++;
// add the next neighbor to stack
stack.push(neighbor);
}
}
}
// return count of connected components for current node
return currentCount;
}
Test case 1
console.log(largestComponent({
0: ['8', '1', '5'],
1: ['0'],
5: ['0', '8'],
8: ['0', '5'],
2: ['3', '4'],
3: ['2', '4'],
4: ['3', '2']
})) // should give -> 4
Test case 2
console.log(largestComponent({
1: ['2'],
2: ['1','8'],
6: ['7'],
9: ['8'],
7: ['6', '8'],
8: ['9', '7', '2']
})); // should give -> 6
Test case 3
console.log(largestComponent({
3: [],
4: ['6'],
6: ['4', '5', '7', '8'],
8: ['6'],
7: ['6'],
5: ['6'],
1: ['2'],
2: ['1']
})); // should give -> 5
Test case 4
console.log(largestComponent({
0: ['4','7'],
1: [],
2: [],
3: ['6'],
4: ['0'],
6: ['3'],
7: ['0'],
8: []
})); // should give -> 3