You are on page 1of 16

‫בעיה ‪)Convex hull( 1‬‬

‫חישוב המעטפת הקמורה של קבוצת נקודות‬


‫• ניתן לחשוב על הקמור (מעטפת קמורה) כעל גומייה שנמתחה מסביב לקבוצת‬
‫נקודות כך שתקיף את כולם‪,‬‬
‫•שני אלגוריתמים ידועים למציאת קמור של אוסף נקודות במישור הדו ממדי הם‬
‫הסריקה של גראהם והצעדה של ג'ארביס‪.‬‬

‫‪http://www.cs.princeton.edu/courses/archive/fall08/cos226/demo/ah/GrahamScan.html‬‬
Convex hull definition

• Intuitive definition 1
• Given a set S = {p1, p2, …, pN} of points in the plane,
the convex hull H(S) is the smallest convex polygon in
the plane that contains all of the points of S.
Convex hull Example
• Example – Find the convex hull of the given set
of points
PN X Y
1 110 190
2 195 164
3 247 194
4 240 120
5 290 210
6 195 280
7 220 230
8 160 120
9 170 220
Algorithm 1 - principle

• Check every pair of points – line, if all the points


are right to this segment the pair of points are
in the convex hall.
Convex Hull – Naive Algorithm
1. For each pair of points construct its connecting segment
and supporting line.
2. Find all the segments that all the points are to the right
of them (below them).
3. Construct the convex hull out of these segments.
How to find that a point is above or below a line
segment?
(x1,y1)

x1 y1 1
1
Det (1  2  3)  x2 y2 1 +
2
x3 y3 1 (x3,y3)
(x2,y2)

• The sign of the determinant indicates the orientation of the points.


• Positive area  counterclockwise orientation  left turn.
• Negative area  clockwise orientation  right turn.
• Zero area  co-linearity, the points are on the same line.

x1 y1 1
1
Area _ of _ triangle   x2 y2 1
2
x3 y3 1
B

Example
(1138.40E,1072.18N)

(1000N,1000E) C
(1199.86E, 996.10N)

• Check that C (1199.86, 996.10) is to the right of points


A(1000,1000) and B(1138.40, 1072.18)
• Negative determinant means (clockwise orientation) that the
point is to the right

xA yA 1 1000 1000 1
1 1
Det ( A  B  C )  xB yB 1  1138.40 1072.18 1  7482.82
2 2
xC yC 1 1199 .86 996.10 1
B
Area of a triangle (1138.40E,1072.18N)

• Compute the area of the following


trianle using the given coordinates A
A(1000,1000), B(1138.40, 1072.18), C (1000N,1000E) C
(1199.86, 996.10) (1199.86E, 996.10N)

X Y
1000 1000
1138400 =1000×1138.4 1138.4 1072.18 1000×1072.18= 1072180
1286466 =1072.18×1199.86 1199.86 996.10 1138.4×996.10= 1133960.24
996100 =996.10×1000 1000 1000 1199.86×1000= 1199860

3420966 3406000.24

3420966 - 3406000.24
Area   7482.82
2
Convex Hull – Naive Algorithm
Complexity

• Time complexity:
– All pairs: n n(n  1)
O ( )  O( )  O(n 2 )
 2 2

– Check all points for each pair: O(n)


– Total: O(n3)
Convex Hull – Naive Algorithm
Degenerate cases

• What is a point is on the line?


• Rounding errors (not using floating point
coordinates) can create problems.
Graham scan Algorithm
Simplified version 1
• Incrementally add points
• Algorithm:
1. Sort the points according to their x coordinates (left to
right).
2. Work on upper hull (from left most point to right most point)
3. Start from p1 (left most) and p2
4. Add a point pi
5. If point p1 , p2 , and pi form a right turn add to Lupper
6. Else remove p2 from Lupper
7. Continue with 4 until end of file

http://www-ma2.upc.es/~geoc/CH_EN.pdf
Graham scan Algorithm
Graham scan Algorithm
Complexity
• Sorting – O(n log n)
• Each point is pushed on the stack L only once.
• If Di is number of points popped out of the stuck (L),
• Once a point is popped – it cannot be popped again.
• Di<n
• Hence O(n log n) + O(n log n) ~ O(n log n)
Quick Sort
Function quicksort (a, low, high )
if ( high > low )
pivot = partition( a, low, high )
quicksort ( a, low, pivot-1 )
quicksort ( a, pivot+1, high )
End Function
• a is the list of records,
• The partition subroutine examines every item in the array and
determine if it is bigger or smaller than the pivot by that divide
the given list into two
• The pivot is taken as the lowest element in the list.
Quicksort is a O(n logn) algorithm. However, in the worst case,
quicksort is an O(n2) algorithm! A better method is the HEAP
SORT
Jarvis' March Algorithm
• At each step, test each
of the points, and find
the one which makes
the largest right-hand
turn. That point has to
be the next one on
the hull.
• Complexity: O(n *n)

http://www-ma2.upc.es/~geoc/CH_EN.pdf
‫סטודנט מבצע תרגיל תכנות ב ‪#C‬‬

You might also like