Quick actions

cmd+k|ctrl+k

Navigation

Languages

graphs-scrapyard

Snippet info

Language

JavaScript

Visibility

public

Author

ralletsfix

Created

2025-06-25T21:19:05.379485Z

Updated

2025-06-26T02:12:10.971027Z



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