Range Minimum and Lowest Common Ancestor Queries Slides by Solon P. - - PowerPoint PPT Presentation

range minimum and lowest common ancestor queries
SMART_READER_LITE
LIVE PREVIEW

Range Minimum and Lowest Common Ancestor Queries Slides by Solon P. - - PowerPoint PPT Presentation

Range Minimum and Lowest Common Ancestor Queries Slides by Solon P. Pissis November 15, 2019 RMQs and LCA queries Range Minimum Queries problem RMQs and LCA queries Range Minimum Queries problem Definition Given an array A [1 . . . n ],


slide-1
SLIDE 1

Range Minimum and Lowest Common Ancestor Queries

Slides by Solon P. Pissis November 15, 2019

RMQs and LCA queries

slide-2
SLIDE 2

Range Minimum Queries problem

RMQs and LCA queries

slide-3
SLIDE 3

Range Minimum Queries problem

Definition

Given an array A[1 . . . n], preprocess A so that a minimum of any fragment A[i . . . j] can be computed efficiently: RMQA(i, j) = arg min A[k] with 1 ≤ i ≤ k ≤ j ≤ n.

RMQs and LCA queries

slide-4
SLIDE 4

Range Minimum Queries problem

Definition

Given an array A[1 . . . n], preprocess A so that a minimum of any fragment A[i . . . j] can be computed efficiently: RMQA(i, j) = arg min A[k] with 1 ≤ i ≤ k ≤ j ≤ n.

i j k 1 n

RMQs and LCA queries

slide-5
SLIDE 5

Range Minimum Queries problem

Definition

Given an array A[1 . . . n], preprocess A so that a minimum of any fragment A[i . . . j] can be computed efficiently: RMQA(i, j) = arg min A[k] with 1 ≤ i ≤ k ≤ j ≤ n.

i j k 1 n

Answering any query in O(1) time is trivial if we allow O(n2) time and space preprocessing.

RMQs and LCA queries

slide-6
SLIDE 6

O(n log n) time and space and O(1)-time queries

RMQs and LCA queries

slide-7
SLIDE 7

O(n log n) time and space and O(1)-time queries

Let us show the following lemma.

RMQs and LCA queries

slide-8
SLIDE 8

O(n log n) time and space and O(1)-time queries

Let us show the following lemma.

Lemma

The RMQ problem can be solved in O(1) time after O(n log n) time and space preprocessing.

RMQs and LCA queries

slide-9
SLIDE 9

O(n log n) time and space and O(1)-time queries

Let us show the following lemma.

Lemma

The RMQ problem can be solved in O(1) time after O(n log n) time and space preprocessing. We apply the well-known doubling technique for preprocessing.

RMQs and LCA queries

slide-10
SLIDE 10

O(n log n) time and space and O(1)-time queries

Let us show the following lemma.

Lemma

The RMQ problem can be solved in O(1) time after O(n log n) time and space preprocessing. We apply the well-known doubling technique for preprocessing. For all k = 0, 1, . . . , log n construct: Bk[i] = min{A[i], A[i + 1], . . . , A[i + 2k − 1]}.

RMQs and LCA queries

slide-11
SLIDE 11

O(n log n) time and space and O(1)-time queries

Let us show the following lemma.

Lemma

The RMQ problem can be solved in O(1) time after O(n log n) time and space preprocessing. We apply the well-known doubling technique for preprocessing. For all k = 0, 1, . . . , log n construct: Bk[i] = min{A[i], A[i + 1], . . . , A[i + 2k − 1]}. How?

RMQs and LCA queries

slide-12
SLIDE 12

O(n log n) time and space and O(1)-time queries

Let us show the following lemma.

Lemma

The RMQ problem can be solved in O(1) time after O(n log n) time and space preprocessing. We apply the well-known doubling technique for preprocessing. For all k = 0, 1, . . . , log n construct: Bk[i] = min{A[i], A[i + 1], . . . , A[i + 2k − 1]}. How? B0[i] = A[i] and Bk+1[i] = min{Bk[i], Bk[i + 2k]}, for all i.

RMQs and LCA queries

