my dsa gallery
GraphsDFS

Connected Components in an Undirected Graph

Problem link: https://www.geeksforgeeks.org/problems/connected-components-in-an-undirected-graph/1

Problem Statement

Given an undirected graph with V vertices numbered from 0 to V-1 and E edges, represented as a 2D array edges[][], where each entry edges[i] = [u, v] denotes an edge between vertices u and v.

Return a list of all connected components. Each connected component should be represented as a list of its vertices.

Input

Integer V (number of vertices) 2D array edges[][] where edges[i] = [u, v] representing undirected edges

Output

Return a list of lists, where each inner list represents a connected component containing its vertices.

Example

Input: V = 5 edges = [[0,1],[2,1],[3,4]]

Output: [[0,1,2],[3,4]]

Explanation

Start DFS from node 0 → reach 1 → reach 2 → first component = [0,1,2] Next unvisited node = 3 → reach 4 → second component = [3,4]

Step 1 of 4- Step 1
Start DFS from node 0
Loading...
Use arrow keys to navigate

Brute Force

Intuition

Try to group nodes by checking reachability manually between nodes. This leads to redundant traversals and inefficient grouping.

Approach

  1. Build adjacency list
  2. For each node, check reachable nodes using DFS
  3. Store reachable nodes as a component
  4. Mark visited nodes to avoid repetition
Step 1 of 3- Step 1
Pick node 0
Loading...
Use arrow keys to navigate

Dry Run

Start from 0 → collect [0,1,2] Then from 3 → collect [3,4]

Step 1 of 2- Step 1
Component 1
Loading...
Use arrow keys to navigate

Visualization

Step 1 of 1- Grouping
Final components
Loading...
Use arrow keys to navigate

Code (Java)

import java.util.*;

public class Solution {
    void dfs(int node, boolean[] visited, ArrayList<ArrayList<Integer>> adj, ArrayList<Integer> comp) {
        visited[node] = true;
        comp.add(node);
        for (int nei : adj.get(node)) {
            if (!visited[nei]) dfs(nei, visited, adj, comp);
        }
    }

    public ArrayList<ArrayList<Integer>> connectedcomponents(int V, int[][] edges) {
        ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
        for (int i = 0; i < V; i++) adj.add(new ArrayList<>());

        for (int[] e : edges) {
            adj.get(e[0]).add(e[1]);
            adj.get(e[1]).add(e[0]);
        }

        boolean[] visited = new boolean[V];
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();

        for (int i = 0; i < V; i++) {
            if (!visited[i]) {
                ArrayList<Integer> comp = new ArrayList<>();
                dfs(i, visited, adj, comp);
                res.add(comp);
            }
        }

        return res;
    }
}
Time: O(V + E)Space: O(V)

Complexity Analysis

DFS visits every node once and processes each edge twice in undirected graph. Storing components requires O(V) space.

Optimal Solution

Intuition

This problem is inherently optimal with DFS/BFS. The only improvement is clean traversal and avoiding redundant work.

Approach

  1. Build adjacency list
  2. Maintain visited array
  3. For each unvisited node, run DFS
  4. Collect nodes into component list
  5. Add component to result
Step 1 of 3- Step 1
Initialize graph
Loading...
Use arrow keys to navigate

Dry Run

i=0 → DFS → [0,1,2] i=3 → DFS → [3,4]

Step 1 of 2- Iteration 1
Visit component 1
Loading...
Use arrow keys to navigate

Code (Java)

/* Same as above */
Time: O(V + E)Space: O(V)

Complexity Analysis

Optimal traversal since every node and edge is processed once.

Quick Revision (Brute Force)

  • Build adjacency list
  • Run DFS from each unvisited node
  • Store nodes in component list

Quick Revision (Optimal)

  • DFS/BFS traversal
  • Each DFS gives one component
  • Time complexity O(V+E)

Study Photos

Upload screenshots/notes for this specific problem.

Drag & drop or click to select
Drop an image anywhere in this box to upload.
No photos uploaded till now.

Comments

Stored in D1. Login required.
  • question with good variation : https://leetcode.com/problems/count-the-number-of-complete-components/description/
    2026-04-11T04:46:54.482Z
  • we might not have the adj list so create them using the edges // Create adjacency list from edge list List<List<Integer>> adj = new ArrayList<>(); for (int i = 0; i < V; i++) { adj.add(new ArrayList<>()); } for (int[] e : edges) { adj.get(e[0]).add(e[1]); adj.get(e[1]).add(e[0]); }
    2026-04-11T03:58:03.807Z