range minimum queries - stanford university

260
Range Minimum Queries Part Two

Upload: others

Post on 16-Oct-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Range Minimum Queries - Stanford University

Range Minimum QueriesPart Two

Page 2: Range Minimum Queries - Stanford University

Recap from Last Time

Page 3: Range Minimum Queries - Stanford University

41 59 26 5331 41 59 26 53 58 97 93

The RMQ Problem

● The Range Minimum Query (RMQ) problem is the following:

Given a fixed array A and two indices i ≤ j, what is the smallest element out of

A[i], A[i + 1], …, A[j – 1], A[j]?

31 58 97 93

Page 4: Range Minimum Queries - Stanford University

Why do we even care?

Page 5: Range Minimum Queries - Stanford University

Lowest Common Ancestors

Page 6: Range Minimum Queries - Stanford University

Lowest Common Ancestors

A

GD

E

B

C F

A B C C B A D E E D F F D A G G A

This is called an Euler tour of the tree. Euler tours have all sorts of nice properties. Depending on what topics we explore, we might see some more

of them later in the quarter.

This is called an Euler tour of the tree. Euler tours have all sorts of nice properties. Depending on what topics we explore, we might see some more

of them later in the quarter.

Page 7: Range Minimum Queries - Stanford University

Lowest Common Ancestors

A

GD

E

B

C F

A B C C B A D E E D F F D A G G A

0 0 0 01 12 2 1 1 12 2 2 2 1 1

Page 8: Range Minimum Queries - Stanford University

C B A E D F D A G A

0 0 012 1 12 2 1

Lowest Common Ancestors

A

GD

E

B

C F

A B C D E F G

0 1 2 1 2 2 1

Page 9: Range Minimum Queries - Stanford University

C B A E D F D A G A

0 0 012 1 12 2 1

Lowest Common Ancestors

A

GD

E

B

C F

A B C D E F G

0 1 2 1 2 2 1

Page 10: Range Minimum Queries - Stanford University

C B A E D F D A G A

0 0 012 1 12 2 1

Lowest Common Ancestors

A

GD

E

B

C F

A B C D E F G

0 1 2 1 2 2 1

Page 11: Range Minimum Queries - Stanford University

C B A E D F D A G A

0 0 012 1 12 2 1

Lowest Common Ancestors

A

GD

E

B

C F

A B C D E F G

0 1 2 1 2 2 1

Page 12: Range Minimum Queries - Stanford University

C B A E D F D A G A

0 0 012 1 12 2 1

Lowest Common Ancestors

A

GD

E

B

C F

A B C D E F G

0 1 2 1 2 2 1

Page 13: Range Minimum Queries - Stanford University

C B A E D F D A G A

0 0 012 1 12 2 1

Lowest Common Ancestors

A

GD

E

B

C F

A B C D E F G

0 1 2 1 2 2 1

Page 14: Range Minimum Queries - Stanford University

C B A E D F D A G A

0 0 012 1 12 2 1

Lowest Common Ancestors

A

GD

E

B

C F

A B C D E F G

0 1 2 1 2 2 1

Page 15: Range Minimum Queries - Stanford University

Solutions to RMQ can be used to createfast solutions to LCA.

We'll use this fact next week!

Page 16: Range Minimum Queries - Stanford University

A Notational Recap

Page 17: Range Minimum Queries - Stanford University

Some Notation

● We'll say that an RMQ data structure has time complexity ⟨p(n), q(n)⟩ if

● preprocessing takes time at most p(n) and

● queries take time at most q(n).

● Last time, we saw structures with the following runtimes:

● ⟨O(n2), O(1)⟩ (full preprocessing)

● ⟨O(n log n), O(1)⟩ (sparse table)

● ⟨O(n log log n), O(1)⟩ (hybrid approach)

● ⟨O(n), O(n1/2)⟩ (blocking)

● ⟨O(n), O(log n)⟩ (hybrid approach)

● ⟨O(n), O(log log n)⟩ (hybrid approach)

Page 18: Range Minimum Queries - Stanford University

Blocking Revisited

31 26 23 62 27

31 41 59 26 53 58 97 93 23 84 62 64 33 83 2731 41 59 26 53 58 97 93 23 84 62 64 33 83 27

31 26 23 62 27

Page 19: Range Minimum Queries - Stanford University

Blocking Revisited

31 26 23 62 27

31 41 59 26 53 58 97 93 23 84 62 64 33 83 2731 41 59 26 53 58 97 93 23 84 62 64 33 83 27

31 26 23 62 27

This is just RMQ on the block minima!

This is just RMQ on the block minima!

Page 20: Range Minimum Queries - Stanford University

Blocking Revisited

31 26 23 62 27

31 41 59 26 53 58 97 93 23 84 62 64 33 83 2731 41 59 26 53 58 97 93 23 84 62 64 33 83 27

