# Fortune's algorithm

Fortune's algorithm animation

Fortune's algorithm is a sweep line algorithm for generating a Voronoi diagram from a set of points in a plane using O(n log n) time and O(n) space.[1][2] It was originally published by Steven Fortune in 1986 in his paper "A sweepline algorithm for Voronoi diagrams."[3]

## Algorithm description

The algorithm maintains both a sweep line and a beach line, which both move through the plane as the algorithm progresses. The sweep line is a straight line, which we may by convention assume to be vertical and moving left to right across the plane. At any time during the algorithm, the input points left of the sweep line will have been incorporated into the Voronoi diagram, while the points right of the sweep line will not have been considered yet. The beach line is not a line, but a complicated, piecewise curve to the left of the sweep line, composed of pieces of parabolas; it divides the portion of the plane within which the Voronoi diagram can be known, regardless of what other points might be right of the sweep line, from the rest of the plane. For each point left of the sweep line, one can define a parabola of points equidistant from that point and from the sweep line; the beach line is the boundary of the union of these parabolas. As the sweep line progresses, the vertices of the beach line, at which two parabolas cross, trace out the edges of the Voronoi diagram. The beach line progresses by keeping each parabola base exactly half way between the points initially swept over with the sweep line, and the new position of the sweep line.

The algorithm maintains as data structures a binary search tree describing the combinatorial structure of the beach line, and a priority queue listing potential future events that could change the beach line structure. These events include the addition of another parabola to the beach line (when the sweep line crosses another input point) and the removal of a curve from the beach line (when the sweep line becomes tangent to a circle through some three input points whose parabolas form consecutive segments of the beach line). Each such event may be prioritized by the x-coordinate of the sweep line at the point the event occurs. The algorithm itself then consists of repeatedly removing the next event from the priority queue, finding the changes the event causes in the beach line, and updating the data structures.

As there are O(n) events to process (each being associated with some feature of the Voronoi diagram) and O(log n) time to process an event (each consisting of a constant number of binary search tree and priority queue operations) the total time is O(n log n).

### Pseudocode

Pseudocode description of the algorithm.[4]

