Chan's algorithm

From HandWiki
Short description: Algorithm for finding the convex hull of a set of points in the plane
A 2D demo for Chan's algorithm. Note however that the algorithm divides the points arbitrarily, not by x-coordinate.

In computational geometry, Chan's algorithm,[1] named after Timothy M. Chan, is an optimal output-sensitive algorithm to compute the convex hull of a set [math]\displaystyle{ P }[/math] of [math]\displaystyle{ n }[/math] points, in 2- or 3-dimensional space. The algorithm takes [math]\displaystyle{ O(n \log h) }[/math] time, where [math]\displaystyle{ h }[/math] is the number of vertices of the output (the convex hull). In the planar case, the algorithm combines an [math]\displaystyle{ O(n \log n) }[/math] algorithm (Graham scan, for example) with Jarvis march ([math]\displaystyle{ O(nh) }[/math]), in order to obtain an optimal [math]\displaystyle{ O(n \log h) }[/math] time. Chan's algorithm is notable because it is much simpler than the Kirkpatrick–Seidel algorithm, and it naturally extends to 3-dimensional space. This paradigm[2] has been independently developed by Frank Nielsen in his Ph.D. thesis.[3]

Algorithm

Overview

A single pass of the algorithm requires a parameter [math]\displaystyle{ m }[/math] which is between 0 and [math]\displaystyle{ n }[/math] (number of points of our set [math]\displaystyle{ P }[/math]). Ideally, [math]\displaystyle{ m = h }[/math] but [math]\displaystyle{ h }[/math], the number of vertices in the output convex hull, is not known at the start. Multiple passes with increasing values of [math]\displaystyle{ m }[/math] are done which then terminates when [math]\displaystyle{ m \geq h }[/math](see below on choosing parameter [math]\displaystyle{ m }[/math]).

The algorithm starts by arbitrarily partitioning the set of points [math]\displaystyle{ P }[/math] into [math]\displaystyle{ K = \lceil n/m \rceil }[/math] subsets [math]\displaystyle{ (Q_k)_{k=1,2,...K} }[/math] with at most [math]\displaystyle{ m }[/math] points each; notice that [math]\displaystyle{ K=O(n/m) }[/math].

For each subset [math]\displaystyle{ Q_k }[/math], it computes the convex hull, [math]\displaystyle{ C_k }[/math], using an [math]\displaystyle{ O(p \log p) }[/math] algorithm (for example, Graham scan), where [math]\displaystyle{ p }[/math] is the number of points in the subset. As there are [math]\displaystyle{ K }[/math] subsets of [math]\displaystyle{ O(m) }[/math] points each, this phase takes [math]\displaystyle{ K\cdot O(m \log m) = O(n \log m) }[/math] time.

During the second phase, Jarvis's march is executed, making use of the precomputed (mini) convex hulls, [math]\displaystyle{ (C_k)_{k=1,2,...K} }[/math]. At each step in this Jarvis's march algorithm, we have a point [math]\displaystyle{ p_{i} }[/math] in the convex hull (at the beginning, [math]\displaystyle{ p_{i} }[/math] may be the point in [math]\displaystyle{ P }[/math] with the lowest y coordinate, which is guaranteed to be in the convex hull of [math]\displaystyle{ P }[/math]), and need to find a point [math]\displaystyle{ p_{i+1} = f(p_{i},P) }[/math] such that all other points of [math]\displaystyle{ P }[/math] are to the right of the line [math]\displaystyle{ p_{i}p_{i+1} }[/math][clarification needed], where the notation [math]\displaystyle{ p_{i+1} = f(p_{i},P) }[/math] simply means that the next point, that is [math]\displaystyle{ p_{i+1} }[/math], is determined as a function of [math]\displaystyle{ p_{i} }[/math] and [math]\displaystyle{ P }[/math]. The convex hull of the set [math]\displaystyle{ Q_k }[/math], [math]\displaystyle{ C_k }[/math], is known and contains at most [math]\displaystyle{ m }[/math] points (listed in a clockwise or counter-clockwise order), which allows to compute [math]\displaystyle{ f(p_{i},Q_k) }[/math] in [math]\displaystyle{ O(\log m) }[/math] time by binary search. Hence, the computation of [math]\displaystyle{ f(p_{i},Q_k) }[/math] for all the [math]\displaystyle{ K }[/math] subsets can be done in [math]\displaystyle{ O(K \log m) }[/math] time. Then, we can determine [math]\displaystyle{ f(p_{i},P) }[/math] using the same technique as normally used in Jarvis's march, but only considering the points [math]\displaystyle{ (f(p_{i},Q_k))_{1\leq k\leq K} }[/math] (i.e. the points in the mini convex hulls) instead of the whole set [math]\displaystyle{ P }[/math]. For those points, one iteration of Jarvis's march is [math]\displaystyle{ O(K) }[/math] which is negligible compared to the computation for all subsets. Jarvis's march completes when the process has been repeated [math]\displaystyle{ O(h) }[/math] times (because, in the way Jarvis march works, after at most [math]\displaystyle{ h }[/math] iterations of its outermost loop, where [math]\displaystyle{ h }[/math] is the number of points in the convex hull of [math]\displaystyle{ P }[/math], we must have found the convex hull), hence the second phase takes [math]\displaystyle{ O(Kh \log m) }[/math] time, equivalent to [math]\displaystyle{ O(n \log h) }[/math] time if [math]\displaystyle{ m }[/math] is close to [math]\displaystyle{ h }[/math] (see below the description of a strategy to choose [math]\displaystyle{ m }[/math] such that this is the case).

