[英]How do I enumerate all *maximal* cliques in a graph using networkx + python?
If you look at https://en.wikipedia.org/wiki/Clique_problem , you'll notice there is a distinction between cliques and maximal cliques. 如果您查看https://en.wikipedia.org/wiki/Clique_problem ,您会注意到,集团与最大集团之间是有区别的。 A maximal clique is contained in no other clique but itself.
最大派别仅包含在其他派别中。 So I want those clique, but networkx seems to only provide:
所以我想要那些团体,但是networkx似乎只提供:
networkx.algorithms.clique.enumerate_all_cliques(G)
So I tried a simple for loop filtering mechanism (see below). 因此,我尝试了一种简单的for循环过滤机制(请参见下文)。
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. 我想过滤掉所有适当的子clicli。 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. enumerate_all_cliques(G)
完全返回此标签列表列表。 Now, given this list of lists, filter out all proper subcliques. 现在,根据此列表列表,筛选出所有适当的子clicli。 So for instance:
因此,例如:
[[a, b, c], [a, b], [b, c, d]] => [[a, b, c], [b, c, d]] [[a,b,c],[a,b],[b,c,d]] => [[[a,b,c],[b,c,d]]
What's the quickest pythonic way of doing that? 最快的pythonic方法是什么?
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. 有一个功能:
networkx.algorithms.clique.find_cliques
,是的,尽管名称中没有“ maximal”,但它仅返回最大集团。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.