DSAVisualiseData Structures & Algorithms Studio
Chapter 09 · sweep-line

Intervals & Sweep Lines

Interval and sweep-line algorithms process geometric ranges by sorting boundary endpoints along a 1D timeline or sweep coordinate. A virtual sweep line moves across events, maintaining an active state structure to solve interval merging, maximum concurrent overlaps, skyline profiles, and closest-pair problems.

Key Invariants & Rules

  • Event sorting: sort events primarily by time/coordinate X; resolve ties deterministically (ends before starts).
  • Interval union merges overlapping ranges [s₁, e₁] and [s₂, e₂] where s₂ <= e₁ into [s₁, max(e₁, e₂)].
  • Skyline sweep tracks active building heights in a max-priority structure, emitting key points on height change.
  • Divide-and-conquer profiles merge sub-problem altitudes chronologically, tracking current max height.
  • Active count tracks concurrency: increment on interval start, decrement on interval end.

Must-Know Algorithms

Interval UnionO(N log N) sort time, O(N) merge

Merges overlapping closed intervals into minimal disjoint coverage.

Training Sessions SweepO(N log N) event sort, O(1) space

Finds peak concurrent room usage by sorting half-open boundary events.

Skyline ProblemO(N log N) sweep line

Generates silhouette key points from overlapping rectangular building ranges.

Learning Path (4 Lessons)

Self-Check & Concept Review

1. When sorting sweep events for half-open intervals [start, end), how must equal-time start and end events be ordered to measure concurrency accurately?

In half-open intervals [start, end), a session ends at the exact instant another begins; processing ends first avoids counting instantaneous boundary overlap.

2. What is the time complexity of merging N intervals after sorting them by start time?

Once intervals are sorted by start time, merging requires a single linear scan comparing adjacent intervals in O(N) time.

3. In the Skyline problem, when is a new key point (X, H) emitted by the sweep line?

The skyline silhouette changes only when the maximum visible altitude among all currently active overlapping buildings rises or drops.

4. In divide-and-conquer Drone Contest profile merging, how are shared event times resolved?

When multiple reservations change height at the same timestamp, both current altitude trackers advance before determining the combined maximum height.

5. In the 1D Closest Pair algorithm, why is sorting the input array sufficient?

On a 1D number line, any non-adjacent pair has a greater difference than adjacent numbers, reducing the search to adjacent differences in sorted order.