By running the two phases described above, the convex hull of [math]\displaystyle{ n }[/math] points is computed in [math]\displaystyle{ O(n \log h) }[/math] time.

Choosing the parameter [math]\displaystyle{ m }[/math]

If an arbitrary value is chosen for [math]\displaystyle{ m }[/math], it may happen that [math]\displaystyle{ m\lt h }[/math]. In that case, after [math]\displaystyle{ m }[/math] steps in the second phase, we interrupt the Jarvis's march as running it to the end would take too much time. At that moment, a [math]\displaystyle{ O(n \log m) }[/math] time will have been spent, and the convex hull will not have been calculated.

The idea is to make multiple passes of the algorithm with increasing values of [math]\displaystyle{ m }[/math]; each pass terminates (successfully or unsuccessfully) in [math]\displaystyle{ O(n \log m) }[/math] time. If [math]\displaystyle{ m }[/math] increases too slowly between passes, the number of iterations may be large; on the other hand, if it rises too quickly, the first [math]\displaystyle{ m }[/math] for which the algorithm terminates successfully may be much larger than [math]\displaystyle{ h }[/math], and produce a complexity [math]\displaystyle{ O(n \log m) \gt O(n \log h) }[/math].

Squaring Strategy

One possible strategy is to square the value of [math]\displaystyle{ m }[/math] at each iteration, up to a maximum value of [math]\displaystyle{ n }[/math] (corresponding to a partition in singleton sets).[4] Starting from a value of 2, at iteration [math]\displaystyle{ t }[/math], [math]\displaystyle{ m = \min \left(n,2^{2^t} \right) }[/math] is chosen. In that case, [math]\displaystyle{ O(\log\log h) }[/math] iterations are made, given that the algorithm terminates once we have

[math]\displaystyle{ m = 2^{2^t} \geq h \iff \log \left( 2^{2^t} \right) \geq \log h \iff 2^t \geq \log h \iff \log {2^t} \geq \log {\log h} \iff t \geq \log {\log h}, }[/math]

with the logarithm taken in base [math]\displaystyle{ 2 }[/math], and the total running time of the algorithm is

[math]\displaystyle{ \sum_{t=0}^{\lceil \log\log h \rceil} O\left(n \log \left(2^{2^t} \right)\right) = O(n) \sum_{t=0}^{\lceil \log\log h \rceil} 2^t = O\left(n \cdot 2^{1+\lceil \log\log h \rceil}\right) = O(n \log h). }[/math]

In three dimensions

To generalize this construction for the 3-dimensional case, an [math]\displaystyle{ O(n \log n) }[/math] algorithm to compute the 3-dimensional convex hull by Preparata and Hong should be used instead of Graham scan, and a 3-dimensional version of Jarvis's march needs to be used. The time complexity remains [math]\displaystyle{ O(n \log h) }[/math].[1]

Pseudocode

In the following pseudocode, text between parentheses and in italic are comments. To fully understand the following pseudocode, it is recommended that the reader is already familiar with Graham scan and Jarvis march algorithms to compute the convex hull, [math]\displaystyle{ C }[/math], of a set of points, [math]\displaystyle{ P }[/math].

