Dsa
Graphs Dfs
Warm-up
Count Connected Components
You are given a list of edges for an undirected graph with 'n' vertices numbered from 1 to n. Write a function to count the number of connected components in the graph. Read 'n' and 'm' (the number of edges) from input, followed by 'm' pairs of integers (u, v) representing the edges. Output the total number of connected components.
Example:
Input:
5 4
1 2
2 3
4 5
Output:
2
Key concepts
graphsdfsconnected components
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