Consider a graph with num
nodes, labeled from 0
to num - 1
. Given num
and a list of edges where each edges[i] = [a, b]
represents an undirected connection between nodes a
and b
, determine the number of connected components in the graph.
In the context of a graph, a component of an undirected graph is a connected subgraph that is not part of any larger connected subgraph.
num: number
: An integer, the number of nodes in the graphedges: Array<[number, number]>
: A 2D array where edges[i] = [a, b]
represents an undirected edge between nodes a
and b
Input: num = 6, edges = [[0,1],[4,5],[2,4],[1,3],[0,2]]Output: 1Explanation: All nodes are connected.
Input: num = 3, edges = [[0,1],[1,2]]Output: 1Explanation: All nodes are connected.
Input: num = 4, edges = [[0,1],[2,3]]Output: 2Explanation: There are two connected components: {0, 1} and {2, 3}.
num
<= 1000edges.length
<= 2000edges[i].length
== 2a
< b
< num
a != b
Consider a graph with num
nodes, labeled from 0
to num - 1
. Given num
and a list of edges where each edges[i] = [a, b]
represents an undirected connection between nodes a
and b
, determine the number of connected components in the graph.
In the context of a graph, a component of an undirected graph is a connected subgraph that is not part of any larger connected subgraph.
num: number
: An integer, the number of nodes in the graphedges: Array<[number, number]>
: A 2D array where edges[i] = [a, b]
represents an undirected edge between nodes a
and b
Input: num = 6, edges = [[0,1],[4,5],[2,4],[1,3],[0,2]]Output: 1Explanation: All nodes are connected.
Input: num = 3, edges = [[0,1],[1,2]]Output: 1Explanation: All nodes are connected.
Input: num = 4, edges = [[0,1],[2,3]]Output: 2Explanation: There are two connected components: {0, 1} and {2, 3}.
num
<= 1000edges.length
<= 2000edges[i].length
== 2a
< b
< num
a != b
console.log()
statements will appear here.