None of the solutions proposed here will uniformly map non-edges (v1, v2). Consider an example graph with 4 nodes and two edges:
1 —— 2 | 3 4
There are 4 non-edges: (1,4), (2,3), (2,4), (3,4). Using the Mary or Philippe method to randomly select the first vertex from all four vertices, and then choosing the second vertex from a limited set of vertices so as not to create any edges (or self-intersections), the following probabilities for each non-selectable edge will be given:
p (1,4) = 1/4 * 1 + 1/4 * 1/3 = 8/24
p (2,3) = 1/4 * 1/2 + 1/4 * 1/2 = 6/24
p (3,4) = p (2,4) = 1/4 * 1/2 + 1/4 * 1/3 = 5/24
Thus, the procedure is not homogeneous.
This means that if you want to evenly discretize non-edges, you will have to select both vertices indefinitely and reject the selection (both vertices) when they form an existing edge (or equal). In NetworkX:
v1 = np.random.choice(G.nodes()) v2 = np.random.choice(G.nodes()) while G.has(edge(v1,v2)) or v1==v2: v1 = np.random.choice(G.nodes()) v2 = np.random.choice(G.nodes())
source share