Data Science, Machine Learning, Python, Mathematics, Math.
two nodes c1 = random.choice(list(G.nodes())) c2 = random.choice(list(G.nodes())) # Ensure the selected nodes are different and there is no existing edge between them if c1 != c2 and G.has_edge(c1, c2) == 0: # Assign a random cost to the edge w = random.choice(costs) G.add_edge(c1, c2, Weight=w)