slide-13
SLIDE 13

O(n log n) time and space and O(1)-time queries

And now the querying part.

RMQs and LCA queries

slide-14
SLIDE 14

O(n log n) time and space and O(1)-time queries

And now the querying part. Two cases:

RMQs and LCA queries

slide-15
SLIDE 15

O(n log n) time and space and O(1)-time queries

And now the querying part. Two cases:

◮ Fragment of length 2k: Trivial, use B;

RMQs and LCA queries

slide-16
SLIDE 16

O(n log n) time and space and O(1)-time queries

And now the querying part. Two cases:

◮ Fragment of length 2k: Trivial, use B; ◮ Otherwise, we can cover any range with two power-of-2

ranges.

RMQs and LCA queries

slide-17
SLIDE 17

O(n log n) time and space and O(1)-time queries

And now the querying part. Two cases:

◮ Fragment of length 2k: Trivial, use B; ◮ Otherwise, we can cover any range with two power-of-2

ranges. Compute k = ⌊log(j − i + 1)⌋.

i j 1 n

RMQs and LCA queries

slide-18
SLIDE 18

O(n log n) time and space and O(1)-time queries

And now the querying part. Two cases:

◮ Fragment of length 2k: Trivial, use B; ◮ Otherwise, we can cover any range with two power-of-2

ranges. Compute k = ⌊log(j − i + 1)⌋.

i j 1 n

Return min{Bk[i], Bk[j − 2k + 1]} in O(1) time!

RMQs and LCA queries

slide-19
SLIDE 19

O(n) time and space and O(log n)-time queries

RMQs and LCA queries

slide-20
SLIDE 20

O(n) time and space and O(log n)-time queries

Let us show the following lemma.

RMQs and LCA queries

slide-21
SLIDE 21

O(n) time and space and O(log n)-time queries

Let us show the following lemma.

Lemma

The RMQ problem can be solved in O(log n) time after O(n) time and space preprocessing.

RMQs and LCA queries

slide-22
SLIDE 22

O(n) time and space and O(log n)-time queries

Let us show the following lemma.

Lemma

The RMQ problem can be solved in O(log n) time after O(n) time and space preprocessing. (Segment tree.)

RMQs and LCA queries

slide-23
SLIDE 23

O(n) time and space and O(log n)-time queries

Let us show the following lemma.

Lemma

The RMQ problem can be solved in O(log n) time after O(n) time and space preprocessing. (Segment tree.) We apply the well-known micro-macro decomposition technique.

RMQs and LCA queries

slide-24
SLIDE 24

O(n) time and space and O(log n)-time queries

Let us show the following lemma.

Lemma

The RMQ problem can be solved in O(log n) time after O(n) time and space preprocessing. (Segment tree.) We apply the well-known micro-macro decomposition technique.

1 n b = log n

RMQs and LCA queries

slide-25
SLIDE 25

O(n) time and space and O(log n)-time queries

Let us show the following lemma.

Lemma

The RMQ problem can be solved in O(log n) time after O(n) time and space preprocessing. (Segment tree.) We apply the well-known micro-macro decomposition technique.

1 n b = log n

Decompose array A into blocks of length b = log n.

RMQs and LCA queries

slide-26
SLIDE 26

O(n) time and space and O(log n)-time queries

1 n b = log n

RMQs and LCA queries

slide-27
SLIDE 27

O(n) time and space and O(log n)-time queries

1 n b = log n

◮ Construct a new array A′:

A′[i] = min{A[i · b + 1], A[i · b + 2], . . . , A[(i + 1) · b]}.

RMQs and LCA queries

slide-28
SLIDE 28

O(n) time and space and O(log n)-time queries

1 n b = log n

◮ Construct a new array A′:

A′[i] = min{A[i · b + 1], A[i · b + 2], . . . , A[(i + 1) · b]}.

◮ Build the previously described structure for A′[1 . . . n log n].

RMQs and LCA queries

slide-29
SLIDE 29

O(n) time and space and O(log n)-time queries

1 n b = log n

◮ Construct a new array A′:

