Count the Number of Complete Components
Medium
Topics
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
k(k-1)/2 edges for k nodes, not just connectivity. Counting nodes and internal edges per component is enough: 3 qualify.Input: n = 6, edges = [[0,1],[0,2],[1,2],[3,4]] Output: 3
Example 2
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
- Union every edge.
- Tally vertices per representative.
- Tally edges per representative.
- 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 2The 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
findon 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 cntimport java.util.*;
class Solution {
public int countCompleteComponents(int n, int[][] edges) {
int[] par = new int[n];
for (int i = 0; i < n; i++) par[i] = i;
for (int[] e : edges) par[find(par, e[0])] = find(par, e[1]);
Map<Integer,Integer> node = new HashMap<>(), edge = new HashMap<>();
for (int i = 0; i < n; i++) {
int r = find(par, i);
node.put(r, node.getOrDefault(r, 0) + 1);
}
for (int[] e : edges) {
int r = find(par, e[0]);
edge.put(r, edge.getOrDefault(r, 0) + 1);
}
int cnt = 0;
for (int r : node.keySet()) {
int k = node.get(r), e = edge.getOrDefault(r, 0);
if (e == k * (k - 1) / 2) cnt++;
}
return cnt;
}
int find(int[] par, int x) {
while (par[x] != x) {
par[x] = par[par[x]];
x = par[x];
}
return x;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int countCompleteComponents(int n, vector<vector<int>>& edges) {
vector<int> par(n);
for (int i = 0; i < n; i++) par[i] = i;
function<int(int)> find = [&](int x) {
while (par[x] != x) {
par[x] = par[par[x]];
x = par[x];
}
return x;
};
for (auto& e : edges) par[find(e[0])] = find(e[1]);
unordered_map<int,int> node, edge;
for (int i = 0; i < n; i++) node[find(i)]++;
for (auto& e : edges) edge[find(e[0])]++;
int cnt = 0;
for (auto& kv : node) {
long long k = kv.second, e = edge.count(kv.first) ? edge[kv.first] : 0;
if (e == k * (k - 1) / 2) cnt++;
}
return cnt;
}
};/**
* @param {number} n
* @param {number[][]} edges
* @return {number}
*/ var countCompleteComponents = function(n, edges) {
const par = [];
for (let i = 0; i < n; i++) par[i] = i;
const find = (x) => {
while (par[x] !== x) {
par[x] = par[par[x]];
x = par[x];
}
return x;
};
for (const [a, b] of edges) par[find(a)] = find(b);
const node = {}, edge = {};
for (let i = 0; i < n; i++) {
const r = find(i);
node[r] = (node[r] || 0) + 1;
}
for (const [a, b] of edges) {
const r = find(a);
edge[r] = (edge[r] || 0) + 1;
}
let cnt = 0;
for (const r in node) {
const k = node[r], e = edge[r] || 0;
if (e === k * (k - 1) / 2) cnt++;
}
return cnt;
};