Count the Number of Complete Components

Medium Topics Company Tags

Count the connected components that are complete in an undirected graph with n vertices labeled 0 through n - 1. Each pair [u, v] in edges connects those two vertices in both directions.

A connected component contains all vertices reachable from any one of its vertices. It is complete when every pair of distinct vertices in the component has a direct edge between them.

An isolated vertex is a complete component of size 1. Count whole connected components, not smaller complete subgraphs inside them.

Example 1:

Count the Number of Complete Components, example 1: illustration of the example

Input: n = 5
edges = [[0,1],[1,2]]

Output: 2

Explanation: The component {0,1,2} is not complete because edge [0,2] is missing. Vertices 3 and 4 are isolated, and each is a complete one-vertex component, giving 2.

Example 2:

Count the Number of Complete Components, example 2: illustration of the example

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

Output: 1

Explanation: All six pairs of vertices in the only component have direct edges. That whole component is complete, so the answer is 1.

Example 3:

Count the Number of Complete Components, example 3: illustration of the example

Input: n = 5
edges = [[0,1],[2,3]]

Output: 3

Explanation: The components {0,1} and {2,3} each contain their one required edge. Vertex 4 is isolated and also complete, giving 3 complete components.

Constraints:

  • 1 <= n <= 50
  • 0 <= edges.length <= n * (n - 1) / 2
  • edges[i].length == 2
  • Edge endpoints are distinct vertex labels between 0 and n - 1.
  • No undirected edge appears more than once.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.