31 26 23 62 27

This is just RMQ inside the blocks!

This is just RMQ inside the blocks!

Page 21: Range Minimum Queries - Stanford University

The Framework

● Suppose we use a ⟨p₁(n), q₁(n)⟩-time RMQ solution for the block minimums and a ⟨p₂(n), q₂(n)⟩-time RMQ solution within each block.

● Let the block size be b.

● In the hybrid structure, the preprocessing time is

O(n + p₁(n / b) + (n / b) p₂(b))

● The query time is

O(q₁(n / b) + q₂(b))

31 26 23 62 27

31 41 59 26 53 58 97 93 23 84 62 64 33 83 2731 41 59 26 53 58 97 93 23 84 62 64 33 83 27

31 26 23 62 27

Page 22: Range Minimum Queries - Stanford University

A Useful Observation

● Sparse tables can be constructed in time O(n log n).

● If we use a sparse table as a top structure, construction time is O((n / b) log n).● See last lecture for the math on this.

● Cute trick: If we choose b = Θ(log n), then the construction time is O(n).

Page 23: Range Minimum Queries - Stanford University

Is there an ⟨O(n), O(1)⟩ solution to RMQ?

Yes!

Page 24: Range Minimum Queries - Stanford University

New Stuff!

Page 25: Range Minimum Queries - Stanford University

An Observation

Page 26: Range Minimum Queries - Stanford University

The Limits of Hybrids

● The preprocessing time on a hybrid structure is

O(n + p₁(n / b) + (n / b) p₂(b))● The query time is

O(q₁(n / b) + q₂(b))● To build an ⟨O(n), O(1)⟩ hybrid, we need to

have p₂(n) = O(n) and q₂(n) = O(1).● We can't build an optimal solution with the

hybrid approach unless we already have one!

● Or can we?

Page 27: Range Minimum Queries - Stanford University

The Limits of Hybrids

The preprocessing time on a hybrid structure is

O(n + p₁(n / b) + (n / b) p₂(b))

The query time is

O(q₁(n / b) + q₂(b))

To build an ⟨O(n), O(1)⟩ hybrid, we need to have p₂(n) = O(n) and q₂(n) = O(1).

We can't build an optimal solution with the hybrid approach unless we already have one!

Or can we?

Page 28: Range Minimum Queries - Stanford University

The Limits of Hybrids

The preprocessing time on a hybrid structure is

O(n + p₁(n / b) + (n / b) p₂(b))

The query time is

O(q₁(n / b) + q₂(b))

To build an ⟨O(n), O(1)⟩ hybrid, we need to have p₂(n) = O(n) and q₂(n) = O(1).

We can't build an optimal solution with the hybrid approach unless we already have one!

Or can we?

This term comes from the cost of building O(n / b) RMQ structures,

one per block of size b.

Is this a tight bound?

This term comes from the cost of building O(n / b) RMQ structures,

one per block of size b.

Is this a tight bound?

Page 29: Range Minimum Queries - Stanford University

A Key Difference

● Our original problem is

Solve RMQ on a single array in time ⟨O(n), O(1)⟩

● The new problem is

Solve RMQ on a large number of small arrays with O(1) query time and total

preprocessing time O(n).● These are not the same problem.● Question: Why is this second problem any

easier than the first?

Page 30: Range Minimum Queries - Stanford University

An Observation

10 30 20 40 166 361 261 464

Page 31: Range Minimum Queries - Stanford University

30 20 40 361 261 46410 30 20 40 166 361 261 464

An Observation

10 166

Page 32: Range Minimum Queries - Stanford University

30 20 40 361 261 46410 30 20 40 166 361 261 464

An Observation

10 166

Page 33: Range Minimum Queries - Stanford University

30 20 40 361 261 46430 20 40 361 261 46410 166

An Observation

10 166

Page 34: Range Minimum Queries - Stanford University

30 36110 16630 20 40 361 261 46420 40 261 46410 166

An Observation

Page 35: Range Minimum Queries - Stanford University

30 36110 16630 20 40 361 261 46420 40 261 46410 166

An Observation

Page 36: Range Minimum Queries - Stanford University

30 20 361 26110 16630 20 361 26110 16640 46440 464

An Observation

Page 37: Range Minimum Queries - Stanford University

30 20 361 26110 16630 20 40 361 261 46440 46410 166

An Observation

Page 38: Range Minimum Queries - Stanford University

30 20 361 26110 16630 20 40 361 261 46440 46410 166

An Observation

Page 39: Range Minimum Queries - Stanford University

30 20 361 26110 16630 20 361 26110 16640 46440 464

An Observation

Claim: The indices of the answers to any range

minimum queries on these two arrays are the same.

Claim: The indices of the answers to any range

minimum queries on these two arrays are the same.

Page 40: Range Minimum Queries - Stanford University

