3

I would like to find all cliques of size k in an undirected graph. A similar problem is described in this question; unlike the OP in linked question, I am not interested in finding all cliques, only those of size k.

I found two approaches so far. The first is to process all cliques in order of increasing size. The second is to compute all maximal cliques, take subsets of size n and remove duplicates.

Each method has drawbacks and is impractical in some cases. Is there a better way to find all cliques of a given size?


Approach 1: Iterate over lists in order of increasing size.

import networkx as nx
from itertools import combinations

# iterate over all cliques in order of increasing size
def find_all_cliques_of_size_k1(g, k):
  for c in nx.algorithms.clique.enumerate_all_cliques(g):
    if len(c) == k:
      yield c
    if len(c) > k:
      break

bad_case_1 = (nx.complete_graph(100), 98)
# find_all_cliques_of_size_k1(*bad_case_1) has to iterate over around 2^100 cliques

Approach 2: Generate maximal cliques & find unique subsets of size k.

# find all maximal cliques, take subsets and size k and remove duplicates
def find_all_cliques_of_size_k2(g, k):
  cliques = set()
  for c in nx.algorithms.clique.find_cliques(g):
    cliques.update(map(frozenset, combinations(c, k)))
  return [list(c) for c in cliques]

bad_case_2 = (nx.random_graphs.erdos_renyi_graph(100, 0.9), 2)
# find_all_cliques_of_size_k2(*bad_case_2) considers a huge number of maximal cliques
hilberts_drinking_problem
  • 11,322
  • 3
  • 22
  • 51
  • If you are saying "impractical", that would surely mean you have large number of nodes/edges to process here. Is there an upper limit? Also for `k`? – vish4071 Feb 01 '22 at 09:31
  • @vish4071 I do not have a particular graph size in mind. One concern is that method 1 works fine with `bad_case_2` and vice versa. "The best of both worlds" method would be a good place to start. More generally, it seems like a natural question to ask and I am surprised that I could not find many resources addressing this problem. – hilberts_drinking_problem Feb 01 '22 at 09:42
  • The thing is, say you have a complete graph of size=100, then, if you want to find cliques of size=40, you will have `100C40` (C from combinatorics) results, which is already a 29-digit number, hence impractical by itself. – vish4071 Feb 01 '22 at 09:52
  • If your `k` is always small (or large -> close to n), or `n` is say, <20, then only this problem itself is practical. – vish4071 Feb 01 '22 at 09:53
  • However, if you need this to solve this as a sub-problem for a bigger task, then if you provide context, may be we can think of a workaround. – vish4071 Feb 01 '22 at 09:55
  • @vish4071 I am aware that there are cases with a large number of cliques of size `k`, like the one you describe. But there are also cases when the number of cliques of size `k` is small, yet neither of the methods I provided works well. I do not have a broader problem to solve, just trying to educate myself on this issue. – hilberts_drinking_problem Feb 01 '22 at 10:02
  • 3
    One way can be to start with cliques of length 2, see how many we have, then try to build cliques of length 3 from them, and so on till we reach `k`. This would be `O(n^k)`, instead of `O(2^n)` as in first approach, or `O(nCn/2)` in second approach if the max clique is of length `n/2`. So as long as `k` is smaller, this approach would work and perform better than the two approaches described. – Jay Feb 01 '22 at 13:10
  • 3
    https://cs.stackexchange.com/questions/18330/onk-1-algorithm-for-k-clique-problem – Jay Feb 01 '22 at 13:12

0 Answers0