A′[i] = min{A[i · b + 1], A[i · b + 2], . . . , A[(i + 1) · b]}.

◮ Build the previously described structure for A′[1 . . . n log n].

How much time and space do we need?

RMQs and LCA queries

slide-30
SLIDE 30

O(n) time and space and O(log n)-time queries

1 n b = log n

◮ Construct a new array A′:

A′[i] = min{A[i · b + 1], A[i · b + 2], . . . , A[(i + 1) · b]}.

◮ Build the previously described structure for A′[1 . . . n log n].

How much time and space do we need? O(

n log n log( n log n)) = O(n).

RMQs and LCA queries

slide-31
SLIDE 31

O(n) time and space and O(log n)-time queries

Let i, j be a query (Notice: endpoints are inside blocks).

1 n i j

RMQs and LCA queries

slide-32
SLIDE 32

O(n) time and space and O(log n)-time queries

Let i, j be a query (Notice: endpoints are inside blocks).

1 n i j

◮ We do not have the min in the prefix and suffix of every block.

RMQs and LCA queries

slide-33
SLIDE 33

O(n) time and space and O(log n)-time queries

Let i, j be a query (Notice: endpoints are inside blocks).

1 n i j

◮ We do not have the min in the prefix and suffix of every block. ◮ For each block, precompute it: O(n) time and space!

RMQs and LCA queries

slide-34
SLIDE 34

O(n) time and space and O(log n)-time queries

Let i, j be a query (Notice: endpoints are inside blocks).

1 n i j

◮ We do not have the min in the prefix and suffix of every block. ◮ For each block, precompute it: O(n) time and space! ◮ For the rest: use the structure we have built for A′.

RMQs and LCA queries

slide-35
SLIDE 35

O(n) time and space and O(log n)-time queries

Let i, j be a query (Notice: endpoints are inside blocks).

1 n i j

◮ We do not have the min in the prefix and suffix of every block. ◮ For each block, precompute it: O(n) time and space! ◮ For the rest: use the structure we have built for A′. ◮ Now any such query takes O(1) time.

RMQs and LCA queries

slide-36
SLIDE 36

O(n) time and space and O(log n)-time queries

Let i, j be a query (Notice: endpoints are inside blocks).

1 n i j

◮ We do not have the min in the prefix and suffix of every block. ◮ For each block, precompute it: O(n) time and space! ◮ For the rest: use the structure we have built for A′. ◮ Now any such query takes O(1) time.

Any problems?

RMQs and LCA queries

slide-37
SLIDE 37

O(n) time and space and O(log n)-time queries

Let i, j be a query (Notice: endpoints are inside blocks).

1 n i j

◮ We do not have the min in the prefix and suffix of every block. ◮ For each block, precompute it: O(n) time and space! ◮ For the rest: use the structure we have built for A′. ◮ Now any such query takes O(1) time.

Any problems? Yes!

RMQs and LCA queries

slide-38
SLIDE 38

O(n) time and space and O(log n)-time queries

Let i, j be a query (Notice: endpoints are inside blocks).

1 n i j

◮ We do not have the min in the prefix and suffix of every block. ◮ For each block, precompute it: O(n) time and space! ◮ For the rest: use the structure we have built for A′. ◮ Now any such query takes O(1) time.

Any problems? Yes! When i, j are fully contained in a single block.

RMQs and LCA queries

slide-39
SLIDE 39

O(n) time and space and O(log n)-time queries

Let i, j be a query (Notice: endpoints are inside blocks).

1 n i j

◮ We do not have the min in the prefix and suffix of every block. ◮ For each block, precompute it: O(n) time and space! ◮ For the rest: use the structure we have built for A′. ◮ Now any such query takes O(1) time.

Any problems? Yes! When i, j are fully contained in a single block. Solution: Na¨ ıve search in block gives O(log n)-time queries!

RMQs and LCA queries

slide-40
SLIDE 40

O(n) time and space and O(1)-time queries

RMQs and LCA queries

slide-41
SLIDE 41

O(n) time and space and O(1)-time queries

  • OK. Now it is time to show the breakthrough.

RMQs and LCA queries

