[英]Networkx- finding the parallel edges of multiDigraph
我有一個像這樣的多向圖-
Import Networkx as nx
G=nx.MultiDiGraph()
G.add_edge(0,1)
G.add_edge(1,0)
G.add_edge(1,3)
任何查找邊緣 0-1 和 1-0 的 networkx 方法都是平行的?
據我所知,這個問題沒有內置的networkx
函數。 但是networkx
將圖節點和邊存儲在可迭代結構中,因此您可以像這樣一一處理它們:
# For every node in graph
for node in G.nodes():
# We look for adjacent nodes
for adj_node in G[node]:
# If adjacent node has an edge to the first node
# Or our graph have several edges from the first to the adjacent node
if node in G[adj_node] or len(G[node][adj_node]) > 1:
# DO MAGIC!!
print(node, adj_node)
我認為這是最networkx
-ish代碼,可以解決你的問題。 請注意,圖形越稀疏,它的工作速度就越快。 在最壞的情況下——完整的圖——復雜度是 O(n^2)。 在最好的情況下 - 非常稀疏的圖 - O(n)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.