![](/img/trans.png)
[英]Adding nodes to a disconnected graph in order to fully connect the graph components, with inter-node distance constraints
[英]How to connect two nodes if they are disconnected
我使用 python NetworkX 圖。 如何檢查 2 個節點是否斷開連接,然后獲取連接這 2 個節點的圖形的新版本。
2個圖之間的差異應該有最小編輯距離( Levenshtein距離)
nodes=[1,2]
之前和之后:
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):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.