如何将巨型组件的中心引用到外部坐标系?

时间:2015-11-16 11:41:19

标签: grid center networkx mathematical-lattices

我在Python(包:NetworkX)中使用一个网络,例如100个节点。我创建它然后通过删除它的一小部分节点(removal)来对其进行分段,如下所示。该脚本计算最大组件及其中心节点的长度。

import networkx as nx
import matplotlib.pyplot as plt
import numpy
N = 10
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() )
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.relabel_nodes(G,labels,False)
pos = {y:x for x,y in labels.iteritems()}
nx.draw_networkx(G, pos=pos, with_labels=True, node_size = 300)
plt.axis('off')
plt.show()
plt.close()
removal=numpy.array([1,5,18,23,54,8,36,95,41,75,77,56,29,39,81,76,27,34,52,50,53,64,45,85])
G.remove_nodes_from(removal)
nx.draw_networkx(G, pos=pos, with_labels=True, node_size = 300)
plt.axis('off')
plt.show()
giant = max(nx.connected_component_subgraphs(G), key=len) #The largest component
center_nodes = nx.center(giant) #The center node(s)
print len(giant)
print center_nodes

这给了我: len(giant)=29center_nodes=[12,13]

删除后网络的外观如下: enter image description here

我的网络嵌入在测量(N+1)x(N+1)的2D网格中,并且有自己的坐标系。网络的每个节点都被看作是在位于以下网格中每个单元格的交叉点处:

enter image description here

我的问题:如何将center_nodes=[12,13]给出的结果“转换”到网格中单元格A 的位置?在这种情况下,我希望center_nodes=[12,13] -> center_coord=13

PS:如果我更改removallen(center_nodes)更改,连接子图的形状也是如此。因此,单元格A 将不会与上面的位置相同。为了解释这一点,我希望能够始终在center_nodes群集的左上角获取单元格的网格坐标,无论其在网络中的形状和位置如何。

0 个答案:

没有答案