Challenge: Print all the Connected Components in a Graph

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

Problem statement

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

Input

The input is an undirected graph.

Output

The output is all the 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

Note: Consider graph nodes as a number. You have to print nodes in a connected component in ascending order of the numbers.

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