Modifying RMQ

● From this point forward, let's have RMQA(i, j) denote the index of the minimum value in the range rather than the value itself.

● Observation: If RMQ structures return indices rather than values, we can use a single RMQ structure for both of these arrays:

30 20 361 26110 16630 20 361 26110 16640 46440 464

Page 41: Range Minimum Queries - Stanford University

Some Notation

● Let B₁ and B₂ be blocks of length b.

● We'll say that B₁ and B₂ have the same block type (denoted B₁ ~ B₂) if the following holds:

For all 0 ≤ i ≤ j < b:RMQB₁(i, j) = RMQB₂(i, j)

● Intuitively, the RMQ answers for B₁ are always the same as the RMQ answers for B₂.

● If we build an RMQ to answer queries on some block B₁, we can reuse that RMQ structure on some other block B₂ iff B₁ ~ B₂.

Page 42: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

Page 43: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

Page 44: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

Page 45: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

Page 46: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

RMQStructure

Page 47: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

RMQStructure

Page 48: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

RMQStructure

Page 49: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

RMQStructure

We don't need to build a new RMQ structure for this

block – we already have one that will work!

We don't need to build a new RMQ structure for this

block – we already have one that will work!

Page 50: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

RMQStructure

Page 51: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

RMQStructure

RMQStructure

Page 52: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

RMQStructure

RMQStructure

Page 53: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

RMQStructure

RMQStructure

Page 54: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

RMQStructure

RMQStructure

Page 55: Range Minimum Queries - Stanford University

Where We're Going

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

RMQStructure

RMQStructure

RMQStructure

Page 56: Range Minimum Queries - Stanford University

Now, the details!

Page 57: Range Minimum Queries - Stanford University

Detecting Block Types

● For this approach to work, we need to be able to check whether two blocks have the same block type.

● Problem: Our formal definition of B₁ ~ B₂ is defined in terms of RMQ.● Not particularly useful a priori; we don't want to

have to compute RMQ structures on B₁ and B₂ to decide whether they have the same block type!

● Is there a simpler way to determine whether two blocks have the same type?

Page 58: Range Minimum Queries - Stanford University

An Initial Idea

● Since the elements of the array are ordered and we're looking for the smallest value in certain ranges, we might look at the permutation types of the blocks.

Claim: If B₁ and B₂ have the same permutation on their elements, then B₁ ~ B₂.

31 41 59

12 2 5

16 18 3

66 26 6

27 18 28

60 22 14

66 73 84

72 99 27

Page 59: Range Minimum Queries - Stanford University

An Initial Idea

● Since the elements of the array are ordered and we're looking for the smallest value in certain ranges, we might look at the permutation types of the blocks.

Claim: If B₁ and B₂ have the same permutation on their elements, then B₁ ~ B₂.

31 41 59

12 2 5

16 18 3

66 26 6

27 18 28

60 22 14

66 73 84

72 99 27

1 2 3 2 3 1 2 1 3 1 2 3

3 1 2 3 2 1 3 1 2 2 3 1

Page 60: Range Minimum Queries - Stanford University

An Initial Idea

● Since the elements of the array are ordered and we're looking for the smallest value in certain ranges, we might look at the permutation types of the blocks.

● Claim: If B₁ and B₂ have the same permutation

on their elements, then B₁ ~ B₂.

31 41 59

12 2 5

16 18 3

66 26 6

27 18 28

60 22 14

66 73 84

72 99 27

1 2 3 2 3 1 2 1 3 1 2 3

3 1 2 3 2 1 3 2 1 2 3 1

Page 61: Range Minimum Queries - Stanford University

Some Problems

● There are two main problems with this approach.

● Problem One: It's possible for two blocks to have different permutations but the same block type.

All three of these blocks have the same block type but different permutation types:

Problem Two: The number of possible permutations of a block is b!.

b has to be absolutely minuscule for b! to be small.

Is there a better criterion we can use?

Page 62: Range Minimum Queries - Stanford University

Some Problems

● There are two main problems with this approach.

● Problem One: It's possible for two blocks to have different permutations but the same block type.

● All three of these blocks have the same block type but different permutation types:

Problem Two: The number of possible permutations of a block is b!.

b has to be absolutely minuscule for b! to be small.

Is there a better criterion we can use?

261 268 161 161 261 167

4 5 1 1 4 3

167 166

3 2

166 268

2 5

167 261 161

3 4 1

268 166

5 2

Page 63: Range Minimum Queries - Stanford University

Some Problems

● There are two main problems with this approach.

● Problem One: It's possible for two blocks to have different permutations but the same block type.

● All three of these blocks have the same block type but different permutation types:

● Problem Two: The number of possible permutations of a block is b!.

● b has to be absolutely minuscule for b! to be small.

Is there a better criterion we can use?

261 268 161 161 261 167

