python中的派系
我有这个问题,我需要帮助,这是我的代码:python中的派系,python,graph-theory,networkx,clique,Python,Graph Theory,Networkx,Clique,我有这个问题,我需要帮助,这是我的代码: cliques=[clique for clique in nx.find_cliques(GC) if len(clique)>2] for clique in cliques: if len (clique)==3: GC.remove_edge() print "Clique to appear: ",clique #draw the graph
cliques=[clique for clique in nx.find_cliques(GC) if len(clique)>2]
for clique in cliques:
if len (clique)==3:
GC.remove_edge()
print "Clique to appear: ",clique
#draw the graph
nx.draw(GC)
plt.show()
首先,我在我的图中搜索以查找团,然后我测试长度为3的团是否为真,如果为真,我想删除一条边,这样我就可以消除完整的图(3)。
我该怎么做
谢谢
或(同等)
但我可能遗漏了一些重要的东西,因为这似乎很明显,而且我不是专家——我只是阅读了networkx文档(其中说一个群组是一个节点列表,
remove\u edge
需要两个节点)。我认为这里最棘手的问题是处理共享边。您不希望每次删除边时都搜索派系,但需要记住已删除的边
查看一下,我们发现find_cliques
函数是一个生成器
此实现是一个列表生成器,每个列表都包含
最大集团的成员
这是否意味着您可以生成一个群组,删除一条边,然后生成下一个群组,我们的生成器将知道我们删除了一条边
用另一个问题来回答这个问题:为什么不在每次你搞垮一个小集团的时候就把发电机关掉呢
edge_removed = True
while edge_removed:
edge_removed = False
for clique in nx.find_cliques(GC):
if len (clique)>=3:
GC.remove_edge(clique[0], clique[1])
edge_removed = True
break # Gotta restart the generator now...
你必须记住,你对派系所做的任何事情都可能非常消耗资源,因为即使只是在图中检测派系也是NP完全的。你的例子没有有效的缩进。在发布Python代码时,请确保您发布的示例具有有效的缩进。所有
大小>=3
的小团体将包含完整的\u图(3)
。你不关心其余的?那么我如何编写一个代码来搜索所有K3并通过从图上所有K3中删除一条边来消除它们呢?不,我参与了一个项目,这只是其中的一小部分。这就是我得到的:回溯(最近一次调用):GC中第119行的文件“C:Graph1.py”。remove_edge(集团[0],集团[1])文件“C:\Python27\lib\site packages\networkx-1.6-py2.7.egg\networkx\classes\graph.py”,第859行,在remove\u edge raise NetworkXError(“边%s-%s不在图形”%(u,v))NetworkXError中:边2-11不在图形ooking中,如果我没有弄错的话,生成器工作于静态值。它不会知道你改变了图表。但是find_claits
返回的派系可能有共同的优势。不客气,如果它能解决您的问题,请随意接受答案。:)@机器学习你能检查一下这个问题吗
if len(clique)==3:
GC.remove_edge(clique[0], clique[1])
edge_removed = True
while edge_removed:
edge_removed = False
for clique in nx.find_cliques(GC):
if len (clique)>=3:
GC.remove_edge(clique[0], clique[1])
edge_removed = True
break # Gotta restart the generator now...