contest algorithms january 2016 triangulation & the art gallery problem contest algorithms1

21
Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms 1

Upload: blaise-thornton

Post on 18-Jan-2018

227 views

Category:

Documents


0 download

DESCRIPTION

 A partition of P into triangles by a set of noncrossing diagonals. 2. Triangulation

TRANSCRIPT

Page 1: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Contest Algorithms 1

Contest AlgorithmsJanuary 2016

Triangulation &the Art Gallery Problem

Page 2: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

1. Simple Polygons

Some definitions would allow this as a “degenerate” simple polygon

P is a simple polygon is one which does not intersect with itself

Page 3: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

A partition of P into triangles by a set of noncrossing diagonals.

2. Triangulation

Page 4: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Contest Algorithms 4

create a stack with all of the vertices in CCW order;pop the top vertex off the stack and store in p0;pop the top vertex off the stack and store in pHelper;while the stack is not empty

pop the top vertex off the stack and store in pTemp;create a triangle with vertices p0, pHelper, pTemp;let pHelper = pTemp

Triangulation of Convex Polygons

p0

Page 5: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

A simple polygon has a triangulation.

An n-gon with n 4 has a diagonal.

Any triangulation of a simple n-sided polygon has: n-3 diagonals n-2 triangles

Triangulation Theory in 2D

5

Page 6: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

A diagonal of the form vi-1vi+1 is an ear diagonal; the triangle vi-1 vi vi+1 is an ear, and vi is the ear tip

There are at most n ears (and a convex polygon has exactly n ears)

Ears

vi-1 vi+1

vi

Page 7: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Contest Algorithms 7

Ear Clipping Triangulation

Page 8: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Contest Algorithms 8

create a list of the vertices (in CCW order, starting anywhere)while true for every vertex let pPrev = the previous vertex in the list let pCur = the current vertex; let pNext = the next vertex in the list if the vertex is not an interior vertex; continue; if there are any vertices in the polygon inside the triangle made by the current vertex and the two adjacent ones continue; create the triangle with the points pPrev, pCur, pNext; remove pCur from the list; if no triangles were made in the above for loop break;

Pseudo-code

isEar()?

see triangulate() and isEar() in Polygon.java

Page 9: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Simple “ear-clipping” methods: O(n2 )

Cases with simple O(n) algorithms: Convex polygons (trivial!) Monotone polygons, monotone mountains

General case (even with holes!): Sweep algorithm to decompose into monotone mountains O(n log n)

Triangulation Running Times

9

Page 10: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Contest Algorithms 10

A polygon is monotone with respect to a line L, if every line orthogonal to L intersects P at most twice.

allows for sweep-based triangulation methods

Monotone Polygon

Page 11: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

11

Example (tri4.txt)

Contest Algorithms

5

10

15

5 10 15 20

0

1

2

3

4

5

7

6

8

910

111

2

13

14

15

16

17

180 010 712 320 813 1710 1212 1414 98 106 1410 157 170 161 132 155 8-2 95 5

Page 12: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Contest Algorithms 12

> java TriangulationTests tri4.txt

Triangulation:Indicies: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]try ear: 17, 0, 1 > 1. Cut off: [ (5.000, 5.000) (0.000, 0.000) (10.000, 7.000) ]Indicies: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]try ear: 17, 1, 2try ear: 1, 2, 3 > 2. Cut off: [ (10.000, 7.000) (12.000, 3.000) (20.000, 8.000) ]Indicies: [1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]try ear: 1, 3, 4try ear: 3, 4, 5try ear: 4, 5, 6 > 3. Cut off: [ (13.000, 17.000) (10.000, 12.000) (12.000, 14.000) ]Indicies: [1, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]try ear: 4, 6, 7 > 4. Cut off: [ (13.000, 17.000) (12.000, 14.000) (14.000, 9.000) ]Indicies: [1, 3, 4, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]try ear: 4, 7, 8try ear: 7, 8, 9try ear: 8, 9, 10

Execution see TriangulationTests.java

Page 13: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Contest Algorithms 13

