3
$\begingroup$

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.)

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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]
$\endgroup$
  • $\begingroup$ Thanks, I am not familiar with graph theory $\endgroup$ – happy fish Aug 1 '16 at 15:01
  • 1
    $\begingroup$ This answer is not quite correct because the OP seems to be looking for all cliques while FindClique only finds maximal ones. $\endgroup$ – Szabolcs Aug 1 '16 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.