问题
Given an undirected graph. How do I check if it can be divided into two sets where every node of one set is connected to every other node of its own set (complete graph). A set can be empty or of only one node. No node should be remaining. Thanks.
EDIT: Edges between two sets is not forbidden.
Basically we have to check if the graph can be divided into two cliques
回答1:
As commented by @Damien, checking whether vertices of a given graph can be partitioned into two cliques is actually the decision problem of clique cover with k = 2. For general k (even for k = 3), the clique cover problem is known to be NP-complete. For k = 2, there exists a O(n2) algorithm, based on the observation below.
Given a graph G = (V, E), denote its complement as G'. Then V can be partitioned into two cliques if and only if G' is 2-colorable.
The proof is simple and thus omitted here. The sketch of the algorithm is shown below.
01. construct G' from G;
02. if G' is bipartite
03. return true;
04. else
05. return false;
Note that the first line requires O(n2) time, while testing whether G' is bipartite requires only O(n + m) time using BFS, where n is the # of vertices and m is the # of edges. Therefore, the total complexity is O(n2).
来源:https://stackoverflow.com/questions/39290413/number-of-complete-graph-components