寻找多有向图的平行边

2024-04-23 15:03:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个像这样的多向图-

Import Networkx as nx

G=nx.MultiDiGraph()
G.add_edge(0,1)
G.add_edge(1,0)
G.add_edge(1,3)

有没有networkx方法可以找到边0-1和1-0是平行的?在


Tags: 方法importnetworkxaddasnxedge平行
1条回答
网友
1楼 · 发布于 2024-04-23 15:03:34

据我所知,这个问题没有内置的networkx函数。但是networkx将图形节点和边存储在iterable结构中,因此您可以像这样逐个处理它们:

# 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式代码。注意,图越稀疏,它的工作速度就越快。在最坏的情况下,完整图的复杂性是O(n^2)。在最好的情况下-非常稀疏的图-O(n)。在

相关问题 更多 >