About

Closest-Pair & Convex-Hull by Brute Force

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}P = \{p_1, p_2, \dots, p_n\} of n≥2n \ge 2 points in the 2D Cartesian plane where pi=(xi,yi)p_i = (x_i, y_i), find the pair of points (pi,pj)(p_i, p_j) with the smallest Euclidean distance:

d(pi,pj)=(xi−xj)2+(yi−yj)2d(p_i, p_j) = \sqrt{(x_i - x_j)^2 + (y_i - y_j)^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 d2d^2, taking the square root only once at the very end when returning sqrt(dmin).

Complexity Analysis

C(n)=∑i=0n−2∑j=i+1n−11=(n−1)n2=Θ(n2)C(n) = \sum_{i=0}^{n-2} \sum_{j=i+1}^{n-1} 1 = \frac{(n - 1)n}{2} = \Theta(n^2)

2. Convex-Hull Problem by Brute Force

Definition of Convex Set & Convex Hull

Diagram loads as you scroll.

Geometric Property Used by Brute Force

A directed line segment connecting two points pi=(x1,y1)p_i = (x_1, y_1) and pj=(x2,y2)p_j = (x_2, y_2) forms part of the convex hull boundary if and only if all other n−2n - 2 points lie on the same side of the line through pip_i and pjp_j.

The equation of a line through (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is:

ax+by=ca x + b y = c

where:

a=y2−y1,b=x1−x2,c=x1y2−y1x2a = y_2 - y_1, \quad b = x_1 - x_2, \quad c = x_1 y_2 - y_1 x_2

For any third point (x,y)(x, y), the sign of the expression:

S(x,y)=ax+by−c=(y2−y1)x+(x1−x2)y−(x1y2−y1x2)S(x, y) = a x + b y - c = (y_2 - y_1)x + (x_1 - x_2)y - (x_1 y_2 - y_1 x_2)

indicates which half-plane (x,y)(x, y) falls into:

Algorithm Pseudocode

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

Θ(n2)×Θ(n)=Θ(n3)\Theta(n^2) \times \Theta(n) = \Theta(n^3)

Comparison Summary

ProblemBrute Force StrategyTime ComplexityImproved D&C Time (Ch. 5)
Closest PairTest all ≈n2/2\approx n^2/2 pairsΘ(n2)\Theta(n^2)Θ(nlog⁡n)\Theta(n \log n)
Convex HullTest all pairs against all other pointsΘ(n3)\Theta(n^3)Θ(nlog⁡n)\Theta(n \log n) (Quickhull)

Exercises

Exercise 1

Given points p1=(0,0)p_1 = (0, 0), p2=(4,0)p_2 = (4, 0), p3=(2,3)p_3 = (2, 3), and p4=(2,1)p_4 = (2, 1). Test whether the line segment connecting p1p_1 and p2p_2 belongs to the convex hull using the half-plane formula.

Show solution ↓
Solution

For p1=(0,0)p_1 = (0, 0) and p2=(4,0)p_2 = (4, 0):

a=y2−y1=0−0=0a = y_2 - y_1 = 0 - 0 = 0b=x1−x2=0−4=−4b = x_1 - x_2 = 0 - 4 = -4c=x1y2−y1x2=0(0)−0(4)=0c = x_1 y_2 - y_1 x_2 = 0(0) - 0(4) = 0

Line equation test function:

S(x,y)=ax+by−c=0x−4y−0=−4yS(x, y) = ax + by - c = 0x - 4y - 0 = -4y

Now test the remaining points:

  • For p3=(2,3)p_3 = (2, 3): S(2,3)=−4(3)=−12<0S(2, 3) = -4(3) = -12 < 0.
  • For p4=(2,1)p_4 = (2, 1): S(2,1)=−4(1)=−4<0S(2, 1) = -4(1) = -4 < 0.

Both remaining points yield strictly negative values (S<0S < 0), meaning they both lie on the same side of the line. Therefore, the segment p1p2‾\overline{p_1 p_2} is an edge of the convex hull.

Exercise 2

Can a set of nn points have fewer than 3 extreme points?

Show solution ↓
Solution
  • If all nn 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≥3n \ge 3, the convex hull is a polygon with at least 3 extreme points.