0% found this document useful (0 votes)
73 views6 pages

Divide and Conquer - Continued

The document discusses two approaches to finding the convex hull of a set of points: 1. A brute force approach that checks all possible line segments between points, having O(n3) time complexity. 2. A divide and conquer approach that recursively divides the point set into halves, finds the convex hulls of the subsets and merges them by finding upper and lower tangents, with overall O(nlogn) time complexity. This approach first sorts points, then recursively computes convex hulls on halves until single points remain, and finally merges by rotating points to find tangent lines.

Uploaded by

S U B R O 0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views6 pages

Divide and Conquer - Continued

The document discusses two approaches to finding the convex hull of a set of points: 1. A brute force approach that checks all possible line segments between points, having O(n3) time complexity. 2. A divide and conquer approach that recursively divides the point set into halves, finds the convex hulls of the subsets and merges them by finding upper and lower tangents, with overall O(nlogn) time complexity. This approach first sorts points, then recursively computes convex hulls on halves until single points remain, and finally merges by rotating points to find tangent lines.

Uploaded by

S U B R O 0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Divide and conquer – More problems

Remember the convex hull problem we saw earlier

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.

We used one brute force technique based on the observations below

- 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

ABC AEF BFG

ABD AEG CDE

ABE AFG CDF

ABF BCD CDG

ABG BCE CEF

ACD BCF CEG

ACE BCG CFG

ACF BDE DEF

ACG BDF DEG

ADE BDG DFG

ADF BEF EFG

ADG BEG

Intuition for finding the convex hull

- 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 will be O(n4).

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.

- S1: Set of all points on the left side


- S2: Set of all points on the right side

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

So the algorithm will be

- 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

Illustration of this merge algorithm

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

Similarly do for getting the lower tangent.


- Keep the p fixed and keep rotating the q anticlockwise as long as the line turns right. If it
turns left at some point stop there.

- 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.

The time complexity of this merge operation is n.


Total time complexity of convex hull problem will be in the order of O(nlogn) – Why?
T(n) = 2*T(n/2) +n, apply Master theorem and verify.

You might also like