Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf ·...

23
Computing Correlation between Piecewise-Linear Functions * Pankaj K. Agarwal Boris Aronov Marc van Kreveld § MaartenL¨offler § Rodrigo I. Silveira June 5, 2013 Abstract We study the problem of computing correlation between two piecewise-linear bi- variate functions defined over a common domain, where the surfaces they define in 3D—polyhedral terrains—can be transformed vertically by a linear transformation of the third coordinate (scaling and translation). We present a randomized algorithm that minimizes the maximum vertical distance between the graphs of the two functions, over all linear transformations of one of the terrains, in O(n 4/3 polylog n) expected time, where n is the total number of vertices in the graphs of the two functions. We also present approximation algorithms for minimizing the mean distance between the graphs of univariate and bivariate functions. For univariate functions we present a (1 + ε)-approximation algorithm that runs in O(n(1 + log 2 (1))) expected time, for any fixed ε> 0. The (1 + ε)-approximation algorithm for bivariate functions runs in O(n/ε) time, for any fixed ε> 0, provided the two functions are defined over the same triangulation of their domain. 1 Introduction Many types of spatial data can be modeled mathematically as a bivariate function f : D R, where D is a (planar) polygonal region of interest. Examples include annual precipitation, * A preliminary version of this paper appeared in the Proceedings of the 26th ACM Symposium on Computational Geometry [1]. Department of Computer Science, Duke University, United States; [email protected]. Work by P.A. is supported by NSF under grants CCF-09-40671, CCF-10-12254, and CCF-11-61359, by ARO grants W911NF-07-1-0376 and W911NF-08-1-0452, and by an ERDC contract W9132V-11-C-0003. Department of Computer Science and Engineering, Polytechnic Institute of NYU, United States; [email protected]. Work by B.A. is supported by a grant from the U.S.-Israel Binational Science Foundation, by NSA MSP Grant H98230-06-1-0016, and NSF Grants CCF-08-30691, CCF-11-17336, and CCF-12-18791. § Department of Information and Computing Sciences, Utrecht University, the Netherlands; {m.j.vankreveld|m.loffler}@uu.nl. Work by M.v.K. and M.L. is supported by the Netherlands Organisa- tion for Scientific Research (NWO) under the Free Competition project GOGO, under FOCUS/BRICKS grant number 642.065.503, and under grant number 639.021.123. M.L. is further supported by the Office of Naval Research under grant N00014-08-1-1015. Departament de Matem`atica Aplicada II, Universitat Polit` ecnica de Catalunya, Spain; [email protected]. Work by R.S. is supported by the FP7 Marie Curie Actions Individual Fel- lowship PIEF-GA-2009-251235, and partially supported by the ESF EUROCORES programme EuroGIGA -ComPoSe IP04-MICINN Project EUI-EURC-2011-4306. 1

Transcript of Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf ·...

Page 1: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

Computing Correlation between Piecewise-Linear Functions∗

Pankaj K. Agarwal† Boris Aronov‡ Marc van Kreveld§ Maarten Loffler§

Rodrigo I. Silveira¶

June 5, 2013

Abstract

We study the problem of computing correlation between two piecewise-linear bi-variate functions defined over a common domain, where the surfaces they define in3D—polyhedral terrains—can be transformed vertically by a linear transformation ofthe third coordinate (scaling and translation). We present a randomized algorithmthat minimizes the maximum vertical distance between the graphs of the two functions,over all linear transformations of one of the terrains, in O(n4/3 polylog n) expectedtime, where n is the total number of vertices in the graphs of the two functions. Wealso present approximation algorithms for minimizing the mean distance between thegraphs of univariate and bivariate functions. For univariate functions we present a(1 + ε)-approximation algorithm that runs in O(n(1 + log2(1/ε))) expected time, forany fixed ε > 0. The (1 + ε)-approximation algorithm for bivariate functions runs inO(n/ε) time, for any fixed ε > 0, provided the two functions are defined over the sametriangulation of their domain.

1 Introduction

Many types of spatial data can be modeled mathematically as a bivariate function f : D→ R,where D is a (planar) polygonal region of interest. Examples include annual precipitation,

∗A preliminary version of this paper appeared in the Proceedings of the 26th ACM Symposium onComputational Geometry [1].†Department of Computer Science, Duke University, United States; [email protected]. Work by

P.A. is supported by NSF under grants CCF-09-40671, CCF-10-12254, and CCF-11-61359, by ARO grantsW911NF-07-1-0376 and W911NF-08-1-0452, and by an ERDC contract W9132V-11-C-0003.‡Department of Computer Science and Engineering, Polytechnic Institute of NYU, United States;

[email protected]. Work by B.A. is supported by a grant from the U.S.-Israel Binational Science Foundation,by NSA MSP Grant H98230-06-1-0016, and NSF Grants CCF-08-30691, CCF-11-17336, and CCF-12-18791.§Department of Information and Computing Sciences, Utrecht University, the Netherlands;

{m.j.vankreveld|m.loffler}@uu.nl. Work by M.v.K. and M.L. is supported by the Netherlands Organisa-tion for Scientific Research (NWO) under the Free Competition project GOGO, under FOCUS/BRICKSgrant number 642.065.503, and under grant number 639.021.123. M.L. is further supported by the Office ofNaval Research under grant N00014-08-1-1015.¶Departament de Matematica Aplicada II, Universitat Politecnica de Catalunya, Spain;

[email protected]. Work by R.S. is supported by the FP7 Marie Curie Actions Individual Fel-lowship PIEF-GA-2009-251235, and partially supported by the ESF EUROCORES programme EuroGIGA-ComPoSe IP04-MICINN Project EUI-EURC-2011-4306.

1

Page 2: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

depth to ground water, soil salinity at the surface, elevation above sea level, and (steepness of)slope of the terrain. Data are usually collected by sampling at a number of points in D, andthe function is extended to the entire D using spatial interpolation schemes [3, 13, 21]. Thereis much work in many disciplines, including environmental sciences, geology, and statisticson analyzing such data, computing correlations among them, and testing hypotheses andmodels [9]. See [11] for a Morse-theory based method for computing the similarity of twofunctions.

The simplest type of model that may capture the correlation between two types ofspatial data is a linear model. If the two types of data are collected at exactly the sameset of locations, i.e., if the data is isotopic, then one can apply standard regression analysison the pairs of values to determine whether a linear dependence of one data set on theother exists [17, 18]. Notwithstanding its popularity because of its simplicity, this approachhas two serious shortcomings. First, the two types of data may have been collected atdifferent locations, i.e., the data is heterotopic [21], due to a difference in resolution of dataacquisition techniques or cost factors, or due to inaccessibility of regions for certain typesof measurement. Second, it does not account for a possible difference in sampling densityin subregions of the region of interest. This will bias the result toward the linear relationthat exists in the more densely sampled subregion. The first problem can be handled byusing spatial interpolation on one data set to obtain pairs of values at the locations of theother data set. The second problem can be handled by assigning a weight to each samplelocation, where the weight is lower in densely sampled subregions. Alternatively, we computepiecewise-linear representations of f and g using spatial interpolation and match them todetermine a linear dependence between f and g. This is likely to provide better resultsthan using weights at the sampled locations, which in a sense form a piecewise-constantrepresentation of each function.

Contribution. We assume the following representation of a function. Let M be a trian-gulation of the domain D. A function value f(v) is assigned to every vertex of M. Bylinearly interpolating the function value within each triangle of M, we can extend f tothe entire D. Thus M defines a piecewise-linear function f : D→ R. The graph1 of f is apolyhedral terrain, which can be represented as an xy-monotone triangulated surface whosetriangulation is induced by M. Given such representations of two functions f and g, we canmatch them directly, over all possible linear transformations on these functions, to determinea linear correlation of f and g. Note that we apply a linear transformation to only one ofthe functions. Since the functions f and g are scalar-valued, a linear function is determinedby two parameters: a scaling and a translation. Figure 1 shows an example of two terrainsrepresenting the functions f and g, and how one can be transformed to match the otherbetter.

This paper discusses the problem of computing the scaling and translation parameters thatminimize the vertical distance between two bivariate functions modeled as polyhedral terrains.In particular, let D be a planar polygonal domain, let Mf and Mg be two triangulations of D,and let f, g : D→ R be two piecewise-linear functions whose linear pieces correspond to thetriangles of Mf and Mg, respectively. We assume that Mf and Mg together have n vertices.

1Slightly abusing the terminology, we will not distinguish between a function and its graph, whenever itdoes not cause confusion.

2

Page 3: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

(a) (b) (c)

Figure 1. Two triangulated terrains (a). The top (red) terrain has been translated (b) and scaled (c)vertically to match the bottom (blue) terrain.

We consider two different ways to measure correlation between f and g: (i) minimizing themaximum vertical distance (L∞-norm) between their graphs, and (ii) minimizing the meandistance between their graphs (L1-norm). More precisely, if s is the scaling parameter and tthe translation parameter, we define functions µp : R2 → R, p = 1,∞, to measure how wellf and g match for a pair s and t in (1)–(2), as follows:

