Open In App

Convex Hull using Divide and Conquer Algorithm

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In computational geometry, a convex hull is the smallest convex polygon that contains a given set of points. It is a fundamental concept with applications in various fields such as computer graphics, robotics, and image processing.

Convex-Hull

Importance of Convex Hull:

Convex hulls are important in computational geometry for several reasons:

  • Collision detection: Convex hulls can be used to efficiently detect collisions between objects in 2D or 3D space.
  • Image processing: Convex hulls can be used to extract meaningful shapes from images, such as the outline of an object.
  • Data visualization: Convex hulls can be used to visualize the distribution of data points in a scatter plot.
  • Input is an array of points specified by their x and y coordinates. The output is the convex hull of this set of points.

Convex Hull using Divide and Conquer Algorithm:

Pre-requisite: Tangents between two convex polygons

Algorithm: Given the set of points for which we have to find the convex hull. Suppose we know the convex hull of the left half points and the right half points, then the problem now is to merge these two convex hulls and determine the convex hull for the complete set. This can be done by finding the upper and lower tangent to the right and left convex hulls. This is illustrated here Tangents between two convex polygons Let the left convex hull be a and the right convex hull be b. Then the lower and upper tangents are named as 1 and 2 respectively, as shown in the figure. Then the red outline shows the final convex hull.

Final-Convex-Hull

Now the problem remains, how to find the convex hull for the left and right half. Now recursion comes into the picture, we divide the set of points until the number of points in the set is very small, say 5, and we can find the convex hull for these points by the brute algorithm. The merging of these halves would result in the convex hull for the complete set of points.

Note: We have used the brute algorithm to find the convex hull for a small number of points and it has a time complexity of O(n3)         O(n^3)          . But some people suggest the following, the convex hull for 3 or fewer points is the complete set of points. This is correct but the problem comes when we try to merge a left convex hull of 2 points and right convex hull of 3 points, then the program gets trapped in an infinite loop in some special cases. So, to get rid of this problem I directly found the convex hull for 5 or fewer points by O(n3)         O(n^3)          algorithm, which is somewhat greater but does not affect the overall complexity of the algorithm. 

Below are the implementation:

C++
Java Python C# JavaScript

Output
convex hull:
-5 -3
-1 -5
1 -4
0 0
-1 1

Time Complexity: The merging of the left and the right convex hulls take O(n) time and as we are dividing the points into two equal parts, so the time complexity of the above algorithm is O(n * log n). 
Auxiliary Space: O(n)

Related Articles : 


Similar Reads