Input: Set [math]\displaystyle{ P }[/math] with [math]\displaystyle{ n }[/math] points .
Output: Set [math]\displaystyle{ C }[/math] with [math]\displaystyle{ h }[/math] points, the convex hull of [math]\displaystyle{ P }[/math].
(Pick a point of [math]\displaystyle{ P }[/math] which is guaranteed to be in [math]\displaystyle{ C }[/math]: for instance, the point with the lowest y coordinate.)
(This operation takes [math]\displaystyle{ \mathcal{O}(n) }[/math] time: e.g., we can simply iterate through [math]\displaystyle{ P }[/math].)
[math]\displaystyle{ p_1 := PICK\_START(P) }[/math]
([math]\displaystyle{ p_0 }[/math] is used in the Jarvis march part of this Chan's algorithm,
so that to compute the second point, [math]\displaystyle{ p_2 }[/math], in the convex hull of [math]\displaystyle{ P }[/math].)
(Note: [math]\displaystyle{ p_0 }[/math] is not a point of [math]\displaystyle{ P }[/math].)
(For more info, see the comments close to the corresponding part of the Chan's algorithm.)
[math]\displaystyle{ p_0 := (-\infty, 0) }[/math]
(Note: [math]\displaystyle{ h }[/math], the number of points in the final convex hull of [math]\displaystyle{ P }[/math], is not known.)
(These are the iterations needed to discover the value of [math]\displaystyle{ m }[/math], which is an estimate of [math]\displaystyle{ h }[/math].)
([math]\displaystyle{ h \leq m }[/math] is required for this Chan's algorithm to find the convex hull of [math]\displaystyle{ P }[/math].)
(More specifically, we want [math]\displaystyle{ h \leq m \leq h^2 }[/math], so that not to perform too many unnecessary iterations
and so that the time complexity of this Chan's algorithm is [math]\displaystyle{ \mathcal{O}(n \log h) }[/math].)
(As explained above in this article, a strategy is used where at most [math]\displaystyle{ \log \log n }[/math] iterations are required to find [math]\displaystyle{ m }[/math].)
(Note: the final [math]\displaystyle{ m }[/math] may not be equal to [math]\displaystyle{ h }[/math], but it is never smaller than [math]\displaystyle{ h }[/math] and greater than [math]\displaystyle{ h^2 }[/math].)
(Nevertheless, this Chan's algorithm stops once [math]\displaystyle{ h }[/math] iterations of the outermost loop are performed,
that is, even if [math]\displaystyle{ m \neq h }[/math], it doesn't perform [math]\displaystyle{ m }[/math] iterations of the outermost loop.)
(For more info, see the Jarvis march part of this algorithm below, where [math]\displaystyle{ C }[/math] is returned if [math]\displaystyle{ p_{i+1} == p_1 }[/math].)
for [math]\displaystyle{ 1\leq t\leq \log \log n }[/math] do
(Set parameter [math]\displaystyle{ m }[/math] for the current iteration. A "squaring scheme" is used as described above in this article.
There are other schemes: for example, the "doubling scheme", where [math]\displaystyle{ m = 2^t }[/math], for [math]\displaystyle{ t=1, \dots, \left \lceil \log h \right\rceil }[/math].
If the "doubling scheme" is used, though, the resulting time complexity of this Chan's algorithm is [math]\displaystyle{ \mathcal{O}(n \log^2 h) }[/math].)
[math]\displaystyle{ m:=2^{2^t} }[/math]
(Initialize an empty list (or array) to store the points of the convex hull of [math]\displaystyle{ P }[/math], as they are found.)
[math]\displaystyle{ C := () }[/math]
[math]\displaystyle{ ADD(C, p_1) }[/math]
(Arbitrarily split set of points [math]\displaystyle{ P }[/math] into [math]\displaystyle{ K = \left\lceil \frac{n}{m} \right\rceil }[/math] subsets of roughly [math]\displaystyle{ m }[/math] elements each.)
[math]\displaystyle{ Q_1, Q_2, \dots , Q_K := SPLIT(P, m) }[/math]
(Compute the convex hull of all [math]\displaystyle{ K }[/math] subsets of points, [math]\displaystyle{ Q_1,Q_2, \dots , Q_K }[/math].)
(It takes [math]\displaystyle{ \mathcal{O}(K m \log m) = \mathcal{O}(n \log m) }[/math] time.)
If [math]\displaystyle{ m \leq h^2 }[/math], then the time complexity is [math]\displaystyle{ \mathcal{O}(n \log h^2) = \mathcal{O}(n \log h) }[/math].)
for [math]\displaystyle{ 1\leq k\leq K }[/math] do
(Compute the convex hull of subset [math]\displaystyle{ k }[/math], [math]\displaystyle{ Q_k }[/math], using Graham scan, which takes [math]\displaystyle{ \mathcal{O}(m \log m) }[/math] time.)
([math]\displaystyle{ C_k }[/math] is the convex hull of the subset of points [math]\displaystyle{ Q_k }[/math].)
[math]\displaystyle{ C_k := GRAHAM\_SCAN(Q_k) }[/math]
(At this point, the convex hulls [math]\displaystyle{ C_1, C_2, \dots, C_K }[/math] of respectively the subsets of points [math]\displaystyle{ Q_1, Q_2, \dots , Q_K }[/math] have been computed.)
(Now, use a modified version of the Jarvis march algorithm to compute the convex hull of [math]\displaystyle{ P }[/math].)
(Jarvis march performs in [math]\displaystyle{ \mathcal{O}(nh) }[/math] time, where [math]\displaystyle{ n }[/math] is the number of input points and [math]\displaystyle{ h }[/math] is the number of points in the convex hull.)
(Given that Jarvis march is an output-sensitive algorithm, its running time depends on the size of the convex hull, [math]\displaystyle{ h }[/math].)
(In practice, it means that Jarvis march performs [math]\displaystyle{ h }[/math] iterations of its outermost loop.
At each of these iterations, it performs at most [math]\displaystyle{ n }[/math] iterations of its innermost loop.)
(We want [math]\displaystyle{ h \leq m \leq h^2 }[/math], so we do not want to perform more than [math]\displaystyle{ m }[/math] iterations in the following outer loop.)
(If the current [math]\displaystyle{ m }[/math] is smaller than [math]\displaystyle{ h }[/math], i.e. [math]\displaystyle{ m \lt h }[/math], the convex hull of [math]\displaystyle{ P }[/math] cannot be found.)
(In this modified version of Jarvis march, we perform an operation inside the innermost loop which takes [math]\displaystyle{ \mathcal{O}(\log m) }[/math] time.
Hence, the total time complexity of this modified version is
[math]\displaystyle{ \mathcal{O}(m K \log m) = \mathcal{O}(m \left\lceil \frac{n}{m} \right\rceil \log m) = \mathcal{O}(n \log m) = \mathcal{O}(n \log 2^{2^t}) = \mathcal{O}(n 2^t). }[/math]
If [math]\displaystyle{ m \leq h^2 }[/math], then the time complexity is [math]\displaystyle{ \mathcal{O}(n \log h^2) = \mathcal{O}(n \log h) }[/math].)
for [math]\displaystyle{ 1\leq i\leq m }[/math] do
(Note: here, a point in the convex hull of [math]\displaystyle{ P }[/math] is already known, that is [math]\displaystyle{ p_1 }[/math].)
(In this inner for loop, [math]\displaystyle{ K }[/math] possible next points to be on the convex hull of [math]\displaystyle{ P }[/math], [math]\displaystyle{ q_{i,1}, q_{i,2}, \dots, q_{i,K} }[/math], are computed.)
(Each of these [math]\displaystyle{ K }[/math] possible next points is from a different [math]\displaystyle{ C_k }[/math]:
that is, [math]\displaystyle{ q_{i,k} }[/math] is a possible next point on the convex hull of [math]\displaystyle{ P }[/math] which is part of the convex hull of [math]\displaystyle{ C_k }[/math].)
(Note: [math]\displaystyle{ q_{i,1}, q_{i,2}, \dots, q_{i,K} }[/math] depend on [math]\displaystyle{ i }[/math]: that is, for each iteration [math]\displaystyle{ i }[/math], there are [math]\displaystyle{ K }[/math] possible next points to be on the convex hull of [math]\displaystyle{ P }[/math].)
(Note: at each iteration [math]\displaystyle{ i }[/math], only one of the points among [math]\displaystyle{ q_{i,1}, q_{i,2}, \dots, q_{i,K} }[/math] is added to the convex hull of [math]\displaystyle{ P }[/math].)
for [math]\displaystyle{ 1\leq k\leq K }[/math] do
([math]\displaystyle{ JARVIS\_BINARY\_SEARCH }[/math] finds the point [math]\displaystyle{ d \in C_k }[/math] such that the angle [math]\displaystyle{ \measuredangle p_{i-1}p_id }[/math] is maximized[why?],
where [math]\displaystyle{ \measuredangle p_{i-1}p_id }[/math] is the angle between the vectors [math]\displaystyle{ \overrightarrow{p_ip_{i-1}} }[/math] and [math]\displaystyle{ \overrightarrow{p_i d} }[/math]. Such [math]\displaystyle{ d }[/math] is stored in [math]\displaystyle{ q_{i,k} }[/math].)
(Angles do not need to be calculated directly: the orientation test can be used .)
([math]\displaystyle{ JARVIS\_BINARY\_SEARCH }[/math] can be performed in [math]\displaystyle{ \mathcal{O}(\log m) }[/math] time.)
(Note: at the iteration [math]\displaystyle{ i = 1 }[/math], [math]\displaystyle{ p_{i-1} = p_0 = (-\infty, 0) }[/math] and [math]\displaystyle{ p_1 }[/math] is known and is a point in the convex hull of [math]\displaystyle{ P }[/math]:
in this case, it is the point of [math]\displaystyle{ P }[/math] with the lowest y coordinate.)
[math]\displaystyle{ q_{i,k} := JARVIS\_BINARY\_SEARCH(p_{i-1}, p_i, C_k) }[/math]
(Choose the point [math]\displaystyle{ z \in \{ q_{i,1}, q_{i,2}, \dots, q_{i,K} \} }[/math] which maximizes the angle [math]\displaystyle{ \measuredangle p_{i-1}p_iz }[/math][why?] to be the next point on the convex hull of [math]\displaystyle{ P }[/math].)
[math]\displaystyle{ p_{i+1} := JARVIS\_NEXT\_CH\_POINT(p_{i-1}, p_i, (q_{i,1}, q_{i,2}, \dots, q_{i,K})) }[/math]
(Jarvis march terminates when the next selected point on the convext hull, [math]\displaystyle{ p_{i+1} }[/math], is the initial point, [math]\displaystyle{ p_{1} }[/math].)
if [math]\displaystyle{ p_{i+1} == p_1 }[/math]
(Return the convex hull of [math]\displaystyle{ P }[/math] which contains [math]\displaystyle{ i = h }[/math] points.)
(Note: of course, no need to return [math]\displaystyle{ p_{i+1} }[/math] which is equal to [math]\displaystyle{ p_{1} }[/math].)
return [math]\displaystyle{ C := (p_1, p_2, \dots, p_{i}) }[/math]
else
[math]\displaystyle{ ADD(C, p_{i+1}) }[/math]
(If after [math]\displaystyle{ m }[/math] iterations a point [math]\displaystyle{ p_{i+1} }[/math] has not been found so that [math]\displaystyle{ p_{i+1} == p_1 }[/math], then [math]\displaystyle{ m \lt h }[/math].)
(We need to start over with a higher value for [math]\displaystyle{ m }[/math].)

Implementation

Chan's paper contains several suggestions that may improve the practical performance of the algorithm, for example:

  • When computing the convex hulls of the subsets, eliminate the points that are not in the convex hull from consideration in subsequent executions.
  • The convex hulls of larger point sets can be obtained by merging previously calculated convex hulls, instead of recomputing from scratch.
  • With the above idea, the dominant cost of algorithm lies in the pre-processing, i.e., the computation of the convex hulls of the groups. To reduce this cost, we may consider reusing hulls computed from the previous iteration and merging them as the group size is increased.

Extensions

Chan's paper contains some other problems whose known algorithms can be made optimal output sensitive using his technique, for example:

  • Computing the lower envelope [math]\displaystyle{ L(S) }[/math] of a set [math]\displaystyle{ S }[/math] of [math]\displaystyle{ n }[/math] line segments, which is defined as the lower boundary of the unbounded trapezoid of formed by the intersections.
  • Hershberger[5] gave an [math]\displaystyle{ O(n\log n) }[/math] algorithm which can be sped up to [math]\displaystyle{ O(n\log h) }[/math], where h is the number of edges in the envelope
  • Constructing output sensitive algorithms for higher dimensional convex hulls. With the use of grouping points and using efficient data structures, [math]\displaystyle{ O(n\log h) }[/math] complexity can be achieved provided h is of polynomial order in [math]\displaystyle{ n }[/math].

See also

References

  1. 1.0 1.1 Chan, Timothy M. (1996). "Optimal output-sensitive convex hull algorithms in two and three dimensions". Discrete & Computational Geometry 16 (4): 361–368. doi:10.1007/BF02712873. 
  2. Nielsen, Frank (2000). "Grouping and Querying: A Paradigm to Get Output-Sensitive Algorithms". Discrete and Computational Geometry. Lecture Notes in Computer Science. 1763. pp. 250–257. doi:10.1007/978-3-540-46515-7_21. ISBN 978-3-540-67181-7. 
  3. Frank Nielsen. "Adaptive Computational Geometry". Ph.D. thesis, INRIA, 1996.
  4. "Derandomizing an output-sensitive convex hull algorithm in three dimensions". Computational Geometry 5: 27–32. 1995. doi:10.1016/0925-7721(94)00018-Q. 
  5. Hershberger, John (1989). "Finding the upper envelope of n line segments in O(n log n) time". Information Processing Letters 33 (4): 169–174. doi:10.1016/0020-0190(89)90136-1.