Convex Hull

advertisement
Convex Hull in Two
Dimensions
Jyun-Ming Chen
Refs: deBerg et al. (Chap. 1)
O’Rourke (Chap. 3)
1
Outline
Definitions of CH
Algorithms for CH
Analysis of algorithms
2
Convex Hull
Definition of
convexity
凸包(Convex Hull)
Convex hull of a
point set S, CH(S)


Smallest convex set
containing S
Intersection of all
convex sets
containing S
3
Convex Hull
Why computing CH?

Many reasons; e.g., faster collision
detection
How to compute CH?

Good solutions come from:
 A thorough understanding of the geometric
properties of the problem
 Proper application of algorithmic techniques
and data structures
4
General Steps
Sketch your problem in general position
Observe the desired output and reason
about the possible steps to get to the
goal
Check for degeneracy
Time/resource complexity


General case
Best/worst cases
5
Computing CH
… of a finite set P of
n points in 2D
Geometric intuition:
Rubberband analogy
Representation:


Input: point set
Output: CH(P): CWordered point set
6
Observation
Def: convex edges
Idea:


Search through all
pairs of points for
convex edges
Form a loop
7
Naïve Algorithm
You can add a
break here if you
like …
8
Analysis of Naïve Algorithm
Assumption: points are in general position


(usually this is done first to simplify the code)
(consider the degenerate cases later)
How to compute “lie to the left of directed
line”?
Complexity analysis: O(n3)

(n2–n)/2 pairs, each with n–2 other points to
examine
High complexity due to:

simply translate geometric insight into an algorithm
in a brute-force manner
9
Considering Degeneracy
Collinearity:


Modify the test to “all points to the right,
or on the line”
Does this solve the problem?!
10
Degenerate Case (cont)
Consider numerical inaccuracy
Two possibilities:
11
Jarvis’ March (Gift Wrapping)
A modification of naïve algorithm
Start at some extreme point, which is
guaranteed to be on the hull.
At each step, test each of the points,
and find the one which makes the
smallest right-hand turn. That point has
to be the next one on the hull.
The final CW loop is the CH.
12
Jarvis’ March (cont)
Complexity

Crude count: O(n2)
Output sensitive

O(nh), h:# of
segments on the hull
Worst case
13
A Better Algorithm
Sort by x-coord

The extreme points
must be on the CH
Goal: in 2 linked lists


Upper hull: p1 to pn
Lower hull: pn to p1
Idea:


In CW manner, always
making right turns
If fail to turn right,
delete previous point
until the turn is correct.
14
15
Step-by-step
7
3
5
9
1
6
2
4
8
[1,2]
16
Step-by-step
7
3
5
9
1
6
2
4
8
[1,2,3]
17
Step-by-step
7
3
5
9
1
6
2
4
8
[1,3]
18
Step-by-step
7
3
5
9
1
6
2
4
8
[1,3,4]
19
Step-by-step
7
3
5
9
1
6
2
4
8
[1,3,4,5]
20
Step-by-step
7
3
5
9
1
6
2
4
8
[1,3,5]
21
Step-by-step
7
3
5
9
1
6
2
4
8
[1,3,5,6]
22
Step-by-step
7
3
5
9
1
6
2
4
8
[1,3,5,6,7]
23
Step-by-step
7
3
5
9
1
6
2
4
8
[1,3,5,7]
24
Step-by-step
7
3
5
9
1
6
2
4
8
[1,3,7]
25
Step-by-step
7
3
5
9
1
6
2
4
8
[1,3,7,8]
26
Step-by-step
7
3
5
9
1
6
2
4
8
[1,3,7,8,9]
27
Step-by-step
7
3
5
9
1
6
2
4
8
[1,3,7,9]
Upper hull completed
28
Algorithm (cont)
This is a variation of Graham Scan
Proof of correctness: p.8
Complexity analysis: O(n log n)


Lexicographical sorting of points: O(n log n)
Computation of lower hull: O(n)
 Consider the for and while
29
Consider Degeneracy Again …
Effects on sorting

