Count the Number of Complete Components

Medium

Topics
GraphDFSBFSUnion Find

Given n nodes labeled 0..n-1 and a list of undirected edges, return the number of complete connected components. A connected component is complete if every pair of its vertices is connected by a direct edge.

Example 1

012345
complete
A component is complete when every pair inside it is directly joined — that is k(k-1)/2 edges for k nodes, not just connectivity. Counting nodes and internal edges per component is enough: 3 qualify.
Nodes are drawn on a circle; an edge list says nothing about layout, only about connections.
Input:  n = 6, edges = [[0,1],[0,2],[1,2],[3,4]]
Output: 3

Example 2

012345
completemissing an edge
Input:  n = 6, edges = [[0,1],[0,2],[1,2],[3,4],[3,5]]
Output: 1

Constraints

  • 1 <= n <= 50
  • 0 <= edges.length <= n*(n-1)/2
  • No repeated edges and no self-loops.
Editorial — intuition, approach, complexity and pitfalls

Intuition

A component is complete when every pair of its vertices is joined — so a component with k vertices must have exactly k(k-1)/2 edges. That turns a structural question into arithmetic: find the components, count vertices and edges in each, compare.

Union-Find groups the vertices in one pass over the edges. Then a second pass tallies, per representative, how many vertices belong to it and how many edges fall inside it. A component passes when its edge count equals k(k-1)/2.

Counting edges per component needs care: iterate the edge list again and attribute each edge to the representative of either endpoint — both endpoints share a representative by then, so either works, and each edge is counted once.

Approach

  1. Union every edge.
  2. Tally vertices per representative.
  3. Tally edges per representative.
  4. Count representatives where edges == k(k-1)/2.

Dry Run

n = 6, edges = [[0,1],[0,2],[1,2],[3,4]]

after unions:  {0,1,2}  {3,4}  {5}

component {0,1,2}: k=3, needs 3*2/2 = 3 edges, has 3   COMPLETE
component {3,4}  : k=2, needs 2*1/2 = 1 edge,  has 1   COMPLETE
component {5}    : k=1, needs 0 edges,         has 0   COMPLETE

answer 3

if edge [1,2] were absent:
  {0,1,2} has 2 edges but needs 3 -> not complete, answer 2

The isolated vertex is worth noting: a single vertex is vacuously complete, since 1*0/2 = 0 edges are required and it has none.

Complexity

  • Time — O(n + e) effectively: initialisation is O(n), and each edge costs two near-constant finds thanks to path compression.
  • Space — O(n): the parent array and the two tally maps.

Common Pitfalls

  • Attributing edges to vertices rather than to representatives, which scatters a component's edges across several buckets.
  • Counting each edge twice by attributing it to both endpoints. They share a representative, so one attribution suffices.
  • Using k*(k-1) without halving, which rejects every component.
  • Forgetting isolated vertices. They form single-vertex components and are complete.
  • Recursive find on 50 vertices is fine, but the iterative form with compression is the habit worth keeping for larger graphs.

Full Solution — all languages

class Solution:
    def countCompleteComponents(self, n, edges):
        par = list(range(n))
        def find(x):
            while par[x] != x:
                par[x] = par[par[x]]; x = par[x]
            return x
        for a, b in edges:
            par[find(a)] = find(b)
        node = {}
        edge = {}
        for i in range(n):
            r = find(i)
            node[r] = node.get(r, 0) + 1
        for a, b in edges:
            r = find(a)
            edge[r] = edge.get(r, 0) + 1
        cnt = 0
        for r in node:
            k = node[r]
            e = edge.get(r, 0)
            if e == k * (k - 1) // 2:
                cnt += 1
        return cnt
SensAI
Beta
Tap the mic to stop voice mode