Advanced Algorithms – COMS31900 Lowest Common Ancestor
(with a bit on on Range Minimum Queries)
Rapha¨ el Clifford
Slides by Benjamin Sach
Advanced Algorithms COMS31900 Lowest Common Ancestor (with a bit - - PowerPoint PPT Presentation
Advanced Algorithms COMS31900 Lowest Common Ancestor (with a bit on on Range Minimum Queries) Rapha el Clifford Slides by Benjamin Sach Advanced Algorithms COMS31900 Lowest Common Ancestor (with a bit on on Range Minimum Queries)
(with a bit on on Range Minimum Queries)
Slides by Benjamin Sach
(with a bit on on Range Minimum Queries)
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . .
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j After preprocessing,
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j After preprocessing,
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
root
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
root
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
ancestors of node i root
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
ancestors of node i
from i to the root root
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
ancestors of node j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
ancestors of node j
from j to the root
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
root
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing,
root common ancestors of i and j the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing,
root common ancestors of i and j
ancestors of both i and j the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing,
root the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing,
root the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing,
root the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j depth =
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing,
root the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j depth = lowest common ancestor of i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing,
root depth = lowest common ancestor of i and j
and j furthest from the root the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
LCA(i, j)
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
LCA(i, j)
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . . After preprocessing, the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j
LCA(i, j)
the nodes are numbered between
Compute an Euler tour of T . . . (a depth first search with repeats) Write down every node you visit . . . and its depth
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D 0
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Find i and j in N
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Find i and j in N
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Find i and j in N
Compute RMQ(i′, j′) in D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Find i and j in N
Compute RMQ(i′, j′) in D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Find i and j in N
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Find i and j in N
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Find i and j in N
Compute RMQ(i′, j′) in D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Find i and j in N
Compute RMQ(i′, j′) in D
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Find i and j in N. . .
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Find i and j in N. . . which copy of i?
Compute an Euler tour of T . . . (a depth first search with repeats)
(node) N
How long is the tour? We follow each edge twice. . . and there are (n − 1) edges Write down every node you visit . . . and its depth
(depth) D
2n−1
how do we find LCA(i,j)?
Find i and j in N. . . which copy of i?
any copy is fine
(node) N (depth) D
2n−1
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
Query time O(1 + queryRMQ(n)) Space O(n + spaceRMQ(n)) depends on the RMQ structure used
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
Query time O(1) Space O(n log log n) using the best result from last lecture
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
Query time O(1) Space O(n log log n) using the best result from last lecture
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
depth > d + 1
We can also define a Euler tour of T recursively. . . children
depth = d depth = d + 1
(node) N (depth) D
depth > d + 1
We can also define a Euler tour of T recursively. . .
subtrees
children
depth = d depth = d + 1
(node) N (depth) D
depth > d + 1
We can also define a Euler tour of T recursively. . .
subtrees
children
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1
d + 1 d + 1 d + 1 d + 1
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1
d + 1 d + 1 d + 1 d + 1
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
imagine LCA(i, j) is not y
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
imagine LCA(i, j) is not y
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
imagine LCA(i, j) is not y
(all of the ys are out of range)
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
again, imagine LCA(i, j) is not y
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
again, imagine LCA(i, j) is not y
so the RMQ location isn’t a y
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
now imagine LCA(i, j) is y
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
now imagine LCA(i, j) is y
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
now imagine LCA(i, j) is y
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
now imagine LCA(i, j) is y
We can also define a Euler tour of T recursively. . .
depth = d depth = d + 1
(node) N (depth) D
tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1 Claim the RMQ reports the location of some y in N iff LCA(i, j) = y
d + 1 d + 1 d + 1 d + 1
now imagine LCA(i, j) is y
so the RMQ reports a y
We have seen an O(n log log n) space, O(n log log n) prep. time and O(1) query time solution
which uses solution 3 for RMQ from last lecture
We have seen an O(n log log n) space, O(n log log n) prep. time and O(1) query time solution
which uses solution 3 for RMQ from last lecture
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
Notice anything interesting about D?
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
Notice anything interesting about D?
Preprocess an integer array A (length n) to answer range minimum queries. . .
After preprocessing, a range minimum query is given by RMQ(i, j) the output is the location of the smallest element in A[i, j] e.g. RMQ(3, 7) = 5, which is the location of the smallest element in A[3, 7]
RMQ(3, 7) = 5
where for all k, we have A[k + 1] = A[k] ± 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(in a tie, report the leftmost)
Key Idea replace A with a smaller, ‘low resolution’ array H
and many small arrays L0, L1, L2 . . . ‘for the details’
Preprocess the array H
2n log n
in O(n) space/prep time Preprocess each array Li (which has length (log n)/2) to answer RMQs. . . in O(log n log log n) space/prep time How do we answer a query in A in O(1) time? Do one query in H and one query in two different Li and return the smallest
log n 2
min of these as there are O(n/ log n) Li arrays, we have O(n log log n) total space/prep time
n
goes in here all of these go in here
Key Idea replace A with a smaller, ‘low resolution’ array H
and many small arrays L0, L1, L2 . . . ‘for the details’
Preprocess the array H
2n log n
in O(n) space/prep time Preprocess each array Li (which has length (log n)/2) to answer RMQs. . . in O(log n log log n) space/prep time How do we answer a query in A in O(1) time? Do one query in H and one query in two different Li and return the smallest
log n 2
min of these as there are O(n/ log n) Li arrays, we have O(n log log n) total space/prep time
too big and slow!
n
goes in here all of these go in here
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to (remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
RMQx(0, 2) = RMQy(0, 2) = 2 RMQx(3, 4) = RMQy(3, 4) = 4 RMQx(0, 4) = RMQy(0, 4) = 2 RMQx(0, 1) = RMQy(0, 1) = 1 (remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
Fact Lx is equivalent to Ly iff dx = dy
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
Fact Lx is equivalent to Ly
iff dx = dy
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
Fact Lx is equivalent to Ly
iff dx = dy
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
Fact Lx is equivalent to Ly
iff dx = dy
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
Fact Lx is equivalent to Ly
iff dx = dy
at most 2(log n)/2 (remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
Fact Lx is equivalent to Ly
iff dx = dy
at most 2(log n)/2
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
Fact Lx is equivalent to Ly
iff dx = dy
at most 2(log n)/2
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
Fact Lx is equivalent to Ly
iff dx = dy
at most 2(log n)/2
(remember these are the locations of the minimum)
log n 2
How many different ±1 RMQ arrays like this. . . are there?
log n 2
We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)
log n 2
is equivalent to
is equivalent to
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
Fact Lx is equivalent to Ly
iff dx = dy . . . this requires O(√n log2 n) = O(n) total space and prep. time
at most 2(log n)/2
(remember these are the locations of the minimum)
Key Idea replace A with a smaller, ‘low resolution’ array H
log n 2
n
n
precompute the value of dx for each Lx in O(n) total space and prep. time Precompute all the RMQ answers for every value 0 d √n in O(n) total space and prep. time To perform a query within some Lx
row d3
This takes O(1) time
Key Idea replace A with a smaller, ‘low resolution’ array H and many small arrays L0, L1, L2 . . . ‘for the details’ Preprocess the array H to answer RMQs. . . in O(n) space/prep time Preprocess each array Li (which has length (log n)/2) to answer RMQs. . . build a complete table of answers How do we answer a query in A in O(1) time? Do one query in H and one query in two different Li and return the smallest
log n 2
min of these
n
goes in here all of these go in here
We have seen an O(n) space, O(n) prep. time and O(1) query time solution
which improves solution 3 for RMQ from last lecture (but only for ±1 inputs) which uses solution 3 for RMQ from last lecture
We have seen an O(n log log n) space, O(n log log n) prep. time and O(1) query time solution
How does this affect our LCA solution?
We have seen an O(n) space, O(n) prep. time and O(1) query time solution
which improves solution 3 for RMQ from last lecture (but only for ±1 inputs) which uses solution 3 for RMQ from last lecture
We have seen an O(n log log n) space, O(n log log n) prep. time and O(1) query time solution
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
This gives us O(n) space, O(n) prep. time and O(1) query time for the LCA problem
(node) N (depth) D
2n−1
Preprocessing Summary
node i to some N[i′] = i Query Summary - LCA(i,j)
This gives us O(n) space, O(n) prep. time and O(1) query time for the LCA problem
by using the solution to ±1RMQ
We have seen an O(n) space, O(n) prep. time and O(1) query time solution
which uses the solution to ±1RMQ
We have seen an O(n) space, O(n) prep. time and O(1) query time solution
which improves solution 3 for RMQ from last lecture (but only for ±1 inputs)
What about the general Range Minimum Query problem?
We have seen an O(n) space, O(n) prep. time and O(1) query time solution
which uses the solution to ±1RMQ
We have seen an O(n) space, O(n) prep. time and O(1) query time solution
which improves solution 3 for RMQ from last lecture (but only for ±1 inputs) (when the inputs aren’t ±1)
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
6 13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
6 13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
6 13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
partitions the array in two
6 13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
partitions the array in two
6 13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
partitions the array in two
recursing left and right. . .
6 13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
partitions the array in two
recursing left and right. . .
6 13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
partitions the array in two
recursing left and right. . .
6 13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
17 14 21
partitions the array in two
recursing left and right. . .
6
19
13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
17 14 21
partitions the array in two
recursing left and right. . .
6
19
13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
17 14 21
partitions the array in two
recursing left and right. . .
6
23 51 67 46 54 32 19
13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
17 14 21
partitions the array in two
recursing left and right. . .
6
23 51 67 46 54 32 19
13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
17 14 21 73 82 91
partitions the array in two
recursing left and right. . .
6
23 51 67 46 54 32 19
13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
This process isn’t very efficient. . . a better one takes O(n) time
17 14 21 73 82 91
partitions the array in two
recursing left and right. . .
6
23 51 67 46 54 32 19
13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
This process isn’t very efficient. . . a better one takes O(n) time
17 14 21 73 82 91
partitions the array in two
recursing left and right. . . it’s not tricky but we don’t have time to cover it
6
23 51 67 46 54 32 19
13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
This process isn’t very efficient. . . a better one takes O(n) time
17 14 21 73 82 91
partitions the array in two
recursing left and right. . . it’s not tricky but we don’t have time to cover it Key Fact: The LCA in TA equals the RMQ in A
6
23 51 67 46 54 32 19
13
RMQ(3, 7) = 6
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
This process isn’t very efficient. . . a better one takes O(n) time
17 14 21 73 82 91
partitions the array in two
recursing left and right. . . it’s not tricky but we don’t have time to cover it Key Fact: The LCA in TA equals the RMQ in A
6
23 51 32 67 46 54 73 32 19
13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
This process isn’t very efficient. . . a better one takes O(n) time
17 14 21 73 82 91
partitions the array in two
recursing left and right. . . it’s not tricky but we don’t have time to cover it Key Fact: The LCA in TA equals the RMQ in A
6
23 51 67 46 54 32 19
13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
This process isn’t very efficient. . . a better one takes O(n) time
17 14 21 73 82 91
partitions the array in two
recursing left and right. . . it’s not tricky but we don’t have time to cover it Key Fact: The LCA in TA equals the RMQ in A
6
23 51 67 46 54 32 19
91 54
13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
This process isn’t very efficient. . . a better one takes O(n) time
17 14 21 73 82 91
partitions the array in two
recursing left and right. . . it’s not tricky but we don’t have time to cover it Key Fact: The LCA in TA equals the RMQ in A This gives us O(n) space, O(n) prep. time and O(1) query time for the RMQ problem
6
23 51 67 46 54 32 19
91 54
13
1 2 3 4 5 7 8 9 10 11 12 14 15
Build the Cartesian tree, TA of the array A:
This process isn’t very efficient. . . a better one takes O(n) time
17 14 21 73 82 91
partitions the array in two
recursing left and right. . . it’s not tricky but we don’t have time to cover it Key Fact: The LCA in TA equals the RMQ in A This gives us O(n) space, O(n) prep. time and O(1) query time for the RMQ problem
6
23 51 67 46 54 32 19
91 54
13
by using the solution to LCA :)
We have seen an O(n) space, O(n) prep. time and O(1) query time solution
which uses the solution to LCA
We have seen an O(n) space, O(n) prep. time and O(1) query time solution
which uses the solution to ±1RMQ
We have seen an O(n) space, O(n) prep. time and O(1) query time solution
which improves solution 3 for RMQ from last lecture (but only for ±1 inputs) (which works for all inputs)