Date post: | 31-Dec-2015 |
Category: |
Documents |
Upload: | demetrius-martinez |
View: | 40 times |
Download: | 0 times |
The LCA Problem Revisited
Michael A.Bender
&
Martin Farach-Colton
Outline
Definitions. Reduction from LCA to RMQ. Trivial Solutions for RMQ Faster solution for sub-problem of
RMQ. Solution for general RMQ.
LCA - Lowest Common Ancestor
The LCA of nodes u and v in a tree T is the shared ancestor of u and v that is located farthest from the root.
LCAT(u,v)
u v
RMQ - Range Minimum Query
For indices i and j between 1 and n, query RMQA(i,j) returns the index of the smallest element in the sub array A[i…j].
RMQA(i,j)
0 1634 13 7 19 10 12 1 2
RMQA(3,7) = 4
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
Complexity Notation
)n(g),n(f
Preprocessing Time
Query Time
LCA - Trivial Algorithm
For each pair of vertices, follow both paths toward the root until the first shared vertex is found.
Complexity = )1(O),n(O 3
Reduction From LCA to RMQ
If there is an -time solution for RMQ, then there is an -time solution for LCA.
The reduction - We build three array from the input tree T.
1. E[1,…,2n-1] stores the nodes visited in an Euler Tour of T.
2. L[1,…,2n-1], where L[i] is the level of E[i] in T.3. R[1,…,n], where R[i] is the index of the first
occurrence of the node i in the array E.
)(),( ngnf
)1()12(),()12( OngnOnf
8
Example
0
1
2 3 6 9
54
7
E: 0 1 2 1 3 1 0 4 0 5 6 5 7 8 7 5 9 5 0
L: 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0
R: 0 1 2 4 7 9 10 12 13 16
Computing LCAT(u,v)
The nodes in the Euler Tour between the first visits to u and to v are E[R[u],…,R[v]].
The shallowest node is at index RMQL(R[u],R[v]).
The node E[RMQL(R[u],R[v])] is LCAT(u,v).
Example - LCAT(6,9)0
1
2 3 6 9
5
8
4
7
E: 0 1 2 1 3 1 0 4 0 5 6 5 7 8 7 5 9 5 0
L: 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0
R: 0 1 2 4 7 9 10 12 13 16
R[6] R[9]
E[10,…,16]
RMQL(10,16) = 11
LCAT(6,9) = E[11]=5
Complexity Analysis
Preprocessing Array Construction : O(n). Preprocessing of the array L : f(2n-1).
Query Three Array references : O(1). RMQ query in L : g(2n-1).
Overall : )1()12(),()12( OngnOnf
Naive Solutions for RMQ
From now on, we will focus on the RMQ problem.
Computing the RMQ for every pair of indices -
Trivial dynamic programming -
)1(),( 3 OnO
)1(),( 2 OnO
ST Algorithm for RMQPreprocessing
Time: O(nlogn).
a1 ... ... an
i i+2j-1i+2j-1-1
)lognj1 n,i(1 j]M[i,matrix thecompute willWe
][min)12,(],[12
kAiiRMQjiM jiki
jA
]1j,2M[i Otherwise
1]-jM[i, ]]1j,2A[M[i1]]-jA[M[i,
]j,i[M1-j
1-j
ST Algorithm for RMQ Cont.
Arbitrary RMQ(i,j) Query
Time: O(1). ST Algorithm Complexity:
a1an... ...
i j2k elements
2k elements
1ij2rmaxk r
k,12jM Otherwise
ki,M k,12jMAk,iMA
)j,i(RMQk
k
)1(O),nlogn(O
RMQ Solution
… ...B[0] B[2n/logn]B[i]
... ... .........
A
… ...A’[0] A’[2n/logn]A’[i]
A’: block min value
B : block min index
blocksn log
2n
nlog2
1 size of blocks
n log
2n intopartition
Size(A’) = 2n / logn.
ST_Preprocessing(A’)
ST(A’)=
RMQ Solution Cont.
)n(Onlog
)nloglogn2(logn2
n log
2nlog
n log
2n
)1(O),n(O
RMQ Solution Cont.
Arbitrary RMQ(i,j) Query
We should compute the following values:
1. The minimum from i to the end of its block.
2. The minimum of all the blocks in between i’s block and j’s block.
3. The minimum from the beginning of j’s block to j.
...A[i] A[j]
1 2 3
RMQ Solution Cont.
ST Preprocessing of a block
Per Block
All Blocks
...A[i] A[j]
1 2 3
)nloglogn(logOn log2
1log n log
2
1
)nloglogn(O
Observation
Let two arrays X & Y such that
CY[i]X[i] i
3 4 6 5 5 4 5 6 4 5
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
0 13 2 2 1 2 3 1 2
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
+1 -1 -1-1 +1 +1 -1+1 +1
)j,i(RMQ)j,i(RMQ j,i YX
1RMQ
Block size =
1 sequence block size =
Number of possible sequences =
Preprocessing all possible 1 blocks:
Determining which table to use for each block: O(n).
Overall time complexity =
nlog2
1
1nlog2
1
n2
12
1nlog2
1
nlognO 2
)1(O),n(O
General RMQ Solution
Reduction from RMQ to LCA.
Claim: If there is an -time solution for LCA, then
there is an -time solution for RMQ.
)1(O),n(O
)1(O),n(O
Cartesian Tree
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
Given an array, we can build a Cartesian tree.
The root of a Cartesian tree is the minimum element of the array.
The root is labeled with the position of this minimum.
Cartesian Tree
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
Cartesian Tree
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
6
Cartesian Tree
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
7
6
Cartesian Tree
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
7
6
9
Cartesian Tree
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
7
6
9
8
Cartesian Tree
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
5 7
6
9
8
Cartesian Tree
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
0
5 7
6
9
8
Cartesian Tree
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
0
2
1 3
5 7
6
9
8
Linear Cartesian Tree Construction
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
Suppose Ci is the Cartesian tree of A[0,…,i].
The i+1 node belongs to the rightmost path of Ci+1.
We climb up the rightmost path of Ci until finding the position where i+1 node belongs.
Linear Cartesian Tree Construction
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
0
10
Linear Cartesian Tree Construction
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
0
1
10
25
Linear Cartesian Tree Construction
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
0
2
1
10
22
25
34
Linear Cartesian Tree Construction
10 1626 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
0
2
1 33
73434
10
22
25 34
Linear Cartesian Tree Construction
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
0
2
1 3
10
22
25 34
7
Linear Cartesian Tree Construction
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
0
2
1 3
5 7
6
9
8
Reduction from RMQ to LCA
If there is an -time solution for LCA, then there is an -time solution for RMQ.
Let A be the input array to the RMQ problem.
Let C be the Cartesian tree of A.
RMQA(i,j) = LCAC(i,j)
)1(O),n(O
)1(O),n(O
Linear Cartesian Tree Construction
10 1634 26 7 19 10 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
0
2
1 3
5 7
6
9
8