I am working in Python (package: NetworkX
) with a network of, say, 100
nodes. I create it and then fragment it by removing a fraction of its nodes (removal
), as shown below. The script computes the length of the largest component and its center node(s).
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
This gives me:
len(giant)=29
and center_nodes=[12,13]
.
What the network looks like after removal:
My network is embedded in a 2D grid which measures (N+1)x(N+1)
, and has its own coordinate system. Every node of the network is to be seen as if it was placed at the intersection of each cell in the grid below:
My problem: How can I "translate" the result given by center_nodes=[12,13]
into the location of cell A in the grid? In this case, I would like to have center_nodes=[12,13] -> center_coord=13
.
PS: if I change removal
, len(center_nodes)
changes, as does the shape of the connected subgraphs. Thus, cell A will not be in the same position as above. To account for this, I would like to be able to always get the grid coordinates of the cell at the top left corner of the center_nodes
cluster, regardless of its shape and location within the network.