For a given graph G,
is there an algorithm that can divide all graph G
nodes N
into two sets – A
and B
(N = A U B),
such that all nodes ai in set A
are connected to all other nodes aj in A,
and all nodes bi in set B
are connected to all other nodes bj in B?
If the answer to the above is yes, is there a polynomial time algorithm?
Example:
The following graph can be divided into sets {1, 2, 3} and {4, 5, 6}.
Edits:
- There can be nodes in both
A
andB:
e.g., a noden
may exist such thatn
belongs toA
andn
belongs toB.
- If the division into two sets, as defined above, is not possible, the algorithm should recognise it and inform us.
Best Answer
Two observations: If you can find $A$ and $B$ so that their union is all of $G$ and they both induce cliques then you can find $A$ and $B$ that are disjoint cover all of $G$ and also induce cliques.}
Now notice that $A$ and $B$ induce cliques if and only if there are no edges between vertices of $A$ and no edges between vertices of $B$ in the complement of $G$.
So your problem is equivalent to determining if $G^c$ is bipartite, and if it is, finding a partition of $G^c$. This can easily be done in $\mathcal O(n^2)$ time.
Here is some c++11 code: