The Art Gallery Problem - CSE-IITM

CS6100: Topics in Design and
Analysis of Algorithms
Guarding and Triangulating Polygons
John Augustine
CS6100 (Even 2012): Guarding and Triangulating Polygons
The Art Gallery Problem
A simple polygon is a region enclosed by a single closed
polygonal chain that does not intersect itself.
Models 2D spaces such as floor plans.
Given a simple polygon P with n vertices, place a small
number of guards inside P so that it is completely
monitored. I.e., for any point p inside P , there must
be a guard positioned at g such that line segment pq
is completely inside P .
Computing the optimum (i.e., minimum) is NP-hard.
CS6100 (Even 2012): Guarding and Triangulating Polygons
1
Triangulation
A convex polygon only needs one guard! So, if we
can decompose P into convex pieces, we need one per
convex piece.
A diagonal of P is a line segment that connects two
vertices of P and is contained in the interior of P .
A triangulation is a decomposition of P into triangles
be a maximal number of non-intersecting diagonals.
Theorem 1. Any simple polygon P with n vertices
admits a triangulation and any triangulation has
(n − 2) triangles.
CS6100 (Even 2012): Guarding and Triangulating Polygons
2
w
w
v
v
v0
u
u
Proof Sketch. First we show that a diagonal always
exists. (See figures above.) Then, we can always cut
along such a diagonal, thereby decomposing P into
two smaller polygons that we can induct on to show
both claims.
Assume for now that a triangulation of P is given. Do
we really need n − 2 guards? Can we do better?
CS6100 (Even 2012): Guarding and Triangulating Polygons
3
Back to the Art Gallery Problem
A 3-colouring of a triangulated polygon P is an
assignment of one of three distinct colours to each
vertex such that any two vertices connected by an
edge or a diagonal are assigned different colours.
Note. The three vertices of any triangle will be
assigned different colours.
How can we show that bn/3c guards suffice if ∃ 3-colouring?
µ
ν
?
The dual graph of a triangulated polygon P is a graph
G = (V, E) such that V is the set of (n − 2) triangles
and edges connect two triangles that share an edge.
We use DFS on the dual graph to 3-colour P .
CS6100 (Even 2012): Guarding and Triangulating Polygons
4
Invariant. Vertices of all triangles corresponding to
dual graph nodes traversed are 3-colored.
Start DFS from any node. 3-Colour the vertices of
corresponding triangle.
Consider any subsequent edge traversed. It goes
through a diagonal d and enters a new triangle. The
ends of d are already coloured and the third vertex
of the newly entered triangle can only be coloured by
one colour. The good news is that the invariant is
maintained.
Theorem 2 (Art Gallery Theorem). To guard a simple
polygon with n vertices, bn/3c guards are sufficient
and sometimes necessary.
bn/3c prongs
CS6100 (Even 2012): Guarding and Triangulating Polygons
5
Overview of Approach
Theorem 3. We can compute the positions of the
bn/3c guards for a simple polygon P with n vertices
in O(n log n) time.
The birds eye view of the algorithm is as follows.
1. Decompose P into y-monotone pieces. (Definition
pending.)
2. Triangulate each y-monotone piece. This gives a
triangulation of P .
3. 3-colour the vertices of P using the triangulation of
P.
4. Find the colour used least and place guards on
vertices of that colour.
CS6100 (Even 2012): Guarding and Triangulating Polygons
6
Decomposing P into y-monotone pieces
A y-monotone polygon is a simple polygon such that
its intersection with any line perpendicular to the y-axis
is connected.
y-axis
To triangulate P , we first partition P into y-monotone
sub-polygons (in O(n log n) time and then triangulate
the y-monotone pieces (again, in O(n log n) time.
CS6100 (Even 2012): Guarding and Triangulating Polygons
7
Classification of Vertices
A vertex at which, as we walk along the edges, the
direction of the edges change from downward to upward
or vice versa is called a turn vertex. A vertex at which
the direction does not change is called a regular vertex.
The following figure illustrates 4 types of turn vertices.
v5
e5
v6
v3
e4 v4 e3
e6
e8 v8
v2
e1
e7
e9
v10
e10
e13
e11
v11
= end vertex
e2
v7
v9
= start vertex
v12 e12
v14
e14
v1
e15
v15
= regular vertex
= split vertex
= merge vertex
v13
Lemma 1. A simple polygon is y-monotone if it does
not contain any split or merge vertex.
The proof is left as an exercise. While this lemma is
easy to “see,” a formal proof needs a bit of care.
CS6100 (Even 2012): Guarding and Triangulating Polygons
8
Plane Sweep Method
Let v1 be the rightmost vertex in P . Traverse the
edges of P starting from v1 so that the polygon is
always to your left. In so doing, denote the ith vertex
you encounter as vi for all 1 < i ≤ n.
Denote edge vivi+1 as ei for 1 ≤ i < n. Denote edge
vnv1 as en.
Let Left be the set of edges of P such that the
polygon lies to its right.
We sweep a line ` from top to bottom. The status of
the sweep line ` at any given time is the edges in Left
that ` intersects. They are stored in L → R order in a
balanced binary tree.
The events are the vertices of P and since they are
known up front, they can be stored in an array sorted
according to the descending order of y coordinates.
CS6100 (Even 2012): Guarding and Triangulating Polygons
9
Let e be an edge in the status of `. We define the helper
of e, denoted helper(e), as the lowest vertex above `
such that the horizontal line segment connecting the
vertex to e lies inside P .
Note: helper(e) may be the upper endpoint of e.
(The figure below shows the helper of an edge ej .)
ej
helper(e j )
ek
vi
ei
ei−1
`
Given Lemma 1, we can focus our efforts on eliminating
merge and split vertices.
CS6100 (Even 2012): Guarding and Triangulating Polygons
10
Pseudocode from BCKO1
Algorithm M AKE M ONOTONE(P)
Input. A simple polygon P stored in a doubly-connected edge list D.
Output. A partitioning of P into monotone subpolygons, stored in D.
1. Construct a priority queue Q on the vertices of P, using their y-coordinates as priority.
If two points have the same y-coordinate, the one with smaller x-coordinate has higher
priority.
2. Initialize an empty binary search tree T.
3. while Q is not empty
4.
do Remove the vertex vi with the highest priority from Q.
5.
Call the appropriate procedure to handle the vertex, depending on its type.
H ANDLE S TART V ERTEX(vi )
1. Insert ei in T and set helper(ei ) to vi .
H ANDLE E NDV ERTEX(vi )
1. if helper(ei−1 ) is a merge vertex
2.
then Insert the diagonal connecting vi to helper(ei−1 ) in D.
3. Delete ei−1 from T.
H ANDLE S PLIT V ERTEX(vi )
1. Search in T to find the edge e j directly left of vi .
2. Insert the diagonal connecting vi to helper(e j ) in D.
3. helper(e j ) ← vi
4. Insert ei in T and set helper(ei ) to vi .
1
Computational Geometry: Algorithms and Applications, by de Berg,
Cheong, van Krevald, and Overmars.
CS6100 (Even 2012): Guarding and Triangulating Polygons
11
H ANDLE M ERGE V ERTEX(vi )
1. if helper(ei−1 ) is a merge vertex
2.
then Insert the diagonal connecting vi to helper(ei−1 ) in D.
3. Delete ei−1 from T.
4. Search in T to find the edge e j directly left of vi .
5. if helper(e j ) is a merge vertex
6.
then Insert the diagonal connecting vi to helper(e j ) in D.
7. helper(e j ) ← vi
H ANDLE R EGULARV ERTEX(vi )
1. if the interior of P lies to the right of vi
2.
then if helper(ei−1 ) is a merge vertex
3.
then Insert the diagonal connecting vi to helper(ei−1 ) in D.
4.
Delete ei−1 from T.
5.
Insert ei in T and set helper(ei ) to vi .
6.
else Search in T to find the edge e j directly left of vi .
7.
if helper(e j ) is a merge vertex
8.
then Insert the diagonal connecting vi to helper(e j ) in D.
9.
helper(e j ) ← vi
Lemma 2. Algorithm MakeMonotone correctly
adds a set of non-intersecting diagonals that
partitions P into monotone subpolygons.
Theorem 4. A simple polygon with n vertices can
be partioned into y-monotone polygons in O(n log n)
time with an algorithm that uses O(n) storage.
Proofs are left as exercises.
CS6100 (Even 2012): Guarding and Triangulating Polygons
12
ek
vi
ej
vm
diagonal will be added
when the sweep line
reaches vm
v5
v
e5 e4 4 e3
v6
e6
v7
v9
v8
e8
e2
v2
e7
v1
e1
e15
e9
v10
e10
v3
e11
v11
e13
v12
e14
v14
v15
e12
v13
CS6100 (Even 2012): Guarding and Triangulating Polygons
13
Triangulating a Monotone Polygon
Given a y-monotone polygon P , we want to triangulate
it. We assume that P is given as a sequence of vertices
ordered anticlockwise.
From the given sequence, we can order the vertices in
decreasing order of their y-coordinates. How?
Once ordered, we pass through the vertices top →
bottom.
As the algorithm progresses, a (connected) portion of
P will be untriangulated, while the rest is triangulated.
Even though we might have passed some of the
vertices, they may still be on the boundary of the
untriangulated part of P .
CS6100 (Even 2012): Guarding and Triangulating Polygons
14
triangles
split off
not yet
triangulated
As can be seen above, the untriangulated, but passed
vertices form a geometric shape that has an inverted
funnel-like structure with the following properties.
Straight side. This is a “part” of an edge.
Concave side. The other side forms a sequence of
reflex vertices (i.e., vertices whose internal angle is
> 180◦). This side might have triangulated pieces
sticking to it.
CS6100 (Even 2012): Guarding and Triangulating Polygons
15
We will need a stack S, which is initially empty. At
any time, it stores the vertices of the untriangulated
part. The vertices are stored in S such that if popped
one after another, we will get the lowest → hightest
sequence.
If next vertex vj is on the straight side: We pop
vertices one-by-one and add diagonals from each
popped vertex to v. The last vertex popped will be
the other side of the straight edge, so we don’t add
a diagonal between the last vertex and v.
popped
e
pushed
vj
popped and
pushed
CS6100 (Even 2012): Guarding and Triangulating Polygons
16
If next vertex vj is on the concave side: We again
pop vertices from the stack and add diagonals as
long as (i) we can add diagonals that don’t intersect2
edges of P and (ii) S is not empty.
If the diagonal to top vertex v 0 in S intersects edges
of P , then do not pop v 0. Furthermore, push the
vertex that was popped immediately prior to v 0 (if
it exists) back into the stack.
Finally, push vj into the stack as well.
popped and
pushed
popped
popped and
pushed
vj
vj
pushed
2
pushed
How can this be tested in O(1) time?
CS6100 (Even 2012): Guarding and Triangulating Polygons
17
Algorithm T RIANGULATE M ONOTONE P OLYGON(P)
Input. A strictly y-monotone polygon P stored in a doubly-connected edge list D.
Output. A triangulation of P stored in the doubly-connected edge list D.
1. Merge the vertices on the left chain and the vertices on the right chain of P into one
sequence, sorted on decreasing y-coordinate. If two vertices have the same y-coordinate,
then the leftmost one comes first. Let u1 , . . . , un denote the sorted sequence.
2. Initialize an empty stack S, and push u1 and u2 onto it.
3. for j ← 3 to n − 1
4.
do if u j and the vertex on top of S are on different chains
5.
then Pop all vertices from S.
6.
Insert into D a diagonal from u j to each popped vertex, except the last one.
7.
Push u j−1 and u j onto S.
8.
else Pop one vertex from S.
9.
Pop the other vertices from S as long as the diagonals from u j to them are
inside P. Insert these diagonals into D. Push the last vertex that has been
popped back onto S.
10.
Push u j onto S.
11. Add diagonals from un to all stack vertices except the first and the last one.
Theorem 5. A strictly y-monotone polygon with n
vertices can be triangulated in O(n) time.
Theorem 6. A simple polygon with n vertices can be
triangulated in O(n log n) time.
Theorem 7. A planar subdivision with n vertices can
be triangulated in O(n log n) time.
CS6100 (Even 2012): Guarding and Triangulating Polygons
18