Finding smallest polygon covering a set of points in a grid
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In computational geometry, finding the smallest polygon that can cover a set of points in a grid, also known as the "polygon covering problem," is an important problem with applications in fields such as computer graphics, geography, and data analysis. This article explores methods for solving this problem, technical explanations of the algorithms involved, and relevant examples to clarify concepts.
Problem Definition
Given a set of points in a 2D grid, the goal is to determine the smallest polygon that encloses all the given points. The polygon must be convex, and the vertices of this polygon must be among the set of given points. This problem is a classic instance of computing the Convex Hull.
Convex Hull
The Convex Hull of a set of points in a plane is the smallest convex polygon that encloses all of the points. Imagine stretching a rubber band around the outer points; when released, it will form the convex hull. Finding the convex hull is a well-studied problem with practical algorithms dedicated to computing it efficiently.
Algorithms for Finding the Convex Hull
Several algorithms can be used to find the convex hull:
- Graham's Scan: • Complexity: • Sequence of steps: • Choose a point with the lowest y-coordinate as the pivot. • Sort remaining points based on the polar angle with respect to the pivot. • Traverse sorted points, ensuring that path makes only left turns, using a stack data structure to maintain vertices of the hull.
- Jarvis March (Gift Wrapping): • Complexity: , where is the number of vertices in the hull. • Works well with small numbers of hull vertices. • Start from the leftmost point and wrap points around the set.
- QuickHull: • Complexity: Average case , worst case . • Similar to QuickSort, recursively find "extreme" points on one side of the initial line connecting most distant points.
- Divide and Conquer: • Complexity: • Divide points into halves, find hulls for both sets recursively, and then merge the results.
Comparing Algorithms
| Algorithm | Complexity | Suitable For |
| Graham's Scan | General-purpose, large datasets | |
| Jarvis March | Small number of hull points | |
| QuickHull | Avg. | Randomly distributed points |
| Divide and Conquer | Parallel processing, large datasets |
Key Concepts
Left Turn Test
Determining the direction of a turn formed by three points is crucial for algorithms like Graham's Scan. This can be calculated using the cross product:
Given three points , , , the cross product is:
• If : Left turn • If : Right turn • If : Collinear
Optimizations
• Pre-processing: Eliminate duplicate points or points within the convex hull. • Parallelization: Divide and conquer algorithm can be improved by parallel processing for massive datasets.
Practical Example
Consider a set of points on a 2D grid:
Points:
By applying Graham's Scan:
- Select point as an anchor point.
- Sort points by polar angle with respect to .
- Process points: Maintain a stack of convex hull vertices.
Final convex hull points:
Conclusion
Finding the smallest polygon that covers a set of points in a grid is a fundamental problem in computational geometry. By leveraging efficient algorithms, we can solve this problem even for large datasets. Understanding the underlying principles such as left-turn tests and utilizing computational optimizations can further enhance performance. This smallest enclosing polygon, the convex hull, has significant applications, benefiting multiple computational fields.
Related reading
- Finding sorted sub-sequences in a permutation
- Finding square root without using sqrt function?
- Finding Strongly Connected Components in a graph through DFS
- Finding subset with max/min set bits under XOR
- finding smallest scale factor to get each number within one tenth of a whole number from a set of doubles
- Finding the best trade-off point on a curve
- Finding Sum Of The Differences OF MAX and MIN of All Possible Subsets
- Finding the closest number that factors given a list of primes

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.