Post · 9 min read· pp. 559–565

Minimum Enclosing Circle

Consider the following problem:

Library Checker - Minimum Enclosing Circle

You're given n≤105n \leq 10^5 points pi=(xi,yi)p_i=(x_i, y_i).

For each pip_i, find whether it lies on the circumference of the minimum enclosing circle of {p1,…,pn}\{p_1,\dots,p_n\}.

Here, by the minimum enclosing circle (MEC) we mean a circle with minimum possible radius that contains all the nn points, inside the circle or on its boundary. This problem has a simple randomized solution that, on first glance, looks like it would run in O(n3)O(n^3), but actually works in O(n)O(n) expected time.

To better understand the reasoning below, we should immediately note that the solution to the problem is unique:

Why is the MEC unique?

Consider the following setup: Let rr be the radius of the MEC. We draw a circle of radius rr around each of the points p1,…,pnp_1,\dots,p_n. Geometrically, the centers of circles that have radius rr and cover all the points p1,…,pnp_1,\dots,p_n form the intersection of all nn circles.

Now, if the intersection is just a single point, this already proves that it is unique. Otherwise, the intersection is a shape of non-zero area, so we can reduce rr by a tiny bit, and still have non-empty intersection, which contradicts the assumption that rr was the minimum possible radius of the enclosing circle.

With a similar logic, we can also show the uniqueness of the MEC if we additionally demand that it passes through a given specific point pip_i or two points pip_i and pjp_j (it is also unique because its radius uniquely defines it).

Alternatively, we can also assume that there are two MECs, and then notice that their intersection (which contains the points p1,…,pnp_1,\dots,p_n already) must have a smaller diameter than initial circles, and thus can be covered with a smaller circle.

Welzl's algorithm

For brevity, let's denote mec⁡(p1,…,pn)\operatorname{mec}(p_1,\dots,p_n) to be the MEC of {p1,…,pn}\{p_1,\dots,p_n\}, and let Pi={p1,…,pi}P_i = \{p_1,\dots,p_i\}.

The algorithm, initially proposed by Welzl in 1991, goes as follows:

  1. Apply a random permutation to the input sequence of points.
  2. Maintain the current candidate to be the MEC CC, starting with C=mec⁡(p1,p2)C = \operatorname{mec}(p_1, p_2).
  3. Iterate over i=3..ni=3..n and check if pi∈Cp_i \in C.
    1. If pi∈Cp_i \in C it means that CC is the MEC of PiP_i.
    2. Otherwise, assign C=mec⁡(pi,p1)C = \operatorname{mec}(p_i, p_1) and iterate over j=2..ij=2..i and check if pj∈Cp_j \in C.
      1. If pj∈Cp_j \in C, then CC is the MEC of PjP_j among circles that pass through pip_i.
      2. Otherwise, assign C=mec⁡(pi,pj)C=\operatorname{mec}(p_i, p_j) and iterate over k=1..jk=1..j and check if pk∈Cp_k \in C.
        1. If pk∈Cp_k \in C, then CC is the MEC of PkP_k among circles that pass through pip_i and pjp_j.
        2. Otherwise, C=mec⁡(pi,pj,pk)C=\operatorname{mec}(p_i,p_j,p_k) is the MEC of PkP_k among circles that pass through pip_i and pjp_j.

We can see that each level of nestedness here has an invariant to maintain (that CC is the MEC among circles that also pass through additionally given 00, 11 or 22 points), and whenever the inner loop closes, its invariant becomes equivalent to the invariant of the current iteration of its parent loop. This, in turn, ensures the correctness of the algorithm as a whole.

Omitting some technical details, for now, the whole algorithm can be implemented in C++ as follows:

text
struct point {...};

// Is represented by 2 or 3 points on its circumference
struct mec {...};

bool inside(mec const& C, point p) {
    return ...;
}