4 5 1 1 4 3

167 166

3 2

166 268

2 5

167 261 161

3 4 1

268 166

5 2

Page 64: Range Minimum Queries - Stanford University

Some Problems

● There are two main problems with this approach.

● Problem One: It's possible for two blocks to have different permutations but the same block type.

● All three of these blocks have the same block type but different permutation types:

● Problem Two: The number of possible permutations of a block is b!.

● b has to be absolutely minuscule for b! to be small.

● Is there a better criterion we can use?

261 268 161 161 261 167

4 5 1 1 4 3

167 166

3 2

166 268

2 5

167 261 161

3 4 1

268 166

5 2

Page 65: Range Minimum Queries - Stanford University

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

Page 66: Range Minimum Queries - Stanford University

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

Page 67: Range Minimum Queries - Stanford University

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

75 35 80 85 83

6 5 3 9 7

Page 68: Range Minimum Queries - Stanford University

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

75 35 80 85 83

6 5 3 9 7

Page 69: Range Minimum Queries - Stanford University

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

75 35 80 85 83

6 5 3 9 7

Page 70: Range Minimum Queries - Stanford University

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

75 35 80 85 83

6 5 3 9 7

Page 71: Range Minimum Queries - Stanford University

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

6 5 3 9 7

Page 72: Range Minimum Queries - Stanford University

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

6 5 3 9 7

Page 73: Range Minimum Queries - Stanford University

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

6 5 3 9 7

Page 74: Range Minimum Queries - Stanford University

161 167 166

11 43 35

3 9 7

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268

14 22

6 5

Page 75: Range Minimum Queries - Stanford University

161 167 166

11 43 35

3 9 7

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268

14 22

6 5

Page 76: Range Minimum Queries - Stanford University

161 167 166

11 43 35

3 9 7

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268

14 22

6 5

Page 77: Range Minimum Queries - Stanford University

161 167 166

11 43 35

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268

14 22

Page 78: Range Minimum Queries - Stanford University

161 167 166

11 43 35

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268

14 22

Page 79: Range Minimum Queries - Stanford University

161 167 166

11 43 35

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268

14 22

261 268 161 167 166

14 22 11 43 35

Page 80: Range Minimum Queries - Stanford University

Cartesian Trees

● A Cartesian tree is a binary tree derived from an array and defined as follows:

● The empty array has an empty Cartesian tree.● For a nonempty array, the root stores the index of

the minimum value. Its left and right children are Cartesian trees for the subarrays to the left and right of the minimum.

261 268 161 167 166 14 55 22 43 116 5 3 9 7

2

0 4

31

2

4

3

1

0

4

0

2

31

Page 81: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

Proof sketch:

(⇒) Induction. B₁ and B₂ have equal RMQs, so corresponding ranges have the same minima.

Page 82: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇒) Induction. B₁ and B₂ have equal RMQs, so corresponding ranges have the same minima.

Page 83: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇒) Induction. B₁ and B₂ have equal RMQs, so corresponding ranges have the same minima.

Page 84: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇒) Induction. B₁ and B₂ have equal RMQs, so corresponding ranges have the same minima.

Page 85: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇒) Induction. B₁ and B₂ have equal RMQs, so corresponding ranges have the same minima.

Page 86: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇒) Induction. B₁ and B₂ have equal RMQs, so corresponding ranges have the same minima.

k k

Page 87: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇒) Induction. B₁ and B₂ have equal RMQs, so corresponding ranges have the same minima.

k k

Page 88: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇒) Induction. B₁ and B₂ have equal RMQs, so corresponding ranges have the same minima.

k k

Page 89: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇐) Induction. It's possible to answer RMQ using a recursive walk on the Cartesian tree.

Page 90: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇐) Induction. It's possible to answer RMQ using a recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

Page 91: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇐) Induction. It's possible to answer RMQ using a recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

7

3

12

14

13

100

1

2

4

5

6 11

9

8

Page 92: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇐) Induction. It's possible to answer RMQ using a recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

7

3

12

14

13

100

1

2

4

5

6 11

9

8

Page 93: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇐) Induction. It's possible to answer RMQ using a recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

7

3

12

14

13

100

1

2

4

5

6 11

9

8

Page 94: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇐) Induction. It's possible to answer RMQ using a recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

7

3

12

14

13

100

1

2

4

5

6 11

9

8

Page 95: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇐) Induction. It's possible to answer RMQ using a recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

7

3

12

14

13

100

1

2

4

5

6 11

9

8

Page 96: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇐) Induction. It's possible to answer RMQ using a recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

7

3

12

14

13

100

1

2

4

5

6 11

9

8

Page 97: Range Minimum Queries - Stanford University

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have equal Cartesian trees.

● Proof sketch:

● (⇐) Induction. It's possible to answer RMQ using a recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

7

3

12

14

13

100

1

2

4

5

6 11

