2013-12-10 4 views
2

저는 파이썬과 networkx를 사용하여 노드를 임의로 끄덕임에 할당합니다. 끄덕임은 세 가지 범주 (흰색, 검은 색 및 기타)로 분류되며 각 범주에는 33 개의 노드가 있습니다. 코드가 작동하지만 두 가지 질문이 있습니다. 1- 하나의 노드가 두 번 선택되지 않도록하려면 어떻게합니까? 내 말은 첫 번째 라운드에서 말은 노드 4와 56 사이에 정의 된 에지를 말합니다. 어떻게하면이 라운드를 다시 선택할 수 있습니까? 2- 다음 단계는 내가하고 싶은 일로 와이트를 할당하는 것입니다. 예를 들어, x가 흰색이고 y가 흰색 일 가능성이 높습니다. 이걸 어떻게 추가 할 수 있습니까?임의의 가장자리를 가진 그래프를 배열하십시오.

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() 

답변

2

인접 행렬을 사용하십시오. 행과 열 사이의 가로 채기는 끄덕임 사이의 관계를 제공합니다. 예를 들어 노드가 3 개인 경우를 가정 해 보겠습니다. 1, 2 및 3이므로 다음 행렬이있는 경우

 1 2 3 
    _________ 
1 | 0 0 0 
2 | 0 0 1 
3 | 0 0 0 

즉 노드 2와 3 사이의 가장자리가 선택되었음을 의미합니다. 다른 매트릭스를 선택하면 (1,2)라고 말하면 매트릭스를 업데이트하면됩니다.

your_matrix[1][2] = 1 
관련 문제