How do I enumerate all *maximal* cliques in a graph using networkx + python?

纵然是瞬间 提交于 2020-01-30 12:56:48

问题


If you look at https://en.wikipedia.org/wiki/Clique_problem, you'll notice there is a distinction between cliques and maximal cliques. A maximal clique is contained in no other clique but itself. So I want those clique, but networkx seems to only provide:

networkx.algorithms.clique.enumerate_all_cliques(G)

So I tried a simple for loop filtering mechanism (see below).

def filter_cliques(self, cliques):
    # TODO: why do we need this?  Post in forum...
    res = []
    for C in cliques:
        C = set(C)
        for D in res:
            if C.issuperset(D) and len(C) != len(D):
                res.remove(D)
                res.append(C)
                break
            elif D.issuperset(C):
                break
        else:
            res.append(C)
    res1 = []
    for C in res:
        for D in res1:
            if C.issuperset(D) and len(C) != len(D):
                res1.remove(D)
                res1.append(C)
            elif D.issuperset(C):
                break
        else:
            res1.append(C)     
    return res1

I want to filter out all the proper subcliques. But as you can see it sucks because I had to filter it twice. It's not very elegant. So, the problem is, given a list of lists of objects (integers, strings), which were the node labels in the graph; enumerate_all_cliques(G) returns exactly this list of lists of labels. Now, given this list of lists, filter out all proper subcliques. So for instance:

[[a, b, c], [a, b], [b, c, d]] => [[a, b, c], [b, c, d]]

What's the quickest pythonic way of doing that?


回答1:


There's a function for that: networkx.algorithms.clique.find_cliques, and yes, it does return only maximal cliques, despite the absence of "maximal" from the name. It should run a lot faster than any filtering approach.

If you find the name confusing (I do), you can rename it:

from networkx.algorithms.clique import find_cliques as maximal_cliques


来源:https://stackoverflow.com/questions/54682789/how-do-i-enumerate-all-maximal-cliques-in-a-graph-using-networkx-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!