Dsa
Graphs Bfs
Warm-up
Connected Components Counter
Given a list of undirected edges representing a graph, count the number of connected components. Input will be provided as a series of edges in the format 'u v', where u and v are the vertices connected by the edge. The input ends with a line containing '0'. Output the number of connected components in the graph.
Example Input:
1 2
1 3
2 3
4 5
0
Example Output:
2
Key concepts
graphsbfscomponents
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now