Leetcode Problem 323. Number of Connected Components in an Undirected Graph
323. Number of Connected Components in an Undirected Graph
Leetcode Solutions
Depth-First Search (DFS) to Find Connected Components
Create an adjacency list from the edges array.
Initialize a visited set to keep track of visited nodes.
Initialize a counter to 0 to count connected components.
Iterate over each node from 0 to n-1.
a. If the node has not been visited, increment the counter.
b. Perform DFS from the node, marking all reachable nodes as visited.
Return the counter as the number of connected components.
Disjoint Set Union (DSU) to Find Connected Components