slide-42
SLIDE 42

O(n) time and space and O(1)-time queries

  • OK. Now it is time to show the breakthrough.

Theorem (Bender and Farach-Colton, LATIN 2000)

The RMQ problem can be solved in O(1) time after O(n) time and space preprocessing.

RMQs and LCA queries

slide-43
SLIDE 43

O(n) time and space and O(1)-time queries

  • OK. Now it is time to show the breakthrough.

Theorem (Bender and Farach-Colton, LATIN 2000)

The RMQ problem can be solved in O(1) time after O(n) time and space preprocessing.

◮ We only have to deal with the strictly-inside-a-block case.

RMQs and LCA queries

slide-44
SLIDE 44

O(n) time and space and O(1)-time queries

  • OK. Now it is time to show the breakthrough.

Theorem (Bender and Farach-Colton, LATIN 2000)

The RMQ problem can be solved in O(1) time after O(n) time and space preprocessing.

◮ We only have to deal with the strictly-inside-a-block case. ◮ We will show how to do that for a very restricted case:

|A[i + 1] − A[i]| = 1

RMQs and LCA queries

slide-45
SLIDE 45

O(n) time and space and O(1)-time queries

  • OK. Now it is time to show the breakthrough.

Theorem (Bender and Farach-Colton, LATIN 2000)

The RMQ problem can be solved in O(1) time after O(n) time and space preprocessing.

◮ We only have to deal with the strictly-inside-a-block case. ◮ We will show how to do that for a very restricted case:

|A[i + 1] − A[i]| = 1

◮ We will then explain why this restricted case is sufficient!

RMQs and LCA queries

slide-46
SLIDE 46

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1.

RMQs and LCA queries

slide-47
SLIDE 47

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

RMQs and LCA queries

slide-48
SLIDE 48

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

◮ Normalize blocks by subtracting the initial offset from every

element.

RMQs and LCA queries

slide-49
SLIDE 49

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

◮ Normalize blocks by subtracting the initial offset from every

element.

◮ Choose b = 1/2 · log n (instead of b = log n).

RMQs and LCA queries

slide-50
SLIDE 50

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

◮ Normalize blocks by subtracting the initial offset from every

element.

◮ Choose b = 1/2 · log n (instead of b = log n). ◮ A normalized block is a vector of length b − 1.

RMQs and LCA queries

slide-51
SLIDE 51

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

◮ Normalize blocks by subtracting the initial offset from every

element.

◮ Choose b = 1/2 · log n (instead of b = log n). ◮ A normalized block is a vector of length b − 1. ◮ There are 2b−1 = Θ(√n) distinct vectors.

RMQs and LCA queries

slide-52
SLIDE 52

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

◮ Normalize blocks by subtracting the initial offset from every

element.

◮ Choose b = 1/2 · log n (instead of b = log n). ◮ A normalized block is a vector of length b − 1. ◮ There are 2b−1 = Θ(√n) distinct vectors. ◮ Precompute and store all answers!

RMQs and LCA queries

slide-53
SLIDE 53

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

RMQs and LCA queries

slide-54
SLIDE 54

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

◮ Create Θ(√n) tables.

RMQs and LCA queries

slide-55
SLIDE 55

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

◮ Create Θ(√n) tables. ◮ In each table put all (log n/2)2 answers to all in-block queries.

RMQs and LCA queries

slide-56
SLIDE 56

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

◮ Create Θ(√n) tables. ◮ In each table put all (log n/2)2 answers to all in-block queries. ◮ Θ(√n log2 n) extra preprocessing.

RMQs and LCA queries

slide-57
SLIDE 57

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

◮ Create Θ(√n) tables. ◮ In each table put all (log n/2)2 answers to all in-block queries. ◮ Θ(√n log2 n) extra preprocessing. ◮ Query time is O(1).

RMQs and LCA queries

slide-58
SLIDE 58

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

◮ Create Θ(√n) tables. ◮ In each table put all (log n/2)2 answers to all in-block queries. ◮ Θ(√n log2 n) extra preprocessing. ◮ Query time is O(1). ◮ Preprocessing time and space is O(n).

