algorithms - cs.princeton.edu
TRANSCRIPT
Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · September 24, 2011 8:59:25 AM
AlgorithmsF O U R T H E D I T I O N
R O B E R T S E D G E W I C K K E V I N W A Y N E
GRAHAM SCAN DEMO
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
2
Graham scan
p
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
3
Graham scan
1
0
4
5
67
8
10
1112
2
3
9
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
1
0
3
4
5
67
8
10
1112
2
4
Graham scan
9
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
1
0
4
5
67
8
10
1112
2
5
Graham scan
3
9
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
1
0
4
5
67
8
10
1112
2
6
Graham scan
3
9
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
7
Graham scan
1
0
3
4
5
2
67
8
10
1112
9
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
5
8
Graham scan
67
8
10
1112
9
1
0
4 2
3
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
1
0
4
5
9
Graham scan
2
67
8
10
1112
9
3
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
1
0
4
5
2
10
Graham scan
67
8
10
1112
9
3
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
1
0
4
5
2
11
Graham scan
67
8
10
1112
9
3
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
5
67
12
Graham scan
1
0
2
8
10
1112
9
3
4
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
5
67
13
Graham scan
8
10
1112
9
1
0
2
3
4
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
5
7
14
Graham scan
8
10
1112
9
1
0
2
3
4
6
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
1
0
5
7
8
10
1112
2
15
Graham scan
9
3
4
6
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
5
7
8
16
Graham scan
1
2
0
10
1112
9
3
4
6
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
5
8
17
Graham scan
10
1112
1
0
2
9
3
4
67
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
18
Graham scan
10
1112
1
0
5
2
9
3
4
67
8
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
10
19
Graham scan
1112
1
0
5
2
9
3
4
67
8
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
10
11
20
Graham scan
12
1
0
5
2
9
3
4
67
8
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
1
0
5
10
1112
2
21
Graham scan
9
3
4
67
8
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
10
11
22
Graham scan
12
1
0
5
6
8
2
9
3
4
7
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
23
Graham scan
12
1
0
5
9
10
11
6
2
3
4
7
8
• Choose point p with smallest y-coordinate.
• Sort points by polar angle with p.
• Consider points in order, and discard unless that would create a ccw turn.
10
1112
24
Graham scan
1
0
5
67
2
3
9
4
8