[英]completely connected subgraphs from a larger graph in networkx
我試着不要在這里重新發布,但我認為我的請求非常簡單,我對網絡圖表缺乏經驗。 在python中使用networkx模塊時,我想從連接圖中恢復所有節點相互連接的子圖(節點數大於2的子圖)。 有一個簡單的方法嗎?
這是我的例子:
一個包含七個節點的簡單圖表。 節點1,2,3是共享連接,節點1,2,4都共享連接,節點5,6,7都共享連接。
import networkx as nx
G=nx.Graph() #Make the graph
G.add_nodes_from([1,2,3,4,5,6,7]) #Add nodes, although redundant because of the line below
G.add_edges_from([(1,2),(1,3),(2,3),(1,4),(2,4),(1,5),(5,6),(5,7),(6,7)]) # Adding the edges
我想要的輸出是:([1,2,3],[1,2,4],[5,6,7])
我可以想到寫一些稍微費力的方法,但是想知道是否有一個簡單的內置函數。
聽起來您想要發現圖表中的派系。 為此,您可以使用nx.clique.find_cliques()
:
>>> list(nx.clique.find_cliques(G))
[[1, 2, 3], [1, 2, 4], [1, 5], [6, 5, 7]]
nx.clique.find_cliques()
返回一個生成器,它將生成圖中的所有派系。 您可以使用列表推導過濾掉少於三個節點的派系:
>>> [g for g in nx.clique.find_cliques(G) if len(g) > 2]
[[1, 2, 3], [1, 2, 4], [6, 5, 7]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.