range minimum and lowest common ancestor queriesjrad/presentation.pdf · range minimum queries...
TRANSCRIPT
Range Minimum and Lowest Common AncestorQueries
Slides by Solon P. Pissis
November 15, 2019
RMQs and LCA queries
Range Minimum Queries problem
DefinitionGiven an array A[1 . . . n], preprocess A so that a minimum of anyfragment A[i . . . j ] can be computed efficiently:
RMQA(i , j) = arg min A[k]
with 1 ≤ i ≤ k ≤ j ≤ n.
i jk1 n
Answering any query in O(1) time is trivial if we allow O(n2) timeand space preprocessing.
RMQs and LCA queries
Range Minimum Queries problem
DefinitionGiven an array A[1 . . . n], preprocess A so that a minimum of anyfragment A[i . . . j ] can be computed efficiently:
RMQA(i , j) = arg min A[k]
with 1 ≤ i ≤ k ≤ j ≤ n.
i jk1 n
Answering any query in O(1) time is trivial if we allow O(n2) timeand space preprocessing.
RMQs and LCA queries
Range Minimum Queries problem
DefinitionGiven an array A[1 . . . n], preprocess A so that a minimum of anyfragment A[i . . . j ] can be computed efficiently:
RMQA(i , j) = arg min A[k]
with 1 ≤ i ≤ k ≤ j ≤ n.
i jk1 n
Answering any query in O(1) time is trivial if we allow O(n2) timeand space preprocessing.
RMQs and LCA queries
Range Minimum Queries problem
DefinitionGiven an array A[1 . . . n], preprocess A so that a minimum of anyfragment A[i . . . j ] can be computed efficiently:
RMQA(i , j) = arg min A[k]
with 1 ≤ i ≤ k ≤ j ≤ n.
i jk1 n
Answering any query in O(1) time is trivial if we allow O(n2) timeand space preprocessing.
RMQs and LCA queries
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe 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
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe 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
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe 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
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe 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
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe 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
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe 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
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe 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
O(n log n) time and space and O(1)-time queries
And now the querying part.
Two cases:
I Fragment of length 2k : Trivial, use B;
I Otherwise, we can cover any range with two power-of-2ranges.
Compute k = blog(j − i + 1)c.
i j1 n
Return min{Bk [i ],Bk [j − 2k + 1]} in O(1) time!
RMQs and LCA queries
O(n log n) time and space and O(1)-time queries
And now the querying part.
Two cases:
I Fragment of length 2k : Trivial, use B;
I Otherwise, we can cover any range with two power-of-2ranges.
Compute k = blog(j − i + 1)c.
i j1 n
Return min{Bk [i ],Bk [j − 2k + 1]} in O(1) time!
RMQs and LCA queries
O(n log n) time and space and O(1)-time queries
And now the querying part.
Two cases:
I Fragment of length 2k : Trivial, use B;
I Otherwise, we can cover any range with two power-of-2ranges.
Compute k = blog(j − i + 1)c.
i j1 n
Return min{Bk [i ],Bk [j − 2k + 1]} in O(1) time!
RMQs and LCA queries
O(n log n) time and space and O(1)-time queries
And now the querying part.
Two cases:
I Fragment of length 2k : Trivial, use B;
I Otherwise, we can cover any range with two power-of-2ranges.
Compute k = blog(j − i + 1)c.
i j1 n
Return min{Bk [i ],Bk [j − 2k + 1]} in O(1) time!
RMQs and LCA queries
O(n log n) time and space and O(1)-time queries
And now the querying part.
Two cases:
I Fragment of length 2k : Trivial, use B;
I Otherwise, we can cover any range with two power-of-2ranges.
Compute k = blog(j − i + 1)c.
i j1 n
Return min{Bk [i ],Bk [j − 2k + 1]} in O(1) time!
RMQs and LCA queries
O(n log n) time and space and O(1)-time queries
And now the querying part.
Two cases:
I Fragment of length 2k : Trivial, use B;
I Otherwise, we can cover any range with two power-of-2ranges.
Compute k = blog(j − i + 1)c.
i j1 n
Return min{Bk [i ],Bk [j − 2k + 1]} in O(1) time!
RMQs and LCA queries
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand 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
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand 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
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand 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
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand 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
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand 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
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand 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
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand 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
O(n) time and space and O(log n)-time queries
1 n
b = log n
I Construct a new array A′:
A′[i ] = min{A[i · b + 1],A[i · b + 2], . . . ,A[(i + 1) · b]}.
I Build the previously described structure for A′[1 . . . nlog n ].
How much time and space do we need? O( nlog n log( n
log n )) = O(n).
RMQs and LCA queries
O(n) time and space and O(log n)-time queries
1 n
b = log n
I Construct a new array A′:
A′[i ] = min{A[i · b + 1],A[i · b + 2], . . . ,A[(i + 1) · b]}.
I Build the previously described structure for A′[1 . . . nlog n ].
How much time and space do we need? O( nlog n log( n
log n )) = O(n).
RMQs and LCA queries
O(n) time and space and O(log n)-time queries
1 n
b = log n
I Construct a new array A′:
A′[i ] = min{A[i · b + 1],A[i · b + 2], . . . ,A[(i + 1) · b]}.
I Build the previously described structure for A′[1 . . . nlog n ].
How much time and space do we need? O( nlog n log( n
log n )) = O(n).
RMQs and LCA queries
O(n) time and space and O(log n)-time queries
1 n
b = log n
I Construct a new array A′:
A′[i ] = min{A[i · b + 1],A[i · b + 2], . . . ,A[(i + 1) · b]}.
I Build the previously described structure for A′[1 . . . nlog n ].
How much time and space do we need?
O( nlog n log( n
log n )) = O(n).
RMQs and LCA queries
O(n) time and space and O(log n)-time queries
1 n
b = log n
I Construct a new array A′:
A′[i ] = min{A[i · b + 1],A[i · b + 2], . . . ,A[(i + 1) · b]}.
I Build the previously described structure for A′[1 . . . nlog n ].
How much time and space do we need? O( nlog n log( n
log n )) = O(n).
RMQs and LCA queries
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
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I 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
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
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I 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
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
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I 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
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
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I 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
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
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I 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
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
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I 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
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
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I 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
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
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I 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
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
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I 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
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) timeand space preprocessing.
I We only have to deal with the strictly-inside-a-block case.
I We will show how to do that for a very restricted case:
|A[i + 1]− A[i ]| = 1
I We will then explain why this restricted case is sufficient!
RMQs and LCA queries
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) timeand space preprocessing.
I We only have to deal with the strictly-inside-a-block case.
I We will show how to do that for a very restricted case:
|A[i + 1]− A[i ]| = 1
I We will then explain why this restricted case is sufficient!
RMQs and LCA queries
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) timeand space preprocessing.
I We only have to deal with the strictly-inside-a-block case.
I We will show how to do that for a very restricted case:
|A[i + 1]− A[i ]| = 1
I We will then explain why this restricted case is sufficient!
RMQs and LCA queries
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) timeand space preprocessing.
I We only have to deal with the strictly-inside-a-block case.
I We will show how to do that for a very restricted case:
|A[i + 1]− A[i ]| = 1
I We will then explain why this restricted case is sufficient!
RMQs and LCA queries
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) timeand space preprocessing.
I We only have to deal with the strictly-inside-a-block case.
I We will show how to do that for a very restricted case:
|A[i + 1]− A[i ]| = 1
I We will then explain why this restricted case is sufficient!
RMQs and LCA queries
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) timeand space preprocessing.
I We only have to deal with the strictly-inside-a-block case.
I We will show how to do that for a very restricted case:
|A[i + 1]− A[i ]| = 1
I We will then explain why this restricted case is sufficient!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
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 somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
Lowest Common Ancestor queries
The lowest common ancestor (LCA) of two nodes u and v is thedeepest node that is an ancestor of both u and v .
u
v
DefinitionGiven a rooted tree T , preprocess T so that the LCA of u and vcan be computed efficiently.
RMQs and LCA queries
Lowest Common Ancestor queries
The lowest common ancestor (LCA) of two nodes u and v is thedeepest node that is an ancestor of both u and v .
u
v
DefinitionGiven a rooted tree T , preprocess T so that the LCA of u and vcan be computed efficiently.
RMQs and LCA queries
Lowest Common Ancestor queries
The lowest common ancestor (LCA) of two nodes u and v is thedeepest node that is an ancestor of both u and v .
u
v
DefinitionGiven a rooted tree T , preprocess T so that the LCA of u and vcan be computed efficiently.
RMQs and LCA queries
Lowest Common Ancestor queries
The lowest common ancestor (LCA) of two nodes u and v is thedeepest node that is an ancestor of both u and v .
u
v
DefinitionGiven a rooted tree T , preprocess T so that the LCA of u and vcan be computed efficiently.
RMQs and LCA queries
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
Linear-Time Reduction: LCA to RMQ
Given a rooted tree T on n nodes we construct:
I E [1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).I L[1 . . . 2n − 1]: the distance of E [i ] from the root.I R[i ] = min{j : E [j ] = i} (first occurrence of E [i ] in the tour).
0
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
Linear-Time Reduction: LCA to RMQ
Given a rooted tree T on n nodes we construct:
I E [1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).I L[1 . . . 2n − 1]: the distance of E [i ] from the root.I R[i ] = min{j : E [j ] = i} (first occurrence of E [i ] in the tour).
0
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
Linear-Time Reduction: LCA to RMQ
Given a rooted tree T on n nodes we construct:
I E [1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).
I L[1 . . . 2n − 1]: the distance of E [i ] from the root.I R[i ] = min{j : E [j ] = i} (first occurrence of E [i ] in the tour).
0
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
Linear-Time Reduction: LCA to RMQ
Given a rooted tree T on n nodes we construct:
I E [1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).I L[1 . . . 2n − 1]: the distance of E [i ] from the root.
I R[i ] = min{j : E [j ] = i} (first occurrence of E [i ] in the tour).
0
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
Linear-Time Reduction: LCA to RMQ
Given a rooted tree T on n nodes we construct:
I E [1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).I L[1 . . . 2n − 1]: the distance of E [i ] from the root.I R[i ] = min{j : E [j ] = i} (first occurrence of E [i ] in the tour).
0
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
Linear-Time Reduction: LCA to RMQ
Given a rooted tree T on n nodes we construct:
I E [1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).I L[1 . . . 2n − 1]: the distance of E [i ] from the root.I R[i ] = min{j : E [j ] = i} (first occurrence of E [i ] in the tour).
0
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
Linear-Time Reduction: LCA to RMQ
Querying:
I LCA(u, v) is translated to E [RMQL(R[u],R[v ])].
I LCA(5, 2) isE [RMQL(R[u],R[v ])] = E [RMQL(3, 6)] = E [4] = 4.
0
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
Linear-Time Reduction: LCA to RMQ
0
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
Linear-Time Reduction: LCA to RMQ
0
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
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I 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) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I 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) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I 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) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I 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) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I 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) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I 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) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I 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) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I 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) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
Note
Lecture by P. Charalampopoulos. I slightly edited the slides, so Iam responsible for any errors in them.
RMQs and LCA queries