Sweep-Line Algorithms
The plane-sweep paradigm turns a static -D geometry problem into a dynamic -D ordered-set problem: a vertical line sweeps left to right, stopping at an -sorted event queue while a balanced-BST status structure tracks the objects it currently crosses, ordered by . We derive Bentley–Ottmann segment intersection in , recover closest-pair in , and reduce skyline, rectangle-area, and overlap problems to event sweeps.
╌╌╌╌
A great many geometric problems share an awkward shape: they ask a question about objects scattered in the plane whose answer seems to depend on every pair of objects at once. Do any two of these segments cross? What is the area covered by the union of these rectangles? The brute-force answer compares all pairs and costs . The plane-sweep paradigm dismantles that quadratic by refusing to look at the whole plane at once. Instead it imagines a vertical line sweeping steadily from left to right, and tracks only what is locally true at the line's current position.1
The payoff is a recurring reduction: a hard -D problem becomes a sequence of cheap updates to a one-dimensional ordered set. At any instant, the only objects that matter are the ones the sweep line currently crosses, and among those, only the ones adjacent in can interact. We have already built every tool this needs: balanced BSTs and ordered sets from the Balanced Trees lesson, Fenwick and segment trees from the structures that follow, and the sweep is the paradigm that puts them to geometric work.
The plane-sweep paradigm
Every sweep-line algorithm is assembled from two data structures and one loop.
The loop is then uniform. Pop the leftmost event; it marks a combinatorial change: an object enters the active set, an object leaves it, or two active objects swap -order. Update the status structure accordingly, inspect the few neighbors the change could affect, and emit any answers. Between events nothing in the active set changes order, so we never need to look there.2
The art in any specific problem is choosing what counts as an event and what the status orders by. The rest is bookkeeping.
Segment intersection: Bentley–Ottmann
Given line segments, report all pairs that cross. The naive test of every pair is , wasteful when is small. The Bentley–Ottmann sweep does it in by exploiting a single geometric fact.1
Sweep a vertical line left to right. The status structure holds the segments currently straddling the line, ordered by the -coordinate at which each segment meets the line. As the line advances this order is stable except at three kinds of event:
- Left endpoint of a segment: insert it into the status at its -position.
- Right endpoint: delete the segment from the status.
- Intersection point of two segments: the two segments swap their relative order in the status (the one that was above is now below).
The engine is the following invariant, which collapses the problem from all pairs to a constant check per event.
So at each event we test only the handful of newly-adjacent pairs for a future crossing, and any crossing we find is pushed into the event queue as a new event:
- On insert, the new segment acquires an upper and a lower neighbor; test each of those two pairs.
- On delete, the departing segment's old neighbors become adjacent; test that one new pair.
- On a swap, the two swapping segments acquire new outer neighbors; test those two new pairs.
The accented segments are exactly the active set; the sweep line meets them at three -values, and the status stores them in that vertical order. Because the total number of events is endpoints plus intersections, and each costs for the ordered-set operations, the total is — a decisive win over whenever .
The swap event is the subtle one. Two segments and are adjacent in the status, above , until the sweep reaches their crossing ; at that they meet at equal , and just past it their order in the status flips — is now above . The swap exposes two new adjacencies (the segment formerly outside now neighbors , and the segment below now neighbors ), and those are the only pairs worth testing next.
We keep the implementation at this level: the careful part is degeneracy (vertical segments, three segments through a point) and reliable orientation tests, which the references treat in full.
Closest pair, swept
The Selection lesson found the closest pair of points by divide-and-conquer
in . A sweep gives the same bound with a different, often simpler,
mechanism, and it generalizes the narrow strip
trick of that proof into a
running invariant.3
Sort the points by and sweep. Let be the smallest distance found so far. Keep the points whose lies within of the sweep line in a balanced set ordered by . When the sweep reaches a new point :
- Evict from the set every point more than to the left of in .
- Among the survivors, only those within of in can beat , so query the set for the -window .
Each point triggers one insertion, deletions amortized, and an range query returning points to test — so overall, dominated by the initial sort. The sweep makes the strip dynamic: rather than recomputing a fresh strip at each recursion, it slides one along, inserting and evicting at the boundary.
Interval and rectangle sweeps
The practice problems are sweeps in disguise, and they reveal a simpler status structure than a full BST: when objects are axis-aligned, events are deltas and the status is a count or a segment tree.
Maximum overlap (My Calendar III, Describe the Painting). Given intervals , find the maximum number covering any point — or the coverage profile. Emit a event at each and a event at each , sort the events by coordinate, and sweep a running sum. The running sum is the number of intervals covering the current coordinate; its maximum is the answer.
- 1
- 2for each interval do
- 3add event tobegins
- 4add event toends
- 5sort by coordinate; break ties with before
- 6
- 7for each event in do
- 8
- 9
- 10return
The tie-break matters: at a shared coordinate, ending an interval before starting the next reflects half-open intervals and avoids spuriously counting an endpoint touch as an overlap. To describe the painting rather than only its peak, emit a coverage segment between consecutive distinct event coordinates whenever , merging equal- neighbors. The sweep costs for the sort and for the pass.
Skyline. Given buildings as , output the silhouette of their union. Sweep -events at building edges; the status is a multiset of active heights. At a left edge insert ; at a right edge remove it. After each event the current skyline height is the multiset's maximum, and a key point is emitted whenever that maximum changes. A balanced multiset (or a heap with lazy deletion) gives .
Union of rectangle areas (Rectangle Area II). Sweep a vertical line across -events at rectangle left and right edges. The status tracks, for the current -slab, the total length of covered by at least one active rectangle. Each rectangle contributes a on its -interval at its left/right edge; a segment tree over compressed -coordinates maintains the covered length under these interval updates in each, exactly the coordinate-compressed segment tree from the Fenwick & Segment Trees lesson. The area is the sum over slabs of (covered -length) (slab width):
With rectangles there are -events and distinct -values, so the sweep runs in .
Takeaways
- The plane-sweep paradigm reduces a -D geometry problem to a -D ordered-set problem by advancing a vertical line through an -sorted event queue while a status structure holds the objects crossing the line, ordered by .
- The geometry is paid only at events and only against adjacent objects in the status, turning all-pairs work into near- sweeps.
- Bentley–Ottmann reports all segment crossings in : events are endpoints plus discovered intersections, and the adjacency invariant means only neighboring segments can next cross.
- Closest pair sweeps a dynamic -strip — a balanced -set queried in a window holding points — for .
- Interval and rectangle sweeps use events: a running sum gives maximum overlap and coverage, a multiset gives the skyline, and a segment tree over compressed gives the union of rectangle areas.
Footnotes
- CLRS, Ch. 33 — Computational Geometry (§33.2): the sweep with an event queue and a -ordered status, and segment-intersection in . ↩ ↩2
- Skiena, § — Sweepline / Geometry: plane-sweep as a general technique; events advance a status structure tracking active objects. ↩
- Erickson, Ch. — (geometry): closest-pair and the packing argument bounding a -rectangle to points. ↩
╌╌ END ╌╌