voronoi_cells#
- graph_tool.structure.voronoi_cells(g, seeds, weights=None, tiebreak=None, radius=None)[git][source]#
Assign every vertex to its closest seed vertex.
- Parameters:
- g
Graph Graph to be used.
- seedsiterable of
Vertexorint The seed vertices.
- weights
EdgePropertyMap(optional, default:None) Edge weights (lengths). If not given, every edge has unit length.
- tiebreak
str(optional, default:None) Assignment of the vertices equidistant to several seeds: to the first of them in
seeds("first"), to the last one ("last"), or to one chosen uniformly at random ("random"). If not given, the order of the search decides (see below).- radius
float(optional, default:None) If given, the vertices farther than this from every seed are not assigned.
- g
- Returns:
- cell
VertexPropertyMap The closest seed of every vertex (
-1if no seed reaches it).- dist
VertexPropertyMap The distance to the closest seed.
- cell
See also
shortest_distanceDistances from a set of sources
steiner_treeApproximate minimum tree connecting a subset of vertices
Notes
The graph Voronoi cells [erwig-graph-2000] are obtained with a single breadth-first search, or Dijkstra search with weights, started from all the seeds at once, in time \(O(V + E)\) or \(O((V + E)\log V)\). For directed graphs, the distances are from the seeds. If
tiebreak is None, ties between equidistant seeds are broken by the order of the search. Otherwise, a second search is done from every seed, visiting only the vertices to which it is a closest seed (which suffices, since the vertices on a shortest path from a closest seed have it as a closest seed too), so that the closest seeds of every vertex are counted, and the random choice is made by reservoir sampling. This multiplies the time by the largest number of seeds equidistant to a vertex. With weights, distances that differ by a relative amount of at most \(10^{-10}\) are considered equal.References
[erwig-graph-2000]Martin Erwig, “The graph Voronoi diagram with applications”, Networks 36, 156-163 (2000), DOI: 10.1002/1097-0037(200010)36:3<156::AID-NET2>3.0.CO;2-L [sci-hub, @tor]
Examples
>>> g = gt.lattice([5, 1]) >>> cell, dist = gt.voronoi_cells(g, [0, 4]) >>> print(cell.a, dist.a) [0 0 0 4 4] [0. 1. 2. 1. 0.] >>> cell, dist = gt.voronoi_cells(g, [4, 0], tiebreak="first", radius=1) >>> print(cell.a, dist.a) [ 0 0 -1 4 4] [ 0. 1. inf 1. 0.]
In a larger example, the cells of four seeds partition the Delaunay triangulation of random points in the plane:
>>> points = np.random.random((200, 2)) * 4 >>> g, pos = gt.triangulation(points, type="delaunay") >>> seeds = [0, 1, 2, 3] >>> cell, dist = gt.voronoi_cells(g, seeds) >>> palette = {0: "#f28c8e", 1: "#9bbedc", 2: "#a6d7a4", 3: "#cba6d1"} >>> vcolor = g.new_vp("string", vals=[palette[c] for c in cell.a]) >>> vsize = g.new_vp("double", val=10) >>> for s in seeds: ... vsize[s] = 20 >>> gt.graph_draw(g, pos, vertex_fill_color=vcolor, vertex_size=vsize, ... edge_color="#cccccc", output="voronoi_cells-triang.pdf") <...>
Voronoi cells of four seed vertices in the Delaunay triangulation of 200 random points. The colors of the vertices indicate their cells, and the seeds are shown larger.#