Find connected components in a graph
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In graph theory, identifying connected components within a graph is a fundamental task utilized across various domains, including social network analysis, computer vision, and bioinformatics. A connected component of an undirected graph is a subgraph in which any two vertices are connected to each other by paths and which is connected to no additional vertices in the supergraph. This means that all nodes within this subset are reachable from each other, and no paths exist to any node outside.
Graph Representation
Before delving into connected components, it's essential to understand graph representation. Graphs are often represented in two primary formats:
- Adjacency List: Each vertex stores a list of adjacent vertices, offering a space-efficient solution for sparse graphs.
- Adjacency Matrix: A two-dimensional matrix is used, where the element at the row and column is true if there's an edge from vertex to vertex .
Algorithmic Approaches
To find connected components, several algorithms can be employed, primarily centered around graph traversal techniques:
Depth-First Search (DFS)
A depth-first search is a robust method for finding connected components:
- Initialization: • Initialize a `visited` list to track visited vertices. • Initialize an empty list `components` to store the connected components.
- DFS Traversal: • For each vertex, if it is unvisited, initiate a DFS starting from this vertex. • Collect all reachable nodes, marking them as visited and storing the discovered subgraph as a connected component.
Example
• Similar to DFS, initialize the `visited` and `components` lists. • For each vertex, if it is unvisited, enqueue it and initiate BFS. • Dequeue vertices, marking reachable and unvisited nodes until discovering all vertices in the component. • Time Complexity: Both DFS and BFS run in , where is the number of vertices and is the number of edges. This complexity arises because every vertex and every edge is explored in the worst case scenario. • Space Complexity: The space complexity is often due to the storage of additional data structures such as the `visited` list. • Social Networks: Identifying groups of connected users. • Image Processing: Segmenting distinct objects within an image. • Network Stability: Understanding clusters and isolated failures.
Related reading
- Find cycle of shortest length in a directed graph with positive weights
- Find duplicate element in array in time On
- Find duplicate entry in array of integers
- Find duplicate in array with a memory efficient approach
- Find duplicates in an array, without using any extra space
- Find duplicates in an unsorted sequence efficiently
- Find earliest time for k empty group
- Find existence of number in a sorted list in constant time? Interview question

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.