try ear: 9, 10, 11 > 5. Cut off: [ (6.000, 14.000) (10.000, 15.000) (7.000, 17.000) ]Indicies: [1, 3, 4, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17]try ear: 9, 11, 12 > 6. Cut off: [ (6.000, 14.000) (7.000, 17.000) (0.000, 16.000) ]Indicies: [1, 3, 4, 7, 8, 9, 12, 13, 14, 15, 16, 17]try ear: 9, 12, 13try ear: 12, 13, 14 > 7. Cut off: [ (0.000, 16.000) (1.000, 13.000) (2.000, 15.000) ]Indicies: [1, 3, 4, 7, 8, 9, 12, 14, 15, 16, 17]try ear: 12, 14, 15try ear: 14, 15, 16try ear: 15, 16, 17 > 8. Cut off: [ (5.000, 8.000) (-2.000, 9.000) (5.000, 5.000) ]Indicies: [1, 3, 4, 7, 8, 9, 12, 14, 15, 17]try ear: 15, 17, 1 > 9. Cut off: [ (5.000, 8.000) (5.000, 5.000) (10.000, 7.000) ]Indicies: [1, 3, 4, 7, 8, 9, 12, 14, 15]try ear: 15, 1, 3 > 10. Cut off: [ (5.000, 8.000) (10.000, 7.000) (20.000, 8.000) ]Indicies: [3, 4, 7, 8, 9, 12, 14, 15]try ear: 15, 3, 4 :

Page 14: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Contest Algorithms 14

[ (5.000, 5.000) (0.000, 0.000) (10.000, 7.000) ][ (10.000, 7.000) (12.000, 3.000) (20.000, 8.000) ][ (13.000, 17.000) (10.000, 12.000) (12.000, 14.000) ][ (13.000, 17.000) (12.000, 14.000) (14.000, 9.000) ][ (6.000, 14.000) (10.000, 15.000) (7.000, 17.000) ][ (6.000, 14.000) (7.000, 17.000) (0.000, 16.000) ][ (0.000, 16.000) (1.000, 13.000) (2.000, 15.000) ][ (5.000, 8.000) (-2.000, 9.000) (5.000, 5.000) ][ (5.000, 8.000) (5.000, 5.000) (10.000, 7.000) ][ (5.000, 8.000) (10.000, 7.000) (20.000, 8.000) ][ (20.000, 8.000) (13.000, 17.000) (14.000, 9.000) ][ (6.000, 14.000) (0.000, 16.000) (2.000, 15.000) ][ (6.000, 14.000) (2.000, 15.000) (5.000, 8.000) ][ (5.000, 8.000) (20.000, 8.000) (14.000, 9.000) ][ (5.000, 8.000) (14.000, 9.000) (8.000, 10.000) ][ (5.000, 8.000) (8.000, 10.000) (6.000, 14.000) ]

try ear: 3, 4, 7 > 11. Cut off: [ (20.000, 8.000) (13.000, 17.000) (14.000, 9.000) ]Indicies: [3, 7, 8, 9, 12, 14, 15]try ear: 3, 7, 8Collinear problem near point 3try ear: 7, 8, 9try ear: 8, 9, 12try ear: 9, 12, 14 > 12. Cut off: [ (6.000, 14.000) (0.000, 16.000) (2.000, 15.000) ]Indicies: [3, 7, 8, 9, 14, 15]try ear: 9, 14, 15 > 13. Cut off: [ (6.000, 14.000) (2.000, 15.000) (5.000, 8.000) ]Indicies: [3, 7, 8, 9, 15]try ear: 9, 15, 3try ear: 15, 3, 7 > 14. Cut off: [ (5.000, 8.000) (20.000, 8.000) (14.000, 9.000) ]Indicies: [7, 8, 9, 15]try ear: 15, 7, 8 > 15. Cut off: [ (5.000, 8.000) (14.000, 9.000) (8.000, 10.000) ]Indicies: [8, 9, 15]try ear: 15, 8, 9 > 16. Cut off: [ (5.000, 8.000) (8.000, 10.000) (6.000, 14.000) ]Indicies: [9, 15]

16 triangles for 18-sided polygon

2 points left at end; 16 used

Page 15: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Contest Algorithms 15

Result

5

10

15

5 10 15 20

0

1

2

3

4

5

7

6

8

910

111

2

13

14

15

16

171

2

3

4

567

89

10

11

12

13

1415

16

16 triangles and15 diagonalsfor 18-sided polygon

Page 16: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

3. The Art Gallery ProblemQuestion: How many guards are needed to guard an art gallery?

Guard: (camera, motion sensors, fire detectors, …) - 2 range visibility - infinite distance - cannot see through walls

x

y

z

Page 17: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Some Gallery Examplesn=3

n=4

n=5

n=6

the best guard position; 1 guard needed

Page 18: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

n=6

n=9

n=3k

Page 19: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Put one guard in each triangle. Since the triangle is convex, the guard can see the whole triangle.

Guard Positioning Using Triangulation

Page 20: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

An assignment of one out of three colors to each vertex. Adjacent vertices must have different colors.

3-Coloring

Page 21: Contest Algorithms January 2016 Triangulation & the Art Gallery Problem Contest Algorithms1

Theorem; For any simple polygon with n vertices guards are sufficient to guard the whole polygon -- this means less guards might be possible

There are polygons for which guards are necessary-- less is not possible, e.g:

Art Gallery Theorem

n3

n3