Dsa
Graphs Dfs
Easy
Isolated Components Count
Given an undirected graph represented by a list of edges, write a program that determines the number of connected components in the graph. Each component is a subset of the graph where nodes are connected directly or indirectly. The input consists of the number of nodes followed by the edges. The output should be an integer representing the count of connected components.
Input format:
- The first line contains two integers, n (1 ≤ n ≤ 1000) representing the number of nodes and m (0 ≤ m ≤ 10,000) representing the number of edges.
- The next m lines contain two integers u and v (1 ≤ u, v ≤ n) representing an undirected edge between nodes u and v.
Output format:
- A single integer representing the number of connected components.
Example:
Input:
5 3
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