µ∞(s, t) = max(x,y)∈D

|sf(x, y) + t− g(x, y)|, (1)

µ1(s, t) =1

areaD

∫(x,y)∈D

|sf(x, y) + t− g(x, y)| dx dy. (2)

We can now define the correlation between f and g as σp(f, g) = mins,t µp(s, t), forp = 1,∞. For each p, our goal is to compute the pair (s∗, t∗) that minimizes the functionµp and to compute this minimum value σp(f, g) = µp(s

∗, t∗). One can also consider p = 2,where the goal is to minimize the root-mean-square vertical distance between the graphs off and g; this problem has been recently studied by Moroz and Aronov, who presented anear-linear algorithm for computing σ2(f, g) [16].

Before proceeding, we note that the functions µ∞(s, t) and µ1(s, t) are convex: Theexpression |sf(x, y)+ t−g(x, y)|, for fixed x and y, is a convex function of s and t. Thereforethe point-wise maximum and the integral of a family of such expressions, respectively, overall suitable pairs (x, y) are also convex. In fact, a variant of this argument applies for allp ≥ 1.

If the triangulations Mf and Mg are identical, then we say that f and g are aligned,otherwise they are unaligned. We study both aligned and unaligned versions of the problem.We can convert unaligned functions to aligned ones by computing a new triangulation Mthat is a common refinement of Mf and Mg. The number of vertices in M can vary betweenΘ(n) and Θ(n2), depending on the complexity of the overlay of Mf and Mg. If the number ofedge-edge intersections between the two triangulations is k, then the overlay has complexityO(n+ k) and can be computed in O(n+ k) time [12]. A triangulation of the overlay alsohas complexity O(n+ k). For triangulations that satisfy realistic input assumptions, one canoften show that k = O(n). Several models where this is the case are defined, for example,in [10]. In this case one can align a pair of unaligned functions without loss of asymptoticefficiency.

In Section 2 we develop algorithms for computing σ∞(f, g). We can compute the optimal

3

Page 4: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

linear transformation in linear time for aligned functions, using linear programming. Forunaligned terrains, we can avoid the potential quadratic running time by combining varioustechniques leading to an O∗(n4/3) time algorithm (the O∗-notation omits polylogarithmicfactors). In Section 4 we discuss the computation of σ1(f, g), but since it is rather technical,we first study the problem for univariate functions in Section 3. This simplified problem isalso interesting and has been studied because of its applications in analyzing time-seriesdata [14, 22]. Computing σ1 even for univariate functions requires minimizing a linear-size sum of rational functions, which we cannot hope to do exactly [19]. We show that arandomized (1 + ε)-approximation algorithm exists that takes O(n(1 + log2(1/ε))) expectedtime for univariate functions (aligned or unaligned), and O(n/ε) time for bivariate functionsin the aligned case. The latter extends immediately to O((n+k)/ε2) time for the non-alignedcase.

The main novelty of this paper lies in the algorithmic approach taken to determinecorrelation between two bivariate functions on the same domain. It gives rise to newgeometric problems that can be solved using a suitable combination of advanced algorithmicand approximation techniques. We prove several geometric properties of the correlationfunctions, which are the basis of the algorithms that we present.

2 Minimizing the Maximum Distance

Let f, g : D→ R be two piecewise-linear bivariate functions as defined above. In this sectionthe goal is to find s∗, t∗ that minimize µ(s, t) according to (1), the maximum vertical distancebetween the graphs of g and sf + t. Let X be the set of vertices in the overlay of Mf and Mg.If Mf and Mg are identical (f and g are aligned), then X is the set of its vertices, otherwiseX is the set of vertices in Mf , vertices in Mg, and intersection points of the edges in Mf andMg. As remarked, X can be as large as Θ(n2) for unaligned functions.

We first observe that the maximum of µ is always realized at a point of X, implying thatthe problem of computing σ∞(f, g) is inherently discrete. For a point v ∈ X, we define abivariate function dv : R2 → R by

dv(s, t) = |sf(v) + t− g(v)| = max{sf(v) + t− g(v), g(v)− sf(v)− t}, (3)

which is the vertical distance between f and g (for a given s and t) at v. Set D = {dv(s, t) |v ∈ X}. The above observation implies that µ(s, t) = maxv∈X dv(s, t), i.e., µ is the upperenvelope of D [20]. Hence, the problem of computing s∗, t∗ reduces to computing theminimum value on the upper envelope of D.

For each v ∈ X, we define two halfspaces (in the stz-space): γ+v : z ≥ sf(v) + t − g(v)

and γ−v : z ≥ g(v) − sf(v) − t. Then, by (3), the upper envelope of D is the same as theboundary of the convex polyhedron

⋂v∈X γ

+v ∩ γ−v . The lowest vertex (in z-direction) of this

convex polytope can be computed in O(|X|) expected time, using a linear-time randomizedalgorithm for linear programming (LP) in fixed dimensions [8]; a more involved linear-timedeterministic algorithm also exists [7].

As mentioned above, if f and g are aligned or if they satisfy realistic-input assumptions,then |X| = O(n), but otherwise it can be Θ(n2). In what follows we show how to reducethe potentially quadratic running time for the general case by not computing X explicitly,

4

Page 5: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

Algorithm 1: MinMaxDistance

1. V ∗ = ∅2. repeat3. R← random subset of X of min{|X|, 9n} points4. ξ0 ← Optimal(R ∪ V ∗)5. V ← {v ∈ X | ξ0 6∈ γ+

v ∩ γ−v }6. if 1 ≤ |V | ≤ 2n then7. V ∗ ← V ∗ ∪ V8. until V = ∅9. return ξ0

Figure 2. The algorithm for computing µ∞(s, t).

but considering only a linear number of intersection points. Roughly speaking, we avoidcomputing the set X (and the set of constraints induced by X) explicitly, by using a random-sampling approach. Our method is similar to the LP algorithm in [8], and is sketched inAlgorithm MinMaxDistance (Figure 2). For a subset Y ⊆ X, let Optimal(Y ) denote theoptimal solution for Y , i.e., the lowest vertex in the three-dimensional convex polyhedron⋂v∈Y γ

+v ∩ γ−v . As mentioned above, this can be computed in O(|Y |) expected time.

Since X is not explicitly computed, it is not straightforward to choose R and compute V ,the set of violated vertices in X. We describe these two steps in detail. Let ξ0 = (s0, t0, z0)and f0(x, y) = s0f(x, y) + t0.

Computing the violated vertices. By definition, a point v ∈ X is violated if dv(s0, t0) > z0,i.e., the vertical distance between f0 and g at v is larger than z0. The vertical distancesbetween f0 and g at the vertices of Mf and Mg can be computed in a total of O(n log n)time. More challenging is therefore finding the intersection points of pairs of edges at whichthe vertical distance is larger than z0—we want to report them only if there are at most 2nof them, without spending more time otherwise.

To report this type of violated points of X, we proceed in two steps. First we use theso-called hereditary segment tree data structure to reduce the vertical-distance problembetween line segments to a problem of reporting lines in 3D that are vertically more thanz0 apart. Then we solve that problem by mapping the lines in R3 to Plucker points andPlucker hyperplanes, and solving a halfspace range reporting problem in R5. By using thetrade-off techniques for halfspace range reporting we find all the violated constraints inO∗(n4/3 + |V |) time, with the option to stop reporting if |V | > 2n; recall that the O∗-notationomits polylogarithmic factors. A more detailed description of the algorithm follows.

For the first part, we use a two-level hereditary segment tree T [5]. Let A and B bethe sets of edges in Mf and Mg, respectively. As in [5], T can be augmented to produce abipartite clique decomposition of the intersecting pairs of segments in A×B. That is, wecompute a family F = {(A1, B1), . . . , (Au, Bu)}, where Ai ⊆ A, Bi ⊆ B and u = O(n log n),such that

(i) every segment in Ai intersects every segment in Bi;

(ii) the left endpoints of all segments of Ai lie below the lines supporting every segment ofBi, or all of them lie above these lines;

5

Page 6: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

(iii) for every intersecting pair (a, b) ∈ A×B there is exactly one i such that a ∈ Ai, b ∈ Bi;

(iv)∑

i(|Ai|+ |Bi|) = O(n log2 n).

F can be computed in O(n log2 n) time. For each (Ai, Bi) ∈ F, we “lift” every line segmenta ∈ Ai (resp. b ∈ Bi) to a line in R3, namely, the line containing the segment f0(a) (resp.g(b)). Let Ai and Bi be the sets of resulting lines in R3. We wish to report the pairs of linesin Ai × Bi that are vertically more than z0 apart.

This second problem is transformed into a halfspace range reporting problem in R5, asfollows. For a line ` ∈ Ai, let `+ = ` + (0, 0, z0) and `− = ` + (0, 0,−z0)—translates of `in the z-direction at distance z0. Set L+

