Skip to main content
mediumBlind 75

Number of Connected Components in an Undirected Graph

Social Network Analysis You're analyzing friend groups in a social network. Given n users (labeled 0 to n-1) and friendship connections as edges where edges[i] = [user_a, user_b], find how many distinct friend groups exist. A friend group is a set of users who are all connected either directly or through mutual friends. Example: n = 5, edges = [[0,1],[1,2],[3,4]] → 2 groups: {0,1,2} and {3,4}

Components are traversals you had to restart. I walk each unvisited node once, then count how many times I started over.

BFS / DFSGraphs

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Typing with Coco is free when you sign in, up to a daily limit. Talking with her out loud comes with the $79 pack. Anyone can open the editor and run their code.

1Count Connected Componentsmedium
Based onLeetCode