This is blog 7 of a series of blogs about algorithmic challenges I came across when creating tgen.
In this blog we will tackle:
- Given distinct points that are not all collinear, order them as the vertices of a random simple polygon, in expected time.

Building a simple polygon by recursively splitting the points.
A polygon is simple if its boundary does not intersect itself. A random permutation of the points usually has many crossings, and repeatedly removing crossings by local fixes can be slow. Instead, we will use divide and conquer to construct a noncrossing Hamiltonian path, then use this construction to build the polygon. I first saw this idea in this Codeforces comment.
The result is not uniformly random among all simple polygons through the points. Random choices only give us varied polygon shapes.
Using paths to create a polygon
For now, assume the points are in general position, meaning no three are collinear.
We first need a smaller problem: given a set of points containing two distinguished endpoints and , construct a simple Hamiltonian path from to through every point in the set.
Suppose we can solve that problem. To build a polygon, take the lexicographically smallest point and largest point . Split every other point by which side of the directed line it lies on. Build one path from to through the points below , and another from to through the points above it.

The red path runs from to below the dashed line ; the blue path returns from to above it.
Since the edges of the two paths stay on opposite sides of the dividing line, the paths meet only at their endpoints and . Together they form a simple polygon.
The Divide-and-Conquer Algorithm
Given a set of points and two designated endpoints and , we want a noncrossing path from to that visits every point exactly once. In later recursive calls, the points may lie on both sides of . If there is at most one other point, the path is immediate. Otherwise:
- Choose a pivot uniformly among the points different from and .
- Independently choose a point uniformly from the interior of segment .
- The line separates from . Put every remaining point on the same side as or on the same side as .

A recursive subproblem: blue points lie on the -side of the dashed line , red points on the -side. The hollow point lies on the dotted segment and is not an input point.
Recursively build the path from to through the first set, and the path from to through the second set. Concatenate them at to obtain the required - path.

The same points after recursion. The blue - path and red - path stay on opposite sides of and meet only at .
There are only finitely many positions of for which passes through another input point, so a continuous random choice avoids all of them with probability .

A generated polygon through 42 points. The red - path and blue - path form its boundary; every input point is a vertex.
Collinear points
We can also polygonize any set of at least three distinct points, as long as they are not all collinear. Consecutive edges may be collinear, but crossings and overlapping edges are still forbidden.
The construction extends to collinear inputs with some additional care; we omit those details here.

A polygonization of all 49 points of a lattice. Collinear vertices are allowed; each point appears exactly once on the boundary.
Complexity
We analyze the version above with points in general position and continuous choices of . The input points can be placed arbitrarily; the expectation is over the algorithm’s random choices.
Lemma 1: In a call with non-endpoint points, with probability at least , each child has at most non-endpoint points.
Proof: Fix , excluding the finitely many positions that make it collinear with two non-endpoint points. Let be the points on the side of containing the most points (choose either side in a tie). Then . Order these points by angle around .
Because lies on , these directions lie in an open semicircle. If is the -th point in this order, leaves points of on one side and on the other.

The nine points above form , numbered in angular order around ; the gray points are outside . Here has rank , so separates four blue points from four red points. Gold rings mark ranks : choosing any of these pivots leaves at least three points of on each side.
Consider pivots whose ranks in are
There are at least such pivots, so a uniform choice among all candidates selects one with probability at least . Each side of then contains at least points. Since becomes an endpoint of both children, either child has at most
non-endpoint points. This holds for every admissible fixed , hence also for an independent continuous random choice of .
Theorem 1: For every input in general position, this version runs in expected time.
Proof: Let count the non-base calls in which point is a non-endpoint. Call a split balanced if both children have at most of the parent’s non-endpoint points. Since goes to only one side of the recursion, after at most balanced splits it reaches a base case. By Lemma 1, even conditioned on previous choices, the expected wait for each balanced split (or for ‘s participation to end) is at most calls. Hence
Charge each non-base call’s linear work equally to its non-endpoint points. The initial split and all base calls cost , so the total running time satisfies
The expected bound also extends to collinear inputs. We omit the additional case analysis to keep the proof short. An unlucky sequence of unbalanced splits can still take time.
The output distribution is not uniform, and the divide-and-conquer structure may bias which polygon shapes appear. As with any specialized generator, it is best combined with other test families when the polygon shape can affect the difficulty of the problem.
References
Thomas Auer and Martin Held, Heuristics for the Generation of Random Polygons, CCCG 1996, pp. 38–43. See “Space Partitioning” in Section 2.2, page 40.