9

8

Page 98: Range Minimum Queries - Stanford University

Building Cartesian Trees

● The previous theorem lets us check whether B₁ ~ B₂ by testing whether they have the same Cartesian tree.

● How efficiently can we actually build these trees?

Page 99: Range Minimum Queries - Stanford University

Building Cartesian Trees

● Here's a naïve algorithm for constructing Cartesian trees:● Find the minimum value.● Recursively build a Cartesian tree for the array

to the left of the minimum.● Recursively build a Cartesian tree with the

elements to the right of the minimum.● Return the overall tree.

● How efficient is this approach?

Page 100: Range Minimum Queries - Stanford University

Building Cartesian Trees

● This algorithm works by● doing a linear scan over the array,● identifying the minimum at whatever position it

occupies, then● recursively processing the left and right halves

on the array.● Similar to the recursion in quicksort: it

depends on where the minima are.● Always get good splits: Θ(n log n).● Always get bad splits: Θ(n2).

● We're going to need to be faster than this.

Page 101: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

Page 102: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

Page 103: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

Page 104: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

Page 105: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

Page 106: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

Observation 1: This new node cannot end up as the left child of any node in the tree.

Observation 1: This new node cannot end up as the left child of any node in the tree.

6

Page 107: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

Observation 2: This new node will end up on the right spine of the tree.

Observation 2: This new node will end up on the right spine of the tree.

Page 108: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

Observation 3: Cartesian trees are min-heaps with respect to the elements in the original array.

Observation 3: Cartesian trees are min-heaps with respect to the elements in the original array.

Page 109: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

Page 110: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

Page 111: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

Page 112: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

Page 113: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

Page 114: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

Page 115: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

Page 116: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

55

Page 117: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

55

Page 118: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

55

7

Page 119: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

55

7

Page 120: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

55

7

Page 121: Range Minimum Queries - Stanford University

A Better Approach

● It turns out that it's possible to build a Cartesian tree over an array of length k in time O(k).

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393

0

84

1

33

2

64

3

62

4

5

83

6

55

7

Page 122: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

Page 123: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

Page 124: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

Page 125: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

Page 126: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

32

Page 127: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

32

Page 128: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

32

45

Page 129: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

32

45

Page 130: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

32

45

45

Page 131: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

32

45

45

Page 132: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

32

45

45

16

Page 133: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

32

45

16

Page 134: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

Page 135: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

Page 136: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

16

Page 137: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

16

Page 138: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

16

18

Page 139: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

16

18

Page 140: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

16

18

18

Page 141: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

16

18

18

Page 142: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

16

18

18

9

Page 143: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

16

18

9

Page 144: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

18

9

Page 145: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

18

9

Page 146: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

18

9

9

Page 147: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

18

9

9

Page 148: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

18

9

9

33

Page 149: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

18

9

9

33

Page 150: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

18

9

9

33

33

Page 151: Range Minimum Queries - Stanford University

A Stack-Based Algorithm

● Maintain a stack of the nodes on the right spine of the tree.

● To insert a new node:

● Pop the stack until it's empty or the top node has a lower value than the current value.

● Set the new node's left child to be the last value popped (or null if nothing was popped).

● Set the new node's parent to be the top node on the stack (or null if the stack is empty).

● Push the new node onto the stack.

32 45 16 18 9 33

32

45

16

18

9

9

33

33

Page 152: Range Minimum Queries - Stanford University

Analyzing the Runtime

● Adding in another node to the Cartesian tree might take time O(n), since we might have to pop everything off the stack.

● Since there are n nodes to add, the runtime of this approach is O(n2).

● Claim: This is a weak bound! The runtime is actually Θ(n).● Proof: Work done per node is directly proportional to the

number of stack operations performed when that node was processed.

● Total number of stack operations is at most 2n.● Every node is pushed once.● Every node is popped at most once.

● Total runtime is therefore Θ(n).

Page 153: Range Minimum Queries - Stanford University

Time-Out for Announcements!

Page 154: Range Minimum Queries - Stanford University
Page 155: Range Minimum Queries - Stanford University

Problem Set One

● Problem Set One goes out today. It's due next Thursday (April 7) at the start of class (3:00PM).● Explore the theory behind RMQ!● Implement what you're seeing here!

● Start early! There aren't many problems, but you definitely don't want to have to figure everything out last-minute.

Page 156: Range Minimum Queries - Stanford University

Problem Set Logistics

● We will be using GradeScope for assignment submissions this quarter.

● To use it, visit the GradeScope website and use the code

93DENM

to register for CS166.● No hardcopy assignments will be accepted.

We're using GradeScope to track due dates and as a gradebook.

Page 157: Range Minimum Queries - Stanford University

Problem Set Logistics

● You're welcome to work on this problem set individually or in a pair.

● If you work in a pair, just submit a single, joint problem set. You'll receive the same grade as your partner.

● Each assignment is independent, so feel free to work individually on one, then in a pair on the next, then in a different pair, etc.

Page 158: Range Minimum Queries - Stanford University

Honor Code

● This probably isn't a surprise, but we take the Honor Code seriously in this class.

● Please review Handout #04 for our policies with regards to the Honor Code as applied to CS166.

Page 159: Range Minimum Queries - Stanford University

Back to CS166!

Page 160: Range Minimum Queries - Stanford University

The Story So Far

● Our high-level idea is to use the hybrid framework, but to avoid rebuilding RMQ structures for blocks when they've already been computed.

● Since we can build Cartesian trees in linear time, we can test if two blocks have the same type in linear time.

● Goal: Choose a block size that's small enough that there are duplicated blocks, but large enough that the top-level RMQ can be computed efficiently.

● So how many Cartesian trees are there?

Page 161: Range Minimum Queries - Stanford University

Theorem: The number of Cartesian trees for an array of length b is at most 4b.

In case you're curious, the actual number is

,

which is roughly equal to

.

Look up the Catalan numbers for more information!

4b

b3 /2√π

1b+1 (2b

b )

Page 162: Range Minimum Queries - Stanford University

Proof Approach

● Our stack-based algorithm for generating Cartesian trees is capable of producing a Cartesian tree for every possible input array.

● Therefore, if we can count the number of possible executions of that algorithm, we can count the number of Cartesian trees.

● Using a simple counting scheme, we can show that there are at most 4b possible executions.

Page 163: Range Minimum Queries - Stanford University

The Insight

● Claim: The Cartesian tree produced by the stack-based algorithm is uniquely determined by the sequence of pushes and pops made on the stack.

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Can represent the execution as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack.)

● We'll call this number the Cartesian tree number of a particular block.

● There are at most 22b = 4b possible 2b-bit numbers, so there are at most 4b possible Cartesian trees.

Page 164: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33

Page 165: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33

Page 166: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33

32

Page 167: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33

32

Page 168: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1

32

32

Page 169: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1

32

32

Page 170: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1

32

32

45

Page 171: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1

32

32

45

Page 172: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1

32

32

45

45

Page 173: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1

32

32

45

45

Page 174: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1

32

32

45

45

16

Page 175: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0

32

32

45

16

Page 176: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0

32

45

16

Page 177: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0

32

45

16

Page 178: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1

32

45

16

16

Page 179: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1

32

45

16

16

Page 180: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1

32

45

16

16

18

Page 181: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1

32

45

16

16

18

Page 182: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1

32

45

16

16

18

18

Page 183: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1

32

45

16

16

18

18

Page 184: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1

32

45

16

16

18

18

9

Page 185: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1 0

32

45

16

16

18

9

Page 186: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1 0 0

32

45

16

18

9

Page 187: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1 0 0

32

45

16

18

9

Page 188: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1 0 0 1

32

45

16

18

9

9

Page 189: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1 0 0 1

32

45

16

18

9

9

Page 190: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1 0 0 1

32

45

16

18

9

9

33

Page 191: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1 0 0 1

32

45

16

18

9

9

33

Page 192: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1 0 0 1 1

32

45

16

18

9

9

33

33

Page 193: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1 0 0 1 1

32

45

16

18

9

9

33

33

Page 194: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1 0 0 1 1 0

32

45

16

18

9

9

33

Page 195: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1 0 0 1 1 0 0

32

45

16

18

9

33

Page 196: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

32 45 16 18 9 33 1 1 0 0 1 1 0 0 1 1 0 0

32

45

16

18

9

33

Page 197: Range Minimum Queries - Stanford University

One Last Observation

● Recall: Our goal is to be able to detect when two blocks have the same type so that we can share RMQ structures between them.

● We've seen that two blocks have the same type if and only if they have the same Cartesian tree.

● Using the connection between Cartesian trees and Cartesian tree numbers, we can see that we don't actually have to build any Cartesian trees!

● We can just compute the Cartesian tree number of each block and use those numbers to test for block equivalence.

Page 198: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45 90 45 23 53 60 28 74 71 35

Page 199: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45 90 45 23 53 60 28 74 71 35

Page 200: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1

90 45 23 53 60 28 74 71 35

27

Page 201: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1

90 45 23 53 60 28 74 71 35

27

Page 202: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0

90 45 23 53 60 28 74 71 35

Page 203: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1

90 45 23 53 60 28 74 71 35

18

Page 204: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1

90 45 23 53 60 28 74 71 35

18

Page 205: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1

90 45 23 53 60 28 74 71 35

18 28

Page 206: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1

90 45 23 53 60 28 74 71 35

18 28

Page 207: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0

90 45 23 53 60 28 74 71 35

18

Page 208: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1

90 45 23 53 60 28 74 71 35

