Two Dimensional Range Minimum Queries and Fibonacci...

26
Two Dimensional Range Minimum Queries and Fibonacci Lattices Gerth Stølting Brodal 1 Pooya Davoodi 2 Moshe Lewenstein 3 Rajeev Raman 4 S. Srinivasa Rao 5 MADALGO, Aarhus University, Denmark. Polytechnic Institute of New York University, United States. Bar-Ilan University, Israel. University of Leicester, UK. Seoul National University, S. Korea. ESA 2012

Transcript of Two Dimensional Range Minimum Queries and Fibonacci...

Page 1: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Two Dimensional Range Minimum Queries andFibonacci Lattices

Gerth Stølting Brodal1 Pooya Davoodi2 Moshe Lewenstein3

Rajeev Raman4 S. Srinivasa Rao5

MADALGO, Aarhus University, Denmark.

Polytechnic Institute of New York University, United States.

Bar-Ilan University, Israel.

University of Leicester, UK.

Seoul National University, S. Korea.

ESA 2012

Page 2: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

2D RMQ problem

We consider the 2D matrix range minimum query (2D RMQ) problem.

This is a data structuring problem.

• Preprocess input data so as to answer series of queries.

• Want to minimize:

1. Query time.2. Space usage of data structure.3. Time of pre-processing.4. Space for pre-processing.

We do not consider updates to data, nor (4).

Falls within the general category of geometric range searching problemsdefined on points in <d .

Page 3: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Problem Definition: 2D RMQ

Input

m × n matrix A (m ≤ n) containing orderedvalues. Let N = m · n.

Queries

Given indices i1, i2, j1, j2, 1 ≤ i1 ≤ i2 ≤ m,1 ≤ j1 ≤ j2 ≤ n, return

argmini1≤i≤i2,j1≤j≤j2A[i , j ].

[4-sided queries. Also: 3- and 2-sided.]

11

24

80

48

9

37

17

12

76

37

25

36

10

38

3

85

5

90

96

63

59

79

30

97

82

50

53

95

57 79

68

59

4-sided query

1024

80

48

9

37

17

12

76

11

37

25

36

38

3

85

5

90

96

63

59

79

30

97

82

50

53

57 79

68

59

95

2-sided query

Problem first considered by [Chazelle, Rosenberg ’89].

Page 4: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Memory Usage Models

General model No restriction.

Indexing model

• Preprocess input to get index.

• Queries read index and access input (maybe expensive).

• Trade-off: index size vs. query time.

QUERY

RESULT

Ind

ex

INPUT

ACCESS

Encoding model

• Preprocess input to get index, delete input.

• Queries can only read index.

• Minimize: index size and query time.

QUERY

RESULT

Ind

ex

INPUTPREPROC

Only interesting if index size is smaller than input size. Motivations forindexing model in [Barbay et al., ’07]

Page 5: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

1D RMQ

• Encoding size is exactly 2N − O(log N) bits.• via Cartesian tree [Vuillemin, ’80, cf. Fischer and Heun, ’11].

• Data structures for 1D RMQ:• 2N + o(N) bits, O(1) query time.• o(N) bits preprocessing space, O(N) preprocessing time.

[Fischer, Heun ’11],[Davoodi, R, Rao ’12], building on [Harel,Tarjan, ’83].

• Indexing 1D RMQ:• For any 1 ≤ c ≤ N, index size O(N/c) bits ⇒ Ω(c) access

operations [Brodal, Davoodi, Rao, ’10].• Trivial matching upper bound.

1D RMQ seems well understood (many applications).

How do 1D RMQ and 2D RMQ differ? [Amir, Fischer, Lewenstein, ’07]

Page 6: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

2D RMQ: General Model

Space Query time Prep time1D RMQ O(N) bits O(1) O(N)

2D RMQ O(N) words O(α2(N)) O(N) [CR’89]

2D RMQ O(kN) words O(1) O(N log(k) N) [AFL ’07]2D RMQ O(N) words O(1) O(N) [Atallah, Yuan, ’10]

