Convex Hull

Distribute Education like Computer Geek

Definition

The convex hull is the smallest convex polygon or shape that encloses a given set of points in a two-dimensional space. It can be visualized as the outer boundary formed by connecting the outermost points in such a way that there are no indentations or concave sections.

For example, imagine placing a rubber band around a group of points on a piece of paper. The convex hull is the shape formed by stretching the rubber band tightly around the outermost points, enclosing all the points within it. It represents the outermost “hull” or boundary of the points, ensuring that all points are inside or on the edge of the polygon.

Convex Hull

Types of Convex Hull
  1. Jarvis Algorithm (Gift wrapping algorithm)
  2. Graham Scan
  3. Quick Hull
  4. Chan’s Algorithm

1. Jarvis Algorithm (Gift wrapping algorithm)

The Jarvis algorithm, also known as the gift-wrapping algorithm, is a simple and natural algorithm used to find the convex hull of a set of points in a two-dimensional space. It is created independently by Chand & Kapur in 1970 but named after R. A. Jarvis, who developed the algorithm in 1973.

Convex Hull made by Jarvis Method or Gift wrapping method

Algorithm of Jarvis Method
  1. Select the leftmost point in the set of points as the starting point. If there are multiple leftmost points, choose the one with the lowest y-coordinate.
  2. Initialize an empty convex hull list to store the points that form the convex hull.
  3. Repeat the following steps until we reach the starting point again:
  4. Select the current point on the convex hull as the next point in the hull.
  5. Iterate through all the remaining points and find the point that forms the smallest counterclockwise angle (left most side) with the current point. This point will be the next point in the hull.
  6. Add the next point to the convex hull list.
  7. Return the convex hull list.

 

Source Code for Jarvis Method

2. Graham Scan

The Graham scan algorithm is a popular method for finding the convex hull of a set of points in a two-dimensional space. It was developed by Ronald Graham in 1972 and is known for its efficiency.

Algorithm of Graham scan
  1. Choose the point with the lowest y-coordinate. If multiple points have the same lowest y-coordinate, select the one with the leftmost x-coordinate. This point will be the starting point of the convex hull.
  2. Sort the remaining points in counterclockwise order based on their angle with the starting point. This can be achieved by calculating the polar angle of each point with respect to the starting point.
  3. Create an empty stack to hold the points that form the convex hull.
  4. Push the starting point and the first point from the sorted list onto the stack.
  5. Iterate through the sorted list of points:
  6. While the current point and the next point in the sorted list make a non-left turn or are collinear, pop the top point from the stack.
  7. Push the current point onto the stack.
  8. Return the points in the stack, which represent the vertices of the convex hull in counterclockwise order.
Working of Graham Scan

Graham's Scan method

  1. Choose the lowest y-coordinate. (0,0) and (6,0)
  2. If multiple coordinates are there, choose the leftmost x-coordinate. This will be our starting point. (0,0)

Graham's Scan method

  1. Sort the remaining points in counterclockwise order.

(0,0), (6,0), (9,5), (4,4), (7,9), (3,7), (1,6), (-1,6)

Graham's Scan method

  1. Push the starting point and the first point from the sorted list onto the stack.

Graham's Scan method

  1. Choose the next point in sorted list (9,5) onto the stack and check if new point (4,4) turns left or right from point (9,5) and (6,0).

(a) If turns right, then (4,4) is in the convex hull

(b) If turns left, then (9,5) is in the convex hull

Graham's Scan method

  1. Now, (4,4) is current point in the sorted list. Choose next point (7,9) onto the stack and check if new point (7,9) turns left or right from point (4,4) and (9,5).

(a) If turns right, then (7,9) is in the convex hull

(b) If turns left, then (4,4) is in the convex hull

Graham's Scan method

  1. Now, (7,9) is current point in the sorted list. Choose next point (3,7) in the sorted list onto the stack.

Convex Hull made by Graham's Scan method

  1. Choose the next point in sorted list (1,6) onto the stack and check if new point (1,6) turns left or right from point (3,7) and (7,9).

(a) If turns right, then (1,6) is in the convex hull

(b) If turns left, then (3,7) is in the convex hull

Convex Hull made by Graham's Scan method

  1. Now, (1,6) is current point in the sorted list. Choose next point (-1,6) onto the stack and check if new point (-1,6) turns left or right from point (1,6) and (3,7).

(a) If turns right, then (-1,6) is in the convex hull

(b) If turns left, then (1,6) is in the convex hull

Convex Hull made by Graham's Scan method

  1. There is a problem in three points (-1,6), (3,7) and (7,9). These three points are collinear. So, algorithm says that the middle point (3,7) should be discarded and (-1,6) and (7,9) remains in the stack.

Convex Hull made by Graham's Scan method

  1. We have used all the points in the sorted list, so Choose the starting point in (0,0) and check if line between (-1,6) to (0,0) point turns left or right from point (-1,6) and (7,9).

(a) If turns right, then (0,0), (6,0), (9,5) and (7,9) points are in the convex hull.

(b) If turns left, then (0,0), (6,0), (9,5), (-1,6) and (7,9) points are in the convex hull.

Convex hull made by Graham's Scan method

Iteration is complete, the hull contains the points of the convex hull in counterclockwise order (0,0), (6,0), (9,5), (-1,6) and (7,9).

The Graham scan algorithm effectively eliminates unnecessary points from the convex hull by checking for left turns. That is why it has a smaller time complexity.

The time complexity of the Graham scan algorithm is O(n log n), where n is the number of points in the input set.

 

