我希望通过由 n 个节点组成的图 G。并为每个第 n 个节点打开其邻居的字典。找出哪个邻居具有最大的数字属性。至少可以有 100 个邻居。并返回每个节点及其最大邻居的列表,即
[node,biggestneighbor]
[node,biggestneighbor]
[node,biggestneighbor]
节点的属性数据如下所示:
G.node[0]
{'type': 'a', 'pos': [0.76, 0.11]}
我感兴趣的属性是
G.node[0]['pos'][0]
0.76
有谁知道这是否存在?或者,如果不是,起始逻辑看起来是否是一个好的起点?还是更聪明的人有更好的主意?
def thebiggestneighbor(G,attribute,nodes=None):
if nodes is None:
node_set = G
else:
node_set = G.subgraph(nodes)
node=G.node
for u,nbrsdict in G.adjacency_iter():
if u not in node_set:
continue
for v,eattr in nbrsdict.items():
vattr=node[v].get(attribute,None)
# then something like this but for many nodes. probably better subtraction
# of all nodes from each other and which one yeilds the biggest numner
#
# if x.[vattra] > x.[vattrb] then
# a
# elif x.[vattra] < x.[vattrb] then
# b
yield (u,b)