Professional Documents
Culture Documents
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
x1 y1 1
1
Det (1 2 3) x2 y2 1 +
2
x3 y3 1 (x3,y3)
(x2,y2)
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)
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)
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
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