Given a set of points in the plane. the convex hull of the set is the smallest convex polygon that contains all the points of it.
We strongly recommend to see the following post first.
How to check if two given line segments intersect?
We have discussed Jarvis’s Algorithm for Convex Hull. The worst case time complexity of Jarvis’s Algorithm is O(n^2). Using Graham’s scan algorithm, we can find Convex Hull in O(nLogn) time. Following is Graham’s algorithm
Let points[0..n-1] be the input array.
1) Find the bottom-most point by comparing y coordinate of all points. If there are two points with the same y value, then the point with smaller x coordinate value is considered. Let the bottom-most point be P0. Put P0 at first position in output hull.
2) Consider the remaining n-1 points and sort them by polar angle in counterclockwise order around points[0]. If the polar angle of two points is the same, then put the nearest point first.
3 After sorting, check if two or more points have the same angle. If two more points have the same angle, then remove all same angle points except the point farthest from P0. Let the size of the new array be m.
4) If m is less than 3, return (Convex Hull not possible)
5) Create an empty stack ‘S’ and push points[0], points[1] and points[2] to S.
6) Process remaining m-3 points one by one. Do following for every point ‘points[i]’
4.1) Keep removing points from stack while orientation of following 3 points is not counterclockwise (or they don’t make a left turn).
a) Point next to top in stack
b) Point at the top of stack
c) points[i]
4.2) Push points[i] to S
5) Print contents of S
The above algorithm can be divided into two phases.
Phase 1 (Sort points): We first find the bottom-most point. The idea is to pre-process points be sorting them with respect to the bottom-most point. Once the points are sorted, they form a simple closed path (See the following diagram).
What should be the sorting criteria? computation of actual angles would be inefficient since trigonometric functions are not simple to evaluate. The idea is to use the orientation to compare angles without actually computing them (See the compare() function below)
Phase 2 (Accept or Reject Points): Once we have the closed path, the next step is to traverse the path and remove concave points on this path. How to decide which point to remove and which to keep? Again, orientation helps here. The first two points in sorted array are always part of Convex Hull. For remaining points, we keep track of recent three points, and find the angle formed by them. Let the three points be prev(p), curr(c) and next(n). If orientation of these points (considering them in same order) is not counterclockwise, we discard c, otherwise we keep it. Following diagram shows step by step process of this phase.
Following is C++ implementation of the above algorithm.
CPP
// A C++ program to find convex hull of a set of points. Refer // for explanation of orientation() #include <iostream> #include <stack> #include <stdlib.h> using namespace std; struct Point { int x, y; }; // A global point needed for sorting points with reference // to the first point Used in compare function of qsort() Point p0; // A utility function to find next to top in a stack Point nextToTop(stack<Point> &S) { Point p = S.top(); S.pop(); Point res = S.top(); S.push(p); return res; } // A utility function to swap two points void swap(Point &p1, Point &p2) { Point temp = p1; p1 = p2; p2 = temp; } // A utility function to return square of distance // between p1 and p2 int distSq(Point p1, Point p2) { return (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y); } // To find orientation of ordered triplet (p, q, r). // The function returns following values // 0 --> p, q and r are collinear // 1 --> Clockwise // 2 --> Counterclockwise int orientation(Point p, Point q, Point r) { int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); if (val == 0) return 0; // collinear return (val > 0)? 1: 2; // clock or counterclock wise } // A function used by library function qsort() to sort an array of // points with respect to the first point int compare( const void *vp1, const void *vp2) { Point *p1 = (Point *)vp1; Point *p2 = (Point *)vp2; // Find orientation int o = orientation(p0, *p1, *p2); if (o == 0) return (distSq(p0, *p2) >= distSq(p0, *p1))? -1 : 1; return (o == 2)? -1: 1; } // Prints convex hull of a set of n points. void convexHull(Point points[], int n) { // Find the bottommost point int ymin = points[0].y, min = 0; for ( int i = 1; i < n; i++) { int y = points[i].y; // Pick the bottom-most or choose the left // most point in case of tie if ((y < ymin) || (ymin == y && points[i].x < points[min].x)) ymin = points[i].y, min = i; } // Place the bottom-most point at first position swap(points[0], points[min]); // Sort n-1 points with respect to the first point. // A point p1 comes before p2 in sorted output if p2 // has larger polar angle (in counterclockwise // direction) than p1 p0 = points[0]; qsort (&points[1], n-1, sizeof (Point), compare); // If two or more points make same angle with p0, // Remove all but the one that is farthest from p0 // Remember that, in above sorting, our criteria was // to keep the farthest point at the end when more than // one points have same angle. int m = 1; // Initialize size of modified array for ( int i=1; i<n; i++) { // Keep removing i while angle of i and i+1 is same // with respect to p0 while (i < n-1 && orientation(p0, points[i], points[i+1]) == 0) i++; points[m] = points[i]; m++; // Update size of modified array } // If modified array of points has less than 3 points, // convex hull is not possible if (m < 3) return ; // Create an empty stack and push first three points // to it. stack<Point> S; S.push(points[0]); S.push(points[1]); S.push(points[2]); // Process remaining n-3 points for ( int i = 3; i < m; i++) { // Keep removing top while the angle formed by // points next-to-top, top, and points[i] makes // a non-left turn while (S.size()>1 && orientation(nextToTop(S), S.top(), points[i]) != 2) S.pop(); S.push(points[i]); } // Now stack has the output points, print contents of stack while (!S.empty()) { Point p = S.top(); cout << "(" << p.x << ", " << p.y << ")" << endl; S.pop(); } } // Driver program to test above functions int main() { Point points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3}}; int n = sizeof (points)/ sizeof (points[0]); convexHull(points, n); return 0; } |
Java
// Java equivalent of the above code import java.util.*; // A Java program to find convex hull of a set of points. Refer // for explanation of orientation() public class ConvexHull { // Define Infinite (Using INT_MAX // caused overflow problems) static final int INF = 10000 ; // To find orientation of ordered triplet (p, q, r). // The function returns following values // 0 --> p, q and r are colinear // 1 --> Clockwise // 2 --> Counterclockwise static int orientation(Point p, Point q, Point r) { // for details of below formula. int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); if (val == 0 ) return 0 ; // colinear return (val > 0 )? 1 : 2 ; // clock or counterclock wise } // Prints convex hull of a set of n points. static void convexHull(Point points[], int n) { // There must be at least 3 points if (n < 3 ) return ; // Initialize Result Vector<Point> hull = new Vector<Point>(); // Find the leftmost point int l = 0 ; for ( int i = 1 ; i < n; i++) if (points[i].x < points[l].x) l = i; // Start from leftmost point, keep moving // counterclockwise until reach the start point // again. This loop runs O(h) times where h is // number of points in result or output. int p = l, q; do { // Add current point to result hull.add(points[p]); // Search for a point 'q' such that // orientation(p, x, q) is counterclockwise // for all points 'x'. The idea is to keep // track of last visited most counterclock- // wise point in q. If any point 'i' is more // counterclock-wise than q, then update q. q = (p + 1 ) % n; for ( int i = 0 ; i < n; i++) { // If i is more counterclockwise than // current q, then update q if (orientation(points[p], points[i], points[q]) == 2 ) q = i; } // Now q is the most counterclockwise with // respect to p. Set p as q for next iteration, // so that q is added to result 'hull' p = q; } while (p != l); // While we don't come to first // point // Print Result for ( int i = 0 ; i < hull.size(); i++) System.out.println( "(" + hull.get(i).x + ", " + hull.get(i).y + ")" ); } // Driver program to test above function public static void main(String[] args) { Point points[] = new Point[ 8 ]; points[ 0 ] = new Point( 0 , 3 ); points[ 1 ] = new Point( 1 , 1 ); points[ 2 ] = new Point( 2 , 2 ); points[ 3 ] = new Point( 4 , 4 ); points[ 4 ] = new Point( 0 , 0 ); points[ 5 ] = new Point( 1 , 2 ); points[ 6 ] = new Point( 3 , 1 ); points[ 7 ] = new Point( 3 , 3 ); int n = points.length; convexHull(points, n); } } //Point class to store points class Point { int x, y; Point() { x = 0 ; y = 0 ; } Point( int a, int b) { x = a; y = b; } }; |
Python3
# A Python3 program to find convex hull of a set of points. Refer # for explanation of orientation() from functools import cmp_to_key # A class used to store the x and y coordinates of points class Point: def __init__( self , x = None , y = None ): self .x = x self .y = y # A global point needed for sorting points with reference # to the first point p0 = Point( 0 , 0 ) # A utility function to find next to top in a stack def nextToTop(S): return S[ - 2 ] # A utility function to return square of distance # between p1 and p2 def distSq(p1, p2): return ((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)) # To find orientation of ordered triplet (p, q, r). # The function returns following values # 0 --> p, q and r are collinear # 1 --> Clockwise # 2 --> Counterclockwise def orientation(p, q, r): val = ((q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y)) if val = = 0 : return 0 # collinear elif val > 0 : return 1 # clock wise else : return 2 # counterclock wise # A function used by cmp_to_key function to sort an array of # points with respect to the first point def compare(p1, p2): # Find orientation o = orientation(p0, p1, p2) if o = = 0 : if distSq(p0, p2) > = distSq(p0, p1): return - 1 else : return 1 else : if o = = 2 : return - 1 else : return 1 # Prints convex hull of a set of n points. def convexHull(points, n): # Find the bottommost point ymin = points[ 0 ].y min = 0 for i in range ( 1 , n): y = points[i].y # Pick the bottom-most or choose the left # most point in case of tie if ((y < ymin) or (ymin = = y and points[i].x < points[ min ].x)): ymin = points[i].y min = i # Place the bottom-most point at first position points[ 0 ], points[ min ] = points[ min ], points[ 0 ] # Sort n-1 points with respect to the first point. # A point p1 comes before p2 in sorted output if p2 # has larger polar angle (in counterclockwise # direction) than p1 p0 = points[ 0 ] points = sorted (points, key = cmp_to_key(compare)) # If two or more points make same angle with p0, # Remove all but the one that is farthest from p0 # Remember that, in above sorting, our criteria was # to keep the farthest point at the end when more than # one points have same angle. m = 1 # Initialize size of modified array for i in range ( 1 , n): # Keep removing i while angle of i and i+1 is same # with respect to p0 while ((i < n - 1 ) and (orientation(p0, points[i], points[i + 1 ]) = = 0 )): i + = 1 points[m] = points[i] m + = 1 # Update size of modified array # If modified array of points has less than 3 points, # convex hull is not possible if m < 3 : return # Create an empty stack and push first three points # to it. S = [] S.append(points[ 0 ]) S.append(points[ 1 ]) S.append(points[ 2 ]) # Process remaining n-3 points for i in range ( 3 , m): # Keep removing top while the angle formed by # points next-to-top, top, and points[i] makes # a non-left turn while (( len (S) > 1 ) and (orientation(nextToTop(S), S[ - 1 ], points[i]) ! = 2 )): S.pop() S.append(points[i]) # Now stack has the output points, # print contents of stack while S: p = S[ - 1 ] print ( "(" + str (p.x) + ", " + str (p.y) + ")" ) S.pop() # Driver Code input_points = [( 0 , 3 ), ( 1 , 1 ), ( 2 , 2 ), ( 4 , 4 ), ( 0 , 0 ), ( 1 , 2 ), ( 3 , 1 ), ( 3 , 3 )] points = [] for point in input_points: points.append(Point(point[ 0 ], point[ 1 ])) n = len (points) convexHull(points, n) # This code is contributed by Kevin Joshi |
Javascript
// JavaScript program to find convex hull of a set of // points. Refer // for explanation of orientation() // A class used to store the x and y coordinates of points class Point { constructor(x = null , y = null ) { this .x = x; this .y = y; } } // A global point needed for sorting points with reference // to the first point let p0 = new Point(0, 0); // A utility function to find next to top in a stack function nextToTop(S) { return S[S.length - 2]; } // A utility function to return square of distance // between p1 and p2 function distSq(p1, p2) { return ((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); } // To find orientation of ordered triplet (p, q, r). // The function returns following values // 0 --> p, q and r are collinear // 1 --> Clockwise // 2 --> Counterclockwise function orientation(p, q, r) { let val = ((q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y)); if (val == 0) return 0; // collinear else if (val > 0) return 1; // clock wise else return 2; // counterclock wise } // A function used by cmp_to_key function to sort an array // of points with respect to the first point function compare(p1, p2) { // Find orientation let o = orientation(p0, p1, p2); if (o == 0) { if (distSq(p0, p2) >= distSq(p0, p1)) return -1; else return 1; } else { if (o == 2) return -1; else return 1; } } // Prints convex hull of a set of n points. function convexHull(points, n) { // Find the bottommost point let ymin = points[0].y; let min = 0; for ( var i = 1; i < n; i++) { let y = points[i].y; // Pick the bottom-most or choose the left // most point in case of tie if ((y < ymin) || ((ymin == y) && (points[i].x < points[min].x))) { ymin = points[i].y; min = i; } } // Place the bottom-most point at first position points[0], points[min] = points[min], points[0]; // Sort n-1 points with respect to the first point. // A point p1 comes before p2 in sorted output if p2 // has larger polar angle (in counterclockwise // direction) than p1 let p0 = points[0]; points.sort(compare); // If two or more points make same angle with p0, // Remove all but the one that is farthest from p0 // Remember that, in above sorting, our criteria was // to keep the farthest point at the end when more than // one points have same angle. let m = 1; // Initialize size of modified array for ( var i = 1; i < n; i++) { // Keep removing i while angle of i and i+1 is same // with respect to p0 while ((i < n - 1) && (orientation(p0, points[i], points[i + 1]) == 0)) i += 1; points[m] = points[i]; m += 1; // Update size of modified array } // If modified array of points has less than 3 points, // convex hull is not possible if (m < 3) return ; // Create an empty stack and push first three points // to it. let S = []; S.push(points[0]); S.push(points[1]); S.push(points[2]); // Process remaining n-3 points for ( var i = 3; i < m; i++) { // Keep removing top while the angle formed by // points next-to-top, top, and points[i] makes // a non-left turn while ( true ) { if (S.length < 2) break ; if (orientation(nextToTop(S), S[S.length - 1], points[i]) >= 2) break ; S.pop(); } S.push(points[i]); } // Now stack has the output points, // print contents of stack while (S.length > 0) { let p = S[S.length - 1]; console.log( "(" + p.x + ", " + p.y + ")" ); S.pop(); } } // Driver Code let points = [ new Point(0, 3), new Point(1, 1), new Point(2, 2), new Point(4, 4), new Point(0, 0), new Point(1, 2), new Point(3, 1), new Point(3, 3) ]; let n = points.length; convexHull(points, n); // This code is contributed by phasing17 |
C#
// C# equivalent of the above code using System; using System.Collections.Generic; public class ConvexHul { // Define Infinite (Using INT_MAX caused overflow problems) const int INF = 10000; // To find orientation of ordered triplet (p, q, r). The function returns following values 0 --> p, q and r are colinear 1 --> Clockwise 2 --> Counterclockwise static int Orientation(Point p, Point q, Point r) { // See https://www.geeksforgeeks.org/orientation-3-ordered-points/ for details of below formula. int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); if (val == 0) return 0; // colinear return (val > 0)? 1: 2; // clock or counterclock wise } // Prints convex hull of a set of n points. static void ConvexHull(Point[] points, int n) { // There must be at least 3 points if (n < 3) return ; // Initialize Result List<Point> hull = new List<Point>(); // Find the leftmost point int l = 0; for ( int i = 1; i < n; i++) if (points[i].x < points[l].x) l = i; // Start from leftmost point, keep moving counterclockwise until reach the start point again. This loop runs O(h) times where h is number of points in result or output. int p = l, q; do { // Add current point to result hull.Add(points[p]); // Search for a point 'q' such that orientation(p, x, q) is counterclockwise for all points 'x'. The idea is to keep track of last visited most counterclock- wise point in q. If any point 'i' is more counterclockwise than q, then update q. q = (p + 1) % n; for ( int i = 0; i < n; i++) { // If i is more counterclockwise than current q, then update q if (Orientation(points[p], points[i], points[q]) == 2) q = i; } // Now q is the most counterclockwise with respect to p. Set p as q for next iteration, so that q is added to result 'hull' p = q; } while (p != l); // While we don't come to first // point // Print Result foreach (Point i in hull) Console.WriteLine( "(" + i.x + ", " + i.y + ")" ); } // Driver program to test above function public static void Main(String[] args) { Point[] points = new Point[8]; points[0] = new Point(0, 3); points[1] = new Point(1, 1); points[2] = new Point(2, 2); points[3] = new Point(4, 4); points[4] = new Point(0, 0); points[5] = new Point(1, 2); points[6] = new Point(3, 1); points[7] = new Point(3, 3); int n = points.Length; ConvexHull(points, n); } } // Point class to store points public class Point { public int x, y; public Point() { x = 0; y = 0; } public Point( int a, int b) { x = a; y = b; } }; |
Output:
(0, 3) (4, 4) (3, 1) (0, 0)
Time Complexity: Let n be the number of input points. The algorithm takes O(nLogn) time if we use a O(nLogn) sorting algorithm.
The first step (finding the bottom-most point) takes O(n) time. The second step (sorting points) takes O(nLogn) time. The third step takes O(n) time. In the third step, every element is pushed and popped at most one time. So the sixth step to process points one by one takes O(n) time, assuming that the stack operations take O(1) time. Overall complexity is O(n) + O(nLogn) + O(n) + O(n) which is O(nLogn).
Auxiliary Space: O(n), as explicit stack is used, since no extra space has been taken.
References:
Introduction to Algorithms 3rd Edition by Clifford Stein, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
http://www.dcs.gla.ac.uk/~pat/52233/slides/Hull1x1.pdf
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
This article is contributed by Aarti_Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!