```let ${\displaystyle *(z)}$ be the transformation ${\displaystyle *(z)=(z_{x},z_{y}+d(z))}$,
where ${\displaystyle d(z)}$ is the Euclidean distance between ${\displaystyle z}$ and the nearest site
let ${\displaystyle T}$ be the "beach line"
let ${\displaystyle R_{p}}$ be the region covered by site ${\displaystyle p}$.
let ${\displaystyle C_{pq}}$ be the boundary ray between sites ${\displaystyle p}$ and ${\displaystyle q}$.
let ${\displaystyle p_{1},p_{2},...,p_{m}}$ be the sites with minimal ${\displaystyle y}$-coordinate, ordered by ${\displaystyle x}$-coordinate
${\displaystyle Q\gets S-{p_{1},p_{2},...,p_{m}}}$
create initial vertical boundary rays ${\displaystyle C_{p_{1},p_{2}}^{0},C_{p_{2},p_{3}}^{0},...C_{p_{m-1},p_{m}}^{0}}$
${\displaystyle T\gets *(R_{p_{1}}),C_{p_{1},p_{2}}^{0},*(R_{p_{2}}),C_{p_{2},p_{3}}^{0},...,*(R_{p_{m-1}}),C_{p_{m-1},p_{m}}^{0},*(R_{p_{m}})}$
while not IsEmpty(${\displaystyle Q}$) do
${\displaystyle p}$ ← DeleteMin(${\displaystyle Q}$)
case ${\displaystyle p}$ of
${\displaystyle p}$ is a site in ${\displaystyle *(V)}$:
find the occurrence of a region ${\displaystyle *(R_{q})}$ in ${\displaystyle T}$ containing ${\displaystyle p}$,
bracketed by ${\displaystyle C_{rq}}$ on the left and ${\displaystyle C_{qs}}$ on the right
create new boundary rays ${\displaystyle C_{pq}^{-}}$ and ${\displaystyle C_{pq}^{+}}$ with bases ${\displaystyle p}$
replace ${\displaystyle *(R_{q})}$ with ${\displaystyle *(R_{q}),C_{pq}^{-},*(R_{p}),C_{pq}^{+},*(R_{q})}$ in ${\displaystyle T}$
delete from ${\displaystyle Q}$ any intersection between ${\displaystyle C_{rq}}$ and ${\displaystyle C_{qs}}$
insert into ${\displaystyle Q}$ any intersection between ${\displaystyle C_{rq}}$ and ${\displaystyle C_{pq}^{-}}$
insert into ${\displaystyle Q}$ any intersection between ${\displaystyle C_{pq}^{+}}$ and ${\displaystyle C_{qs}}$
${\displaystyle p}$ is a Voronoi vertex in ${\displaystyle *(V)}$:
let ${\displaystyle p}$ be the intersection of ${\displaystyle C_{qr}}$ on the left and ${\displaystyle C_{rs}}$ on the right
let ${\displaystyle C_{uq}}$ be the left neighbor of ${\displaystyle C_{qr}}$ and
let ${\displaystyle C_{sv}}$ be the right neighbor of ${\displaystyle C_{rs}}$ in ${\displaystyle T}$
create a new boundary ray ${\displaystyle C_{qs}^{0}}$ if ${\displaystyle q_{y}=s_{y}}$,
or create ${\displaystyle C_{qs}^{+}}$ if ${\displaystyle p}$ is right of the higher of ${\displaystyle q}$ and ${\displaystyle s}$,
otherwise create ${\displaystyle C_{qs}^{-}}$
replace ${\displaystyle C_{qr},*(R_{r}),C_{rs}}$ with newly created ${\displaystyle C_{qs}}$ in ${\displaystyle T}$
delete from ${\displaystyle Q}$ any intersection between ${\displaystyle C_{uq}}$ and ${\displaystyle C_{qr}}$
delete from ${\displaystyle Q}$ any intersection between ${\displaystyle C_{rs}}$ and ${\displaystyle C_{sv}}$
insert into ${\displaystyle Q}$ any intersection between ${\displaystyle C_{uq}}$ and ${\displaystyle C_{qs}}$
insert into ${\displaystyle Q}$ any intersection between ${\displaystyle C_{qs}}$ and ${\displaystyle C_{sv}}$
record ${\displaystyle p}$ as the summit of ${\displaystyle C_{qr}}$ and ${\displaystyle C_{rs}}$ and the base of ${\displaystyle C_{qs}}$
output the boundary segments ${\displaystyle C_{qr}}$ and ${\displaystyle C_{rs}}$
endcase
endwhile
output the remaining boundary rays in ${\displaystyle T}$
```

## Weighted sites and disks

As Fortune describes in [1] a modified version of the sweepline algorithm can be used to construct an additively weighted Voronoi diagram, in which the distance to each site is offset by the weight of the site; this may equivalently be viewed as a Voronoi diagram of a set of disks, centered at the sites with radius equal to the weight of the site.

Weighted sites may be used to control the areas of the Voronoi cells when using Voronoi diagrams to construct treemaps. In an additively weighted Voronoi diagram, the bisector between sites is in general a hyperbola, in contrast to unweighted Voronoi diagrams and power diagrams of disks for which it is a straight line.

## References

1. {{#invoke:citation/CS1|citation |CitationClass=citation }} Section 7.2: Computing the Voronoi Diagram: pp.151–160.
2. {{#invoke:citation/CS1|citation |CitationClass=citation }}.
3. Steven Fortune. A sweepline algorithm for Voronoi diagrams. Proceedings of the second annual symposium on Computational geometry. Yorktown Heights, New York, United States, pp.313–322. 1986. ISBN 0-89791-194-6. ACM Digital LibrarySpringerLink
4. {{#invoke:citation/CS1|citation |CitationClass=citation }}.