18 18

Page 209: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1

90 45 23 53 60 28 74 71 35

18 18

Page 210: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1

90 45 23 53 60 28 74 71 35

18 18 28

Page 211: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1

90 45 23 53 60 28 74 71 35

18 18 28

Page 212: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1

90 45 23 53 60 28 74 71 35

18 18 28 45

Page 213: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1

90 45 23 53 60 28 74 71 35

18 18 28 45

Page 214: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1

90 45 23 53 60 28 74 71 35

18 18 28 45 90

Page 215: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1

90 45 23 53 60 28 74 71 35

18 18 28 45 90

Page 216: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0

90 45 23 53 60 28 74 71 35

18 18 28 45

Page 217: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1

90 45 23 53 60 28 74 71 35

18 18 28 45 45

Page 218: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1

90 45 23 53 60 28 74 71 35

18 18 28 45 45

Page 219: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

18 18 28 45

Page 220: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0

18 18 28

Page 221: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0

18 18

Page 222: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1

18 18 23

Page 223: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1

18 18 23

Page 224: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1

18 18 23 53

Page 225: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1

18 18 23 53

Page 226: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1

18 18 23 53 60

Page 227: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1

18 18 23 53 60

Page 228: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0

18 18 23 53

Page 229: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0

18 18 23

Page 230: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1

18 18 23 28

Page 231: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1

18 18 23 28

Page 232: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1

18 18 23 28 74

Page 233: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1

18 18 23 28 74

Page 234: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0

18 18 23 28

Page 235: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1

18 18 23 28 71

Page 236: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1

18 18 23 28 71

Page 237: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0

18 18 23 28

Page 238: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1

18 18 23 28 35

Page 239: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1 0

18 18 23 28

Page 240: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1 0 0

18 18 23

Page 241: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0

18 18

Page 242: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0

18

Page 243: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0 0

Page 244: Range Minimum Queries - Stanford University

Cartesian Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0 0

Page 245: Range Minimum Queries - Stanford University

Finishing Things Up

● Using the previous algorithm, we can compute the Cartesian tree number of a block in time O(b) and without actually building the tree.

● We now have a simple and efficient linear-time algorithm for testing whether two blocks have the same block type.

● And, we bounded the number of Cartesian trees at 4b using this setup!

Page 246: Range Minimum Queries - Stanford University

The Fischer-Heun Structure

● In 2005, Fischer and Heun introduced a (slight variation on) the following RMQ data structure.

● Use a hybrid approach with block size b (we'll choose b later), a sparse table as a top RMQ structure, and the full precomputation data structure for the blocks.

● However, make the following modifications:

● Make a table of length 4b storing pointers to RMQ structures. The index corresponds to the Cartesian tree number. Initially, the array is empty.

● When computing the RMQ for a particular block, first compute its Cartesian tree number t.

● If there's an RMQ structure for t in the array, use it.

● Otherwise, compute the RMQ structure for the current block, store it in the array and index t, then use it.

Page 247: Range Minimum Queries - Stanford University

31 26 23 62 27

Fischer-Heun, Schematically

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n log n), O(1)⟩

Page 248: Range Minimum Queries - Stanford University

31 26 23 62 27

Fischer-Heun, Schematically

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n log n), O(1)⟩

What's the query time on this structure?What's the query time on this structure?

Page 249: Range Minimum Queries - Stanford University

31 26 23 62 27

Fischer-Heun, Schematically

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n log n), O(1)⟩

What's the query time on this structure?

Answer: O(1)

What's the query time on this structure?

Answer: O(1)

Page 250: Range Minimum Queries - Stanford University

31 26 23 62 27

Fischer-Heun, Schematically

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n log n), O(1)⟩

What's the preprocessing time for this structure if the block size is b?

O(n) time to compute block minima.O((n / b) log n) time to build the sparse table.O(b2) per smaller RMQ structure, of which at most 4b are built.

Total: O(n + (n / b) log n + 4b b2)

What's the preprocessing time for this structure if the block size is b?

O(n) time to compute block minima.O((n / b) log n) time to build the sparse table.O(b2) per smaller RMQ structure, of which at most 4b are built.

Total: O(n + (n / b) log n + 4b b2)

Page 251: Range Minimum Queries - Stanford University

31 26 23 62 27

Fischer-Heun, Schematically

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n log n), O(1)⟩

What's the preprocessing time for this structure if the block size is b?

O(n) time to compute block minima.O((n / b) log n) time to build the sparse table.O(b2) per smaller RMQ structure, of which at most 4b are built.

Total: O(n + (n / b) log n + 4b b2)

What's the preprocessing time for this structure if the block size is b?

O(n) time to compute block minima.O((n / b) log n) time to build the sparse table.O(b2) per smaller RMQ structure, of which at most 4b are built.

Total: O(n + (n / b) log n + 4b b2)

