0

I want to use the Delaunay triangulation to calculate the minimum spanning tree for a network for an array of coordinates. How can I obtain all the edges from the triangulation and their distances so I can use them for the MST?

1 Answers1

0

The following code is modified from my previous answer here. It stores the edges (pairs of indices in the points array) as keys of a dictionary with the distances as the dictionary values.

# Computing Delaunay
tri = Delaunay(points)

# Building the edges-distance map:
edges_dist_map = {}
for tr in tri.simplices:
    for i in range(3):
        edge_idx0 = tr[i]
        edge_idx1 = tr[(i+1)%3]
        if (edge_idx1, edge_idx0) in edges_dist_map:
            continue  # already visited this edge from other side
        p0 = points[edge_idx0]
        p1 = points[edge_idx1]
        dist = np.linalg.norm(p1 - p0)
        edges_dist_map[(edge_idx0, edge_idx1)] = dist
Iddo Hanniel
  • 1,636
  • 10
  • 18
  • What does the line `edge_idx1 = tr[(i+1)%3]` do? Thank you by the way. – Luke Pitman Jan 24 '21 at 08:31
  • You're welcome - each row in `tri.simplices` holds the three indices of the triangle ordered counter-clockwise (in 2D triangulations). The edges are then just `tr[(0, 1)], tr[(1, 2)], tr[(2, 0)]` and that's what the loop does. The `%3` (modulo 3) is needed for the last pair. (BTW I updated the code to use `tri.simplices` instead of the deprecated `tri.vertices` that I used in my older answer). – Iddo Hanniel Jan 24 '21 at 10:55