Bentley-Ottmann Algorithm in Haskell?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The Bentley-Ottmann algorithm is an efficient algorithm used to find all intersections in a set of line segments. This geometric algorithm is particularly notable for its capability to handle intersections in time complexity, where is the number of line segments and is the number of intersection points.
In this article, we'll explore the theoretical underpinnings of the Bentley-Ottmann algorithm and present an implementation using Haskell, a functional programming language well-suited for expressing complex algorithms in a concise manner.
Overview of the Bentley-Ottmann Algorithm
The Bentley-Ottmann algorithm efficiently detects intersections by primarily utilizing two data structures:
- Event Queue: A priority queue (usually a min-heap) that stores events, which can be either segment endpoints (start or end) or intersection points. Events are processed in order of increasing x-coordinates.
- Sweep Line Status: A data structure, typically a balanced binary search tree, that maintains the vertical ordering of segments intersecting the current vertical sweep line.
Key Steps
- Initialization: Insert all segment endpoints into the event queue.
- Process Events: Dequeue the event with the smallest x-coordinate from the event queue and process it.
- Handle segment start events by inserting the segment into the sweep line status and checking for intersections.
- Handle segment end events by removing the segment from the sweep line status and checking for intersections.
- If it’s an intersection event, report the intersection and adjust the sweep line status.
- Update Status: At each event, update the sweep line status and the event queue by performing necessary intersection checks between newly adjacent segments.
Haskell Implementation Details
Data Structures
We'll represent a `Point` and a `Segment` as follows:
- Computational Geometry: Useful in graphics and computer-aided design for detecting geometric relationships.
- Geospatial Applications: Employed in mapping software to detect overlapping regions or paths.
Related reading
- Best algorithm for delete duplicates in array of strings
- Best algorithm for determining the high and low in an array of numbers?
- Best algorithm for matching colours.
- Best algorithm for synchronizing two IList in C 2.0
- Best data structure for nearest neighbour in 1 dimension
- Best dynamic data structure for 2d circle nearest neighbor
- Best Algorithm to Invite People to Party
- Best algorithm to test if a linked list has a cycle

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.