Number of Connected Components In An Undirected Graph Problem

Number of Connected Components In An Undirected Graph Problem

 Given an undirected graph with n nodes, an edges array is provided where edges[i] = [a, b] indicates an edge between node a and node b.

The nodes are numbered from 0 to n – 1. The task is to return the total number of connected components in the graph.

Number of Connected Components In An Undirected Graph Question

Examples related to Number of Connected Components In An Undirected Graph

Example 1:

Example 2:

Constraints:

  • 1 <= n <= 100
  • 0 <= edges.length <= n * (n – 1) / 2

Hints to solve Number of Connected Components In An Undirected Graph

Recommended Time & Space Complexity

Aim for a solution with a time complexity of O(V + E) and a space complexity of O(V + E), where V represents the number of vertices and E represents the number of edges in the graph.

Hint 1:

  • Initially, assume there are no edges, so we have n components, corresponding to the n nodes. Then, add the given edges between the nodes.

  • Can you think of an algorithm to add these edges?

  • After adding an edge, how do you calculate the number of remaining components?

Hint 2:

  • We can use the Union-Find (DSU) algorithm to add the edges. For simplicity, we use Union-Find by size, where the smaller component is merged into the larger one.

  • Union-Find algorithm only adds an edge between two nodes if they are from different components. If the nodes are already in the same component, the edge is not added.

  • How do you determine the number of components after adding the edges?

  • For example, if nodes 0 and 1 are initially disconnected (i.e., two components), adding an edge between them merges them into one component, reducing the total number of components.

Hint 3:

  • Create a DSU object and initialize the result variable, res = n, which represents the initial number of components.

  • Then, iterate through the given edges. For each edge, try to connect the nodes using the union function of the DSU.

  • If the union is successful, decrement res; otherwise, continue. Finally, return res as the total number of components.

Methods to Solve Number of Connected Components In An Undirected Graph Problem

There are mainly 3 approaches to solve Solve Number of Connected Components In An Undirected Graph Problem:

  1. Depth First Search
  2. Breadth First Search
  3. Disjoint Set Union (Rank | Size)

1. Depth First Search Method

Depth First Search (DFS): DFS explores the graph by starting from a node and visiting all reachable nodes, marking them as visited. It continues this process recursively for each unvisited node, counting the connected components as separate groups of connected nodes.

  • Time complexity: O(V+E)
  • Space complexity: O(V+E)

Where V is the number of vertices and E is the number of edges.

Code:

2. Breadth First Search

BFS explores the graph level by level, starting from a node and visiting all its neighbors before moving to the next level. It counts the connected components by ensuring each node is visited once, with each BFS call representing a new component

  • Time complexity: O(V + E)
  • Space complexity: O(V + E)

Where V is the number of vertices and E is the number of edges.

Code:

3. Disjoint Set Union (Rank | Size)

DSU tracks the connected components using a union-find approach, where nodes are grouped into sets. Using union by rank or size ensures efficient merging of components, and the number of connected components is determined by the number of distinct sets.

  • Time complexity: O(V+(E∗α(V)))
  • Space complexity: O(V)

Where V is the number of vertices and E is the number of edges.

Code:

More Articles