Interval Trees

1 Windowing Queries zoom in on a window application of range queries

2 Interval Trees a data structure for a simplified problem storing intervals in a query an interval tree

3 Rectangular Query Windows a more elaborate data structure

MCS 481 Lecture 29 Computational Geometry Jan Verschelde, 1 April 2019

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 1 / 25 Interval Trees

1 Windowing Queries zoom in on a window application of range queries

2 Interval Trees a data structure for a simplified problem storing intervals in a tree query an interval tree

3 Rectangular Query Windows a more elaborate data structure

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 2 / 25 windowing queries

Given a map, we zoom in on a window ⇒ windowing query.

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 3 / 25 Interval Trees

1 Windowing Queries zoom in on a window application of range queries

2 Interval Trees a data structure for a simplified problem storing intervals in a tree query an interval tree

3 Rectangular Query Windows a more elaborate data structure

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 4 / 25 orthogonal range queries A range is a rectangle [x : x0] × [y : y 0]. A range may be entirely contained in some region, but it may also overlap with several regions.

`5 `1 `7

3 ` 6 1 8 4

1 `2 `3 `2 7 2 `3 5 `4 `5 `6 `7

`4 `6 1 2 3 4 5 7 6 8

A is a balanced on the x-coordinate, every node stores a balanced binary tree on the y-coordinate.

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 5 / 25 fractional cascading We can save a binary search by linking sorted arrays. Consider A = [11, 20, 24, 29, 41, 48, 61, 85, 92] and B = [4, 38, 40, 54, 72, 84, 85, 85, 99].

11 20 24 29 41 48 61 85 92

4 38 40 54 72 84 85 85 99

Every element x in A has a pointer to the first element in B which is either equal to x or larger than x. Thanks to these pointers we save a binary search in B. The query time is O(log(n) + k), in a 2D range tree of n points, where k is the number of reported points.

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 6 / 25 axis parallel query windows

Consider line segments parallel either to the x- or to the y-axis. Definition (axis parallel query window) An axis parallel query window W is determined by two intervals, W = [x : x0] × [y : y 0].

Applying the results from range queries, we have the following. Lemma (cost of axis parallel query window) Let S be a of n axis parallel line segments and W be an axis parallel query window. Segments having at least one endpoint inside W can be reported in O(n log(n) + k) time with a data structure using O(n log(n)) storage and preprocessing time, where k is the number of reported segments.

Can we reduce the storage cost from O(n log(n)) to O(n)?

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 7 / 25 Interval Trees

1 Windowing Queries zoom in on a window application of range queries

2 Interval Trees a data structure for a simplified problem storing intervals in a tree query an interval tree

3 Rectangular Query Windows a more elaborate data structure

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 8 / 25 search horizontal line segments

Problem Statement: preprocess a set of n horizontal line segments such that segments intersected by a vertical line can be reported efficiently.

0 0 0 Input: S = {[x1 : x1], [x2 : x2],..., [xn : xn]}, a set of n intervals. xmid is the median of the 2n end points so that at most half of the end points of S lies at the left of xmid, and at most half of the end points of S lies at the right of xmid.

xmid xmid

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 9 / 25 classification of intervals

0 0 0 Input: S = {[x1 : x1], [x2 : x2],..., [xn : xn]}, a set of n intervals.

The median xmid leads to a classification of the intervals in S.

xmid

0 0 Ileft = { [x : x ] ∈ S | x < xmid } 0 0 Imid = { [x : x ] ∈ S | x ≤ xmid ≤ x } 0 0 Iright = { [x : x ] ∈ S | xmid < x }

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 10 / 25 query horizontal line segments

Query a set S of n horizontal line segments. Input: (S, qx ), qx represents the line x = qx . Output: { s ∈ S | s ∩ (x = qx ) 6= ∅ }.

qx xmid

1 If qx = xmid, then report Imid.

2 If qx < xmid, then report in Ileft, Imid.

3 If qx > xmid, then report in Iright, Imid.

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 11 / 25 Interval Trees

1 Windowing Queries zoom in on a window application of range queries

2 Interval Trees a data structure for a simplified problem storing intervals in a tree query an interval tree

3 Rectangular Query Windows a more elaborate data structure

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 12 / 25 the interval tree

Goal: report intervals that contain qx in O(log(n) + k) time, where k is the number of reported intervals.

0 0 0 S = {[x1 : x1], [x2 : x2],..., [xn : xn]}, median xmid. 0 0 Imid = { [x : x ] ∈ S | x ≤ xmid ≤ x } We sort s ∈ S on left and right end points.

The set Imid is stored twice:

1 Lleft contains segments of Imid sorted on the left end point,

2 Lright contains segments of Imid sorted on the right end point.

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 13 / 25 a recursive structure

1 Lleft contains segments of Imid sorted on the left end point,

2 Lright contains segments of Imid sorted on the right end point.