Lexico. ordering
Effects on making
turns…
30
Other Version of Graham Scan
1. Find an extreme point. This point will be the pivot,
2.
3.
is guaranteed to be on the hull, and is chosen to be
the point with smallest y coordinate.
Sort the points in order of increasing angle about
the pivot. We end up with a star-shaped polygon
(one in which one special point, in this case the
pivot, can "see" the whole polygon).
Build the hull, by marching around the star-shaped
poly, adding edges when we make a left turn, and
back-tracking when we make a right turn.
31
Graham Scan (ver2)
Also handles collinearity
32
Incremental Algorithm
Suitable for dynamic
CGeom and 3D hull
If pnCHn-1,
CHn=CHn-1
Else
Find two tangents,
update CHn
33
Incremental Algorithm
Input: a set of points P in 2D
Output: a list L containing the points of CH(P) in CCW
order
1. Take first three points of P to form a triangle
2. For i = 4 to n


If (pi is in CHi-1) do nothing
Else
pi
t1
 FindTangent (pi, CHi-1)t1, t2
t2
CHi-1
 Replace the items {t2 … t1} in L by {t2, pi, t1}
34
FindTangent (p, CH)
(Go through all points in CH circularly)
For each point pi
If XOR (p is left_or_on (pi-1,pi), p is
left_or_on(pi,pi+1))

Mark pi as the tangent point
pi
(There will be two tangent points)
Determine t1, t2
t1
CHi-1
t2
35
Incremental Algorithm (cont)
Finding Tangents …
Analysis
36
Quick Hull
Discard points in the
extreme quadrilateral
37
Quick Hull (cont)
Time complexity:


Find extreme point c: O(n)
Cost of recursive call:
 T(n) = O(n) + T(|A|) + T(|B|)

Best case: |A| = |B| = n/2
 T(n) = 2T(n/2) + O(n); T(n) = O(n log n)

Worst case: |A| = 1, |B| = n-1
 T(n) = T(n-1) + O(n); T(n) = O(n2)
38
Divide and Conquer Algorithm
Computing 3D hull:

Graham scan
(probably not
∵angle sort)
Preparata and Hong
(1977)
a-1 & a+1 at left side of ab
b-1 & b+1 at left side of ab
T(n) = 2T(n/2) + O(n); T(n) = O(n log n)
39
Extend to 3D
40
Floating Point Inaccuracies
Handled by
Interval arithmetic
Exact (rational) arithmetic
41
Interval arithmetic
Ref:
http://www.eng.mu.edu/corlissg/VC02/READ_ME.html
42
Exact Math
Rational arithmetic (never floating point)


In[6]:= 1/12 - 7/9 + 31/36
Out[6]= - 1/6
The Exact Arithmetic Competition: Level
0 Tests
Ref: XR
43
Exercise
From this applet, develop a
Graham scan algorithm that
returns a CW-ordered convex
hull


Express the algorithm in
pseudocode. Make sure it works
for general position and
degenerate cases (shown right)
Run your example on test cases
(general position and
degeneracies)
Explain the correctness and time complexity of
divide-and-conquer algorithm
44
T ( n )  2T ( n / 2 )  cn
T ( n )  T ( n  1)  cn
T ( n  1)  T ( n  2 )  c ( n  1 )
n  2 ( k  log
k
T ( 2 )  2T ( 2
k
T (2
k 1
k 1

T (2
k 2
k 2
)  2T ( 2

T ( 2 )  2 2T ( 2
k
2
T ( n )  T ( n  2 )  c ( n  1)  cn
)  c2
)  2T ( 2
T ( 2 )  2 2T ( 2
k
n)
2
k 2
k
k 3
k 1
)  c2
)  c2
k 3
 T ( n  3 )  c ( n  2 )  c ( n  1)  cn
k 1
)  c2
2
 c 2
)  c2
   c 1  2   n   O ( n )
k
 2 T (2
2
k 2
)  2c 2
k
k 2
k 2
 2c 2
k
 2 T (2
3
k 3
)  3c 2
k

T (2 )  2 T (2
k
m
k m
mk
)  mc 2  
  T ( 2 )  2  kc 2
k
k
k
k
 T ( n )  n  cn log n  O ( n log n )
45
More reference on the web.
46
Download