Before reading this article, I recommend you to visit following two articles. Many concepts and codes are referred from there.

- Divide and Conquer Algorithm
- Convex Hull Algorithms: Jarvis’s March (Introduction Part)
- An efficient way of merging two convex hulls

## Introduction

In this article, I talk about computing convex hull using the divide and conquer technique. The divide and conquer algorithm takes $O(n\log n)$ time. As in the usual divide and conquer algorithms, it has three major steps:

**Divide**: We divide the set of $n$ points into two parts by a vertical line into the left and right halves.**Conquer**: We recursively find the convex hull on left and right halves.**Combine or Merge**: We combine the left and right convex hull into one convex hull.

Divide and Conquer steps are straightforward. The merge step is a little bit tricky and I have created separate post to explain it.

## Algorithm

- Before calling the method to compute the convex hull, once and for all, we sort the points by x-coordinate. This step takes $O(n\log n)$ time.
- Divide Step: Find the point with median x-coordinate. Since the input points are already sorted by x-coordinate, this step should take constant time. Depending upon your implementation, sometime it may take up to $O(n)$ time.
- Conquer Step: Call the procedure recursively on both the halves.
- Merge Step: Merge the two convex hulls computed by two recursive calls in the conquer step. The merge procedure
**should**take $O(n)$ time.

Again, I have described the merging procedure on a separate post. The merge procedure given in the implementation is used from that post.

## Implementation

The python implementation of the main procedure is given below.

1 | def convex_hull(points): |

Remember the input `points`

must be pre-sorted by x-coordinates before calling this function.

1 | points = sorted(points, key = lambda p: p.x) |

The program returns when there is only one point left to compute convex hull. The convex hull of a single point is always the same point. *Note: You can return from the function when the size of the points is less than 4. In that case you can use brute force method in constant time to find the convex hull*.

## Complexity

The divide step and conquer steps require $O(n\log n)$ time. So the recurrence relation for the divide and conquer part is:

$$T(n) = 2T(\frac{n}{2}) + O(n)$$.

Which gives the complexity of $O(n\log n)$. The initial pre-sort requires extra $O(n\log n )$ time. The overall complexity is, therefore, $O(n\log n)$.

## References

- Mount, D. M. (n.d.). CMSC 754 Computational Geometry. Lecture. Retrieved August 23, 2018, from https://www.cs.umd.edu/class/spring2012/cmsc754/Lects/cmsc754-lects.pdf
- Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (n.d.). Introduction to algorithms (3rd ed.). The MIT Press.