Page 252: Range Minimum Queries - Stanford University

31 26 23 62 27

Fischer-Heun, Schematically

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n log n), O(1)⟩

What's the preprocessing time for this structure if the block size is b?

O(n) time to compute block minima.O((n / b) log n) time to build the sparse table.O(b2) per smaller RMQ structure, of which at most 4b are built.

Total: O(n + (n / b) log n + 4b b2)

What's the preprocessing time for this structure if the block size is b?

O(n) time to compute block minima.O((n / b) log n) time to build the sparse table.O(b2) per smaller RMQ structure, of which at most 4b are built.

Total: O(n + (n / b) log n + 4b b2)

Page 253: Range Minimum Queries - Stanford University

31 26 23 62 27

Fischer-Heun, Schematically

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n log n), O(1)⟩

What's the preprocessing time for this structure if the block size is b?

O(n) time to compute block minima.O((n / b) log n) time to build the sparse table.O(b2) per smaller RMQ structure, of which at most 4b are built.

Total: O(n + (n / b) log n + 4b b2)

What's the preprocessing time for this structure if the block size is b?

O(n) time to compute block minima.O((n / b) log n) time to build the sparse table.O(b2) per smaller RMQ structure, of which at most 4b are built.

Total: O(n + (n / b) log n + 4b b2)

Page 254: Range Minimum Queries - Stanford University

31 26 23 62 27

Fischer-Heun, Schematically

31 41 59 26 53 58 97 23 93 84 62 64 33 83 27

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n2), O(1)⟩RMQ Structure

⟨O(n log n), O(1)⟩

What's the preprocessing time for this structure if the block size is b?

O(n) time to compute block minima.O((n / b) log n) time to build the sparse table.O(b2) per smaller RMQ structure, of which at most 4b are built.

Total: O(n + (n / b) log n + 4b b2)

What's the preprocessing time for this structure if the block size is b?

O(n) time to compute block minima.O((n / b) log n) time to build the sparse table.O(b2) per smaller RMQ structure, of which at most 4b are built.

Total: O(n + (n / b) log n + 4b b2)

Page 255: Range Minimum Queries - Stanford University

The Finishing Touches

● The runtime is

O(n + (n / b) log n + 4b b2)● As we saw earlier, if we set b = Θ(log n), then

(n / b) log n = O(n)● Suppose we set b = log₄ (n1/2) = ¼log₂ n. Then

4b b2 = n1/2 (log₂ n)2 = o(n)● With b = ¼log₂ n, the preprocessing time is

= O(n + n + n1/2 (log n)2) = O(n)● We finally have an ⟨O(n), O(1)⟩ RMQ solution!

Page 256: Range Minimum Queries - Stanford University

Practical Concerns

● This structure is actually reasonably efficient; preprocessing is relatively fast.

● In practice, the ⟨O(n), O(log n)⟩ hybrid we talked about last time is a bit faster.● Constant factor in the Fischer-Heun's O(n)

preprocessing is a bit higher.● Constant factor in the hybrid approach's O(n)

and O(log n) are very low.

● Check the Fischer-Heun paper for details.

Page 257: Range Minimum Queries - Stanford University

Wait a Minute...

● This approach assumes that the Cartesian tree numbers will fit into individual machine words!

● If b = ¼ log₂ n, then each Cartesian tree number will have ½ log₂ n bits.

● Cartesian tree numbers will fit into a machine word if n fits into a machine word.

● In the transdichotomous machine model, we assume the problem size always fits into a machine word.

● Reasonable – think about how real computers work.

● So there's nothing to worry about.

Page 258: Range Minimum Queries - Stanford University

The Method of Four Russians

● The technique employed here is an example of the Method of Four Russians.

● Idea:● Split the input apart into blocks of size Θ(log n).● Using the fact that there can only be polynomially

many different blocks of size Θ(log n), precompute all possible answers for each possible block and store them for later use.

● Combine the results together using a top-level structure on an input of size Θ(n / log n).

● This technique is used frequently to shave log factors off of runtimes.

Page 259: Range Minimum Queries - Stanford University

Why Study RMQ?

● I chose RMQ as our first problem for a few reasons:

● See different approaches to the same problem. Different intuitions produced different runtimes.

● Build data structures out of other data structures. Many modern data structures use other data structures as building blocks, and it's very evident here.

● See the Method of Four Russians. This trick looks like magic the first few times you see it and shows up in lots of places.

● Explore modern data structures. This is relatively recent data structure (2005), and I wanted to show you that the field is still very active!

● So what's next?

Page 260: Range Minimum Queries - Stanford University

Next Time

● Tries● A powerful and versatile data structure for

sets of strings.

● Substring Searching● Challenges in implementing .indexOf.

● The Aho-Corasick Algorithm● A linear-time substring search algorithm that

doubles as a data structure!