// Choose some good generator of randomness for the shuffle
mt19937_64 gen(...);
mec enclosing_circle(vector<point> &p) {
    int n = p.size();
    ranges::shuffle(p, gen);
    auto C = mec{p[0], p[1]};
    for(int i = 0; i < n; i++) {
        if(!inside(C, p[i])) {
            C = mec{p[i], p[0]};
            for(int j = 0; j < i; j++) {
                if(!inside(C, p[j])) {
                    C = mec{p[i], p[j]};
                    for(int k = 0; k < j; k++) {
                        if(!inside(C, p[k])) {
                            C = mec{p[i], p[j], p[k]};
                        }
                    }
                }
            }
        }
    }
    return C;
}

Now, it is to be expected that checking that a point pip_i is inside the MEC of 22 or 33 points can be done in O(1)O(1) (we will discuss this later on). But even then, the algorithm above looks as if it would take O(n3)O(n^3) in the worst case just because of all the nested loops. So, how come we claimed the linear expected runtime? Let's figure out!

Complexity analysis

For the inner-most loop (over kk), clearly its expected runtime is O(j)O(j) operations. What about the loop over jj?

It only triggers the next loop if pjp_j is on the boundary of the MEC of PjP_j that also passes through point ii, and removing pjp_j would further shrink the circle. Of all points in PjP_j there can only be at most 22 points with such property, because if there are more than 22 points from PjP_j on the boundary, it means that after removing any of them, there will still be at least 33 points on the boundary, sufficient to uniquely define the circle.

In other words, after initial random shuffle, there is at most 2j\frac{2}{j} probability that we get one of the at most two unlucky points as pjp_j. Summing it up over all jj from 11 to ii, we get the expected runtime of

∑j=1i2j⋅O(j)=O(i).\sum\limits_{j=1}^i \frac{2}{j} \cdot O(j) = O(i).

In exactly same fashion we can now also prove that the outermost loop has expected runtime of O(n)O(n).

Checking that a point is in the MEC of 2 or 3 points

Let's now figure out the implementation detail of point and mec. In this problem, it turns out to be particularly useful to use std::complex as a class for points:

text
using ftype = int64_t;
using point = complex<ftype>;

As a reminder, a complex number is a number of type x+yix+yi, where i2=−1i^2=-1 and x,y∈Rx, y \in \mathbb R. In C++, such complex number is represented by a 2-dimensional point (x,y)(x, y). Complex numbers already implement basic component-wise linear operations (addition, multiplication by a real number), but also their multiplication and division carry certain geometric meaning.

Without going in too much detail, we will note the most important property for this particular task: Multiplying two complex numbers adds up their polar angles (counted from OxOx counter-clockwise), and taking a conjugate (i.e. changing z=x+yiz=x+yi into z‾=x−yi\overline{z} = x-yi) multiplies the polar angle with −1-1. This allows us to formulate some very simple criteria for whether a point zz is inside the MEC of 22 or 33 specific points.

MEC of 2 points

For 22 points aa and bb, their MEC is simply the circle centered at a+b2\frac{a+b}{2} with the radius ∣a−b∣2\frac{|a-b|}{2}, in other words the circle that has abab as a diameter. To check if zz is inside this circle we simply need to check that the angle between zaza and zbzb is not acute.

Inner angles are obtuse, external angles are acute and angles on the circumference are right

Equivalently, we need to check that

I0=(b−z)(a−z)‾I_0=(b-z)\overline{(a-z)}

doesn't have a positive real coordinate (corresponding to points that have a polar angle between −90∘-90^\circ and 90∘90^\circ).

MEC of 3 points

Adding zz to the triangle abcabc will make it a quadrilateral. Consider the following expression:

∠azb+∠bca\angle azb + \angle bca

In a cyclic quadrilateral, if cc and zz are from the same side of abab, then the angles are equal, and will add up to 0∘0^\circ when summed up signed (i.e. positive if counter-clockwise and negative if clockwise). Correspondingly, if cc and zz are on the opposite sides, the angles will add up to 180∘180^\circ.

Adjacent inscribed angles are same, opposing angles complement to 180 degrees

