Geometric Problems: Closest-Pair and Convex-Hull by Brute Force
Computational geometry algorithms process geometric objects such as points, lines, and polygons.
1. Closest-Pair Problem by Brute Force
Given a set P={p1,p2,…,pn} of n≥2 points in the 2D Cartesian plane where pi=(xi,yi), find the pair of points (pi,pj) with the smallest Euclidean distance:
d(pi,pj)=(xi−xj)2+(yi−yj)2
Algorithm
The brute-force approach computes the distance between every possible pair of distinct points and returns the minimal pair.
ClosestPoints(P): // Input: An array P of n points (x_i, y_i), n >= 2 // Output: Indices of two closest points and the minimal distance dmin = infinity index1 = null index2 = null for i = 0 to n - 2 do: for j = i + 1 to n - 1 do: d = (P[i].x - P[j].x)^2 + (P[i].y - P[j].y)^2 if d < dmin then: dmin = d index1 = i index2 = j return index1, index2, sqrt(dmin)
Optimization Tip: To avoid expensive square root operations inside the inner loop, compare the squared distances d2, taking the square root only once at the very end when returning sqrt(dmin).
Complexity Analysis
The basic operation is the squared distance calculation between points P[i] and P[j].
Number of pairs examined:
C(n)=i=0∑n−2j=i+1∑n−11=2(n−1)n=Θ(n2)
Time Complexity:Θ(n2).
Auxiliary Space:Θ(1) (excluding input storage).
2. Convex-Hull Problem by Brute Force
Definition of Convex Set & Convex Hull
A set of points S in the plane is convex if for any two points p,q∈S, the entire line segment pq connecting them lies inside S.
The convex hull of a set of n points S is the smallest convex polygon that encloses all points in S.
The vertices of this polygon are called extreme points. Intuitively, imagine a rubber band stretched around all the pins on a board—the pins that stretch the rubber band are the extreme points.
Diagram loads as you scroll.
Geometric Property Used by Brute Force
A directed line segment connecting two points pi=(x1,y1) and pj=(x2,y2) forms part of the convex hull boundary if and only if all other n−2 points lie on the same side of the line through pi and pj.
The equation of a line through (x1,y1) and (x2,y2) is:
ax+by=c
where:
a=y2−y1,b=x1−x2,c=x1y2−y1x2
For any third point (x,y), the sign of the expression:
BruteForceConvexHull(P): // Input: A set P of n points in the plane // Output: Set of line segments that make up the convex hull HullSegments = empty set for i = 0 to n - 2 do: for j = i + 1 to n - 1 do: a = P[j].y - P[i].y b = P[i].x - P[j].x c = P[i].x * P[j].y - P[i].y * P[j].x sign_positive = 0 sign_negative = 0 for k = 0 to n - 1 do: if k != i and k != j then: val = a * P[k].x + b * P[k].y - c if val > 0 then sign_positive = sign_positive + 1 if val < 0 then sign_negative = sign_negative + 1 // If no points fall on one of the sides, P[i]-P[j] is a hull segment if sign_positive == 0 or sign_negative == 0 then: HullSegments.add(segment(P[i], P[j])) return HullSegments
Complexity Analysis
Number of candidate point pairs (pi,pj): (2n)=2n(n−1)=Θ(n2).
For each pair, testing all other points pk takes n−2=Θ(n) operations.
Total time complexity:
Θ(n2)×Θ(n)=Θ(n3)
This cubic algorithm is extremely slow for large point sets. In Chapter 5, we will see that divide-and-conquer (Quickhull) improves this to Θ(nlogn).
Comparison Summary
Problem
Brute Force Strategy
Time Complexity
Improved D&C Time (Ch. 5)
Closest Pair
Test all ≈n2/2 pairs
Θ(n2)
Θ(nlogn)
Convex Hull
Test all pairs against all other points
Θ(n3)
Θ(nlogn) (Quickhull)
Exercises
Exercise 1
Given points p1=(0,0), p2=(4,0), p3=(2,3), and p4=(2,1). Test whether the line segment connecting p1 and p2 belongs to the convex hull using the half-plane formula.
Both remaining points yield strictly negative values (S<0), meaning they both lie on the same side of the line. Therefore, the segment p1p2 is an edge of the convex hull.
Exercise 2
Can a set of n points have fewer than 3 extreme points?
Show solution ↓Hide solution ↑
Solution
If all n points are collinear (lie on a single straight line), the convex hull is a line segment having only 2 extreme points (the two endpoints of the line segment).
If all points are identical (a single distinct point), there is only 1 extreme point.
For any non-collinear point set in 2D with n≥3, the convex hull is a polygon with at least 3 extreme points.