Quickhull Algorithm
Quickhull Algorithm
A. F. M. Minhazur Rahman
September 22, 2024
Convexity
A shape is convex if, for any two points inside the shape, the line between these
two points is also inside the shape.
Convex Hull
1
Defintion: Given a set of N input points, a convex hull is the smallest
convex set containing all input points.
Alternative Definition: Convex hull of a set Q of points, denoted by CH(Q),
is the smallest convex polygon P for which each point in Q is either on the
boundary of P or in its interior.
We implicitly assume that all points in the set Q are unique and that Q
contains at least three points which are not colinear. Intuitively, we can think
of each point in Q as being a nail sticking out from a board. The convex hull is
then the shape formed by a tight rubber band that surrounds all the nails.
Quickhull Algorithm
Quikhull is a divide and conquer algorithm. The 2-dimensional Quick-hull al-
gorithm can be broken down into the following steps:
1. Find the points with minimum and maximum x-coordinates, as these will
always be part of the convex hull. If many points with the same min-
imum/maximum x exist, use the ones with the minimum/maximum y,
respectively.
2. Use the line formed by the two points to divide the set into two subsets
of points, which will be processed recursively. We next describe how to
determine the part of the hull above the line; the part of the hull below
the line can be determined similarly.
3. Determine the point above the line with the maximum distance from the
line. This point forms a triangle with the two points on the line.
4. The points lying inside of that triangle cannot be part of the convex hull
and can therefore be ignored in the next steps.
5. Recursively repeat the previous two steps on the two lines formed by the
two new sides of the triangle.
6. Continue until no more points are left, the recursion has come to an end,
and the points selected constitute the convex hull.
Side Calculation
The following function calculates which side of a line a point lies on:
int get_side ( Point p1 , Point p2 , Point p ) {
int cross_prod = ( p2 . first - p1 . first ) * ( p . second
- p1 . second )
- ( p2 . second - p1 . second ) * ( p .
first - p1 . first ) ;
2
if ( cross_prod > 0)
return 1;
else if ( cross_prod < 0)
return -1;
return 0;
}
This code uses the principle of the cross product of two 2D vectors to de-
termine which side of a given line (formed by points p1 and p2) the point p lies
on.
Mathematical Principle
Given two points p1(x1 , y1 ) and p2(x2 , y2 ) that form a line, and another point
p(x, y), we can create two vectors:
• Vector v from p1 to p2: (x2 − x1 , y2 − y1 )
• Vector u from p1 to p: (x − x1 , y − y1 )
The cross product of v and u is calculated as:
Distance Calculation
If the line passes through two points P1 = (x1 , y1 ) and P2 = (x2 , y2 ) then the
distance of another point P (x, y) from the line is:
Time Complexity
Best Case
In the best case, the points are evenly distributed around the convex hull, al-
lowing Quickhull to divide the problem optimally at each step.
• At each step, the points are divided evenly between the two halves (above
and below the line).
3
• Each recursive call handles approximately half the points.
Let T (n) represent the time complexity of Quickhull with n points. In the
best case, the recurrence relation is:
n
T (n) = 2T + O(n)
2
This recurrence is similar to merge sort. Using the master theorem, we can
solve this recurrence:
T (n) = O(n lg n)
Thus, the best-case time complexity of the Quickhull algorithm is O(n lg n).
T (n) = T (n − 1) + O(n)
This recurrence describes the scenario where, at each step, only one point is
eliminated, and the algorithm must recursively process n − 1 points.
Solving this recurrence gives:
T (n) = O(n2 )
Thus, the worst-case time complexity of the Quickhull algorithm is
O(n2 ).
References
[1] Cormen, T. H., & Leiserson, C. E. (2001). Introduction to Algorithms, Sec-
ond Edition (Prentice-Hall of India Private Limited edition). The MIT Press.
[2] Wikipedia contributors. (2023, April 25). Quickhull. Wikipedia, The Free
Encyclopedia. https://en.wikipedia.org/w/index.php?title=Quickhull