In terms of complex numbers, we can note that ∠azb\angle azb is the polar angle of (b−z)(a−z)‾(b-z)\overline{(a-z)} and ∠bca\angle bca is the polar angle of (a−c)(b−c)‾(a-c)\overline{(b-c)}. Thus, we can conclude that ∠azb+∠bca\angle azb + \angle bca is the polar angle of

I1=(b−z)(a−z)‾(a−c)(b−c)‾I_1 = (b-z) \overline{(a-z)} (a-c) \overline{(b-c)}

If the angle is 0∘0^\circ or 180∘180^\circ, it means that the imaginary part of I1I_1 is 00, otherwise we can deduce whether zz is inside or outside of the enclosing circle of abcabc by checking the sign of the imaginary part of I1I_1. Positive imaginary part corresponds to positive angles, and negative imaginary part corresponds to negative angles.

But which one of them means that zz is inside or outside of the circle? As we already noticed, having zz inside the circle generally increases the magnitude of ∠azb\angle azb, while having it outside the circle decreases it. As such, we have the following 4 cases:

  1. ∠bca>0∘\angle bca > 0^\circ, cc on the same side of abab as zz. Then, ∠azb<0∘\angle azb < 0^\circ, and ∠azb+∠bca<0∘\angle azb + \angle bca < 0^\circ for points inside the circle.
  2. ∠bca<0∘\angle bca < 0^\circ, cc on the same side of abab as zz. Then, ∠azb>0∘\angle azb > 0^\circ, and ∠azb+∠bca>0∘\angle azb + \angle bca > 0^\circ for points inside the circle.
  3. ∠bca>0∘\angle bca > 0^\circ, cc on the opposite side of abab to zz. Then, ∠azb>0∘\angle azb > 0^\circ and ∠azb+∠bca>180∘\angle azb + \angle bca > 180^\circ for points inside the circle.
  4. ∠bca<0∘\angle bca < 0^\circ, cc on the opposite side of abab to zz. Then, ∠azb<0∘\angle azb < 0^\circ and ∠azb+∠bca<180∘\angle azb + \angle bca < 180^\circ for points inside the circle.

In other words, if ∠bca\angle bca is positive, points inside the circle will have ∠azb+∠bca<0∘\angle azb + \angle bca < 0^\circ, otherwise they will have ∠azb+∠bca>0∘\angle azb + \angle bca > 0^\circ, assuming that we normalize the angles between −180∘-180^\circ and 180∘180^\circ. This, in turn, can be checked by the signs of imaginary parts of I2=(a−c)(b−c)‾I_2=(a-c)\overline{(b-c)} and I1=I0I2I_1 = I_0 I_2.

Note: As we multiply four complex numbers to get I1I_1, the intermediate coefficients can be as large as O(A4)O(A^4), where AA is the largest coordinate magnitude in the input. On the bright side, if the input is integer, both checks above can be done fully in integers.

Implementation

Now, to actually implement the check, we should first decide how to represent the MEC. As our criteria work with the points directly, a natural and efficient way to do this is to say that MEC is directly represented as a pair or triple of points that defines it:

text
using mec = variant<
    array<point, 2>,
    array<point, 3>
>;

Now, we can use std::visit to efficiently deal with both cases in accordance with criteria above:

text
/* I < 0 if z inside C,
   I > 0 if z outside C,
   I = 0 if z on the circumference of C */
ftype indicator(mec const& C, point z) {
    return visit([&](auto &&C) {
        point a = C[0], b = C[1];
        point I0 = (b - z) * conj(a - z);
        if constexpr (size(C) == 2) {
            return real(I0);
        } else {
            point c = C[2];
            point I2 = (a - c) * conj(b - c);
            point I1 = I0 * I2;
            return imag(I2) < 0 ? -imag(I1) : imag(I1);
        }
    }, C);
}

bool inside(mec const& C, point p) {
    return indicator(C, p) <= 0;
}

Now, we can finally ensure that everything works by submitting the problem to the Library Checker: #308668.

Practice problems