The LCA Problem Revisited

39
The LCA Problem Revisited Michael A.Bender & Martin Farach-Colton

description

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. LCA T ( u,v ). - PowerPoint PPT Presentation

Transcript of The LCA Problem Revisited

Page 1: The LCA Problem Revisited

The LCA Problem Revisited

Michael A.Bender

&

Martin Farach-Colton

Page 2: The LCA Problem Revisited

Outline

Definitions. Reduction from LCA to RMQ. Trivial Solutions for RMQ Faster solution for sub-problem of

RMQ. Solution for general RMQ.

Page 3: The LCA Problem Revisited

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

Page 4: The LCA Problem Revisited

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]

Page 5: The LCA Problem Revisited

Complexity Notation

)n(g),n(f

Preprocessing Time

Query Time

Page 6: The LCA Problem Revisited

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

Page 7: The LCA Problem Revisited

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

Page 8: The LCA Problem Revisited

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

Page 9: The LCA Problem Revisited

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).

Page 10: The LCA Problem Revisited

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

Page 11: The LCA Problem Revisited

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

Page 12: The LCA Problem Revisited

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

Page 13: The LCA Problem Revisited

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

Page 14: The LCA Problem Revisited

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

Page 15: The LCA Problem Revisited

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

Page 16: The LCA Problem Revisited

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

Page 17: The LCA Problem Revisited

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

Page 18: The LCA Problem Revisited

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

Page 19: The LCA Problem Revisited

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

Page 20: The LCA Problem Revisited

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

Page 21: The LCA Problem Revisited

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

Page 22: The LCA Problem Revisited

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.

Page 23: The LCA Problem Revisited

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

Page 24: The LCA Problem Revisited

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

Page 25: The LCA Problem Revisited

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

Page 26: The LCA Problem Revisited

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

Page 27: The LCA Problem Revisited

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

Page 28: The LCA Problem Revisited

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

Page 29: The LCA Problem Revisited

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

Page 30: The LCA Problem Revisited

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

Page 31: The LCA Problem Revisited

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.

Page 32: The LCA Problem Revisited

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

Page 33: The LCA Problem Revisited

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

Page 34: The LCA Problem Revisited

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

Page 35: The LCA Problem Revisited

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

Page 36: The LCA Problem Revisited

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

Page 37: The LCA Problem Revisited

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

Page 38: The LCA Problem Revisited

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

Page 39: The LCA Problem Revisited

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