# How to find a subgraph of a size which is a complete graph?

I have a list of 2 tuples and I want to find a set of numbers, any 2 combination of which exists in the list (order does not matter). For example, here is a set of data and I want to find subgraph which is a complete graph with 4 vertices:

UndirectedEdge @@@
Join[{{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}},
RandomInteger[{1, 20}, {100, 2}] /. {i_, i_} :> Nothing]


which should return me {1, 2, 3, 4} (and other pairs if randomly generated). My current brute force solution is to find ConnectedComponents of size 3, and try to add new vertex one by one.(And using CompleteGraphQ seems not feasible too). Are there better solutions? (In my real problem the number of vertices is not 4, so please do not use any special properties of it.)

You are looking for cliques, i.e. complete subgraphs. FindClique finds only maximal cliques, i.e. cliques that are not part of any larger clique. So your question is really this one:

See Ralph Dratman's answer there for doing it with builtin functions.

The IGraph/M package has direct support for finding all cliques. If there are not too many cliques, it will significantly outperform Mathematica. See for example this post on Wolfram Community where clique finding finishes in 1000 seconds with FindClique, in 60 second with igraph's maximal clique finder and in 0.8 seconds with its generic (non-maximal) clique finder (which is based on the Cliquer library). However, whether you get any speedup generally depends on the type of problem. If there are a huge number of cliques then just transferring the result back to Mathematica takes such a long time that IGraph/M may be slightly slower then FindClique.

The syntax to find size-k cliques is

IGCliques[g, {k}]


just like with FindClique.

Is this what you want:

g = Graph[
UndirectedEdge @@@
Join[{{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}},
RandomInteger[{1, 20}, {100, 2}] /. {i_, i_} :> Nothing]];
cs = FindClique[g, {4}, All]

• Thanks, I am not familiar with graph theory – happy fish Aug 1 '16 at 15:01
• This answer is not quite correct because the OP seems to be looking for all cliques while FindClique only finds maximal ones. – Szabolcs Aug 1 '16 at 15:04