RMQs and LCA queries

slide-59
SLIDE 59

O(n) time and space and O(1)-time queries

Assume: |A[i + 1] − A[i]| = 1. Observation: If two arrays X[1 . . . k], Y [1 . . . k] differ by some fixed value at each position; i.e. there is a c such that X[i] = Y [i] + c for every i, then all RMQ answers will be the same for X and Y .

◮ Create Θ(√n) tables. ◮ In each table put all (log n/2)2 answers to all in-block queries. ◮ Θ(√n log2 n) extra preprocessing. ◮ Query time is O(1). ◮ Preprocessing time and space is O(n).

We still need to explain why this restricted case is sufficient!

RMQs and LCA queries

slide-60
SLIDE 60

Lowest Common Ancestor queries

RMQs and LCA queries

slide-61
SLIDE 61

Lowest Common Ancestor queries

The lowest common ancestor (LCA) of two nodes u and v is the deepest node that is an ancestor of both u and v.

RMQs and LCA queries

slide-62
SLIDE 62

Lowest Common Ancestor queries

The lowest common ancestor (LCA) of two nodes u and v is the deepest node that is an ancestor of both u and v.

u v

RMQs and LCA queries

slide-63
SLIDE 63

Lowest Common Ancestor queries

The lowest common ancestor (LCA) of two nodes u and v is the deepest node that is an ancestor of both u and v.

u v

Definition

Given a rooted tree T, preprocess T so that the LCA of u and v can be computed efficiently.

RMQs and LCA queries

slide-64
SLIDE 64

Linear-Time Reduction: RMQ to LCA

RMQs and LCA queries

slide-65
SLIDE 65

Linear-Time Reduction: RMQ to LCA

Given array A we can build the Cartesian tree T of A:

RMQs and LCA queries

slide-66
SLIDE 66

Linear-Time Reduction: RMQ to LCA

Given array A we can build the Cartesian tree T of A:

◮ The root is a minimum element.

RMQs and LCA queries

slide-67
SLIDE 67

Linear-Time Reduction: RMQ to LCA

Given array A we can build the Cartesian tree T of A:

◮ The root is a minimum element. ◮ Removing this element splits the array into two.

RMQs and LCA queries

slide-68
SLIDE 68

Linear-Time Reduction: RMQ to LCA

Given array A we can build the Cartesian tree T of A:

◮ The root is a minimum element. ◮ Removing this element splits the array into two. ◮ Recurse on the two subarrays.

RMQs and LCA queries

slide-69
SLIDE 69

Linear-Time Reduction: RMQ to LCA

Given array A we can build the Cartesian tree T of A:

◮ The root is a minimum element. ◮ Removing this element splits the array into two. ◮ Recurse on the two subarrays.

Querying:

RMQs and LCA queries

slide-70
SLIDE 70

Linear-Time Reduction: RMQ to LCA

Given array A we can build the Cartesian tree T of A:

◮ The root is a minimum element. ◮ Removing this element splits the array into two. ◮ Recurse on the two subarrays.

Querying:

A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12] 2 5 17 11 3 7 20 8 21 12 i j T

RMQs and LCA queries

slide-71
SLIDE 71

Linear-Time Reduction: LCA to RMQ

RMQs and LCA queries

slide-72
SLIDE 72

Linear-Time Reduction: LCA to RMQ

Given a rooted tree T on n nodes we construct:

RMQs and LCA queries

slide-73
SLIDE 73

Linear-Time Reduction: LCA to RMQ

Given a rooted tree T on n nodes we construct:

◮ E[1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).

RMQs and LCA queries

slide-74
SLIDE 74

Linear-Time Reduction: LCA to RMQ

Given a rooted tree T on n nodes we construct:

◮ E[1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS). ◮ L[1 . . . 2n − 1]: the distance of E[i] from the root.

RMQs and LCA queries

slide-75
SLIDE 75

Linear-Time Reduction: LCA to RMQ

Given a rooted tree T on n nodes we construct:

◮ E[1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS). ◮ L[1 . . . 2n − 1]: the distance of E[i] from the root. ◮ R[i] = min{j : E[j] = i} (first occurrence of E[i] in the tour).

