【问题标题】:networkx construct graph by euclidean distance thresholdnetworkx 通过欧几里得距离阈值构造图
【发布时间】:2021-03-19 16:41:03
【问题描述】:

我想在给定节点时通过欧几里得阈值边构造几何图。

例如,

给定的节点是二维地图上的位置

x1(1,0) x2(3,4) x5(5,6)

那么当我设置欧式距离阈值比如5时, 图表看起来像 x1-x2-x5。由于x1和x5都比5远,所以不允许连接。

如何使用 networkx 或其他库方便地做到这一点?

【问题讨论】:

    标签: python graph networkx


    【解决方案1】:

    您可以使用 kd-tree,特别是您可能希望使用 scipy.spatial.cKDTree(kd-tree 用于快速最近邻查找)。

    一般来说,kd-tree(k-dimensional tree的缩写)是一种空间划分数据结构,用于组织k维空间中的点。它们对于在空间中找到最接近给定输入点的点很有用。

    from networkx import Graph
    from scipy.spatial import cKDTree
    
    # your data and parameters
    points = [(1, 0), (3, 4), (5, 6)]
    dist = 5
    
    # build KDTree
    tree = cKDTree(points)
    
    # build graph
    G = Graph()
    G.add_nodes_from(points)
    G.add_edges_from((point, points[idx2])
                     for idx1, point in enumerate(points)
                     for idx2 in tree.query_ball_point(point, dist)
                     if idx1 != idx2)
    

    【讨论】:

      猜你喜欢
      • 2017-02-28
      • 2013-03-02
      • 2014-01-17
      • 2015-07-15
      • 2014-02-04
      • 1970-01-01
      • 2021-10-01
      相关资源
      最近更新 更多