Problem · Graph
Count the Number of Complete Components
Learn this problemProblem statement
You are given an integer n and an undirected graph whose vertices are numbered from 0 through n - 1. The array edges contains each undirected edge [u, v].
A connected component is complete when every pair of distinct vertices in that component is joined by an edge.
Return the number of complete connected components. A component containing one vertex is complete.
Function
countCompleteComponents(n: int, edges: int[][]) → intExamples
Example 1
n = 6edges = [[0,1],[0,2],[1,2],[3,4]]return = 3The components are {0,1,2}, {3,4}, and {5}. Each contains every possible internal edge, so all three are complete.
Example 2
n = 6edges = [[0,1],[0,2],[1,2],[3,4],[3,5]]return = 1The component {0,1,2} is complete. The component {3,4,5} is missing edge [4,5], so it is not complete.
Example 3
n = 1edges = []return = 1The only vertex forms a one-vertex complete component.