• CR’89 result holds for general semigroup summation (non-linearspace lower bound for O(1) time in this setting).

General model: 1D RMQ ≈ 2D RMQ

Page 7: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

2D RMQ: Encoding Model

Encoding model

• Preprocess input to get index, delete input.

• Queries can only read index.

• Minimize: index size and query time.

QUERY

RESULT

Ind

ex

INPUTPREPROC

Space (bits) Query time Reference1D RMQ O(N) O(1)

2D RMQ Ω(N log m) − [Demaine, Landau, Weimann ’09][Brodal, Davoodi, Rao ’10]

2D RMQ O(N) (exp) O(1) [Golin, Iacono, Krizanc, R, Rao, ’11].(random)

• m is the smaller side of the matrix.

• Encoding model: 1D RMQ 6= 2D RMQ

Page 8: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

2D Matrix RMQ: Indexing Model

Indexing model

• Preprocess input, create index.

• Queries read index and access input (readentry of A, assume O(1) time).

• Trade-off: index size vs. query time.

QUERY

RESULT

Ind

ex

INPUT

ACCESS

Space (bits) Query time1D RMQ O(N/c) Θ(c) [Brodal, Davoodi, Rao ’10]

2D RMQ O(N/c) O(c(log c)2) [Brodal, Davoodi, Rao ’10]2D RMQ O(N/c) O(c log c(log log c)2) (NEW)

Indexing model: 1D RMQ?= 2D RMQ

Page 9: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Main Result

Theorem

There is an index of size O(N/c) bits that supports 2D RMQs in amatrix of size N in O(c log c(log log c)2) query time, for any c ≤ N.

Key Step: 2-sided RMQs

Given an r × r matrix, give an index of size O(r) bits that answers2-sided RMQs in O(r log r) time.

• Previous solution: index size O(r log r) bits,O(r log r) time [Brodal, Davoodi, Rao, ’10].

• Main new ideas: use of Fibonacci lattice [Fiat,Shamir ’00] and succinct index for geometric2-sided RMQ [Farzan, Munro, R. ’12].

Page 10: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Fibonacci Lattice

• Fibonacci lattice of order r specifies rpoints in an r × r lattice, providedr = Fk for some k .

• Fibonacci points (FPs) are(i , (iFk−1) mod r) fori = 0, 1, . . . , r − 1.

• Fibonacci lattice used in graphics,circuit layout and for lower boundson geometric problems, we use it forupper bounds.

Fibonacci lattice (order 8)

Fibonacci lattice property [Fiat, Shamir ’00]

Any rectangle of area ≥ αr for some constant α > 0 contains at leastone FP.

Page 11: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Answering a Query

2-sided RMQ

Given an r × r sub-matrix. Answer 2-sided RMQs in O(r log r) time andO(r) bits space.

• Place r FPs in the sub-matrix. EachFP’s priority is set to the smallest valuein the sub-matrix above and to its left.

• Given query point q, find FP in queryregion with smallest priority.

• The FPs define a lower envelope abovethe boundaries specified by the query.

• Query divided into “upper” and“lower” parts. Scan entire lower part.

Page 12: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Answering a Query

2-sided RMQ

Given an r × r sub-matrix. Answer 2-sided RMQs in O(r log r) time andO(r) bits space.

• Place r FPs in the sub-matrix. EachFP’s priority is set to the smallest valuein the sub-matrix above and to its left.

• Given query point q, find FP in queryregion with smallest priority.

• The FPs define a lower envelope abovethe boundaries specified by the query.

• Query divided into “upper” and“lower” parts. Scan entire lower part.

Page 13: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Answering a Query

2-sided RMQ

Given an r × r sub-matrix. Answer 2-sided RMQs in O(r log r) time andO(r) bits space.

• Place r FPs in the sub-matrix. EachFP’s priority is set to the smallest valuein the sub-matrix above and to its left.

• Given query point q, find FP in queryregion with smallest priority.

• The FPs define a lower envelope abovethe boundaries specified by the query.

• Query divided into “upper” and“lower” parts. Scan entire lower part.

Page 14: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Answering a Query

2-sided RMQ

Given an r × r sub-matrix. Answer 2-sided RMQs in O(r log r) time andO(r) bits space.

• Place r FPs in the sub-matrix. EachFP’s priority is set to the smallest valuein the sub-matrix above and to its left.

• Given query point q, find FP in queryregion with smallest priority.

• The FPs define a lower envelope abovethe boundaries specified by the query.

• Query divided into “upper” and“lower” parts. Scan entire lower part.

Page 15: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Answering a Query

2-sided RMQ

Given an r × r sub-matrix. Answer 2-sided RMQs in O(r log r) time andO(r) bits space.

• Place r FPs in the sub-matrix. EachFP’s priority is set to the smallest valuein the sub-matrix above and to its left.

• Given query point q, find FP in queryregion with smallest priority.

• The FPs define a lower envelope abovethe boundaries specified by the query.

• Query divided into “upper” and“lower” parts. Scan entire lower part.

Page 16: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Area Under Lower Envelope

2-sided RMQ

Given an r × r sub-matrix. Answer 2-sided RMQs in O(r log r) time andO(r) bits space.

We scan the area under the lower envelope — how large is it?

• Let z = 2d(log√αr)/2e.(z2 ≥ αr).

• Align z × z square to query point.• Must intersect lower envelope.

• Consider z/2× 2z , 2z × z/2 rectanglesand place as shown.• Must intersect lower envelope.

• Total area O(r log r).

• Double width of rectangles — coverslower envelope, area still O(r log r).

Page 17: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Area Under Lower Envelope

2-sided RMQ

Given an r × r sub-matrix. Answer 2-sided RMQs in O(r log r) time andO(r) bits space.

We scan the area under the lower envelope — how large is it?

• Let z = 2d(log√αr)/2e.(z2 ≥ αr).

• Align z × z square to query point.• Must intersect lower envelope.

• Consider z/2× 2z , 2z × z/2 rectanglesand place as shown.• Must intersect lower envelope.

• Total area O(r log r).

• Double width of rectangles — coverslower envelope, area still O(r log r).

r

r

Page 18: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Area Under Lower Envelope

2-sided RMQ

Given an r × r sub-matrix. Answer 2-sided RMQs in O(r log r) time andO(r) bits space.

We scan the area under the lower envelope — how large is it?

• Let z = 2d(log√αr)/2e.(z2 ≥ αr).

• Align z × z square to query point.• Must intersect lower envelope.

• Consider z/2× 2z , 2z × z/2 rectanglesand place as shown.• Must intersect lower envelope.

• Total area O(r log r).

• Double width of rectangles — coverslower envelope, area still O(r log r).

r

r

2r

Page 19: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Area Under Lower Envelope

2-sided RMQ

Given an r × r sub-matrix. Answer 2-sided RMQs in O(r log r) time andO(r) bits space.

We scan the area under the lower envelope — how large is it?

• Let z = 2d(log√αr)/2e.(z2 ≥ αr).

• Align z × z square to query point.• Must intersect lower envelope.

• Consider z/2× 2z , 2z × z/2 rectanglesand place as shown.• Must intersect lower envelope.• Total area O(r log r).

• Double width of rectangles — coverslower envelope, area still O(r log r).

r

r

2r

Page 20: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Area Under Lower Envelope

2-sided RMQ

Given an r × r sub-matrix. Answer 2-sided RMQs in O(r log r) time andO(r) bits space.

We scan the area under the lower envelope — how large is it?• Let z = 2d(log

√αr)/2e.(z2 ≥ αr).

• Align z × z square to query point.• Must intersect lower envelope.

• Consider z/2× 2z , 2z × z/2 rectanglesand place as shown.• Must intersect lower envelope.• Total area O(r log r).

• Double width of rectangles — coverslower envelope, area still O(r log r).

r

r

2r

Page 21: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Solving Upper Part of Query

2-sided RMQ

Given an m ×m sub-matrix. Answer 2-sided RMQs in O(r log r) timeand O(r) bits space.

