I use a python NetworkX graph. How to check if 2 nodes are disconnected and then get a new version of the graph where these 2 nodes are connected.
The difference between 2 graphs should have min edit distance (Levenshtein distance)
Before and after for nodes=[1,2]
:
Advertisement
Answer
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
you can also have a condition to check for an edge of either direction with:
if not G.has_edge(source, target) and not G.has_edge(target, source):