graph-theory
connected-components
algorithms
computer-science
network-analysis

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.

Practice algorithms

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:

  1. Adjacency List: Each vertex stores a list of adjacent vertices, offering a space-efficient solution for sparse graphs.
  2. Adjacency Matrix: A two-dimensional matrix is used, where the element at the ithi^{th} row and jthj^{th} column is true if there's an edge from vertex ii to vertex jj.

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:

  1. Initialization: • Initialize a `visited` list to track visited vertices. • Initialize an empty list `components` to store the connected components.
  2. 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 O(V+E)O(V + E), where VV is the number of vertices and EE 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 O(V)O(V) 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.