- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

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

When it is required to find all the connected components using depth first search in an undirected graph, a class is defined that contains methods to initialize values, perform depth first search traversal, find the connected components, add nodes to the graph and so on. The instance of the class can be created and the methods can be accessed and operations and be performed on it.

Below is a demonstration of the same −

## Example

class Graph_struct: def __init__(self, V): self.V = V self.adj = [[] for i in range(V)] def DFS_Utililty(self, temp, v, visited): visited[v] = True temp.append(v) for i in self.adj[v]: if visited[i] == False: temp = self.DFS_Utililty(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_compnent = [] for i in range(self.V): visited.append(False) for v in range(self.V): if visited[v] == False: temp = [] conn_compnent.append(self.DFS_Utililty(temp, v, visited)) return conn_compnent my_instance = Graph_struct(5) my_instance.add_edge(1, 0) my_instance.add_edge(2, 3) my_instance.add_edge(3, 0) print("1-->0") print("2-->3") print("3-->0") conn_comp = my_instance.connected_components() print("The connected components are :") print(conn_comp)

## Output

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

## Explanation

A class named ‘Graph_struct’ is defined.

A method named ‘add_edge’ is defined that helps add elements to the tree.

The ‘DFS_Utility’ method is defined that helps traverse the tree using depth first search approach.

A method named ‘connected_components’ is defined, that helps determine the nodes that are connected to each other.

An instance of the class is created, and the methods are called on it.

The node are displayed on the console.

The connected components are displayed as output on the console.

- Related Articles
- Python Program to Find All Connected Components using BFS in an Undirected Graph
- C++ Program to Find the Connected Components of an UnDirected Graph
- Number of Connected Components in an Undirected Graph in C++
- Sum of the minimum elements in all connected components of an undirected graph in C++
- C++ Program to Check the Connectivity of Undirected Graph Using DFS
- C++ Program to Check if an UnDirected Graph is a Tree or Not Using DFS
- Python Program to Find if Undirected Graph contains Cycle using BFS
- Print all the cycles in an undirected graph in C++
- Check if a graph is strongly connected - Set 1 (Kosaraju using DFS) in C++
- C++ Program to Find Strongly Connected Components in Graphs
- Program to find out if a vertex in an undirected graph has a lesser cost path in Python
- Product of lengths of all cycles in an undirected graph in C++
- C++ Program to Check the Connectivity of Undirected Graph Using BFS
- Find if an undirected graph contains an independent set of a given size in Python
- C++ Program to Check whether Graph is a Bipartite using DFS