Divide and Conquer - Continued
Divide and Conquer - Continued
Given a set of n points you want to find a minimum cover of those points such that no point lies
outside the cover. It will be the smallest polygon such that all possible line segments between the
points should fall within that polygon.
- If you draw a line segment connecting any two points, the line segment divides the plane
into two parts
- All the other points in the given set can either be on
o one side of the plane
o on the other side
o on both the sides
- Any line segment connecting any two points will be an edge of a convex hull if
o all other points lie on the same side
o if not, that cannot be the boundary of the cover that we are looking for
- Brute force
o Try to form all possible line segments between all the points in the set
o Check as to which side of the plane do all the other points lie with respect to each of
these line segments
o All those line segments for which the remaining points fall on the same side of the
plane => you can add them to the set of edges of the convex hull
o Connect all such edges => you get the convex hull
- Time complexity
o Given n points, we can have n*(n-1)/2 possible line segments between them
o You have to compare each of these n*(n-1)/2 line segments against the remaining
(n-2) points in each case => O(n3)
There are also other brute force ways to solve the problem. Observe the following diagram
You have seven points in the given set {A, B, C, D, E, F, G}. You can find the convex hull by finding all
possible triangles between any three points in the set. i.e. nC3 combinations of triangles can be
formed
n∗(n−1)∗(n−2)∗(n−3)!
( n−3 ) !∗3!
n∗(n−1)∗(n−2)
6
We have 7 points and therefore we can find 35 triangles between the given points as shown above.
They are listed below
ADG BEG
- Notice that all these triangles are formed with at least one of the points inside the actual
convex hull we want to find
- So convex hull points i.e., those points which should be connected to form the edges of the
convex hull will not lie inside any of these triangles
- By these observations, solution to find convex hull becomes like this
o For all possible triangles that can be formed
For each point in your given set, check if it lies inside the triangle or not
If at least one point in the given set lies inside the triangle, we know it is a
convex hull boundary
If no point is inside the triangle, it means it will not be a convex hull
boundary
Order of time complexity for finding the convex hull
= Order of time complexity for finding number of triangles * Order of time complexity for comparing
each point in the set against all these triangles
n∗(n−1)∗(n−2)
*n
6
This can be significantly reduced if you used divide and conquer approach. For the given set S of n
points {A, B, C, D, E, F, G} divide the set into two subsets S1 and S2.
The division should be such that all points in S1 are to the left of all points in the set S2. Now
suppose you are able to find the convex hull for S1 as C1 and for S2 as C2 through some method as
shown below. Now to find the full convex hull C_Full we have to merge the convex hulls C1 and C2
by finding the upper and lower tangents T1 and T2.
A T1
C1 G C2
E C_Full C
T2
- Sort the given set of points in the ascending order of x-axis. The point with the smallest x-
coordinate will be on the left and the point with the largest x-coordinate will be on the right
- Divide the array of points S into two parts S1 and S2. Now you have all points in S1 less than
all points in S2 along the x-axis
- Find the convex hull of S1 points and S2 points individually which are C1 and C2
- Find the upper and lower tangents T1 and T2 to merge C1 and C2 to C_full.
Divide step: Sort the points and divide into two sets S1 and S2. Time complexity – nlogn for sorting
and constant time for dividing
Conquer step: Recursively call the same function so that the smaller sets S1 and S2 become smaller
and smaller until only single point is left which is the base case. In base case, the same point is
returned as the left and right part. Time complexity nlogn for this recursive call
Merge: Find the upper and lower tangents using a merge function. The algorithm for merge will be
like
- Find the right-most point p1 of the left convex hull C1 and left most point p2 of right convex
hull C2
- Create two copies of p1 and p2 so that you have two versions of p1 and p2 points.
- Raise the first copy of p1 and p2 in order to get the upper tangent T1
- Lower the second copy of p1 and p2 in order to get the lower tangent T2
Initially
Next for upper tangent – keep rotating the q point clockwise as long as the line segment moves to
the left. When it moves right after some point you can stop there
Now you got the upper tangent
- Next keep q fixed and keep rotating p anticlockwise as long as the line turns left. If at some
point it turns right, stop there.
You obtained the lower tangent now since both the points cannot be lowered any further.