Convex Hull Algorithm Analysis
Rex Yuan
March 29, 2015
Brute Force Approach
The most naive brute force approach to the con...
Time Complexity
The worst total operations of naive brute force algorithm takes three iterations
over n point, which is ap...
Pseudo Code for Helper Functions
Algorithm 3 Furthest Point in ps from p1p2
function Furp(p1p2, ps)
result = None, furd = ...
Input points form a hollow circle:no points will be eliminated in all calls.
Data Structure
In the actual implementation, ...
Prochain SlideShare
Chargement dans…5
×

Convex Hull Algorithm Analysis

356 vues

Publié le

A Convex Hull Algorithm Running Time Analysis

Publié dans : Sciences
  • Soyez le premier à commenter

  • Soyez le premier à aimer ceci

Convex Hull Algorithm Analysis

  1. 1. 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(Points) results = [] for all p1 in Points do for all p2 in Points do for all p3 in Points do Currentside ← side point3 is on respecting p1p2 if side not defined then side ← Currentside else if Currentside = side then side ← False break end if end for if side = False then append p1 and p2 to results end if end for end for end procedure 1
  2. 2. Time Complexity The worst total operations of naive brute force algorithm takes three iterations over 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 pair of two points (Rp, Lp) which are the rightmost and leftmost points in Points, regarding x axis. Next, find the furthest point Furthestp from RpLp, and divide Points into four parts with LpFurp and RpFurp. Then, recurse left with (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 case return Θ(1) end if Furp ← 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 function procedure ConvexHullDC(results) Rightp, Leftp ← rightmost and leftmost points in input, regarding x axis Ups ← points in the upper sections of line RightpLeftp Los ← points in the Lower sections of line RightpLeftp DC(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 furthest distance 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 collection of 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
  3. 3. Pseudo Code for Helper Functions Algorithm 3 Furthest Point in ps from p1p2 function Furp(p1p2, ps) result = None, furd = 0 for all p in ps do if Dist(p, p1p2) > furd then furd ← Dist(p, p1p2) result ← p end if end 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 side right ← check the slope(m) of p1p2, p1p3, p2p3 to find out how is ps divided, and then decide which is the right side for all p in ps do if p in left then append p to leftps else if p in right then append p to rightps end if end for return 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 for the two subsequent recursion calls it makes, the worst case would be no points were eliminated in that call, so the sum of two subsequent inputs is still n. An example of this of this would be when input points form a circle, making the two subsequent input n 2 . According to Master Method, this is Case 1(a = bd ). Thus, O(nlogn). 3
  4. 4. 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 keys representing index number of a point and its content a tuple of the coordinates of that point. {index : (x coordinate, y coordinate)} Run Time Comparison I created the following table using UNIX POSIX time function and round the mean time of 10 trials to five digits after decimal point to calculate the time past 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.00115 2 1.14565 0.00237 3 > 300 0.22048 4 > 300 24.31725 4

×