25
Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Embed Size (px)

Citation preview

Page 1: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Why Graham-Scan Needs toSort Vertices Before Scanning

Bill ThiesNovember 12, 2004

6.046 Recitation Supplement

Page 2: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Graham Scan: Sorting Step• The first stage of Graham-Scan sorts the points

by their polar angle from the bottom-left vertex, p0:

• In recitation, we asked: would the scanning phase of Graham-Scan work on any simple polygon? That is, can you omit this sorting phase if you start from a simple polygon? – The answer is NO.

p0

Page 3: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Graham Scan: Sorting Step• The first stage of Graham-Scan sorts the points

by their polar angle from the bottom-left vertex, p0:

• In recitation, we asked: would the scanning phase of Graham-Scan work on any simple polygon? That is, can you omit this sorting phase if you start from a simple polygon? – The answer is NO.

p0

Page 4: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Graham Scan: Sorting Step• The first stage of Graham-Scan sorts the points

by their polar angle from the bottom-left vertex, p0:

• In recitation, we asked: would the scanning phase of Graham-Scan work on any simple polygon? That is, can you omit this sorting phase if you start from a simple polygon? – The answer is NO.

p0p1

p2

p3p4p5

p6

Page 5: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Graham Scan: Sorting Step• The first stage of Graham-Scan sorts the points

by their polar angle from the bottom-left vertex, p0:

• In recitation, we asked: would the scanning phase of Graham-Scan work on any simple polygon? That is, can you omit this sorting phase if you start from a simple polygon? – The answer is NO.

p0p1

p2

p3p4p5

p6

Simple polygon(not convex)

Page 6: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Graham Scan on Simple Polygons

• The first stage of Graham-Scan sorts the points by their polar angle from the bottom-left vertex, p0:

• In recitation, we asked: would the scanning phase of Graham-Scan work on any simple polygon? That is, can you omit this sorting phase if you start from a simple polygon? – The answer is NO.

p0p1

p2

p3p4p5

p6

Simple polygon(not convex)

Page 7: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

Page 8: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p1

p2p3

p4

p5

p6

Stack

Page 9: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0

p2p1p0

Stackp1

p2p3

p4

p5

p6

Page 10: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p4

p5

p6

p2p1p0

Stack

Page 11: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p4

p5

p6p3p2p1p0

Stack

Page 12: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p4

p5

p6p3p2p1p0

Stack

Page 13: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p4

p5

p6 p4p3p2p1p0

Stack

Page 14: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p4

p5

p6 p4p3p2p1p0

Stack

Page 15: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p4

p5

p6 p4p3p2p1p0

Stack

Page 16: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p4

p5

p6p3p2p1p0

Stack

Page 17: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p3p2p1p0

Stack

p4

p5

p6

Page 18: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p3p2p1p0

Stack

p4

p5

p6

Page 19: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p5p3p2p1p0

Stack

p4

p5

p6

Page 20: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p5p3p2p1p0

Stack

p4

p5

p6

Page 21: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p6p5p3p2p1p0

Stack

p4

p5

p6

Page 22: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

• Run Graham’s Scan starting from p0

p0p0p1

p2p3

p6p5p3p2p1p0

Stack

p4

p5

p6

At end, get convex hull = <p0, p1, p2, p3, p5, p6>But actually, convex hull = <p0, p1, p2, p3>

Page 23: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Counter-Example• Consider the same set of points with the following

simple polygon:

p0p0p1

p2p3

p6p5p3p2p1p0

Stack

p4

p5

p6

What went wrong? Vertex p3 was not “visible” from p0, so the line p3->p5 crossed p6->p0 whenbuilding the convex hull. That is, the polygon became non-simple during course of the algorithm.

p0p1

p2p3

p4

p5

p6

original

Page 24: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

Graham-Scan Builds Star-Shaped Polygons

• When vertices are sorted by polar angle from p0, all other vertices are visible from p0 in resulting polygon:

• A polygon with a point visible from each vertex is called star-shaped (CLRS p. 957, Ex. 33.3-4).

• Graham-Scan works for all star-shaped polygons,but not for all simple ones

p0p1

p2

p3p4p5

p6

Page 25: Why Graham-Scan Needs to Sort Vertices Before Scanning Bill Thies November 12, 2004 6.046 Recitation Supplement

For More Information

• There do exist linear-time algorithms for building the convex hull of a simple polygon. Many of the first algorithms proposed were actually incorrect!

See here for an interesting history:

http://cgm.cs.mcgill.ca/~athens/cs601/intro.html

• Is the question we considered a million-dollar question? Probably not, but it can be worth up to $200! Click here for a good time:http://answers.google.com/answers/threadview?id=137349