May-24-2017, 02:30 PM
I have an array of nodes and edges like this (in reality, there are over 200,000 edges):
for example in this case, 101_123 means that there are two nodes, called 101 and 123, that have an interaction/edge between them. I would like to randomise this network using an edge swap approach, while maintaining the topology of the network (i.e. the degree of the node after randomisation should be the same before and after randomisation). I cannot do this with networkx, as the network is too big/the randomisation takes too long, so I must code it myself.
The code I have is like this:
[101_123,12312_2345,346_234,45676_345,234_457,568_234,234_346,23297823_23834734]
for example in this case, 101_123 means that there are two nodes, called 101 and 123, that have an interaction/edge between them. I would like to randomise this network using an edge swap approach, while maintaining the topology of the network (i.e. the degree of the node after randomisation should be the same before and after randomisation). I cannot do this with networkx, as the network is too big/the randomisation takes too long, so I must code it myself.
The code I have is like this:
import random import sys ## First, make a list as described above. Put the smaller number first in all cases (is this necessary?) list_of_edges = [] for line in open(sys.argv[1]): line = line.strip().split() if int(line[0]) < int(line[1]): edge = line[0] + "_" + line[1] elif int(line[0]) > int(line[1]): edge = line[1] + "_" + line[0] list_of_edges.append(edge) #The aim: 5 times, take two random edges from the array (e.g. A_B and C_D) and swap them (e.g. A_C and B_D). update the list to include the new swap and then use this as the base list for the next iteration (so this time A_C and B_D could become e.g. B_C and A_D for example). Once the iteration has been done 5 times, print the final randomised network. number_of_times = len(list_of_edges) for i in range(5): #This number will be changed, it is just for testing random_edge1 = random.choice(list_of_edges) random_edge2 = random.choice(list_of_edges) list_of_edges = [] split_random_edge1 = random_edge1.split("_") split_random_edge2 = random_edge2.split("_") random.shuffle(split_random_edge1) random.shuffle(split_random_edge2) new_list1 = [split_random_edge1[0],split_random_edge2[0]] new_list2 = [split_random_edge1[1],split_random_edge2[1]] if int(new_list1[0]) < (new_list1[1]): edge1 = new_list1[0] + "_" + new_list1[1] elif int(new_list1[0]) > int(new_list1[1]): edge1 = new_list1[1] + "_" + new_list1[0] list_of_edges.append(edge1) if int(new_list2[0]) < (new_list2[1]): edge2 = new_list2[0] + "_" + new_list2[1] elif int(new_list2[0]) > int(new_list2[1]): edge2 = new_list2[1] + "_" + new_list2[0] list_of_edges.append(edge2) print list_of_edges print "*" print list_of_edgesAs you can see, I'm stuck because with each iteration, I do not update the list to the newest set of edges. Also, the print statement at the end does not print out the correct randomised network. If someone could demonstrate what I should have done/where I have gone wrong, I would appreciate it.