Python Program to Find All Connected Components using DFS in an Undirected Graph

When working with undirected graphs, connected components are subsets of vertices where each vertex is reachable from every other vertex within the same component. We can find all connected components using Depth-First Search (DFS) traversal.

Below is a demonstration of finding connected components using a graph class ?

Graph Class Implementation

First, let's create a graph class with methods for DFS traversal and finding connected components ?

class Graph_struct:
    def __init__(self, V):
        self.V = V
        self.adj = [[] for i in range(V)]

    def DFS_Utility(self, temp, v, visited):
        visited[v] = True
        temp.append(v)
        
        for i in self.adj[v]:
            if visited[i] == False:
                temp = self.DFS_Utility(temp, i, visited)
        return temp

    def add_edge(self, v, w):
        self.adj[v].append(w)
        self.adj[w].append(v)

    def connected_components(self):
        visited = []
        conn_component = []
        
        for i in range(self.V):
            visited.append(False)
            
        for v in range(self.V):
            if visited[v] == False:
                temp = []
                conn_component.append(self.DFS_Utility(temp, v, visited))
        return conn_component

# Create graph instance with 5 vertices
my_instance = Graph_struct(5)

# Add edges to create connections
my_instance.add_edge(1, 0)
my_instance.add_edge(2, 3)
my_instance.add_edge(3, 0)

print("Graph edges:")
print("1 --> 0")
print("2 --> 3")
print("3 --> 0")

# Find and display connected components
conn_comp = my_instance.connected_components()
print("\nThe connected components are:")
print(conn_comp)
Graph edges:
1 --> 0
2 --> 3
3 --> 0

The connected components are:
[[0, 1, 3, 2], [4]]

How It Works

The algorithm works by visiting each unvisited vertex and performing DFS to find all vertices in that component ?

0 1 3 2 4 Component 1: {0,1,2,3} Component 2: {4}

Key Points

  • DFS_Utility: Recursively visits all connected vertices from a starting vertex
  • Visited array: Tracks which vertices have been processed to avoid cycles
  • Connected components: Groups of vertices where each vertex is reachable from others in the same group
  • Time complexity: O(V + E) where V is vertices and E is edges

Alternative Implementation with Iterative DFS

Here's an iterative approach using a stack instead of recursion ?

class Graph_Iterative:
    def __init__(self, V):
        self.V = V
        self.adj = [[] for i in range(V)]
    
    def add_edge(self, v, w):
        self.adj[v].append(w)
        self.adj[w].append(v)
    
    def DFS_iterative(self, start, visited):
        stack = [start]
        component = []
        
        while stack:
            vertex = stack.pop()
            if not visited[vertex]:
                visited[vertex] = True
                component.append(vertex)
                
                for neighbor in self.adj[vertex]:
                    if not visited[neighbor]:
                        stack.append(neighbor)
        
        return component
    
    def connected_components(self):
        visited = [False] * self.V
        components = []
        
        for v in range(self.V):
            if not visited[v]:
                component = self.DFS_iterative(v, visited)
                components.append(component)
        
        return components

# Test the iterative approach
graph = Graph_Iterative(5)
graph.add_edge(0, 1)
graph.add_edge(2, 3)
graph.add_edge(0, 3)

components = graph.connected_components()
print("Connected components (iterative):")
for i, comp in enumerate(components):
    print(f"Component {i + 1}: {comp}")
Connected components (iterative):
Component 1: [0, 3, 2, 1]
Component 2: [4]

Conclusion

DFS is an efficient algorithm for finding connected components in undirected graphs. The recursive approach is simpler to understand, while the iterative version avoids potential stack overflow for large graphs.

Updated on: 2026-03-25T19:14:01+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements