CS663: Computational Geometry
Matrix-Searching and its Applications
Lecturer: Prof. Asish Mukhopadhyay
Scribe: Sudipta Sengupta (93280)
1
Introduction
The farthest pairs problem was discussed in the class. The edge connecting the farthest pair is
called the diameter of the point set. The observation that the diameter is achieved by two points
on the convex hull reduces the problem to computing the diameter of a convex polygon. Thus, the
algorithm consists of the following two broad steps:
1. Compute the convex hull CH of the point set.
2. Compute the diameter of CH.
Step 1 can be done optimally in time O(n log n). An O(n) algorithm to achieve step 2 was discussed
in the class. It involved looking at the antipodal pairs of CH. This yields an O(n log n) algorithm
for the farthest pairs problem.
In the next section, we prove that the farthest pairs problem has a lower bound of Ω(n log n).
We then move on to the all farthest pairs problem, and discuss an O(n log n) algorithm for this
problem which makes use of a new paradigm called matrix searching.
2
Lower bound for the farthest pairs problem
We begin by defining the set-disjointness problem.
Definition: (Set-disjointness problem) Given two sets X = {x1 , x2 , . . . , xn }, and Y = {y1 , y2 , . . . , yn },
where xi , yi ∈ ℜ+ ∪ {0} for all i, we want to check whether there exists a (i, j) such that xi = yj .
Using Ben-Or’s decision tree model, it has been shown that set-disjointness problem has Ω(n log n)
complexity. The optimal O(n log n) algorithm consists of sorting the two sets and then merging
them.
We will describe an O(n) time reduction from the set-disjointness problem to the farthest pairs
problem. This will imply the Ω(n log n) lower bound. Consider an instance of the set-disjointness
problem A = {a1 , a2 , . . . , an } and B = {b1 , b2 , . . . , bn }. We need to find out whether there exist i
and j such that ai = bj . We map the sets A and B to the first and third quadrant of the unit circle
C in the plane as follows: aj is mapped to the intersection of C with the line y = aj x in the first
quadrant, while bi is mapped to the anologous intersection in the third quadrant (see figure 1). Let
S be the set of these 2n intersections. It is easy to see that the diameter of S equals 2 if and only
if there are two diametrically opposite points on C,that is, A ∩ B 6= Φ. Hence, the reduction.
1
y = aj x
P oints of setA
P oints of set B
y = bj x
Figure 1: Mapping A and B to points on the unit circle C
3
All-farthest neighbours problem for convex polygons
Problem definition: For a set of vertices and a distance function d, the all-farthest neighbours
problem is to find for each i ∈ S a farthest neighbour f (i) ∈ S such that d(i, f (i)) ≥ d(i, j) for all
j ∈ S. Here d(i, j) denotes the euclidean distance between i and j.
Assumptions: For the remainder of this lecture, we will make the following two assumptions:
1. We consider the special case in which S is the set of vertices of a convex polygon in E 2 . By
convention, the vertices appear in the order v0 , v1 , . . . , vn−1 in a clockwise traversal of the
polygon.
2. If there does not exist a unique farthest neighbour for some vertex vi , we will content ourselves
with finding any one of its farthest neighbours. Alternatively, if the distances are not unique,
they can be made so by adding lexicographic information. This will ensure that for each vi ,
there is a unique f (vi ).
In one of the previous lectures, we solved the problem of finding the farthest pair of vertices for a
convex polygon in linear time by examining all antipodal pairs. It turns out that the same idea
does not work here. As figure 2 illustrates, vertices i and f (i) may not lie on an antipodal pair. In
this example, vertex v0 lies on an antipodal pair with v1 , v2 and v3 , but f (v0 ) = v4 .
v1
v2
v3
v4
v0
Figure 2: A counterexample
Note: The approach of examining all antipodal pairs in linear time does work for solving the
all-symmetric-farthest neighbours problem. Two vertices i and j are called symmetric farthest
neighbours if f (i) = j and f (j) = i.
In designing an efficient algorithm for the all-farthest neighbours problem, we will rely on the
following property of convex polygons.
Lemma: Let u, v, x, and y be any four vertices of a convex polygon in clockwise order. Then it is
not possible that d(u, y) + d(v, x) > d(u, x) + d(v, y).
2
x
y
o
u
v
Figure 3: A convex quadrilateral
Proof: The proof is fairly simple and uses the basic triangle inequality theorem.
3.1
Monotone Matrices
It is convenient to describe the all-farthest neighbours problem in terms of the n×(2n−1) matrix M
pictured in figure 4. In the ith row of M , cell (i, i + k) contains d(i, (i + k) mod n), for 0 ≤ i ≤ n − 1
and 1 ≤ k ≤ n − 1. All other squares hold −∞. Solving the all-farthest neighbours problem is
equivalent to finding the maximum value in each row of M .
j
i
0
1
2
3
4
5
6
7
8
0
∞
1
2
∞
3
4
Figure 4: The matrix M : the shaded entries contain −∞
Note: It is not necessary to allocate space for M in the implementation of the algorithm since the
value of any entry can be computed in constant time. Also, the algorithm cannot examine all of
the vertices in the matrix, because that requires Ω(n2 ) time.
Because S is the set of vertices of a convex polygon, matrix M has a nice structure.
Definition: A matrix is said to be totally monotone if for any 2 × 2 submatrix
"
#
a b
, it is not
c d
simultaneously possible that a < b and c > d.
We now prove that the matrix M defined above is totally monotone. This fact is crucial in
obtaining an O(n) algorithm for finding the maximum value in each row.
Theorem: Matrix M is monotone.
"
#
a b
Proof: Let
be the submatrix formed by the intersection of rows i1 and i2 (i1 < i2 ) with
c d
columns j1 and j2 (j1 < j2 ). The proof is broken into two cases:
3
i1
i2
j1
j2
i1
i2
Figure 5: Case 1
Case 1: i1 and i2 do not separate j1 and j2 . See figure 5. Suppose that a < b and c > d. Then,
we have a + d < b + c. This violates the property of convex quadrilaterals proved in the previous
section.
i1
j1
i2
j2
i1
i2
Figure 6: Case 2
Case 2: i1 and i2 separate j1 and j2 . See figure 6. In this case, the only possibility is that either
b = −∞ or c = −∞. This follows from the structure of matrix M . Thus, it is not simultaneously
possible that a < b and c > d.
3.2
The Algorithm
The algorithm follows the prune-and-search paradigm. To solve a problem of size n, a sub-problem
of size n/2 is solved recursively. The solution to the sub-problem is then augmented in O(n) time
to yield a complete solution. The runnning time T (n) of the algorithm thus follows the recurrence
relation T (n) = T (n/2) + cn for some constant c > 0, which yields T (n) = Θ(n).
A subroutine REDU CE is responsible for reducing the problem size.
Subroutine
REDU CE(n, m) takes as input a n × m (m ≥ n) (totally) monotone matrix, eliminates m − n
columns without decreasing the maximum value in any row, and outputs the resulting n × n monotone matrix. In the next section, we discuss the subroutine REDU CE which requires at most
2m − n comparisons.
The algorithm consists of the following steps:
1. REDU CE(n, m) is called on input matrix M . REDU CE makes 2m − n comparisons and
4
outputs an n × n monotone matrix M ′ , without decreasing the maximum value in each row.
2. The even rows of M ′ are thrown out to form an n/2 × n matrix M ′′ .
3. REDU CE(n/2, n) is called on M ′′ . It makes 2n − n/2 = 3n/2 comparisons and outputs an
n/2 × n/2 matrix M ′′′ . This matrix forms the recursive sub-problem instance.
4. The algorithm calls itself recursively to determine the positions of the maxima in rows of M ′′′ .
5. The rows and columns that were thrown out in steps 2 and 3 are put back to obtain M ′ once
again. Note that the maxima found in the fourth steps are the maxima in the odd rows of
M ′.
6. Given the positions of the maxima in the odd rows, the maxima in the even rows of M ′ are
now determined. We make use of the fact that for any even i, the position of the maxima in
row i is sandwiched between the position of the maxima in row i − 1, and the position of the
maxima in row i + 1. This requires only 3n/2 elements of M ′ to be examined, as we prove
below.
The proof that only 3n/2 elements need to be examined in the sixth step rests on the following
lemma.
Lemma: The column position of the maximum in each row of M is not to the left of the position
of the maximum in the previous row, nor is it to the right of the position of the maximum in the
successive row.
a
b
c
d
Proof: Consider the above figure. If b and c are the maxima in adjacent rows, then a < b and
c > d, which violates the monotonicity property.
Thus, to determine the maximum in an even row, we must only examine the elements in that
row whose positions are between the positions of the maxima in the two adjacent odd rows. As
figure ?? shows, the leftmost position examined in an even row is equal to the rightmost position
examined in the previous even row. Disregarding the first element examined in each even row, at
most one element is examined in each of the n columns. Taking the n/2 first elements into account,
we see that the total number of elements examined is at most 3n/2.
The number of comparisons C(n, n) required to find the maxima in each row of an n × n
monotone matrix is given by the recurrence
C(n, n) ≤ 3n + C(n/2, n/2)
5
with the boundary condition C(1, 1) = 1. This yields C(n, n) ≤ 6n. For the more general case of
an n × m matrix, we have
C(m, n) ≤ 6n + (2m − n) = 5n + 2m
In the next section, we present the subroutine REDU CE(n, m) and show that it makes only 2m−n
comparisons.
3.3
The subroutine REDUCE
"
#
a b
REDU CE uses the monotonicity property that given any 2 × 2 sub-matrix
, it is not
c d
simultaneously possible that a < b and c > d. If we compare two elements a and b in a given row,
then either:
1. a < b which implies by monotonicity that all elements of the matrix in the same column as a
and below a (including a) can be eliminated from consideration.
4×4
staircase
a
b
Figure 7: The two cases in procedure REDU CE
2. a > b which similarly implies that all elements in the same column as b and above b (including
b) can be eliminated.
These two cases are illustrated in figure 7.
We make use of the above two facts and arrive at the subroutine REDU CE, which we describe
informally below:
1. Compare the leftmost two elements a, b in the first row. If a loses (a < b), then we can delete
the entire first column by case 1 above. If a wins (a > b), we eliminate b.
2. If we had the first case above, then do the same as in step 1 using the rest of the matrix.
Otherwise, let the new a be right below the old b and the new b be just to the right of the
new a.
Compare a and b. If a loses, then we can delete all of column 2 and concatenate column 1
to the rest of the matrix. If a wins, we can eliminate b and the one above it. Note that each
time we do not delete a whole column, we are increasing a staircase of eliminated cells.
6
The following lemma tells us about the general appearance of the matrix after any number of steps
of the above algorithm.
Lemma: In step k, we have an i × i staircase of “losers” for some i ≤ k.
Proof: By induction on i. The basis case k = 1 is obvious. By the induction hypothesis, at the
end of step k, we have an i × i staircase of losers for some i ≤ k (see figure ??). Consider what
happens in step k + 1. We compare a and b where a = M (i + 1, i + 1) and b = M (i + 1, i + 2).
There are two possible cases:
1. a loses. Then, we eliminate column (i + 1). We now have an (i − 1) × (i − 1) staircase, so the
induction holds.
2. b loses. By monotonicity, all elements above b also lose, so we have an (i+ 1)× (i+ 1) staircase
and the induction also holds.
Note that the staircase is square. Eventually, we will either finish the staircase, in which case
we can eliminate the last column, or we eliminate enough columns so we never get that far. In
either case, after we have processed all the columns, we will end up with a staircase of maximum
length n − 1, so the maximum number of columns we keep is n − 1 + 1 = n. The “+1” is for the
first column. The procedure REDU CE can be stated more formally as follows:
REDUCE(n, m, M)
C ← M; k ← 1
while C has more than n columns do
case
C(k, k) ≥ C(k, k + 1) and k < n: k ← k + 1
C(k, k) ≥ C(k, k + 1) and k = n: delete column C k+1
C(k, k) < C(k, k + 1): delete column C k ; if k > 1 then k ← k − 1
endcase
return(C)
We now prove that subroutine REDU CE makes at most 2m − n comparisons.
Theorem: Algorithm REDU CE makes at most 2m − n comparisons.
Proof: Let a, b, and c denote, respectively, the number of times the first, second, and third branches
of the case statement are executed. A column is deleted only in the last two cases, and since a
total of m − n columns are deleted, we have b + c = m − n. The index increases in the first case,
decreases or stays the same in the last case, and is unchanged in the second case. Since the index
starts at 1, and ends no larger than n, we have a − c ≤ the net increase in the index ≤ n − 1.
Combining these two facts, we have the number of comparisons t given by
t = a + b + c ≤ a + 2b + c = (a − c) + 2(b + c) ≤ (n − 1) + 2(m − n) = 2m − n − 1.
We close this section after describing the subroutine M AXCOM P U T E, which solves the maximum
problem on an n × m totally monotone matrix, where m ≥ n. We have described this procedure
7
before, and we merely restate it more formally here.
MAXCOMPUTE(A)
B ← REDU CE(A)
if n = 1 then
output the maximum and return
C ← B[2, 4, . . . , 2⌊n/2⌋; 1, 2, . . . , n]
M AXCOM P U T E(C)
From the known positions of the maxima in the even rows of B,
find the maxima in its odd rows
end
3.4
How to maintain the matrix without explicitly computing it ?
In the previous sections, we saw how the all-farthest-neighbours problem can be reduced to the
maximum problem for a totally monotone n × (2n − 1) matrix, when the point set contains the
vertices of a convex n−gon. Clearly, Ω(n2 ) time would be required if we first construct this matrix
and then solve the maximum problem. We now show that because this matrix contains only
Euclidean distances and negative numbers as its entries, we can use some simple data structure
and compute only O(n) entries of this matrix to solve the maximum problem.
First, note that each entry of the matrix A can be computed in constant time. Procedure
REDU CE can be executed in linear time by storing a list of all those columns that are live at
any step during its execution. For solving the maximum problem for this n × (2n − 1) matrix,
observe that M AXCOM P U T E(A) calls procedure REDU CE ⌈log n⌉ + 1 times, and for i ≥ 1,
during the ith level of the recursion, procedure REDU CE is executed on a matrix of size at most
⌈n/2i ⌉ × ⌈n/2i−1 ⌉. Since only those columns of the matrix that were live after the execution of
(i − 1)th level are used in the ith level, hence a list of size at most ⌈n/2i−1 ⌉ is sufficient for executing
procedure REDU CE at the ith level of the recursion. Therefore, the overhead in time and space
P n+1
required for storing and maintaining these lists is only O( log
n/2i ) = O(n). Hence, procedure
i=1
M AXCOM P U T E requires O(n) time and space.
Note: For more applications of the matrix-searching technique, see the references.
References
[1] Alok Aggarwal, Maria M. Klawe, Shlomo Moran, Peter Shor, and Robert Wilber, Geometric
Applications of a Matrix-Searching Algorithm, Algorithmica (1987), Vol. 2, pp. 195–208.
[2] A. Aggarwal and R. C. Melville, Fast computations of the modality of polygons, Journal of
Algorithms, 7 (1986), 369–381.
[3] A. Aggarwal, J. S. Chang, and C. K. Yap, Minimum area circumscribing polygons, Technical
Report, Courant Institute of Mathematical Sciences, New York University, 1985.
8
[4] Alok Aggarwal, Lecture notes for 18.409: Topics in Computational Geometry, Laboratory for
Computer Science, Massachusetts Institute of Technology.
[5] F. Preparata and M. Shamos, Computational Geometry: An Introduction, Springer-Verlag,
1985.
9
© Copyright 2026 Paperzz