Given a set of points, how do I find the two points that are farthest from each other?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In computational geometry, determining the two points that are farthest apart in a given set, often refers to an instance of the "farthest pair problem." This problem is significant for various applications, including computer graphics, geographic information systems (GIS), and pattern recognition. Here, we will explore multiple approaches to solving this problem, ranging from straightforward brute-force methods to more advanced geometric algorithms.
Understanding the Problem
Given a set of points in a two-dimensional space, our goal is to identify the pair of points that have the maximum Euclidean distance between them. Mathematically, the Euclidean distance between two points and is calculated as:
Approaches to Solve the Farthest Pair Problem
1. Brute-force Approach
The brute-force approach involves computing the distance between every pair of points and selecting the pair with the maximum distance. This method has a time complexity of , where is the number of points.
Algorithm
- Initialize a variable `max_distance` to 0.
- Iterate over each point and for each point, compute the distance to every other point.
- Update `max_distance` and store the pair of points if a larger distance is found.
- Return the pair with `max_distance`.
Example:
For points: , , and , compute distances:
•
•
•
The farthest distance is between points and .
2. Convex Hull Approach
A more sophisticated algorithm leverages the concept of the convex hull, the smallest convex polygon enclosing all the points. The farthest points in a set must lie on the boundary defined by the convex hull.
Algorithm
- Compute the convex hull of the given points using algorithms such as Graham's scan or Andrew's monotone chain.
- Use the Rotating Calipers method to determine the farthest pair on the hull.
- This approach improves the average time complexity to because of the convex hull calculation.
3. Rotating Calipers Method
After constructing the convex hull, the Rotating Calipers method can be applied. This geometric technique involves rotating a pair of calipers around the hull to find the maximum distance.
- Place one caliper against an edge of the convex hull.
- Sweep around the hull by rotating the calipers, measuring distances.
- Track the maximum distance encountered.
Handling 3D and Higher Dimensions
The above methods generalize to three dimensions and higher, though the computational complexity may increase. In such cases, more advanced data structures and algorithms are necessary to handle additional computational demands effectively.
Summary Table
The following table summarizes the methods and their characteristics:
| Method | Complexity | Pros | Cons |
| Brute-force | Simple to implement, no preconditions | Inefficient for large n | |
| Convex Hull + Rotating Calipers | Efficient for large datasets | More complex implementation | |
| 3D Extensions | Variable | Handles higher dimensions | Increased complexity |
Additional Considerations
- Numerical Precision: For large coordinate values, consider precision loss in floating-point arithmetic. Using extended precision libraries or types may be necessary.
- Applications: • GIS uses the farthest pair for defining boundaries, maps, and models. • Robotics can assist in range calculations and obstacle avoidance strategies.
- Implementation: Diverse libraries, such as CGAL in C++ or Shapely in Python, offer built-in functions for geometric calculations, facilitating easier implementation.
In conclusion, although the farthest pair problem can be approached by simple brute-force, the efficiency of the solution can be significantly improved via more sophisticated geometric methods like the convex hull and rotating calipers. Such approaches are crucial when scaling to large datasets or requiring computational efficiency.

