【问题标题】:How to connect two nodes if they are disconnected如果两个节点断开连接,如何连接它们
【发布时间】:2020-07-27 13:07:17
【问题描述】:

我使用 python NetworkX 图。如何检查 2 个节点是否断开连接,然后获取连接这 2 个节点的图的新版本。

两张图之间的差异应该有最小编辑距离(Levenshtein distance

nodes=[1,2]的前后对比:

|

【问题讨论】:

    标签: python graph networkx graph-theory


    【解决方案1】:
    import networkx as nx
    
    G = nx.DiGraph([(1, 1), (2, 3), (4, 3)])
    
    
    def check_edge(source, target):
        if not G.has_edge(source, target):
            print('adding edge between {} and {}'.format(source, target))
            G.add_edge(source, target)
        else:
            print('edge exists between {} and {}'.format(source, target))
    
    
    check_edge(1, 2)
    # adding edge between 1 and 2
    check_edge(1, 2)
    # edge exists between 1 and 2
    

    您还可以使用以下条件检查任一方向的边缘:

    if not G.has_edge(source, target) and not G.has_edge(target, source):
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 2016-12-02
      • 1970-01-01
      • 2016-06-24
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多