Number of Connected Components

Prereq: DSU Introduction

For this question we start with n nodes in a graph that are all independent of each other. We will then begin connecting nodes in the graph. After each connection connecting two different nodes we ask you to compute the number of connected components in a graph. A connected component is a series of node such that there exists a path between any two nodes in the graph. Nodes will be 0-indexed and will be integers from 0 to n - 1.


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

Try it yourself



Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

1  >>> a = [1, 2, 3]
2  >>> a[-1]
3  3

Get premium for instant access to all content and solutions