2013-05-20 3 views
2

고도로 상호 연결된 네트워크에서 3 개의 노드를 선택하여 인쇄하려고합니다. 주어진 노드에서 시작하여 함수는 가장 높은 중심성을 가진 인접 노드에 대한 보행을위한 두 번째 노드로 선택해야합니다.가장 높은 속성 값을 가진 노드 중에서 무작위 노드 선택

동점의 경우, 나는이 노드 사이에서 무작위로 프로그램을 선택하게하고 싶다. 여기

는 지금까지이 작업은 다음과 같습니다

import networkx as nx 
from random import choice 
g =nx.Graph() 
g.add_nodes_from(range(1,5)) 
g.add_edges_from([(1,5),(2,5),(3,5),(4,5), (1,2),(2,3),(3,4),(4,5)]) 

nx.set_node_attributes(g,'degree_cent',nx.degree_centrality(g)) 
degree_walk =[]     

node1=g.nodes()[2] 
degree_walk.append(node1) 
for node2 in g.neighbors(node1): 
    if max(g.node[node2]['degree_cent'], g.node[node2]['degree_cent'].get): 
      node2 = choice(g.neighbors(node1)) 
      degree_walk.append(node2) 
print degree_walk 
+0

안녕하세요. 웰컴 오메. 그냥 친절한 제안. 여기에 질문을 쓸 때 긴 연속 줄을 피하십시오. 행운을 빕니다! –

답변

1
다음

당신이 (사전의 최대 값의 키 값을 찾는 this SO answer에서 영감) 이동

# Find all the neighbors with maximum centrality: 
highest_centrality = max([g.node[n]['degree_cent'] 
          for n in g.neighbors(node1)) 
most_central_neighbors = [n for n in g.nodes() 
          if g.node[n]['degree_cent'] == highest_centrality] 
# Pick one at random: 
random_central_neighbor = choice([most_central_neighbors]) 
# Add it to the list: 
degree_walk.append(random_central_neighbor) 
print degree_walk 

주 당신이하지 않으면 넥타이에 관심이 있다면 (원래 목록의 순서대로 첫 번째 항목을 기꺼이 받아 들일 수 있음) 다음을 수행 할 수 있습니다.

# Find all the neighbors with maximum centrality: 
most_central_neighbors = max(g.neighbors(node1), 
          key=lambda(n): g.node[n]['degree_cent']) 
관련 문제