A Simple and fast
A Simple and fast
Abstract
trapezoidal
Seidel, R., A simple and fast incremental randomized algorithm for computing
triangulatin g polygons, Computatio nal Geometry: Theory and Appli-
decompositions and for
cations | (1991) 51-64.
the
This paper presents a very simple incremental randomized algorithm for computing
If 5S is given as a
trapezoidal decomposition induced by a set S of 2 line segments in the plane.
7). This leads to
simple polygonal chain the expected running time of the algorithm is O(” log*
More generally, if S is
a simple algorithm of the same complexity for triangulating polygons.
graph with k connected components , then the expected running time of
presented as a plane
structure
the algorithm is O(n log* n + k logn). Asa by-product our algorithm creates a search
size that allows point location queries in the resulting trapezoidat ion in
of expected linear
time. The analysis of the expected performance is elementary and
logarithmic expected
and
straightforward. All expectations are with respect to ‘coinflips’ generated by the algorithm
are not based on assumptions about the geometric distribution of the input.
1. Introduction
2. Trapezoidations
Our setting is the Euclidean plane with the usual Cartesian x-y coordinate
system. We will feel free to use intuitive notions such as ‘vertical’ (meaning
parallel to the y-axis), ‘horizontal,’ ‘left of,’ etc.
We call two straight line segments in the plane noncrossing iff their intersection
is either empty or a common endpoint. Consider a set S of n nonhorizontal,
noncrossing closed line segments. Starting at each endpoint of each segment in S
draw two horizontal rays, one towards the left and one towards the right, each
extending until it hits a segment of S. For a segment endpoint p we call the union
Trapezoidal decompositions and triangulating polygons 53
of these two possibly truncated rays emanating from p the horizontal extension
through p. The segments of S together with the horizontal extensions through the
endpoints form a plane graph, which we call the trapezoidation of S, or F(S) for
short (see Fig. 1). As each face of 7(S) has two horizontal sides (one of which
might have length 0) we are justified in calling the faces of F(S) trapezoids.
What is the complexity of 7(S)? If S consists of n segments, then there are at
most 2n segment endpoints. Through each endpoint there is a horizontal
extension whose endpoints in turn can contribute at most two vertices to F(S).
Thus 7(S) has at most 6n vertices, and since it is a planar graph it follows
_immediately that it has O{7) edges and faces. In particular, the number of faces
(trapezoids) is at most 3n +1, which can be easily established using a sweep
argument.
For our purposes it would be advantageous if each trapezoid of J (S) had at
most two ‘neighboring’ trapezoids above it and at most two neighboring
trapezoids below it. By this we mean that if a trapezoid has a nondegenerate
upper (lower) side, then this side should have non-empty intersection with the
lower (upper) sides of at most two other trapezoids. This condition holds
automatically if no two distinct endpoints of segments in S have the same
y-coordinate. From now on we will assume that S satisfies this ‘nondegeneracy’
assumption. As noted already in [5], no generality is lost with this assumption,
since it can always be achieved by rotating the coordinate system by a sufficiently
small amount. Better yet, this perturbative rotation can simply be simulated using
a lexicographic technique: if two distinct points have the same y-coordinate, then
the one with smaller x-coordinate is considered the ‘lower’ one (see Fig. 2).
We end this section by giving the connection between computing trapezoida-
tions and triangulating simple polygons. This connection was originally estab-
lished in [6] and [3]. We include it for the sake of completeness.
Let S = {so, 51, -- + ,S,-1} be a set of m segments that form a simple and closed
polygonal curve; i.e. s; and s;,, share a common endpoint, and 5,1s;,=9 iff
|i —j| > 1 (all index arithmetic here is modulo 1). This polygonal curve bounds a
34 R. Seidel
Lemma 1. Let S = {50, 5), .. - Sn_1} be the set of edges around a simple polygon
P. If F(S) is available, then a triangulation of P can be computed in O(n) time.
Proof. (Sketch) Assume that no two vertices of P have the same y-coordinate.
As mentioned above, this assumption entails no loss of generality. We compute a
triangulation of P from a trapezoidation 7(S) in three stages:
First remove from consideration all trapezoids of Z(S) that do not lie in the
interior of P.
Second, for each of the remaining trapezoids check whether it has two vertices
of P on its boundary that do not lie on the same side. If such a pair of vertices
Fig. 2(b). Degeneracies removed by rotation of coordinate system (or lexicographic method).
Trapezoidal decompositions and triangulating polygons 55
Fig. 3. (a) Trapezoids inside the polygon; (b) Introducing diagonals; (c) Decomposition of polygon
into pieces that are easy to triangulate.
3. The Algorithm
T({s1,52})
Q({s1, s2})
[_]...¥-node
C7)... x-nede
O .. sink
Fig. 4. A trapezoidation and an associated query structure.
Trapezoidal decompositions and triangulating polygons 57
Does
segment? At a Y-node: Is q’s y-coordinate less or greater than the key? or:
q lie below or above the horizontal extension?). We assume that the trapezoid s of
of 7(S)
J(S) and the sinks of 2(S) are properly correlated, i.e. for a trapezoid
(see
one can tell in constant time its corresponding sink of 2(S), and vice versa
Fig. 4).
b
Let s be a nonhorizontal segment with upper endpoint a and lower endpoint
consider the problem of
that crosses no segment of S, and let S’ be SU {s}. We
computing 7(S') and 2(S‘), given 7(S) and 2(S).
If the upper endpoint a is not an endpoint of some segment in S already, we
with
first use 2(S) to locate the trapezoid of t, of 7(S) that contains a. We split t,
a to obtain a new trapezoidal decomposition 7’. The
a horizontal line through
sink of 2(S) that corresponds to t, becomes a Y-node whose key is the
are sinks
y-coordinate of a and whose two successors are two new nodes, which
and correspond to the two new trapezoids of 7’. Thus we obtained besides 7’
also a new query structure 2’.
If a was an endpoint of some segment of S already, then let F' be F and let 2’
be 2.
Next we proceed likewise with the lower endpoint b to obtain from 7’ and 2'a
trapezoidation 7" and a corresponding query structure Q".
Now we ‘thread’ segment s through J”, i.e. we determine all trapezoids of 7”
that are intersected by s, cut them in two, and on each side of s we merge
contiguous sequences of trapezoids that agree in their bounding segments. Thus
we have obtained 7(S').
We obtain 2(S’) as follows: For each ‘new’ trapezoid of F(S') along s we
to a
create a new corresponding sink-node. Each sink of 2” that corresponds
trapezoid of J” that is cut by s becomes an X-node whose key is s and whose two
successors are the appropriate new sinks.
The time necessary to obtain J(S’) and 2(S’) from 7 (S) and 2(S) consists of
the query time for locating the two endpoints of s and the ‘threading time’ for s.
Note that the latter is proportional to the number of horizontal lines of F(S) that
are cut by s, or equivalently, it is proportional to the number of horizontal lines
of 7(S‘) that abut upon s.
What is the total time necessary for constructing J(S) and 2(S) by successive
insertion of the segments of S, starting with empty structures? Clearly this
depends very much on the order of insertion. Note that F(S) is independent of
this order, but 2(S) and the query time it admits very much depend on this order.
It is not hard to concoct bad examples: Consider, for instance, a set S of n vertical
segments that all intersect the x-axis. If these segments are inserted by increasing
x-coordinate, then the resulting query structure will have very long source-sink
paths and locating a query point on the x-axis to the right of all segments will take
O(n) time. .
In the following we argue that if the segments of S are inserted in random
order, with each ordering occuring equally likely, then 2(S) will behave nicely in
38 R. Seidel
expectation, and the overall expected construction time will be reasonable. For
this purpose we will need to show something about the expected point location
query time and the expected ‘threading’ time.
Proof. For a segment s eS; let deg(s, 7(S;)) denote the number of horizontal
extensions of 7(S;) that abut upon 5,, i.e. they end in the relative interior of s.
As the number of horizontal extensions of 7(S,_,) that are intersected by s; is
the same as the number of horizontal extensions of J(S,) that abut upon s,;, we
are interested in the expected value Exp(deg(s; 7(S,))).
As there are at most 2i horizontal extensions in J(S,), each abutting upon at
most two segments, we have ¥,.s deg(s, 7(S;)) <4i. Because of the random
ordering, 5; is any one of the i segments in S, with equal probability. Thus
Exp(deg(s,, 7(S)))=4 O
Proof. Let 1; be the trapezoid in 7(S,) that contains g. Assuming 1,_; is known,
what is E;, the expected number of comparisons necessary to identify 1,? In other
words, assuming we know the trapezoid of 7(S,_,) that contains g, what is the
expected number of comparisons necessary to determine the trapezoid of J(S;)
that contains q?
Clearly no comparisons are necessary if t;_, and t; are the same. If they are
different, then at least one of the sides of 1; has to be part of the new segments s,
or part of the horizontal extension through one of the endpoints of s;.
If the right side of 1, is part of s;, then exactly one comparison is necessary to
identify t;, namely an X-comparison between g and s,. Because of the random
ordering every one of the i segments of S, has the same chane 1/i of being s;. Thus
the probability that s; happens to be the segment bounding 7; to its right is at most
1/i (t; might be unbounded to the right). Thus the expected number of
comparisons between q and the right side of 1; is at most 1/i. By symmetry the
same holds for the left side of 1;.
Trapezoidal decompositions and triangulating polygons 59
Lemmas 2 and 3 together with the preceding discussion immediately imply the
following theorem.
chain C, then an obvious fix seems to be to insert the segments in order along C.
This way one endpoint of s; coincides with an endpoint of the just inserted
segment s;_, whose location is known. The location of the other endpoint could
be determined while ‘threading’ s; through the current trapezoidation. Unfortun-
ately this strategy forgoes the use of randomization, and it is not true any more
that the expected cost of ‘threading’ s; through the current trapezoidation is
constant. Indeed it is not hard to concoct examples where this ‘threading’ cost is
O(i) for n/2<i<n, which leads to an O(n’) algorithm.
We will pursue the following strategy: We will still insert the segments in
random order. But every once in a while we will stop and locate all endpoints
(which are vertices of C) in the current trapezoidation by tracing out C. To
document the efficiency of this approach we will need two lemmas: one tells how
much this intermediate location information helps with the searches later on; the
other one tells how expensive it is to trace C through the current trapezoidation.
Proof. Proceed as in the proof of Lemma 3. However, the expected query time
in this case is Djcjc, E;. O
Proof. For T cS and seéT let deg(s, J(T)) denote the number of horizontal
extensions of endpoints of segments in T that abut upon the relative interior of s.
As every horizontal extension abuts upon at most two segments we have
User deg(s, T(T)) <4 |T|.
For Rc S and s ¢R observe that the number of horizontal trapezoid sides of
F(R) that are intersected by s is exactly deg(s, F(R U {s})). We are interested in
+NA\ SS
R'cS
deg(s, 718)
SER’
r IR'jere1
(ices (")
IK
Before presenting the final algorithm and its analysis a bit of notation: Let
logn denote the ith iterated logarithm, i.e. log n =n and for i>0 we have
logn = log(log— n). For n>0 let log* n denote the largest integer / so that
log” n=1, and for n>O and 0<h<log*n let N(h) be shorthand for
[n/log™ n].
The input to the algorithm below is a simple polygonal chain C of n segments in
consecutive order along C.
What is the expected running time of this algorithm? We assume that Step 1
can be executed in linear time (see Section 4 for a discussion of this assumption).
Step 2 takes constant time.
For Step 3 let us consider some h, with 1<h <log* n. By Lemma 5 Step 3.2
can be performed on O(n) expected time. What about Step 3.1? The expected
cost of Step 3.1.1, inserting segment s;, is the sum of the expected time necessary
to locate s,’s endpoints in Y;_, and the expected time for ‘threading’ s; through
J,_,. By Lemma 2 the latter is constant. Since the location of s,’s endpoints in
Tniwr-1) is known already, by Lemma 4 the expected location time is only
O(log(i/N(h — 1))). Since i<n and N(h - 1) = [n/log“~? n], this is O(log” n).
For fixed A Step 3.1.1 is executed at most N(h)= [n/log™ n] times. Thus for
fixed A the total cost of Step 3 is O(n), and hence the entire expected cost of step
3 over all A is O(n log* n).
62 R. Seidel
The analysis for step 4 is similar to the one of Step 3.1. Note that
N(log* n)=n/e, and therefore the expected point location cost is constant. It
follows that Step 4 takes O(7) expected time. Thus we have proved the following
theorem.
This theorem admits easy generalizations. Note that the algorithm exploits only
one property of the chain C, namely its connectedness. If 5 was presented as a
connected plane graph G with the order of the incident edges around each vertex
given explicitly, then the result would continue to hold. Only the tracing of Step 3.2
of the algorithm would have to be performed in order of some graph traversal of
G. Allowing the graph G to have several connected components leads to a
unification of Theorems 1 and 2.
Proof. Apply the algorithm outlined above, but modify Step 3.2 so as to trace
the segments of each connected component of the graph through the current
trapezoidation in the order of some graph traversal, say, depth-first-search. In
order to start the tracing of one connected component, one of its vertices has to
be located in the current trapezoidation. Because of Lemma 3 the cumulative
expected cost for this over the entire algorithm is O(log) per component. O
4, Remarks
An algorithm somewhat similar to the one described in this paper has also been
discovered by Clarkson, Cole, and Tarjan [4]. However, their approach is based
on divide-and-conquer and the main thrust of their approach is towards a fast
parallel trapezoidation algorithm.
Trapezoidal decompositions and triangulating polygons 63
Acknowledgements
1 would like to thank Mike Hohmeyer for pointing out an error in the original
version of the proof of Lemma 3. I would also like to thank Emo Welzl for
inviting me to the Freie Universitat Berlin. I doubt this research would have
happened without the stimulating visit there.
References
IEEE Symp. on
{1} B. Chazelle, A theorem on polygon cutting with applications, Proc. 23rd Annual
Found. of Comput. Sci. (1982) 339-349.
[2] B. Chazelle, Triangulating a simple polygon in linear time, Princton Univ. Computer Science
Rep. CS-TR-264-90, to appear in Proc. 31st Annual IEEE Symp. on Found. of Comput.
Tech.
Sci. (1990).
and J. Incerpi, Triangulation and shape-complexity, ACM Trans. on Graphics 3
{3] B. Chazelle
(1984) 135-152.
[4] K. Clarkson, R. Cole and R.E. Tarjan, Randomized parallel algorithms for trapezoidal
diagrams, to appear in Proc. 7th ACM Symposium on Computational Geometry (1991).
64 R. Seidel
[5] K. Clarkson, R.E. Tarjan and C.J. Van Wyk, A fast Las Vegas algorithm for triangulating a
simple polygon, Discrete Comput. Geom. 4 (5) (1989) 423—432.
[6] A. Fournier and D.Y. Montuno, Triangulating simple polygons and equivalent problems, ACM
Trans. on Graphics 3 (1984) 153-174.
[7] M.R. Garey, D.S. Johnson, F.P. Preparata and R.E. Tarjan, Triangulating a simple polygon,
Inform. Process. Lett. 7 (1978) 175--180.
[8} S. Hertel and K. Mehthorn, Fast triangulation of a simple polygon, Proc. Conf. Found. Comput.
Theory, Lecture Notes in Comput. Sci. 158 (Springer, Berlin, 1983) 207-218.
{9] K. Hoffman, K. Mehlhorn, P. Rosenstiehl and R.E. Tarjan, Sorting Jordan sequences in linear
time using level-linked search trees, Inform. and Control 68 (1986) 170-184.
[10] D.G. Kirkpatrick, M.M. Klawe and R.E. Tarjan, O(n log logn) polygon triangulation with
simple data structures, Proc. 6th Annual ACM Symp. Comput. Geom. (1990) 34-43.
[11] R.E. Tarjan and C.J. Van Wyk An O(n log log 7)-time algorithm for triangulating a simple
polygon, SIAM J. Comput. 17 (1988) 143-178.
[12) G. Toussaint and D. Avis, On a convex huil algorithm for polygons and its applications to
triangulation problems, Pattern Recognition 15 (1) (1982) 23-29.
{13] G. Toussaint, An output-complexity-sensitive polygon triangulation algorithm, Report SICS-
86.3, McGill University, Montreal, 1988.