pythonnetworkxgraph-theoryclique-problem

What is the best way to count the cliques of size k in an undirected graph using networkx in python?


I am surprised that networkx does not seem to have a built in function to do this, but maybe I am missing some clever way to do this using the built-in algorithms?


Solution

  • When using the find_cliques function you need to be carfull when you are going through all the possibilities (itertools.combinations) - in some cases you will count the same clique more than once. For example, if you have a graph of six nodes (let's name them A-G). Four of them are fully connected (A-D) and E is connected to A-D, and G is also connected to A-D (but E is not connected to G). In this situation you have two 5-cliques that share 4 nodes (A,B,C,D,E and A,B,C,D,G). Now let's say that you are looking for 4-cliques in this suggested garph, by using find_cliques you will go over the two 5-cliques, and in each one of them you will count every 4-clique, which includes the 4-clique A,B,C,D, so it will be counted twice (!).

    here is a version of the suggested function that fix this problem by using set so you will count each clique only once:

    def find_cliques_size_k(G, k):
        all_cliques = set()
        for clique in nx.find_cliques(G):
            if len(clique) == k:
                all_cliques.add(tuple(sorted(clique)))
            elif len(clique) > k:
                for mini_clique in itertools.combinations(clique, k):
                    all_cliques.add(tuple(sorted(mini_clique)))
        return len(all_cliques)
    

    (If you want the cliques themselves you can return the 'all_cliques' itself)