Challenge: Print all Connected Components in a Graph

Given an undirected graph, find all connected components in a graph.

Problem Statement

Implement a function which takes an undirected graph and prints all the connected components of a graph.

Input

An undirected graph.

Output

All connected components in a graph

Sample Input

graph = {
    0 -- 1
    1 -- 2
    3 -- 4
    5 -- 3
    5 -- 6
    3 -- 6
}

Sample Output

0 1 2
3 4 5 6

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.