Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Count Connected Components

Easy Acceptance 72.85% Points 20.00

You are given an undirected graph with V vertices and its adjacency list. Your task is to determine the number of connected components in the graph.

A connected component is a group of vertices where every vertex is reachable from every other vertex in that group.

Examples
Example 1

image Input: V = 5, edges = [[0,1],[1,2],[3,4]]
Output: 2
Explanation: The graph has two connected components.

Example 2

image Input: V = 4, edges = [[0,1],[1,2],[2,3]]
Output: 1
Explanation: All vertices belong to a single connected component.

Example 3

image Input: V = 3, edges = []
Output: 3
Explanation: Each vertex forms its own connected component.

Constraints
    • 1 = V = 105
    • 0 = E = 2 × 105
Companies
Paytm Wipro
Topics
Graph
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions