bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

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

  1. Create an adjacency list from the edges array.
  2. Initialize a visited set to keep track of visited nodes.
  3. Initialize a counter to 0 to count connected components.
  4. 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.
  5. Return the counter as the number of connected components.
UML Thumbnail

Disjoint Set Union (DSU) to Find Connected Components

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...