i = {`+ | ` ∈ Ai} and L−i = {`− | ` ∈ Ai}. A line

`2 ∈ Bi is more than z0 apart from `1 ∈ Ai if `2 lies above `+1 or below `−1 . We thus wish toreport all pairs `, `′ in L+

i × Bi (resp. L−i × Bi) such that `′ lies above (resp. below) `.We now use Plucker points and hyperplanes to rephrase the question in terms of range

reporting. Informally, Plucker coordinates are a way to represent oriented lines as eitherpoints or hyperplanes in oriented projective 5-space. The key property of the transformationis that a line `1 intersecting another line `2 is transformed into the Plucker point p(`1) of`1 lying on the Plucker hyperplane π(`2) of `2; moreover, `1 has positive orientation withrespect to `2 if and only if p(`1) lies in the positive halfspace bounded by π(`2). We referthe reader to [6] for the technical details of the transformation.

The property (ii) of (Ai, Bi) ensures that Plucker points of all lines in Bi that lie above aline ` ∈ L+

i lie in one of the halfspaces bounded by the Plucker hyperplane of ` [6]. Hence, the

problem of reporting all pairs of `, `′ ∈ L+i × Bi such that `′ lies above ` reduces to halfspace

range reporting in R5. Using a partition-tree based data structures, all such κ+ pairs can be

reported in O∗(n4/3i + κ+) time; see e.g. [2]. Similarly, we can report, in O∗(n

4/3i + κ−) time,

all κ− pairs `, `′ ∈ L−i × Bi such that `′ lies below `. Hence, we can report all κ pairs of lines

of Ai × Bi that are more than z0 apart in O∗(n4/3i + κ) time. Furthermore, if we wish to

report at most κ′ such pairs, the algorithm takes O∗(n4/3i + min{κ, κ′}) time. Repeating

this procedure for all bipartite cliques in F, in O∗(n4/3) time we either report that morethan 2n vertices of X are violated or report all violated pairs of X (if there are at most 2nsuch points). Thus the total time spent in line 5 is O∗(n4/3).

Choosing a random sample. We can use the bipartite clique decomposition F to choosea random point of X, as follows. We first choose a random bipartite clique (Ai, Bi) ∈ F;the probability of (Ai, Bi) being chosen is |Ai| · |Bi|/k, where k =

∑j |Aj | · |Bj | is the total

number of intersecting pairs in A×B. Next, we choose a random pair (a, b) ∈ Ai ×Bi, eachpair is chosen with equal probability, and the desired point is a∩b. After having computed F,it takes O(log n) time to choose a random point. Hence, we can choose R in O(n log n) time.

Number of iterations. It can be shown that line 7 of Algorithm MinMaxDistance (i.e.,V ∗ ← V ∗ ∪ V ) will be executed at most three times using the argument in [8]. Indeed, ifξ0 6= Optimal(X), then V 6= ∅ and a point of X corresponding to one of the three halfspacesthat define Optimal(X) belongs to V and is not currently in V ∗ ∪R. It is added to V ∗ inline 7, and from then on it remains in V ∗. Therefore after executing line 7 at most threetimes, V ∗ will contain the three vertices of X that define the optimum, hence there will notbe any violated constraints in the next iteration. Following the same argument as in [8],

6

Page 7: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

based on random sampling, we can argue that |V | > 2n with a probability at most 12 . The

expected number of iterations of the loop is therefore O(1). Hence, we can conclude:

Theorem 1 Given two bivariate piecewise-linear functions over a common domain, a lineartransformation minimizing the maximum vertical distance between them can be found inO∗(n4/3) expected time, where n is the total number of vertices in the graphs of thesefunctions. If the functions are aligned, the transformation can be found in linear time.

3 Minimizing the Mean Distance: Univariate Functions

In this section, we assume D ⊆ R to be a bounded interval and f, g : D → R to be twounivariate piecewise-linear functions. Since the overlay of two one-dimensional subdivisionshas linear complexity, we can assume that f and g are aligned and they are defined by acommon subdivision M of D. For any value of s and t, let

µ(s, t) =

∫x∈D|sf(x) + t− g(x)| dx (4)

be the area between the graphs of sf + t and g over D. In order to minimize the meanvertical distance between f and g, it suffices to compute arg mins,t µ(s, t). We begin byanalyzing the function µ.

Analytic form of µ. The analytic form of the bivariate function µ(s, t) depends on the setof pairs of intersecting edges of sf + t and g. Let x1 < · · · < xn be the vertices of M, andlet ai = f(xi), bi = g(xi). Consider the vertical slab [xi, xi+1]× R; see Figure 3. Let fi andgi be the functions f and g, restricted to the interval [xi, xi+1], and let µi(s, t) be the areabetween the graphs of sfi + t and gi in the interval [xi, xi+1]. Then we have

µ(s, t) =

n−1∑i=1

µi(s, t).

Unless fi is constant,2 the region in the st-plane where sfi+ t and gi intersect is a doublewedge whose bounding lines are `i : t = −ais+ bi and `i+1 : t = −ai+1s+ bi+1. The lines `iand `i+1 correspond to the s, t pairs at which the functions gi and sfi + t intersect abovexi and xi+1, respectively, and these lines intersect at the apex (si, ti) of the double wedge,where sifi + ti = gi and µi(si, ti) = 0. Slightly abusing the notation, we also view `i as thegraph of the linear function `i(s) = bi − ais and regard `i+1 similarly. The following lemmadescribes the form of µi, illustrated in Figures 3 and 4.

Lemma 2 The function µi is linear in s and t above `i and `i+1 (in the direction of t),and below `i and `i+1. For points lying below `i and above `i+1, in the situation depicted inFigure 3, where t < `i(s) and t > `i+1(s), we have

µi(s, t) = (xi+1 − xi) ·(`i(s)− t)2 + (`i+1(s)− t)2

2(`i(s)− `i+1(s)).

A symmetric expression holds for points where t > `i(s) and t < `i+1(s).

7

Page 8: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

ai

bi

ai+1

bi+1 sai + t

bi sai+1 + t

bi+1

µi(s, t)

Figure 3. Edges of f and g, and the function µi(s, t).

s

t

(a)

s

t

(b)

Figure 4. (a) Graph of µi(s, t); red (dashed) lines show `i and `i+1, blue (solid) lines show the rays wherethe different function pieces meet. (b) Another view of µi(s, t).

Figure 4 shows the function µi(s, t) graphically. We observe that inside the double wedgedefined by `i and `i+1, µi is a fraction (i.e., rational function) that has the variable s in thedenominator. It will be useful to analyze the function µi closer. For a fixed scaling factor s,consider µi(s, t) as a function of t. By Lemma 2, µi(s, t) is linear in t below `i and `i+1, orabove both of them, and a convex quadratic function in t between `i and `i+1, i.e., µi(s, t)consists of three pieces. Moreover, µi(s, t) is symmetric and differentiable everywhere; seeFigure 5(b). Despite its complicated form, we can prove the following for µi:

2If fi is a constant the expressions that follow simplify.

s

t

(si, ti)

s = s

(a)

t

µi(s, t)

(b)

s

t

(si, ti)

s = s

(c)

Figure 5. (a) Double wedge arising from fi and gi in the st-plane. (b) Cross-section in the plane s = sshowing µi(s, t). (c) Subdivision into double wedges in the st-plane.

8

Page 9: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

Lemma 3 For every i = 1, . . . , n− 1, µi is a convex function, and the restriction of µi to aray starting at (si, ti) is a linear function.

Proof. The convexity of µi follows from the same arguments as the convexity of µ, asexplained in the introduction. As for the rest, we assume, without loss of generality, thatxi+1− xi = 1. The lines `i and `i+1 partition the st-plane into four wedges. We assume thatwe are in the situation depicted in Figure 3, where sai + t < bi and sai+1 + t > bi+1; theother situation is symmetric. The function µi is linear in the wedge lying above (or below)both `i and `i+1, so µi is obviously linear along any ray emanating from (si, ti) within them.Next, we consider the two other wedges. Consider the wedge W+ lying below `i and above`i+1; the other case is symmetric. We can then write µi(s, t) for W+ (see Lemma 2) as

µi(s, t) =(t− (`i(s) + `i+1(s))/2)2

`i(s)− `i+1(s)+`i(s)− `i+1(s)

4.

We apply the affine transform t′ = t− (`i(s) + `i+1(s))/2 and s′ = `i(s)− `i+1(s), and rewriteµi as

µi(s′, t′) =

t′2

s′+s′

4.

Along a ray from (s′, t′) = (0, 0) emanating into W+, t′ = αs′, so µi(s′, αs′) = (α2 + 1/4)s′

is linear. Hence the graph of µ(s′, t′) restricted to W+ is a cone with apex at the origin. Asimilar argument holds in the diametrically opposite wedge W−, completing the proof ofthe lemma. �

Approximating µ. Since it seems difficult to compute the minimum value of µ(s, t) exactly,we define a convex piecewise-linear function µ(s, t), for a given ε > 0, such that µ(s, t) ≤µ(s, t) ≤ (1 + ε)µ(s, t) for all s, t ∈ R, and we compute (s, t) = arg mins,t µ(s, t).

For each i, we define an approximation µi of µi: µi is already linear outside the doublewedge, so we set µi to µi there and focus on approximating the part of µi(s, t) inside thedouble wedge. We first fix a value s and approximate the univariate function µ(s, t). Wechoose d2/√εe points on the graph of µi(s, t), equally spaced along the line s = s, such thatthe maximum distance between the polygonal line through the chosen points and µi(s, t)itself is at most εµi(s, t), see Figure 5(c). We extend this approximation to all values of sby choosing lines through the new points and (si, ti), giving a partition of the plane intoΘ(1/

√ε) wedges over each of which µi is linear; note that the graph of µi is once again a

convex cone with its apex at the origin. Lemma 3 and the discussion above ensure thatµi(s, t) ≤ µi(s, t) ≤ (1 + ε)µi(s, t) everywhere. We set µ(s, t) :=

∑i µi(s, t).

Lemma 4 The function µ is convex, and

µ(s, t) ≤ µ(s, t) ≤ (1 + ε)µ(s, t).

Approximation algorithm. We now describe the algorithm for computing the point π∗ =(s, t) that minimizes µ. For each i = 1, . . . , n− 1, let Li denote the set of lines, including`i, `i+1, that define µi; |Li| = O(1/

√ε). We assume that the lines in Li are ordered from

`i to `i+1. Since the lines in Li are regularly distributed, the wedge of the arrangement

9

Page 10: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

A(Li) containing a given point x ∈ R2 can be determined in O(1) time, assuming that theb·c operation takes O(1) time. Set L =

⋃n−1i=1 Li and |L| = m. Note that the lines in L are

not in general position, as all lines within each Li pass through the point `i ∩ `i+1. Forsimplicity, assume that these are the only high-degree vertices of the arrangement A(L);every other vertex of A(L) is incident on two lines of L.

The function µ is linear within each cell of A(L), therefore the minimum of µ is achievedat a vertex of A(L). By traversing A(L) and computing the value of µ at each vertex, π∗

can be computed in O(n2/ε) time. We next describe a more efficient procedure by exploitingthe convexity of µ and using a prune-and-search approach.

Let γ be a 0-, 1-, or 2-dimensional simplex (point, segment, or triangle) in the st-plane;we treat them as closed. We use L|γ ⊆ L to denote the set of lines that cross the interiorof γ, i.e., they intersect the interior of γ but do not contain γ; set |L|γ | = mγ . (If γ is apoint, then L|γ = ∅.) Next, we use Dγ ⊆ [1 : n− 1] (where [a : b] stands for {a, a+ 1, . . . , b})to denote the set of indices j for which a line of Lj intersects γ but does not cross its interior.By the general position assumption above, |Dγ | ≤ 6; notice that there might be many suchlines; we only claim that they come from at most six families Lj . Let

π∗γ = arg minx∈γ

µ(x).

In order to compute µ(x) efficiently for any point x ∈ γ, we represent µ compactly, asfollows. We observe that if γ is not crossed by any line of Li, then µi restricted to γ is alinear function, which we denote by hi,γ . We represent µ as a pair Ψγ = (Jγ , hγ) whereJγ ⊆ [1 : n − 1] is the set of indices j for which at least one of the lines of Lj crosses theinterior of γ, i.e., Jγ = {1 ≤ j ≤ n− 1 | Lj ∩ L|γ 6= ∅}, and hγ =

∑j 6∈Jγ hj,γ . Put |Jγ | = nγ .

For any point x ∈ γ,

µ(x) =∑j∈Jγ

µj(x) + hγ(x). (5)

Let γ be a segment or a triangle. Given Ψγ and Dγ , each of the following operations canbe performed in O(nγ) time. A low-level description of some of these operations is somewhattedious, but they are nevertheless straightforward and the details are omitted.

(A1) For any x ∈ γ, we can compute µ(x) using (5). Furthermore, if γ is a segment, we canalso determine whether x = π∗γ , and, if the answer is no, whether π∗γ lies to the left ofx or to its right.

(A2) Let γ be a segment. Given π∗γ , we can determine whether π∗ = π∗γ , and, if the answeris no, whether π∗ lies in γ− or γ+, where γ− (resp. γ+) is the halfplane lying below(resp. above) the line containing γ.

(A3) We can compute the value of mγ , the number of lines of L crossing the interior of γ.Indeed, for every i ∈ Jγ , by locating the vertices of γ in A(Li), we can computemi,e = |L|γ ∩Li| in O(1) time. By repeating this for all i ∈ Jγ , the value of mγ can becomputed in O(nγ) time.

(A4) We can choose a random line from L|γ . First, we compute mi,γ for every i ∈ Jγ , asin (A3). Next, we choose an index j ∈ Jγ randomly, with the probability of j being

10

Page 11: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

chosen equal to mj,γ/mj . Then we choose a random line of Lj ∩ L|γ ; each line hasequal probability of being chosen. Since Lj∩L|γ is composed of at most two contiguoussubsequences of Lj , this step can be done in O(1) time.

We now describe two procedures. The first one computes π∗e for a segment e, and thesecond procedure computes π∗4 for a triangle 4.

Computing the minimum along a segment. Let e be a segment in the plane. Assuming thatΨe and De are given, we describe how to compute π∗e . We first check in O(ne) time whetherπ∗e is one of its endpoints. If so, we return that endpoint and stop. So assume that π∗e lies inthe interior of e. We describe a recursive procedure for computing π∗e .

Fix a sufficiently large integer constant r > 1. If me ≤ r, then we set R = L|e. Otherwise(i.e., me > r), R ⊆ L|e is a subset of r randomly and independently chosen lines (if the sameline is picked more than once, we keep only one copy). Using (A4), R can be computed inO(ne) time.

For each line ` ∈ R, we compute π = e ∩ ` and µ(e ∩ `), and we determine whetherπ∗e = π using (A1). If the answer is yes, then we return π and stop; otherwise we also knowwhether π∗e lies to the left or right of π. If the algorithm has not stopped in this step, i.e.,π∗e 6= e ∩ ` for any ` ∈ R, then we find the two consecutive intersection points π1, π2 of Rwith e such that π∗e lies in the interior of the segment e = π1π2 ⊂ e. We compute Ψe fromΨe: we begin by setting Je = ∅ and he = he. Next, for each j ∈ Je, we check whether eintersects any line of Lj . If yes, then we add j to Je, otherwise we compute hj,e in O(1)time and set he = he + hj,e. We also compute De from De. Using (A3), we compute thevalue of me. If |R| < r/2 or me > me/4, then we choose another sample R and repeat theabove steps; otherwise we recursively solve the problem for e.

Putting everything together, the running time of each iteration is O(ne). Notice that,in the recursive case, |R| ≥ r/2 with probability at least 1

2 and, if r is chosen sufficientlylarge, then a random sampling argument [15] implies that the probability that me > me/4is at most 1

4 . In particular, the probability of restarting the procedure is at most 34 , so the

number of restarts required to reach the next level of recursion is expected to be constant.Let ϕ(ne,me) be the expected running time of the overall algorithm with |Je| = ne and|L|e| = me. We obtain the following recurrence for ϕ(·, ·):

ϕ(ne,me) ≤{ϕ(ne,me) + c1ne if me > r,

c2 if me ≤ r,

where c1, c2 > 0 are constants, me ≤ me/4, and ne ≤ min{ne,me}. We claim that thesolution to the above recurrence is

ϕ(ne,me) ≤ Ane(

1 + log2

me

ne

),

where A ≥ max{2c1, c2} is a constant. The claim is obviously true for me ≤ r, so assumethat me > r. By the induction hypothesis,

ϕ(ne,me) ≤ Ane(

1 + log2

me

ne

)+ c1ne ≤ Ane

(1 + log2

me

4ne

)+ c1ne.

11

Page 12: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

First, assume that ne ≥ ne/2. Then

ϕ(ne,me) ≤ Ane(

1 + log2

me

2ne

)+ c1ne ≤ Ane

(1 + log2

me

ne

),

because A ≥ 2c1. Next, assume ne < ne/2. Then we obtain

ϕ(ne,me) ≤ Ane(

1 + log2

me

4ne

)+ c1ne

= Ane2

(1 + log2

me

ne

)−Ane

(ne2ne

(1 + log2

me

ne

)− 1− log2

me

4ne

)+ c1ne

≤ Ane(

1 + log2

me

ne

)−Ane

((ne2ne− 1

)(1 + log2

me

ne

)− log2

ne4ne

)≤ Ane

(1 + log2

me

ne

)−Ane

(ne2ne− log2

ne2ne

)(as ne ≥ 2ne and log2(me/ne) ≥ 0)

≤ Ane(

1 + log2

me

ne

).

The last inequality follows because x− log2 x ≥ 1 for x ≥ 1. Hence, the expected runningtime of the algorithm is O(ne(1 + log(me/ne))).

Computing the minimum in a triangle. Let 4 be a triangle in the st-plane. Assuming thatwe are given Ψ4 and D4, we describe how to compute π∗4. We assume that π∗ = π∗4 andthat it lies in the interior of 4, because otherwise π∗4 lies on the boundary of 4 and canbe computed using the previous procedure (see below how we detect whether π∗ lies in theinterior of 4). We describe a recursive procedure, an extension of the one described above,for computing π∗4 = π∗.

Fix a sufficiently large integer constant r > 1. If m4 ≤ r, then we set R = L|4, otherwiseR ⊆ L|4 is a subset of r randomly and independently chosen lines, with duplicates removed.By (A4), R can be computed in O(n4) time. We construct A(R), the arrangement of Rclipped within 4, and its bottom-vertex triangulation A∇(R); see [15] for the definitionof bottom-vertex triangulation. For each edge e ∈ A∇(R), we first compute Ψe from Ψ4in O(n4) time and then compute π∗e in O(ne(1 + log(me/ne))) expected time. We alsocompute Dπ∗e . Using (A2), we now determine whether π∗e = π∗. If the answer is yes, wereturn π∗ and stop. Otherwise (A2) also determines on which side of (the line supporting) ethe point π∗ lies.

Suppose π∗e 6= π∗ for any edge e of A∇(R). Since µ is convex, π∗ lies in the interior ofa unique triangle of A∇(R). Fix a triangle τ ∈ A∇(R), and let e1, e2, e3 be the edges of τ .Then π∗ lies in the interior of τ if and only if π∗ lies on the same side of ei as τ , for everyi = 1, 2, 3. For each edge e ∈ A∇(R) we know whether π∗ lies in e− or e+, therefore weidentify in O(1) time the triangle τ ∈ A∇(R) that contains π∗. We first compute Dτ ,Ψτ

from D4,Ψ4 and then mτ , using (A3), in O(nτ ) time. If mτ > m4/4 or |R| < r/2, wechoose another sample R and repeat the above steps. Otherwise, we recursively search in τ .

Again, |R| > r/2 with probability at least 12 , and if r is chosen sufficiently large then

the theory of random sampling [15, Chapter 4] implies that mτ ≤ m4/4 with probability atleast 3

4 ; so we expect a constant number of retrials. Let ψ(n4,m4) be the expected running

12

Page 13: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

time of the above algorithm. Then

ψ(n4,m4) ≤{ψ(nτ ,mτ ) + c3n4

(1 + log2

m4n4

)if m4 > r,

c4 if m4 ≤ r,

where c3, c4 are constants, nτ ≤ min{n4,mτ}, and mτ ≤ m4/4. Following the samereasoning as above, the solution to the above recurrence can be shown to be

ψ(n4,m4) = O(n4(1 + log22(m4/n4))).

Finally, to compute π∗, we call this procedure with 4 = R2, and we set J4 = [1 : n − 1],D4 = ∅, and h4 = 0. Since |L| = O(n/

√ε), the expected running time is O(n(1+log2(1/ε))).

Theorem 5 Let f and g be two univariate piecewise-linear functions with a total of n pieces.For any fixed ε > 0, a pair (s, t) can be computed in O(n(1 + log2(1/ε))) expected time suchthat σ1(f, g) ≤ µ(s, t) ≤ (1 + ε)σ1(f, g).

4 Minimizing the Mean Distance: Bivariate Functions

We extend the approach of the previous section to the case where f and g are two alignedbivariate functions, defined by a common triangulation M of D. Extending the definitionfrom the last section, for any values of s and t, let µ(s, t) denote the volume between thegraphs of sf + t and g over D, that is,

µ(s, t) =

∫(x,y)∈D

|sf(x, y) + t− g(x, y)| dx dy . (6)

The main difficulty in generalizing the previous approach is that the analogue of function µi,though also convex, is much less well-behaved. In the previous case, the bivariate functionµi(s, t) behaves essentially as a univariate function, in the sense that its graph is the boundaryof a convex cone. Now µi is a “truly bivariate” function and we are forced to use a differentmethod to approximate it, resulting in an O(n/ε)-time algorithm to compute a pair s, t suchthat

σ1(f, g) ≤ 1

areaDµ(s, t) ≤ (1 + ε)σ1(f, g).

Analysis of µ over a triangle. Let ∆ = 4vivjvk be a triangle of M, and let ∆f and ∆g bethe triangles in the graph of f and g, respectively, defined by ∆ (i.e., ∆ is their commonxy-projection). Let ai, aj , and ak be the vertices of ∆f , and let bi, bj , and bk be thecorresponding vertices of ∆g. With slight abuse of notation, we use ai, aj , and ak, and bi,bj , and bk to denote the function values of these vertices as well (i.e., ai = f(vi), bi = g(vi),etc.). Depending on the scaling s and translation t, triangle s ·∆f + t may intersect, lieabove, or lie below ∆g. The volume between the two triangles is therefore composed of oneor two parts.

In the st-plane, let `i : t = bi − sai represent the set of points (s, t) at which the verticesof ∆g and s∆f + t above vi coincide; `j and `k are defined analogously. We also view `ias a univariate linear function `i(s) = bi − sai and use a similar convention for `j and `k.

13

Page 14: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

bibk

sak + t

sai + t

saj + t

bj

(a)

s

t

`i`j`k

s = s

(b)

t

µijk(s, t)

`k(s) `i(s)`j(s)

(c)

Figure 6. (a) A pair of intersecting triangles with common xy-projection. (b) The situation of (a) correspondsto values of (s, t) in the dark cell of the parameter space. (c) The vertical cross-section of the surface µijk(s, t)for a fixed value s = s consists of four pieces (two linear and two cubic), separated by three breakpoints thatcorrespond to the three vertices of the triangle.

Generally, these three lines partition the st-plane into seven cells,3 see Figure 6(b). Let A3

denote this arrangement. Inside a cell of A3, for every choice of s and t, the same subset ofvertices of s ·∆f + t lies above ∆g, and the volume between these triangles can be expressedby a single analytic function. For example, for two triangles in the configuration shown inFigure 6(a), the expression for the volume between the triangles is

µijk(s, t) =A

3

[(`i(s) + `j(s) + `k(s)− 3t) +

2(t− `k(s))3

(`i(s)− `k(s)) · (`j(s)− `k(s))

], (7)

where A = area ∆, the area of the common projection of ∆g and s∆f + t; µijk(s, t) is thesigned volume between ∆g and s∆f + t plus twice the volume of the tetrahedron formed bythe intersection segment of the two triangles and the vertices bk and sak + t (see Figure 6(a)).The above formula is valid whenever t ≥ `k(s), t ≤ `i(s), and t ≤ `j(s). Consider a value sof s at which the line s = s intersects the lines `k, `i, `j , in this order, bottom to top (seeFigure 6(c)). The form of the function µijk in the cells of A3 met by this line are, in bottomto top order:

A

3(`i(s) + `j(s) + `k(s)− 3t), (8)

the form given by (7),

A

3

[(3t− (`i(s) + `j(s) + `k(s))) +

2(`j(s)− t)3

(`j(s)− `i(s)) · (`j(s)− `k(s))

], (9)

and finallyA

3(3t− `i(s)− `j(s)− `k(s)). (10)

The equations correspond, respectively, to s ·∆f + t lying below all vertices of ∆g, aboveonly bk, above bk and bi, and above all of them. In the remaining three faces of this

3In the degenerate case where some choice of s, t makes ∆g = s ·∆f + t, the st-plane is divided into onlysix cells, and the expressions that follow simplify.

14

Page 15: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

bk = aksbj

aj s

bi

ais

α

β∆g

s∆f

(a)

bkbj

aj s+ t

bi

ais+ t

α− t

β − t∆g

s∆f + t

aks+ tt

(b)

`k(s) `i(s)`j(s)t

V0

V (t)

β

α

(c)

Figure 7. Illustration of the proof of Lemma 6: (a) t = `k(s), (b) t ∈ [`k(s), `i(s), (c) function µijk(s, t).

arrangement, the form of the function is given by similar formulas, obtained from (7) or (9)by a permutation of indices.

A constant-factor approximation for µijk. We show that µijk(s, t) can be approximatedwithin a factor of

√2 + 2 by a constant-complexity piecewise-linear function. Recall that

µijk(s, t) is convex, due to the same arguments that hold for µ and µi. We first consider thesituation along a line s = s, i.e., fix the scaling parameter to s. As evident from (7) and (9),the univariate function µijk(s, t) has (at most) three breakpoints—it is a cubic functionbetween consecutive breakpoints and linear outside the breakpoints, see Figure 6 (c). Thenext lemma suggests how to approximate µijk(s, t) by a piecewise-linear convex function.

Lemma 6 The value of µijk(s, t) at its breakpoints is at most (√

2 + 2) mint∈R µijk(s, t), forany fixed s.

Proof. Without loss of generality, we assume that area ∆ = 1. For any t ∈ R, let ∆t denotes∆f + t. Suppose the line s = s intersects `j , `i, and `k from top to bottom in this order.Then ∆g and ∆t intersect if t lies in the interval [`k(s), `j(s)], which we refer to as theintersection interval. The breakpoints of µijk(s, t) occur at t = `k(s), t = `i(s), and t = `j(s).We set α = `j(s)− `k(s) and β = `i(s)− `k(s), 0 ≤ β ≤ α; see Figure 7(a), (c).

Among the three breakpoints of µijk(s, ·), the volume is maximum at t = `k(s) ort = `j(s) (Figure 7 (c)), and, by (8) and (10) the values at these two breakpoints are 1

3(α+β)and 1

3(2α−β), respectively. For simplicity, we assume β ≥ α/2, i.e., `i(s) ≥ (`j(s)+`k(s))/2,then the volume is maximum at t = `k(s), i.e., when ∆g and ∆t touch each other above thevertex vk of ∆ (see Figure 7(a)), and its value is

V0 := µijk(s, `k(s)) =1

3(α+ β).

(If β < α/2, we reverse the direction of the t-axis and use a symmetric argument.)Next, consider the t-interval [`k(s), `i(s)]. In this interval, ∆g and ∆t intersect in the

configuration shown in Figures 6(a) and 7(b), so using (7), µijk(s, t) can be written as

V (t) := µijk(s, t) =1

3(α+ β − 3(t− `k(s)) +

2(t− `k(s))3

3αβ.

V (t) is minimized at t = t :=√αβ/2 + `k(s), and

V (t) =1

3(α+ β − 3

√αβ/2) +

2

3αβ(αβ/2)3/2 =

1

3(α+ β −

√2αβ).

15

Page 16: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

`k(s) `j(s)

χijk(s)

γ+(s)

(a)

`k(s) `j(s)

χijk(s)

γ−(s)

(b)

Figure 8. Function µijk(s, t) and its approximation µ(s, t) (thick lines): (a) β ≥ α/2, (b) β < α/2. Thedashed lines denote the four linear functions defined in (11); µ(s, t) is the upper envelope of these fourfunctions.

Since β ≥ α/2, t lies in the interval [`k(s), `i(s)]. The convexity of µijk implies thatmint∈R µijk(s, t) = V (t) (Figure 7). Hence,

mint∈R µijk(s, t)

V0=V (t)

V0= 1−

√2αβ

α+ β.

For 0 ≤ β ≤ α, the above expression is always at least 1−1/√

2, for the minimum is attainedwhen β = α. Hence,

V0 ≤√

2√2− 1

V (t) = (√

2 + 2) mint∈R

µijk(s, t),

as claimed. �

It follows that a piecewise-linear (√

2 + 2)-approximation of µijk(s, t) can be created bytaking its highest breakpoint, choosing the unique other point on µijk(s, t) that has thesame height, using a constant function between them, and the linear functions of µijk(s, t)outside, see Figure 8. Let χijk(s) denote the t-interval on which the function is constant; werefer to this interval as the valley of µijk(s, ·).

We now return to the entire st-plane and extend the above function to a piecewise-linearbivariate function µijk : R2 → R. For an arbitrary value of s, we denote the valley of µijk asχijk(s) = [γ−(s), γ+(s)]; the precise form is given below in (12). Assume `j(s) ≥ `i(s) ≥ `k(s).(For other cases, the expressions for µijk and χijk can be written by permuting the indicesi, j, k.) Let A = area ∆ and ¯(s) = 1

2(`j(s) + `k(s)). Then (8), (10), and the proof ofLemma 6 imply that

µijk(s, t) =

A3 (`i(s) + `j(s) + `k(s)− 3t) for t < γ−(s),A3 (`i(s) + `j(s)− 2`k(s)) for t ∈ χijk(s), `i(s) ≥ ¯(s),A3 (2`j(s)− `i(s)− `k(s)) for t ∈ χijk(s), `i(s) < ¯(s),A3 (3t− `i(s)− `j(s)− `k(s)) for t > γ+(s).

(11)

Recall from the proof of Lemma 6 that, if `i(s) ≥ ¯(s), then the highest breakpoint ofµijk(s) is `k(s), so in this case γ−(s) = `k(s) and γ+(s) is the value of t for which

A

3(`i(s) + `j(s)− 2`k(s)) =

A

3(3t− `i(s)− `j(s)− `k(s)),

16

Page 17: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

γ+

γ−

λ

`i

`j

`k

(a)

s1 s3s2

Σ1 Σ2 Σ3Σ0

(b)

Figure 9. (a) Arrangement A, λ+, λ1, λ− (dotted lines), λ (dashed line), and γ−, γ+; (b) A, valley (shadedregion) of µijk, and strips Σ0, . . . ,Σ3; black circles denote the intersection points of λ and λ1, the s-valueswhere µijk changes inside the valley.

i.e., t = 13(2`i(s) + 2`j(s)− `k(s)). Similarly we can compute the values of γ−(s), γ+(s) when

`i(s) < ¯(s). We can thus write χijk as

χijk(s) =

{[`k(s),

13(2`i(s) + 2`j(s)− `k(s))

]for `i(s) ≥ ¯(s),[

13(2`i(s) + 2`k(s)− `j(s)), `j(s)

]for `i(s) < ¯(s).

(12)

The following lemma is straightforward from the construction; see Figure 8.

Lemma 7 For any fixed value of s, say, s, µijk(s, t) is the upper envelope of the four linearfunctions on the right-hand side of Eq. (11).

The convexity of µijk over the entire st-plane follows from the following lemma.

Lemma 8 The function µijk can be represented as the upper envelope of at most six linearfunctions.

Proof. We will describe a set of six linear functions such that µijk is their upper envelope.Consider the arrangement A3 of `i, `j , `k. Without loss of generality, assume that area ∆ = 1,that `i, `j , `k appear from top to bottom in this order at s = −∞, that the three lines are ingeneral position, and that two vertices of A3 appear on the upper envelope λ+ and one onthe lower envelope λ− of the three lines; refer to Figure 9. Let λ1 be the median level ofA3. We view λ+, λ−, λ1 as polygonal lines as well as piecewise-linear functions of s. Notethat γ−, the lower boundary of χijk, lies on or below λ−, and γ+, the upper boundary ofχijk, lies on or above λ+. By (11), for points (s, t) lying above γ+, µijk(s, t) is defined bythe linear function t− 1

3(`i(s) + `j(s) + `k(s)), and for points lying below γ−, it is definedby the linear function 1

3(`i(s) + `j(s) + `k(s))− t.We next focus on points lying between γ− and γ+ (shaded area in Figure 9(b)). Let

λ(s) = (λ+(s) +λ−(s))/2, which is also a polygonal chain. Let s1, s2, s3 be the s-coordinatesof the vertices of A3. The lines s = si, i = 1, 2, 3, partition the st-plane into four stripsΣ0, . . . ,Σ3. Note that the vertices of λ lie on the boundaries of these strips, so λ is a linearfunction within each strip.

We claim that λ1 intersects λ exactly once in each of Σ1 and Σ2, and at most once inΣ0 ∪ Σ3. Indeed, λ1(s1) = λ+(s1) > λ(s1) and λ1(s2) = λ+(s2) < λ(s2), implying that λ1

17

Page 18: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

and λ intersect in Σ1. A similar argument shows that they intersect once in Σ2. Finally,λ(s) = (`i(s) + `k(s))/2 and λ1 = `j in Σ0 ∪ Σ3, therefore they intersect at most once inΣ0 ∪ Σ3. This proves the claim.

Let s1, s2, s3 be the s-coordinates of the intersection points of λ1 and λ (we assume thatthey intersect at three points; fewer intersections are easier to handle). If an s-interval Idoes not contain any sa or sb, for 1 ≤ a, b ≤ 3, then clearly µijk is defined by a single linearfunction for all points (s, t) with s ∈ I and t ∈ χijk(s). Next, we claim that the functionalform of µijk does not change even at sa, for a = 1, 2, 3. Indeed, suppose sa is the s-coordinateof a vertex on the upper envelope λ+ formed by `i ∩ `j . Since λ1(sa) = λ+(sa) > λ(sa),by (11), we observe that µijk(s, t) = 1

3(`i(s) + `j(s)− 2`k(s)) for s ↑ sa as well as for s ↓ sa(and t ∈ χijk(s)). That is, the functional form of µijk does not change at sa, as λ1 and λ+

switch between `i and `j that vertex. A similar argument holds if sa is a vertex of the lowerenvelope. Hence, inside the valley, the functional form of µijk only changes at s1, s2, s3,implying that it is defined by at most four linear functions inside the valley. Altogether,µijk is defined by at most six linear functions.

Finally, we prove that µijk is the upper envelope of these six functions. Fix a value s = s.Assume that `j(s) ≥ `i(s) ≥ `k(s). By Lemma 7, µijk(s, t) is the upper envelope of the fourfunctions

f1(s, t) =1

3(`i(s) + `j(s) + `k(s))− t,

f2(s, t) = t− 1

3(`i(s) + `j(s) + `k(s)),

f3(s, t) =1

3(`i(s) + `j(s)− 2`k(s)),

f4(s, t) =1

3(2`j(s)− `i(s)− `k(s)).

The remaining two functions f5(s, t), f6(s, t) are of the form f3 or f4, obtained by permutingthe indices i, j, k. Suppose f5 is obtained from f3 by permuting the indices. Since `k(s) ≤`i(s), `j(s), we have f3(s, t) ≥ f5(s, t). Similarly if f5 is obtained from f4 by permuting theindices, then f4(s, t) ≥ f5(s, t) because `j(s) ≥ `k(s), `i(s). Hence, for every (s, t) ∈ R2,µijk(s, t) = max1≤h≤6 fh(s, t). This completes the proof of the lemma. �

Lemmas 6 and 8 together imply the following:

Corollary 9 (i) The function µijk is a piecewise-linear convex function, consisting of atmost six pieces, and can be constructed in O(1) time.

(ii) For all s, t ∈ R2 with t 6∈ χijk(s), µijk(s, t) = µijk(s, t).

(iii) For all s, t ∈ R2 with t ∈ χijk(s), µijk(s, t) ≤ µijk(s, t) ≤ (√

2 + 2)µijk(s, t).

A (1 + ε)-approximation for µijk. Using µijk we now construct a new function µijk, whichis a (1+ε)-approximation of µijk. Let ε > 0 be a parameter. Set δ =

√ε/c1, for a sufficiently

large constant c1 > 0, chosen so that 1/δ is an integer. We partition ∆ = 4vivjvk into afamily Ξ of Θ(1/δ2) = Θ(1/ε) congruent triangles by placing 1

δ − 1 equally-spaced points oneach edge of ∆ and forming a triangular grid; refer to Figure 10(a). Each triangle ρ ∈ Ξ is of

18

Page 19: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

1δ − 1

(a)

e(t0)

(b)

Figure 10. (a) Partitioning of a triangle by segments parallel to the sides; (b) µρ(s, t) = µρ(s, t) forsubtriangles ρ that do not intersect the gray strip Ψ.

the form ρ = δ∆ + a or ρ = −δ∆ + a for some a ∈ R2. For each triangle ρ ∈ Ξ, we define thefunctions µρ and µρ analogous to µijk and µijk. By construction,

∑ρ∈Ξ µρ(s, t) = µijk(s, t).

We defineµijk(s, t) =

∑ρ∈Ξ

µρ(s, t) for all s, t ∈ R2.

Lemma 10 The function µijk is convex, and for all (s, t) ∈ R2,

µijk(s, t) ≤ µijk(s, t) ≤ (1 + ε)µijk(s, t).

Proof. The convexity of µijk follows from that of µρ, for each ρ ∈ Ξ, and the first inequalityfollows since µρ is an upper bound on µρ. It thus suffices to prove the second inequality.

Fix a point (s0, t0). Without loss of generality, assume that area ∆ = 1 and `k(s0) ≤`i(s0) ≤ `j(s0). Following the notation from the proof of Lemma 6, set α = `j(s0)− `k(s0)and β = `i(s0)− `k(s0). Assume β ≥ α/2, so the value of µijk(s0, ·) at its highest breakpoint(which corresponds to the pair s0, t1, for t1 ∈ R, at which ∆g and s0∆f + t1 touch each otherabove the vertex vk; refer to Figure 7) is V0 = 1

3(α+ β).Let Ξ1 = {ρ ∈ Ξ | t0 ∈ χρ(s0)}. Since µρ(s0, t0) = µρ(s0, t0) for ρ ∈ Ξ \ Ξ1,

µijk(s0, t0) =∑ρ∈Ξ1

µρ(s0, t0) +∑

ρ∈Ξ\Ξ1

µρ(s0, t0).

Hence,

µijk(s0, t0)− µijk(s0, t0) ≤∑ρ∈Ξ1

µρ(s0, t0). (13)

We prove the following two claims below:

Claim 1 For any ρ ∈ Ξ1, µρ(s0, t0) = δ3V0.

Claim 2 There exists a constant c2 > 0 such that |Ξ1| ≤ c2/δ.

Plugging the bounds from Claims 1 and 2 into (13), we obtain

µijk(s0, t0)− µijk(s0, t0) ≤ δ3V0|Ξ1| ≤ c2δ2V0 ≤

c2

c21

εV0.

19

Page 20: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

ρvk

vi

vj

ρgs0ρf + ¯

k(s0)

s0ρf + ¯j(s0)

s0ρf + t0

(a)

`k(s0) `j(s0) t0 γ+(s0)

χρ(s0)

µρ(s0, t)

t

(b)

Figure 11. Illustration of the proof of Claim 2: (a) ρ, ρg, and s0ρf + t0; (b) function µρ(s0, t).

By Lemma 6, V0 ≤ (√

2 + 2)µijk(s0, t0), so by choosing c1 ≥√

(√

2 + 2)c2, we have

µijk(s0, t0) ≤ (1 + ε)µijk(s0, t0), as claimed. �

Proof of Claim 1. Let ρ be a triangle in Ξ. We assume ρ = δ∆ + a for some a ∈ R2;a symmetric argument holds if ρ = −δ∆ + a.

Let vk be the vertex of ρ corresponding to the vertex vk of ∆, and let ρf and ρg bethe triangles in the graphs of f and g, respectively, corresponding to ρ. If ρ ∈ Ξ1, then bydefinition, µρ(s0, t0) is the value of µρ(s0, ·) at its highest breakpoint. Since ρ = δ∆ + a and∆g and s0∆f + t1 touch each other above vk at the highest breakpoint (s0, t1) of µijk(s0, ·),the highest breakpoint of µρ(s0, ·) occurs at a pair (s0, t2), for some t2 ∈ R, at which ρg ands0ρf + t2 touch each other above the vertex vk. Furthermore, the volume between ρg ands0ρf + t2 is δ3 times the volume between ∆g and s0∆f + t1 (because the tetrahedron formedby ρg and s0ρf + t2 is a homothetic copy of that formed by ∆g and s0∆f + t1, with a scalingfactor of δ), which is V0. Hence, µρ(s0, t0) = µρ(s0, t2) = δ3V0. �

Proof of Claim 2. We keep s fixed at s0 and vary t. For any t ∈ R, let e(t) be the xy-projection of the intersection line of the planes supporting ∆g and s0∆ + t; see Figure 10(b).The slope of e(t) is fixed and e(t) translates as t varies. Let u be the direction (in thexy-plane) normal to e(t), and let ω be the width of ∆ in direction u, i.e., the distancebetween the two lines parallel to e(t) that support ∆. Recall that s0∆f + t and ∆g intersectfor t ∈ [`k(s0), `k(s0) + α], i.e., α is the vertical distance between the two planes parallel tos0∆f that support ∆g. Therefore,

(?) for any l > 0, the distance between e(t) and e(t+ lα) is lω.

Let ρ be a triangle in Ξ1. Assume ρ is of the form δ∆ + a. Extending the notation fromthe proof of Claim 1, let vi, vj , vk be the vertices of ρ corresponding to the vertices vi, vj , vkof ∆, let ρf and ρg be the triangles in the graphs of f and g, respectively, correspondingto ρ, and let ¯

i, ¯j , ¯

k be the lines for ρ analogous to `i, `j , `k for ∆. Since ρ = δ∆ + a,¯k(s0) ≤ ¯

i(s0) ≤ ¯j(s0), α := ¯

j(s0) − ¯k(s0) = δα, β := ¯

i(s0) − ¯k(s0) = δβ, and

β ≥ α/2. Since β ≥ α/2, by Eq. (12), χρ(s0) = [¯k(s0), 13(2¯

j(s0) + 2¯i(s0) − ¯

k(s0))].Note that ρg and s0ρf + t intersect for t ∈ [¯k(s0), ¯

j(s0)], and e(t0) intersects ρ in thiscase. If the two triangles do not intersect and ρ ∈ Ξ1 then s0ρf + t0 lies above ρg,

20

Page 21: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

`j(s0) < t0 ≤ 13(2¯

j(s0)+2¯i(s0)− ¯

k(s0)), and the height difference between ρg and s0ρf + t0above the vertex vj of ρ is at most r := 1

3(¯j(s0) − ¯

k(s0)) = α/3 = δα/3; the last claimfollows from the fact that ρg and s0ρf + ¯

j(s0) touch each other above the vertex vj . In eithercase, ρ intersects e(t0 − r′) for some 0 ≤ r′ ≤ r. Similarly we can argue that if β < α/2,then ρ intersects e(t0 + r′) for some 0 ≤ r′ ≤ r.

In other words, ρ intersects the strip Ψ bounded by the lines e(t0 − r) and e(t0 + r).A symmetric argument shows that ρ intersects Ψ even if it is of the form −δ∆ + a. Sincer ≤ δ

3 , by (?), the width of Ψ is 23δω, while the width of ρ in direction u is δω. Therefore

ρ 6⊆ Ψ, and if ρ intersects Ψ then it also intersects e(t0 − r) or e(t0 + r). Each of these twolines intersects O(1/δ) triangles of Ξ. Hence, |Ξ1| = O(1/δ). �

A (1 + ε)-approximation algorithm. We have all ingredients for a (1 + ε)-approximationalgorithm to compute σ1(f, g). Given ε, we choose the parameter δ as above. We partitioneach triangle ∆ = 4vivjvk ∈ M into a set Ξ∆ of O(1/δ2) = O(1/ε) tiny triangles andconstruct the function µρ for each ρ ∈ Ξ∆, as described above. Let Lρ be the set of O(1)lines containing the edges of the st-projection of the graph of µρ. Set Ξ =

⋃∆∈M Ξ∆,

L =⋃ρ∈Ξ Lρ, and m := |L| = O(n/ε). We define

µ(s, t) =∑ρ∈Ξ

µρ(s, t).

Obviously µ is a piecewise-linear convex function, and it is linear within a cell of thearrangement A(L). We compute the minimum value of µ by using a prune-and-searchmethod similar to the one in Section 3.

For a simplex γ in the st-plane, let L|γ ⊂ L denote the set of lines that cross the interiorof γ; set mγ = |L|γ |. If, for a triangle ρ ∈ Ξ, the lines of Lρ do not intersect γ, then µρrestricted to γ is a linear function, so let hγ(s, t) =

∑ρ∈Ξ:Lρ∩L|γ=∅ µρ(s, t). Then

µ(s, t) =∑

ρ∈Ξ:Lρ∩L|γ 6=∅

µρ(s, t) + hγ(s, t). (14)

Let π∗γ = minp∈γ µ(p). Given a segment γ, L|γ , and hγ , each of the following two operations,which are analogous to (A1) and (A2) in Section 3, can be performed in O(mγ) time.

(B1) For any x ∈ γ, we can compute µ(x) using Eq. (14). Furthermore, we can alsodetermine whether x = π∗γ , and, if the answer is no, whether π∗γ lies to the left of x orto its right.

(B2) Given π∗γ , we can determine whether π∗ = π∗γ , and, if the answer is no, whether π∗

lies in γ− or γ+, where γ− (resp. γ+) is the halfplane lying below (resp. above) theline containing γ.

Let τ be a triangle in the st-plane. Given L|τ and hτ , we use the same recursive procedureas in Section 3 to compute π∗τ . Initially τ is the entire st-plane, L|τ = L, and hτ = 0. Using(B1) and (B2), the procedure computes, in expected O(mτ ) time, either π∗τ or a subtriangleτ ′ ⊂ τ that contains π∗τ and mτ ′ ≤ mτ/r for some constant r ≥ 2. In the latter case, theprocedure also computes L|τ ′ and hτ ′ . Hence, the overall expected running time spent in

21

Page 22: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

computing π∗τ is O(mτ ). Initially mτ = O(n/ε), so the minimum value of µ can be computedin O(n/ε) expected time.

Putting everything together, we obtain the main result of this section:

Theorem 11 Let f and g be two aligned piecewise-linear bivariate functions, defined by atriangulation composed of n triangles, and let ε > 0 be a parameter. A pair s, t and the valueµ(s, t) can be computed in O(n/ε) expected time such that σ1(f, g) ≤ µ(s, t) ≤ (1+ε)σ1(f, g).

Remark. (i) The bound on the running time can be made worst-case by using a deterministicalgorithm for computing a (1/r)-cutting at each step of the recursive procedure [4].

(ii) It is not clear whether the expected running time can be improved to n polylog(1/ε)as in Theorem 5, for we do not know how to represent µijk compactly. However, usingClaim 2 and some geometric data structures, it is likely that the expected running time canbe improved to roughly n/

√ε but we prefer simplicity over this improvement.

(iii) For unaligned functions, the factor n in the time bound is replaced by n+ k, wherek is the number of edge intersections in the overlay, as described in the introduction. In theworst case, k = Θ(n2), but in practice, one can expect it to be close to linear.

5 Conclusions

We studied the algorithmic aspects of determining the best linear model for two bivariatefunctions, modeled as polyhedral terrains. A number of open problems remain. It is possiblethat for the minimum mean distance measure, a strong linear-time approximation schemeexists even for the bivariate case, i.e., an algorithm with running time of the form O(n+q(ε)),for some function q. It may also be possible to improve on the worst-case quadratic runningtime results for the bivariate minimum mean distance measure, for non-aligned terrains.Another natural extension would be to increase the dimension of the domain (e.g. D = R3);this paper focused on the cases D = R and D = R2. Finally, optimizing over more complextransformations than linear, or finding a linear model for more than two bivariate functions,leads to problems with more than two unknowns that are worth studying further.

Acknowledgments. The authors thank the anonymous referees for their useful comments.

References

[1] P. K. Agarwal, B. Aronov, M. J. van Kreveld, M. Loffler, and R. I. Silveira. Computing similaritybetween piecewise-linear functions. In Proc. 26th Annu. ACM Symposium on ComputationalGeometry, pages 375–383, 2010.

[2] P. K. Agarwal and J. Erickson. Geometric range searching and its relatives. In B. Chazelle, J. E.Goodman, and R. Pollack, editors, Advances in Discrete and Computational Geometry, volume223 of Contemporary Mathematics, pages 1–56. American Mathematical Society, Providence, RI,1999.

[3] P. Burrough and R. McDonnell. Principles of Geographical Information Systems. OxfordUniversity Press, 1998.

[4] B. Chazelle. Cutting hyperplanes for divide-and-conquer. Discrete & Computational Geometry,9:145–158, 1993.

22

Page 23: Computing Correlation between Piecewise-Linear Functionspankaj/publications/papers/terr-sim.pdf · representation of each function. Contribution. We assume the following representation

[5] B. Chazelle, H. Edelsbrunner, L. J. Guibas, and M. Sharir. Algorithms for bichromatic linesegment problems and polyhedral terrains. Algorithmica, 11:116–132, 1994.

[6] B. Chazelle, H. Edelsbrunner, L. J. Guibas, M. Sharir, and J. Stolfi. Lines in space: Combinatoricsand algorithms. Algorithmica, 15:428–447, 1996.

[7] B. Chazelle and J. Matousek. On linear-time deterministic algorithms for optimization problemsin fixed dimension. J. Algorithms, 21(3):579–597, 1996.

[8] K. L. Clarkson. Las Vegas algorithms for linear and integer programming when the dimensionis small. J. ACM, 42(2):488–499, 1995.

[9] N. Cressie. Statistics for Spatial Data. Chichester, John Wiley, 1991.

[10] M. de Berg, H. J. Haverkort, S. Thite, and L. Toma. I/O-efficient map overlay and point locationin low-density subdivisions. In Proc. 18th Annu. Int. Symp. on Algorithms and Computation,volume 4835 of Lecture Notes in Computer Science, pages 500–511, 2007.

[11] H. Edelsbrunner, J. Harer, V. Natarajan, and V. Pascucci. Local and global comparison ofcontinuous functions. In IEEE Visualization, pages 275–280. IEEE Computer Society, 2004.

[12] U. Finke and K. Hinrichs. Overlaying simply connected planar subdivisions in linear time. InProc. 11th Annu. ACM Symp. on Computational Geometry, pages 119–126, 1995.

[13] E. Isaaks and R. Srivastava. Applied Geostatistics. Oxford University Press, 1989.

[14] H. Li and R. Tasay. A unified approach to identifying multivariate time series models. J.American Statistical Association, 93:770–782, 1998.

[15] J. Matousek. Lectures on Discrete Geometry. Springer, 2002.

[16] G. Moroz and B. Aronov. Computing the distance between piecewise-linear bivariate functions.In Proc. 23rd Ann. ACM-SIAM Symposium on Discrete Algorithms, pages 288–293, 2012.

[17] L. O’Brian. Introducing Quantitative Geography. Routledge, 1992.

[18] D. O’Sullivan and D. Unwin. Geographic Information Analysis. Wiley, 2003.

[19] S. Schaible and J. Shi. Fractional programming: the sum-of-ratios case. Optim. MethodsSoftware, 18(2):219–229, 2003.

[20] M. Sharir and P. K. Agarwal. Davenport-Schinzel Sequences and Their Geometric Applications.Cambridge University Press, New York, 1995.

[21] H. Wackernagel. Multivariate Geostatistics: An Introduction with Applications. Springer, 3rdedition, 2003.

[22] W. Wei. Time Series Analysis: Univariate and Multivariate Methods. Addison-Wesley, 1990.

23