-1

Is there an automated way to delete 5 edges from an existing adjacency list, while at the same time adding 5 new random edges that connect existing nodes to the list? Meaning that the deleted edges will be replaced by random edges.

Alternatively, I think it could also work if a program randomly deletes 5 edges from the list and saves that as a new file. Maybe, then, I could use a graph generator that creates random adjacency list with 5 edges that must be made out of nodes ranging from 0-25 for instance.

What I am trying to archive:

I transformed a Causal Loop Diagram into a directed network, so that I can analyze some of its properties such as average path length and other centrality measures. The network comprises 26 nodes and 67 edges. Now that I analyzed all of the important features of the network, I would like to create 1000 slightly changed networks, so that I can check how robust the centrality measures are to random perturbations.

In other words, I would like to create 1000 mutated networks to analyze the changes in betweenness centrality (BC) and closeness centrality (CC) for each of the variables/nodes in the network.

Turbo_Ulf
  • 1
  • 2
  • Hi, and welcome. Can you tell us a little bit more about what you're starting with and then what you're hoping to end up with? If you start with a networkx graph in place, it won't be hard to write something that would delete 5 edges and generate 5 edges, but we need to understand a little better what you're starting from. – Joel Jul 28 '21 at 13:22
  • This might help ask a good question: https://stackoverflow.com/help/how-to-ask – Matt Hall Jul 28 '21 at 13:30
  • I transformed a Causal Loop Diagram into a directed network, so that I can analyse some of its properties such as average path length and other centrality measures. This network comprises 26 nodes and 67 edges. Now that I analysed all of the important features of the network, I would like to create 1000 slightly changed networks, so that I can check how robust the centrality measures are to random perturbations. – Turbo_Ulf Jul 29 '21 at 10:41

1 Answers1

0

The method would be to create a list of the edges of G (a simple way is L = list(G.edges()).

Then choose 5 random edges from the list. For that, use random.sample (which requires the random module). random.sample is better than random.choice because you can control how many it chooses (note, it chooses 'without replacement'). So edges_to_delete = random.sample(L, 5)

Then choose 5 random pairs of edges to join. If you want to be careful that they don't already exist do it in a for loop, selecting 2 random nodes and if they aren't joined, add them to the graph.

All told it looks like this:

import networkx as nx
import random
G = nx.fast_gnp_random_graph(100,0.1)
L = list(G.edges())
edges_to_delete = random.sample(L, 5)
G.remove_edges_from(edges_to_delete)

#it's probably better to create a copy of `G` rather than act on `G` directly.
node_list = list(G.nodes())
for counter in range(5):
    candidate_edge = tuple(random.sample(node_list,2))
    if not G.has_edge(*candidate_edge):
        G.add_edge(*candidate_edge)

note the * in the last two lines. This is because the functions expect 2 arguments, but instead I'm passing a single tuple of length 2. The * tells it to actually treat the entries in the tuple as the arguments of the function.

Joel
  • 22,598
  • 6
  • 69
  • 93