• Upper region could be of area Θ(r2).

• Modify priorities:• If FP p has FP q above and to left of

p with same priority, change priorityof p to +∞.

• RMQ value lies in area shown.• Size of this area is O(r log r).

Overall O(r log r) time. Space usage?

Page 22: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Solving Upper Part of Query

2-sided RMQ

Given an m ×m sub-matrix. Answer 2-sided RMQs in O(r log r) timeand O(r) bits space.

• Upper region could be of area Θ(r2).

• Modify priorities:• If FP p has FP q above and to left of

p with same priority, change priorityof p to +∞.

• RMQ value lies in area shown.• Size of this area is O(r log r).

Overall O(r log r) time. Space usage?

Page 23: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Solving Upper Part of Query

2-sided RMQ

Given an m ×m sub-matrix. Answer 2-sided RMQs in O(r log r) timeand O(r) bits space.

• Upper region could be of area Θ(r2).

• Modify priorities:• If FP p has FP q above and to left of

p with same priority, change priorityof p to +∞.

• RMQ value lies in area shown.• Size of this area is O(r log r).

Overall O(r log r) time. Space usage?

Page 24: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Space Usage

Subtask

To represent m FPs with priorities in O(r) bits, and answer 2-sided RMQon them in O(r log r) time.

• Each FP p has a line of influence Inf (p).

• 2-sided RMQ at q: shoot ray upwarduntil you hit Inf (q) for some q – this isthe answer. [Makris, Tsakalidis ’90]

• Given FP coordinates, encode prioritiesin O(r) bits [Farzan, Munro, R. ’12]:• plane sweep, note how many current

Inf lines are terminated by new point.

• Answer queries naively: O(r log r) time.2

6

4

9

7

8

3

1q

5

2-sided RMQ

Given an r × r sub-matrix, we can answer 2-sided RMQs in O(r log r)time and O(r) bits space.

Page 25: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Putting Things Together

Main steps of final result:

1. Given an r × r sub-matrix, we can answer 2-sided RMQs inO(r log r) time O(r) time and O(r) bits O(r log r) bits space.

• ”Dense” Fibonacci lattice, with Θ(r log r) FPs in r × r sub-matrix.• Cannot naively answer 2-sided RMQs on FPs:

• Use 2-sided succinct index of [Farzan, Munro, R. ’12].• New sub-routine needed: given any sub-rectangle of the m ×m

sub-matrix, report coordinates of all FPs inside the sub-rectangle.• Space O(r log r) bits, O(|output|) time.

2. Reduce (4-sided) RMQ on general m × n matrix to (4-sided) RMQon c × c matrix, using O(N/c) bits and O(c) time.

3. Recursively decompose 4-sided RMQs into 2-sided RMQs (solvedusing (1)) and “block-aligned” 4-sided RMQs (solved using [Brodal,Davoodi, Rao ’10]).

Page 26: Two Dimensional Range Minimum Queries and Fibonacci Latticesalgo12.fri.uni-lj.si/reg/proc/presentations/... · We consider the 2D matrix range minimum query (2D RMQ) problem. This

Introduction 2-sided RMQs Summary and Conclusions

Conclusions/Open Problems

• Improved indexing data structure for 2D RMQ:• O(N/c) bits, time O(c log c(log log c)2) [NEW].• O(N/c) bits, time O(c(log c)2) [BDR’10].• 1D RMQ O(N/c) bits, Θ(c) time [BDR ’10].

• Extending BDR’10’s 1D lower bound method to 2D gives weakresults (not in paper).• Is tradeoff O(N/c) space, O(c log c) time optimal for 2-sided

queries?

• BDR’10 considered 0-1 arrays A for lower bound (”emptinesstesting”).• Cannot show new lower bound for 2-sided RMQ for 0-1 case.• 3-sided RMQ for 0-1 case has optimal tradeoff.• 4-sided RMQ for 0-1 case has better tradeoff than general case.

• Amir, Fischer, Lewenstein’s original question still not fully answered!