Design and Analysis of Algorithms
Convex Hull
1
Convex vs. Concave
• A polygon P is convex if for every pair of
points x and y in P, the line xy is also in P;
otherwise, it is called concave.
P x y P x y
concave convex
2
The convex hull problem
concave polygon: convex polygon:
• The convex hull of a set of planar points is the
smallest convex polygon containing all of the points.
3
Graham’s Scan
• Graham scan is method of finding convex hull of a finite set of
points
• Steps:
• Start at point guaranteed to be on the hull. (the point with the
minimum y value i.e is leftmost and bottom most point in Y
co-ordinate)
• Sort remaining points by polar angles of vertices relative to
the first point.
• Go through sorted points, keeping vertices of points that have
left turns and dropping points that have right turns.
4
Graham’s Scan
5
Graham’s Scan
6
Graham’s Scan
7
Graham’s Scan
8
Graham’s Scan
9
Graham’s Scan
10
Graham’s Scan
11
Graham’s Scan
12
Graham’s Scan
13
Graham’s Scan
14
Graham’s Scan
15
Graham’s Scan
16
Graham’s Scan
17
Graham’s Scan
18
Graham’s Runtime
• Graham’s scan is O(n log n) due to initial
sort of angles.
19
A more detailed algorithm
Graham-Scan(Q)
1. Let p0 be the point in Q with the minimum y-coordinate, or the leftmost
such point in case of tie.
2. Let <p1, p2,…, pm> be the remaining points in Q, sorted by polar angle in
counterclockwise order around p0(if more than one point has the same angle,
remove all but the one that is farthest from p0).
3. Push(p0, S)
4. Push(p1, S)
5. Push(p2, S)
6. for i 3 to m:
7. do while the angle formed by points NEXT-TO-TOP(S), TOP(S), and
pi makes a nonleft turn.
8. do POP(S)
9. PUSH(pi, S)
10. Return S
20
21
22
Ex: Draw the convex hull for the following
points
23
Thank You