Two Clique Problem
Pattern: Bipartite graphs
Splitting a graph into two cliques means any two vertices without an edge must go to different groups. So colour the complement graph (missing edges) with two colours: it is possible exactly when the complement is bipartite.
The problem
Given an adjacency matrix of an undirected graph, return whether its vertices can be split into two groups that are each a clique (every two vertices in a group are joined). A group may be empty.
Think first
- What must hold for two vertices with no edge?
- What graph do those pairs form?
- Which earlier problem decides it?
Hints
Brute force, and why it fails
Try every split: O(2ⁿ × n²).
The key insight
The two-clique condition is exactly a two-colouring of the complement, which BFS checks.
Dry run
Matrix for edges 0-1, 0-2, 0-3, 1-2, 3-4. Missing edges include 0-4, 1-3, 1-4, 2-3, 2-4.
Step 1 of 3
The plan
BFS two-colouring where u and v are neighbours when m[u][v] == 0 (u != v)
Complexity
- Time
O(V²)- Space
O(V)
Every pair is checked from both ends.
Edge cases
- One vertex: true.
- No edges and 3 vertices: false (an empty triangle).
- A complete graph: true.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public boolean twoCliques(int[][] m) {
int n = m.length;
int[] color = new int[n]; // colour the complement: two non-adjacent vertices must be apart
for (int s = 0; s < n; s++) {
if (color[s] != 0) continue;
color[s] = 1;
Queue<Integer> q = new ArrayDeque<>();
q.add(s);
while (!q.isEmpty()) {
int u = q.poll();
for (int v = 0; v < n; v++) {
if (v == u || m[u][v] == 1) continue; // only missing edges matter
if (color[v] == 0) { color[v] = -color[u]; q.add(v); }
else if (color[v] == color[u]) return false;
}
}
}
return true; // each colour class is a clique of the original graph
}
}C++
class Solution {
public:
bool twoCliques(vector<vector<int>>& m) {
int n = static_cast<int>(m.size());
vector<int> color(n, 0); // colour the complement: two non-adjacent vertices must be apart
for (int s = 0; s < n; s++) {
if (color[s] != 0) continue;
color[s] = 1;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v = 0; v < n; v++) {
if (v == u || m[u][v] == 1) continue; // only missing edges matter
if (color[v] == 0) { color[v] = -color[u]; q.push(v); }
else if (color[v] == color[u]) return false;
}
}
}
return true; // each colour class is a clique of the original graph
}
};Python
from collections import deque
class Solution:
def twoCliques(self, m: list[list[int]]) -> bool:
n = len(m)
color = [0] * n # colour the complement: two non-adjacent vertices must be apart
for s in range(n):
if color[s]:
continue
color[s] = 1
q = deque([s])
while q:
u = q.popleft()
for v in range(n):
if v != u and not m[u][v]: # only missing edges matter
if color[v] == 0:
color[v] = -color[u]
q.append(v)
elif color[v] == color[u]:
return False
return True # each colour class is a clique of the original graphCommon mistakes
- Colouring the original graph instead of the complement.
- Counting u as its own non-neighbour.