convex hull algorithm analysis

4
Convex Hull Algorithm Analysis Rex Yuan March 29, 2015 Brute Force Approach The most naive brute force approach to the convex hull problem iterates over all points three time. First two iterations yield all possible lines formed by the points, and the last iteration checks all points against all lines. If a pair of point (p1,p2) satisfies the condition that all other points are in the same side from p1p2, we confirm that (p1,p2) are two of the most outer points, and thus are of the convex hull. Pseudo Code Algorithm 1 Convex Hull Brute Force Algorithm procedure ConvexHullBF(P oints) results = [] for all p1 in P oints do for all p2 in P oints do for all p3 in P oints do Currentside side point3 is on respecting p1p2 if side not defined then side Currentside else if Currentside 6= side then side F alse break end if end for if side 6= F alse then append p1 and p2 to results end if end for end for end procedure 1

Upload: chih-cheng-yuan

Post on 05-Aug-2015

50 views

Category:

Science


1 download

TRANSCRIPT

Page 1: Convex Hull Algorithm Analysis

Convex Hull Algorithm Analysis

Rex Yuan

March 29, 2015

Brute Force Approach

The most naive brute force approach to the convex hull problem iterates overall points three time. First two iterations yield all possible lines formed by thepoints, and the last iteration checks all points against all lines. If a pair of point(p1, p2) satisfies the condition that all other points are in the same side fromp1p2, we confirm that (p1, p2) are two of the most outer points, and thus are ofthe convex hull.

Pseudo Code

Algorithm 1 Convex Hull Brute Force Algorithm

procedure ConvexHullBF(Points)results = []for all p1 in Points do

for all p2 in Points dofor all p3 in Points do

Currentside ← side point3 is on respecting p1p2if side not defined then

side ← Currentsideelse if Currentside 6= side then

side ← Falsebreak

end ifend forif side 6= False then

append p1 and p2 to resultsend if

end forend for

end procedure

1

Page 2: Convex Hull Algorithm Analysis

Time Complexity

The worst total operations of naive brute force algorithm takes three iterationsover n point, which is approximately 3n3 which is O(n3).

Divide and Conquer Approach

The divide and conquer approach takes a collection of points, Points, and a pairof two points (Rp,Lp) which are the rightmost and leftmost points in Points,regarding x axis. Next, find the furthest point Furthestp from RpLp, anddivide Points into four parts with LpFurp and RpFurp. Then, recurse leftwith (Lp, Furp) and the left/down portion of Points and right with (Furp,Rp)and the right/up portion of Points.

Pseudo Code for Main Algorithm

Algorithm 2 Convex Hull Divide and Conquer Algorithm

results = []function DC(Rp,Lp, Points)

if Points is empty then . base casereturn . Θ(1)

end ifFurp ← Furp(RpLp, Points) . Θ(n)Lps,Rps ← LR(RpLp, FurpLp, RpFurp, Points) . Θ(n)append Furp to results . Θ(1)DC(Lp, Furp, Lps)DC(Furp,Rp,Rps)

end functionprocedure ConvexHullDC(results)

Rightp, Leftp ← rightmost and leftmost points in input, regarding x axisUps ← points in the upper sections of line RightpLeftpLos ← points in the Lower sections of line RightpLeftpDC(Rightp, Leftp, Ups)DC(Rightp, Leftp, Los)

end procedure

The algorithm(Furp) to calculate furthest point takes a line(p1p2) and a col-lection of point(ps), and then iterates through ps to find the point with furthestdistance from p1p2 in ps and return that point(result). The algorithm(LR)to find left/right sections takes three lines(p1p2, p1p3, p2p3) and a collectionof points(ps) and use the slope(m) to find the appropriate left/right sections.It then iterates over ps to find two collections of points(leftps, rightps), corre-sponding to left/right sections of ps and return leftps and rightps

2

Page 3: Convex Hull Algorithm Analysis

Pseudo Code for Helper Functions

Algorithm 3 Furthest Point in ps from p1p2

function Furp(p1p2, ps)result = None, furd = 0for all p in ps do

if Dist(p, p1p2) > furd thenfurd ← Dist(p, p1p2)result ← p

end ifend for

end function

Algorithm 4 Left and Right Sections of ps

function LR(p1p2, p1p3, p2p3, ps)leftps = []rightps = []left ← check the slope(m) of p1p2, p1p3, p2p3 to find out how is ps

divided, and then decide which is the left sideright ← check the slope(m) of p1p2, p1p3, p2p3 to find out how is ps

divided, and then decide which is the right sidefor all p in ps do

if p in left thenappend p to leftps

else if p in right thenappend p to rightps

end ifend forreturn leftps, rightps

end function

Divide and Conquer Approach Analysis

The recurrence of the worst case of the divide and conquer approach is

T (n) = 2T (n

2) +O(n)

because every recursion call takes about 2n+1 operations which is O(n), and forthe two subsequent recursion calls it makes, the worst case would be no pointswere eliminated in that call, so the sum of two subsequent inputs is still n. Anexample of this of this would be when input points form a circle, making thetwo subsequent input n

2 .According to Master Method, this is Case 1(a = bd). Thus, O(nlogn).

3

Page 4: Convex Hull Algorithm Analysis

Input points form a hollow circle:no points will be eliminated in all calls.

Data Structure

In the actual implementation, I used the dictionary type in Python. With keysrepresenting index number of a point and its content a tuple of the coordinatesof that point.

{index : (x coordinate, y coordinate)}

Run Time Comparison

I created the following table using UNIX POSIX time function and round themean time of 10 trials to five digits after decimal point to calculate the timepast for either brute force and divide and conquer method.

Table 1: Multi-column table

Run Time Comparison(s)Sample Brute Force Divide and Conquer

1 0.18544 0.001152 1.14565 0.002373 > 300 0.220484 > 300 24.31725

4