Title: Today
1Todays Material
- Computational Geometry Problems
- Closest Pair Problem
- Convex Hull
- Jarviss March, Grahams scan
- Farthest Point Problem
2Closest pair problem
- Given a set of points on the plane, find the two
points whose distance from each other is the
maximum - Whats the brute-force solution?
- An efficient divide-and-conquer solution?
3Convex Hull (CH)
- Convex Hull (CH) of a set Q of points is the
smallest convex polygon P, for which each point
in Q is either on the boundary of P or in its
interior
4Convex Hull Jarviss March
p10
p12
p11
p6
p9
p7
p5
p8
p4
p3
p2
p1
p0
- Take the horizontal line going through p0, and
gift wrap it around the remaining points by
turning the wrap to the left. - The first point touched is the next point on CH
- In this example, it is p1
5Convex Hull Jarviss March
p10
p12
p11
p6
p9
p7
p5
p8
p4
p3
p2
p1
p0
- Continue gift-wrapping from p1. Next point is p3
6Convex Hull Jarviss March
p10
p12
p11
p6
p9
p7
p5
p8
p4
p3
p2
p1
p0
- Continue gift-wrapping from p3. Next point is p10
7Convex Hull Jarviss March
p10
p12
p11
p6
p9
p7
p5
p8
p4
p3
p2
p1
p0
- Continue gift-wrapping from p10. Next point is p12
8Convex Hull Jarviss March
p10
p12
p11
p6
p9
p7
p5
p8
p4
p3
p2
p1
p0
- Continue gift-wrapping from p12. Next point is p0
9Convex Hull Jarviss March
p10
p12
p11
p6
p9
p7
p5
p8
p4
p3
p2
p1
p0
- Continue gift-wrapping from p12. Next point is
p0. We are done now.
10Jarviss March Running Time
- O(nh), where h is the number of points on the
convex hull - If h is O(logn), then we get a running time of
O(nlogn) - If h is O(n), then we a quadratic running time of
O(n2) - Can we make sure that the running time is always
O(nlogn)? - Grahams scan
11Grahams Scan
- Let p0 be the point in Q with the min
y-coordinate or the leftmost such point in the
case of a tie - Let ltp1, p2, .., pmgt be the remaining points in Q
sorted by polar angle in counter-clockwise order
around p0. If more than one point has the same
angle, remove all but the one that is farthest
from p0 -
12Grahams Scan
- Maintain a stack S of candidate points
- Each point of the input set Q is pushed once onto
the stack, and the points that are not vertices
of CH(Q) are eventually popped off the stack - When the algorithm terminates, S contains exactly
the vertices of CH(Q) in counter-clockwise order
of their appearance on the boundary -
13Grahams Scan - Algorithm
- Push(p0, S)
- Push(p1, S)
- Push(p2, S)
- for i3 to m do
- while the angle formed by points
NEXT_TO_TOP(S), TOP(S) and pi - makes a non-left turn do
- Pop(S)
- endwhile
- Push(pi, S)
- endfor
- return S
14Convex Hull Initial State
p10
p11
p6
p9
p7
p12
p8
p5
p4
p3
p2
p1
p2
p0
p1
p0
Stack
15Convex Hull p1p2p3
p10
p11
p6
p9
p7
p12
p8
p5
p4
p3
p2
p1
p2
p0
p1
p0
Stack
p1p2p3 is a right turn. Pop p2, push p3
16Convex Hull p1p3p4
p10
p11
p6
p9
p7
p12
p8
p5
p4
p3
p2
p1
p3
p0
p1
p0
Stack
p1p3p4 is a left turn. Push p4
17Convex Hull p3p4p5
p10
p11
p6
p9
p7
p12
p8
p5
p4
p3
p2
p1
p4
p3
p0
p1
p0
Stack
p3p4p5 is a right turn. Pop p4, push p5
18Convex Hull p3p5p6
p10
p11
p6
p9
p7
p12
p8
p5
p4
p3
p2
p1
p5
p3
p0
p1
p0
Stack
p3p5p6 is a left turn. Push p6
19Convex Hull p5p6p7
p10
p11
p6
p9
p7
p12
p8
p5
p4
p3
p2
p6
p1
p5
p3
p0
p1
p0
Stack
p5p6p7 is a left turn. Push p7
20Convex Hull p6p7p8
p10
p11
p6
p9
p7
p12
p8
p5
p4
p3
p7
p2
p6
p1
p5
p3
p0
p1
p0
Stack
p6p7p8 is a left turn. Push p8
21Convex Hull p7p8p9
p10
p11
p6
p9
p7
p12
p5
p8
p4
p3
p8
p7
p2
p6
p1
p5
p3
p0
p1
p0
Stack
p7p8p9 is a right turn. Pop p8
22Convex Hull p6p7p9
p10
p11
p6
p9
p7
p12
p5
p8
p4
p3
p7
p2
p6
p1
p5
p3
p0
p1
p0
Stack
p6p7p9 is a right turn. Pop p7, push p9
23Convex Hull p6p9p10
p10
p11
p6
p12
p9
p5
p7
p8
p4
p3
p9
p2
p6
p1
p5
p3
p0
p1
p0
Stack
p6p9p10 is a right turn. Pop p9, p6, p5 and p6,
push p10
24Convex Hull p3p10p11
p10
p12
p6
p11
p9
p7
p5
p8
p4
p3
p2
p1
p10
p3
p0
p1
p0
Stack
p3p10p11 is a left turn. Push p11
25Convex Hull p10p11p12
p10
p12
p6
p11
p9
p7
p5
p8
p4
p3
p2
p11
p1
p10
p3
p0
p1
p0
Stack
p10p11p12 is a right turn. Pop p11, push p12
26Convex Hull Final
p10
p12
p6
p11
p9
p7
p5
p8
p4
p3
p2
p12
p1
p10
p3
p0
p1
p0
Stack
27Grahams Scan Running Time
- Computing and sorting the polar angles O(nlogn)
- The remaining scan O(n)
- Total O(nlogn)
28Farthest pair problem
- Given a set of points on the plane, find the two
points whose distance from each other is the
maximum - It is shown than the farthest points must be on
the Convex Hull of the points - The idea is to compute the convex hull in
O(nlogn), and then find the pair that is
farthest, which can be done in O(n) - So, we have a total running time of O(nlogn)