VOOZH about

URL: https://www.geeksforgeeks.org/dsa/convex-hull-using-graham-scan/

⇱ Convex Hull using Graham Scan - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Convex Hull using Graham Scan

Last Updated : 23 Jul, 2025

A convex hull is the smallest convex polygon that contains a given set of points. It is a useful concept in computational geometry and has applications in various fields such as computer graphics, image processing, and collision detection.

A convex polygon is a polygon in which all interior angles are less than 180degrees. A convex hull can be constructed for any set of points, regardless of their arrangement.

👁 Image
Convex Hull


Examples

Input: points[][] = [ [0, 0], [1, -4], [-1, -5], [-5, -3], [-3, -1], [-1, -3],
[-2, -2], [-1, -1], [-2, -1], [-1, 1]]
Output: [[-5, -3], [-1, 1], [0, 0], [1, -4], [-1, -5]
Explantation: The figure below shows the points of a convex polygon. These points define the boundary of the polygon.

👁 22

Approach:

Prerequisite: How to check if two given line segments intersect?

The Graham scan algorithm is a simple and efficient algorithm for computing the convex hull of a set of points. It works by iteratively adding points to the convex hull until all points have been added.

  • The algorithm starts by finding the point with the smallest y-coordinate. This point is always on the convex hull. The algorithm then sorts the remaining points by their polar angle with respect to the starting point.
  • The algorithm then iteratively adds points to the convex hull. At each step, the algorithm checks whether the last two points added to the convex hull form a right turn. If they do, then the last point is removed from the convex hull. Otherwise, the next point in the sorted list is added to the convex hull.

Step by Step Approach

Phase 1 (Sort points): The first step of the Graham Scan algorithm is to sort the points by their polar angle relative to the starting point. After sorting, the starting point is added to the convex hull, and the sorted points form a simple closed path.

👁 Convex-Hull-using-Graham-Scan-1

Phase 2 (Accept or Reject Points): After forming the closed path, we traverse it to remove concave points. Using orientation, we keep the first two points and check the next point by considering the last three points be prev(p)curr(c) and next(n). If the angle formed by these three points is not counterclockwise, we discard (reject) the current point, otherwise, we keep (accept) it.

👁 Convex-Hull-using-Graham-Scan-2

Output
-1 -5
1 -4
0 0
-3 -1
-5 -3

Time Complexity: O(n log n), for finding the bottom-most point takes O(n), sorting the points takes O(n log n), and building the hull through stack operations takes O(n).
Space Complexity: O(n), due to the stack used for storing the points during the hull construction, with no significant additional space required.

Applications of Convex Hulls:

Convex hulls have a wide range of applications, including:

  • Collision detection: Convex hulls can be used to quickly determine whether two objects are colliding. This is useful in computer graphics and physics simulations.
  • Image processing: Convex hulls can be used to segment objects in images. This is useful for tasks such as object recognition and tracking.
  • Computational geometry: Convex hulls are used in a variety of computational geometry algorithms, such as finding the closest pair of points and computing the diameter of a set of points.
Comment