Challenge: Find All Connected Components in a Graph
Given an undirected graph, find all connected components.
We'll cover the following
Problem statement
Implement a function that takes an undirected graph and prints all the connected components of a graph.
Input
An undirected graph
Output
All connected components in a graph in a list as a list
Sample input
Graph:
Vertex | Edges |
---|---|
0 | 1, 3 |
1 | 0, 2 |
2 | 3, 1 |
3 | 2, 0 |
4 | 5 |
5 | 4, 6 |
6 | 5 |
Sample output
result = [[0, 1, 2, 3], [4, 5, 6]]
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.