1

我有一个 Delaunay 三角剖分(DT)(scipy),如下所示:

# Take first 50 rows with 3 attributes to create a DT-
d = data.loc[:50, ['aid', 'x', 'y']].values
dt = Delaunay(points = d) 

# List of triangles in the DT-
dt.simplices                                       
'''
array([[1, 3, 4, 0],
       [1, 2, 3, 0],
       [1, 2, 3, 4]], dtype=int32)
'''

现在,我想使用“networkx”包创建一个图形,并添加使用 DT 从上面找到的节点和边。

# Create an empty graph with no nodes and no edges.
G = nx.Graph()

我想出的将 DT 单纯形中的唯一节点添加到“G”中的代码是-

# Python3 list to contain nodes
nodes = []

for simplex in data_time_delaunay[1].simplices.tolist():
    for nde in simplex:
        if nde in nodes:
            continue
        else:
            nodes.append(nde)

nodes
# [1, 3, 4, 0, 2]

# Add nodes to graph-
G.add_nodes_from(nodes)

如何使用“dt.simplices”将边添加到“G”?例如,第一个三角形是 [1, 3, 4, 0] 并且位于节点/顶点 1、3、4 和 0 之间。如何确定哪些节点相互连接,然后将它们作为边添加到'G'?

另外,有没有更好的方法将节点添加到“G”?

我正在使用 Python 3.8。

谢谢!

4

1 回答 1

2

您可以将数组中的行添加为路径。一条路径仅构成一系列边,因此路径1,2,3转换为边列表(1,2),(2,3)。所以遍历行并使用nx.add_path

simplices = np.array([[1, 3, 4, 0],
                      [1, 2, 3, 0],
                      [1, 2, 3, 4]])

G = nx.Graph()
for path in simplices:
    nx.add_path(G, path)

nx.draw(G, with_labels=True, node_size=500, node_color='lightgreen')

在此处输入图像描述

于 2020-06-30T12:44:27.170 回答