python - 如何定位NetworkX中巨型组件的中心节点?

标签 python numpy graph-theory networkx

我正在使用一个具有二维网格形状的常规网络。它由 NxN=100x100=10000 个节点组成。它有一个坐标系,其中节点 0 位于左上角,(0,0) 位置,节点 10000 位于右下角,(100,100) 位置。

网络是这样创建的:

N=100 #The number of nodes per side
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() ) #Dictionary of all positions
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.set_node_attributes(G, 'pos', pos) #Store pos attributes in the nodes
nx.set_node_attributes(G, 'labels', labels) #Store labels attributes in the nodes
nx.draw_networkx(G, pos=nx.get_node_attributes(G, 'pos'),
                 labels=nx.get_node_attributes(G, 'labels'),
                 with_labels=False, node_size=10)

该网络由于对大量负载的响应而变得支离 splinter 。这些是许多 csv 文件,其值用作节点的输入。失败后,网络是这样的(这是单个加载文件的结果):

enter image description here

我的问题:如何确定巨型组件中心节点位置,并确定其距左上角的距离例如,角点的坐标为 (0,0)

编辑

由于响应的可变性,位置 (0,0) 很少有节点,因此使用 nx.shortest_path() 是没有意义的该节点大多数时候都会丢失。因此,我想测量网络的一个点(巨型组件的中心)与同一“区域”的另一点(可能不属于网络的一部分)之间的距离。因此,不能使用函数nx.shortest_path(),否则当路径不存在时会抛出错误

最佳答案

  • 首先使用以下方法检索图形的巨大组成部分:( referenced here )

    giant = max(nx.connected_component_subgraphs(G), key=len)

  • 检索中心节点:

    center_nodes = center(giant)

  • 节点的位置是中心节点本身,因为键就是位置。例如,要显示中心节点的位置:

    打印center_nodes

  • 要确定从中心节点之一到 (i,j) 坐标的距离,您必须保留包含所有 100x100 节点的原始图的副本。我将在这里使用它作为 org_G

    # i,j can represent any coordinate in the 100x100 grid (0,0) for instance
    n = (i,j) 
    print nx.shortest_path(org_G,n,center_nodes[0])
    

关于python - 如何定位NetworkX中巨型组件的中心节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33707237/

相关文章:

python 导入错误: cannot import name 'Faker' from 'faker'

python - 由于numpy,Python脚本无法与Windows任务计划程序一起运行

python - 有关如何将正则表达式转换为状态机图的任何指针

Python 多处理 - 进程终止(不能启动进程两次)

python - 如何将 block 转换为 block 对角矩阵 (NumPy)

python - 向量化用于修改图像分割中使用的掩码的 python 代码

algorithm - 每个节点最多有一个出站边缘的完全连接的有向图叫什么?

algorithm - 如何以最少的步骤删除图形?

c++ - 带索引二维数组的逗号运算符

python - 求列表中三个和的个数