149: Connected components in an undirected graph

Given a number of nodes labeled from 0 to nodes - 1 and a list of undirected edges, return number for connected components in the undirected graph

Note: An edge is a pair of nodes

 

Example 1

Input:   nodes = 4, edges = [[0, 1], [2, 3]]

Output:  2

Explanation: 0--1 2--3

Example 2

Input:   nodes = 3, edges = [[0, 1], [1, 2]]

Output:  1

Explanation: 0--1--2

Difficulty:Medium
Topic:Graph
Problem #:149