Source Code for Graham Scan

  1. Quick Hull is a recursive algorithm that employs a divide and conquer approach. It starts by finding the leftmost and rightmost points and divides the points into two subsets based on the line connecting these extreme points. It recursively finds the convex hull for each subset and then merges them to obtain the final convex hull. Quick Hull has an average time complexity of O(n log n), but it can reach O(n^2) in the worst case.
  1. Chan’s algorithm combines the benefits of both Gift Wrapping and Graham’s Scan. It initially applies Gift Wrapping to obtain an initial convex hull and then uses Graham’s Scan with a smaller subset of points to improve the performance. Chan’s algorithm developed by Timothy M Chan has a time complexity of O(n log h), where n is the number of points and h is the number of points on the convex hull.
Applications of Convex Hull
  1. Computational Geometry
  2. Image Processing
  3. Robotics and Path Planning
  4. Geographic Information Systems (GIS)
  5. Pattern Recognition

Test Yourself

Q1- Explain the concept of the convex hull and its significance in computational geometry.

The convex hull is the smallest convex polygon that encloses a given set of points in a two-dimensional space.

It plays a crucial role in computational geometry as it helps identify the outermost boundary of a set of points, ensuring that all points are inside or on the edge of the polygon.

The convex hull is widely used in various algorithms and applications, such as collision detection, shape recognition, and path planning.

Q2- Compare and contrast the Jarvis Algorithm and Graham Scan for finding the convex hull of a set of points.

Both the Jarvis Algorithm and Graham Scan are methods for finding the convex hull of a set of points. The main difference lies in their approaches. The Jarvis Algorithm (Gift Wrapping) is simpler to implement and finds the convex hull by repeatedly selecting the next point with the smallest counterclockwise angle with the current point. On the other hand, Graham Scan is more efficient, using a divide-and-conquer approach to find the convex hull by sorting points based on their polar angles and then merging the subsets.

Q3- Discuss the time complexity of the Graham Scan algorithm and its implications on the efficiency of finding the convex hull.

The Graham Scan algorithm has an average time complexity of O(n log n), where n is the number of points in the input set. This time complexity is due to the sorting step, which dominates the overall complexity. The efficient time complexity makes Graham Scan suitable for large datasets, making it a popular choice for finding the convex hull.

Q4- Explain the divide-and-conquer strategy employed in the Quick Hull algorithm to find the convex hull.

The Quick Hull algorithm uses a divide-and-conquer strategy to find the convex hull of a set of points.

It starts by identifying the leftmost and rightmost points and divides the points into two subsets based on the line connecting these extreme points.

It then recursively finds the convex hull for each subset and merges the results to obtain the final convex hull. The algorithm divides the problem into smaller subproblems, making it more efficient for larger datasets.

Q5- Compare the performance of Chan’s Algorithm to the other convex hull algorithms and explain its advantage.

Chan’s Algorithm combines the benefits of Gift Wrapping and Graham Scan algorithms.

It uses Gift Wrapping to obtain an initial convex hull and then applies Graham Scan with a smaller subset of points to further improve efficiency.

Chan’s Algorithm has a time complexity of O(n log h), where h is the number of points on the convex hull. This makes it more efficient than other algorithms, especially when dealing with datasets containing a large number of points on the convex hull.

Q6- Explain the significance of the convex hull in robotics and its applications in path planning.

In robotics, the convex hull is crucial for path planning as it defines the safe and accessible regions of the robot’s workspace.

By identifying the convex hull, a robot can determine the outer boundary of obstacles and plan collision-free paths. Convex hull-based algorithms enable efficient path planning, which is essential for autonomous navigation and robotic tasks.

Q7- How does the convex hull find applications in geographic information systems (GIS)?

In GIS, the convex hull is used to determine the boundaries of geographic regions or sets of points. It helps simplify complex geographic data by identifying the outermost boundary of a collection of points or regions. Convex hull-based algorithms are applied in GIS for tasks such as regional analysis, boundary delineation, and spatial data simplification.

Q8- Discuss the role of the convex hull in image processing and pattern recognition applications.

In image processing and pattern recognition, the convex hull plays a vital role in object detection and shape analysis.

By finding the convex hull of objects or regions in images, algorithms can distinguish between different shapes and identify their outer boundaries.

Convex hull-based techniques are used in various applications, such as object recognition, fingerprint analysis, and character recognition.

Q9- The time complexity of the Graham Scan algorithm is:
  1. O(n)
  2. O(n log n)
  3. O(n^2)
  4. O(log n)

Ans – (2)

Explanation – The Graham Scan algorithm has an average time complexity of O(n log n), where n is the number of points.

Q10- In the Jarvis Algorithm, the next point in the convex hull is selected based on:
  1. Smallest clockwise angle
  2. Smallest counterclockwise angle
  3. Smallest Euclidean distance
  4. Smallest Manhattan distance

Ans – (2)

Explanation –  The Jarvis Algorithm selects the next point in the convex hull based on the smallest counterclockwise angle with the current point.

Q11- Chan’s Algorithm is a combination of which two convex hull algorithms?
  1. Jarvis Algorithm and Graham Scan
  2. Quick Hull and Graham Scan
  3. Gift Wrapping and Quick Hull
  4. Gift Wrapping and Graham Scan

Ans – (4)

Explanation – Chan’s Algorithm combines the benefits of Gift Wrapping and Graham Scan algorithms.

Q12- Which algorithm employs sorting based on polar angles to find the convex hull?
  1. Jarvis Algorithm
  2. Graham Scan
  3. Quick Hull
  4. Chan’s Algorithm

Ans – (2)

Explanation –  Graham Scan algorithm uses sorting based on polar angles to find the convex hull.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha

Â