【发布时间】:2019-02-14 03:28:05
【问题描述】:
如果您查看https://en.wikipedia.org/wiki/Clique_problem,您会注意到派系和最大派系之间存在区别。一个最大团不包含在其他团中,而是包含在它自身中。所以我想要那些集团,但networkx似乎只提供:
networkx.algorithms.clique.enumerate_all_cliques(G)
所以我尝试了一个简单的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
我想过滤掉所有合适的子集团。但是你可以看到它很糟糕,因为我必须过滤它两次。它不是很优雅。所以,问题是,给定一个对象列表(整数、字符串),它们是图中的节点标签; enumerate_all_cliques(G) 准确返回这个标签列表列表。现在,给定这个列表列表,过滤掉所有正确的子团。比如:
[[a, b, c], [a, b], [b, c, d]] => [[a, b, c], [b, c, d]]
最快的pythonic方法是什么?
【问题讨论】:
标签: python python-3.x algorithm networkx graph-theory