2
我正在使用python和networkx随机分配边缘点。点头分为三类(白色,黑色和其他),每个类别有33个节点。该代码正在工作,但我有两个问题: 1-如何确保一个节点不会选择两次?我的意思是让我们说在第一轮中,节点4和56之间定义了一条边。我怎样才能确保在第4轮中不会再次选择这条边? 2-下一步,我想要做的是分配怀特。这意味着例如如果x是白色,那么y是更可能是白色的A%。我如何添加到这?排列具有随机边缘的图形
import networkx as nx
import matplotlib.pyplot as plt
import random
import numpy
G=nx.Graph()
w=1
b=34
o=67
while w < 34:
G.add_node(w, race='white')
w+=1
while b < 67:
G.add_node(b, race='black')
b+=1
while o < 100:
G.add_node(o, race='other')
o+=1
from numpy import random as rand
###first round edges assignment
num1edge = int(raw_input("Please enter number of edges you want to start with: "))
i=0
while i< num1edge:
x1 = rand.randint (1, 99)
y1 = rand.randint (x1, 99)
G.add_edge(x1,y1)
i+=1
numrounds = int(raw_input("Please enter how many times you want to run: "))
numedge = int(raw_input("Please enter number of edges you want to be created in each round: "))
j = 0
k = 0
while j < numrounds:
while k < num1edge:
x = rand.randint (1, 99)
y = rand.randint (x, 99)
G.add_edge(x,y)
k+=1
j+=1
nx.draw(G)
plt.show()