Count the connected components of an undirected graph given its edge list

Quick Overview

Given n labeled nodes and a list of undirected edges, count how many connected components the graph contains. The problem tests building an adjacency list, traversing a graph, and handling isolated nodes and edges that close cycles.

Count the connected components of an undirected graph given its edge list

Company: Tripadvisor

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an undirected graph with `n` nodes, labeled from `0` to `n - 1`. You also have a list of edges, where each edge `[a, b]` means there is a two-way connection between node `a` and node `b`. Return how many separate groups of connected nodes (connected components) the graph has. A connected component is a set of nodes in which every node can reach every other node through a sequence of edges, and no node outside the set can be reached from a node inside it. ### Function Signature ```python def count_components(n: int, edges: list[list[int]]) -> int: ... ``` ### Rules - Each edge `[a, b]` is undirected, so `[a, b]` and `[b, a]` describe the same connection. - A node that appears in no edge is a connected component by itself. - Return a single integer. ### Constraints - `1 <= n <= 125` - `0 <= len(edges) <= n * (n - 1) / 2` - Every edge `[a, b]` satisfies `0 <= a < n` and `0 <= b < n`. ### Examples ```text Input: n = 4, edges = [[0, 1], [1, 2]] Output: 2 ``` Nodes 0, 1 and 2 are joined through node 1, and node 3 has no edges, so the components are `{0, 1, 2}` and `{3}`. ```text Input: n = 5, edges = [[0, 1], [1, 3], [2, 4]] Output: 2 ``` The edges join `{0, 1, 3}` into one group and `{2, 4}` into another, and every node belongs to one of the two. ```text Input: n = 6, edges = [[0, 1], [1, 2], [2, 0], [3, 4]] Output: 3 ``` The edge `[2, 0]` closes a cycle among nodes that are already connected, so it merges nothing. The components are `{0, 1, 2}`, `{3, 4}` and `{5}`.

Overview: Given n labeled nodes and a list of undirected edges, count how many connected components the graph contains. The problem tests building an adjacency list, traversing a graph, and handling isolated nodes and edges that close cycles.

Tripadvisor logo
Tripadvisor
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an undirected graph with n nodes, labeled from 0 to n - 1. You also have a list of edges, where each edge [a, b] means there is a two-way connection between node a and node b.

Return how many separate groups of connected nodes (connected components) the graph has. A connected component is a set of nodes in which every node can reach every other node through a sequence of edges, and no node outside the set can be reached from a node inside it.

Function Signature

def count_components(n: int, edges: list[list[int]]) -> int:
    ...

Rules

  • Each edge [a, b] is undirected, so [a, b] and [b, a] describe the same connection.
  • A node that appears in no edge is a connected component by itself.
  • Return a single integer.

Constraints

  • 1 <= n <= 125
  • 0 <= len(edges) <= n * (n - 1) / 2
  • Every edge [a, b] satisfies 0 <= a < n and 0 <= b < n .

Examples

Input: n = 4, edges = [[0, 1], [1, 2]]
Output: 2

Nodes 0, 1 and 2 are joined through node 1, and node 3 has no edges, so the components are {0, 1, 2} and {3}.

Input: n = 5, edges = [[0, 1], [1, 3], [2, 4]]
Output: 2

The edges join {0, 1, 3} into one group and {2, 4} into another, and every node belongs to one of the two.

Input: n = 6, edges = [[0, 1], [1, 2], [2, 0], [3, 4]]
Output: 3

The edge [2, 0] closes a cycle among nodes that are already connected, so it merges nothing. The components are {0, 1, 2}, {3, 4} and {5}.