RMQs and LCA queries

slide-76
SLIDE 76

Linear-Time Reduction: LCA to RMQ

Given a rooted tree T on n nodes we construct:

◮ E[1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS). ◮ L[1 . . . 2n − 1]: the distance of E[i] from the root. ◮ R[i] = min{j : E[j] = i} (first occurrence of E[i] in the tour).

4 5 1 2 3 E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0] L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0] R = [1, 5, 6, 7, 2, 3]

RMQs and LCA queries

slide-77
SLIDE 77

Linear-Time Reduction: LCA to RMQ

Querying:

◮ LCA(u, v) is translated to E[RMQL(R[u], R[v])]. ◮ LCA(5, 2) is

E[RMQL(R[u], R[v])] = E[RMQL(3, 6)] = E[4] = 4.

4 5 1 2 3 E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0] L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0] R = [1, 5, 6, 7, 2, 3] v u u v i j

RMQs and LCA queries

slide-78
SLIDE 78

Linear-Time Reduction: LCA to RMQ

4 5 1 2 3 E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0] L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0] R = [1, 5, 6, 7, 2, 3] v u u v i j

Any observation about array L?

RMQs and LCA queries

slide-79
SLIDE 79

Linear-Time Reduction: LCA to RMQ

4 5 1 2 3 E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0] L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0] R = [1, 5, 6, 7, 2, 3] v u u v i j

Any observation about array L? Yes: |L[i + 1] − L[i]| = 1.

RMQs and LCA queries

slide-80
SLIDE 80

O(n) time and space and O(1)-time queries

RMQs and LCA queries

slide-81
SLIDE 81

O(n) time and space and O(1)-time queries

Let’s put things together:

RMQs and LCA queries

slide-82
SLIDE 82

O(n) time and space and O(1)-time queries

Let’s put things together:

◮ Reduce RMQ problem to the LCA problem.

RMQs and LCA queries

slide-83
SLIDE 83

O(n) time and space and O(1)-time queries

Let’s put things together:

◮ Reduce RMQ problem to the LCA problem. ◮ Reduce LCA problem back to the RMQ problem.

RMQs and LCA queries

slide-84
SLIDE 84

O(n) time and space and O(1)-time queries

Let’s put things together:

◮ Reduce RMQ problem to the LCA problem. ◮ Reduce LCA problem back to the RMQ problem. ◮ The reduction leads to a restricted RMQ problem.

RMQs and LCA queries

slide-85
SLIDE 85

O(n) time and space and O(1)-time queries

Let’s put things together:

◮ Reduce RMQ problem to the LCA problem. ◮ Reduce LCA problem back to the RMQ problem. ◮ The reduction leads to a restricted RMQ problem. ◮ Solve it and we are done!

RMQs and LCA queries

slide-86
SLIDE 86

O(n) time and space and O(1)-time queries

Let’s put things together:

◮ Reduce RMQ problem to the LCA problem. ◮ Reduce LCA problem back to the RMQ problem. ◮ The reduction leads to a restricted RMQ problem. ◮ Solve it and we are done!

Theorem (Bender and Farach-Colton, LATIN 2000)

The RMQ problem can be solved in O(1) time after O(n) time and space preprocessing.

RMQs and LCA queries

slide-87
SLIDE 87

O(n) time and space and O(1)-time queries

Let’s put things together:

◮ Reduce RMQ problem to the LCA problem. ◮ Reduce LCA problem back to the RMQ problem. ◮ The reduction leads to a restricted RMQ problem. ◮ Solve it and we are done!

Theorem (Bender and Farach-Colton, LATIN 2000)

The RMQ problem can be solved in O(1) time after O(n) time and space preprocessing.

Theorem (Bender and Farach-Colton, LATIN 2000)

The LCA problem can be solved in O(1) time after O(n) time and space preprocessing.

RMQs and LCA queries

slide-88
SLIDE 88

Note

Lecture by P. Charalampopoulos. I slightly edited the slides, so I am responsible for any errors in them.

RMQs and LCA queries