Imid = { s1, s2, s3, s4, s5} is a set. Lleft = [ s5, s2, s1, s4, s3 ] and Lright = [ s4, s2, s5, s3, s1 ] are lists.

s1 s2 s3 s4 s5

xmid

Exercise 1: On the example above, describe Lleft, Lright for Ileft, Iright.

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 14 / 25 a recursive algorithm Algorithm CONSTRUCTINTERVALTREE(S)

Input: a set S of intervals. Output: the root of the interval tree which stores S.

1 if S = ∅ then 2 return an empty leaf else

3 xmid = MEDIAN(S)

4 [Ileft, Imid, Iright] = SPLIT(S, xmid)

5 [Lleft, Lright] = SORT(Imid)

6 v = NODE(xmid, Lleft, Lright)

7 LEFTCHILD(v) = CONSTRUCTINTERVALTREE(Ileft)

8 RIGHTCHILD(v) = CONSTRUCTINTERVALTREE(Iright) 9 return v

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 15 / 25 the cost of an interval tree Lemma (storage cost of an interval) An interval tree for n horizontal line segments occupies O(n) storage and has depth O(log(n)).

The depth O(log(n)) follows from the median xmid. The left and right child of the root hold at most n/2 intervals. The children of each node split the data of each node in half.

0 0 A segment [x : x ] is stored only at the node where xmid ∈ [x : x ], once in the list Lleft and once in the list Lright. The total number of nodes equals n and 2n is O(n).

Lemma (cost of Algorithm CONSTRUCTINTERVALTREE) An interval tree for n segments can be constructed in O(n log(n)) time.

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 16 / 25 Interval Trees

1 Windowing Queries zoom in on a window application of range queries

2 Interval Trees a data structure for a simplified problem storing intervals in a tree query an interval tree

3 Rectangular Query Windows a more elaborate data structure

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 17 / 25 query an interval tree

1 Lleft contains segments of Imid sorted on the left end point,

2 Lright contains segments of Imid sorted on the right end point.

Imid = { s1, s2, s3, s4, s5} is a set. Lleft = [ s5, s2, s1, s4, s3 ] and Lright = [ s4, s2, s5, s3, s1 ] are lists.

s1 s2 s3 s4 s5

xmid qx

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 18 / 25 the algorithm to query an interval tree

Algorithm QUERYINTERVALTREE(v, qx )

Input: root v of an interval tree and a query point qx . Output: all intervals that contain qx .

1 if v is not a leaf then

2 if qx = xmid(v) then

3 report Lleft(v)

4 else if qx < xmid(v) then

5 walk Lleft(v), starting at interval with leftmost end point

6 in this walk, report all intervals that contain qx

7 stop walking at the first interval that does not contain qx

8 QUERYINTERVALTREE(LEFTCHILD(v), qx ) else ...

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 19 / 25 Algorithm QUERYINTERVALTREE continued

else

9 walk Lright(v), starting at interval with rightmost end point

10 in this walk, report all intervals that contain qx

11 stop walking at the first interval that does not contain qx

12 QUERYINTERVALTREE(RIGHTCHILD(v), qx )

What is the cost of QUERYINTERVALTREE? The depth of the interval tree is O(log(n)).

At node v, we report kv intervals, in time O(kv ). X So the total time is O(log(n) + k), where k = kv . node v

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 20 / 25 summary of the results

Theorem (cost of interval trees) Given n intervals, an interval tree uses O(n) storage and takes O(n log(n)) time to construct; and a query takes O(log(n) + k) time, where k is the size of the output.

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 21 / 25 Interval Trees

1 Windowing Queries zoom in on a window application of range queries

2 Interval Trees a data structure for a simplified problem storing intervals in a tree query an interval tree

3 Rectangular Query Windows a more elaborate data structure

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 22 / 25 a rectangular query window

Query a set S of n horizontal line segments. 0 0 Input: S and the query window W = [qx : qx ] × [qy : qy ]. Output: { s ∈ S | s ∩ W 6= ∅ }.

0 0 Consider replacing W on the left by qx × [qy : qy ] on the right:

W

xmid xmid

Exercise 2: What happens when we run Algorithm QUERYINTERVALTREE on the example shown above?

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 23 / 25 application of range trees

In the interval tree, instead of the sorted lists Lleft and Lright, we have two range trees at each node v, Tleft and Tright:

Tleft stores the left end points of the segments in Imid(v),

Tright stores the right end points of the segments in Imid(v).

Applying results on range trees, we obtain:

1 construction takes O(n log(n)) time, 2 storage cost is O(n log(n)), and 2 3 query time is O(log (n) + k), where k is the output size.

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 24 / 25 recommended assignments

We covered section 10.1 in the textbook. Consider the following activities, listed below.

1 Write the solutions to exercises 1 and 2. 2 Consider the exercises 1, 4, 9 in the textbook.

Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 25 / 25