Connected Components
Given V vertices numbered 0 through V-1 and unique undirected edges, return the number of connected components. Vertices belong to the same component when a…
Given V vertices numbered 0 through V-1 and unique undirected edges, return the number of connected components. Vertices belong to the same component when a path connects them. Count isolated vertices as components too. A self-loop does not join different vertices.
- 1 <= V <= 10000 - 1 <= edges.length <= 10000 - Each edge contains two integer endpoints in 0..V-1 - Undirected edges are unique; reversing an edge does not create a different edge - The preset visualizer uses at most 24 vertices and 48 edges; larger valid graphs are execution-only
Example 1: input V = 4, edges = [[0,1]]; output 3. The components are {0,1}, {2}, and {3}.
Example 2: input V = 4, edges = [[0,1],[1,2],[2,0]]; output 2. The triangle is one component and isolated vertex 3 is another.
Category: Graphs. Topic: graph. Difficulty: Medium. Patterns: graph-traversal.