lowest common ancestor

Post on 16-Feb-2017

37 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Advanced Algorithms – COMS31900

Lowest Common Ancestor(with a bit on on Range Minimum Queries)

Benjamin Sach

Advanced Algorithms – COMS31900

Benjamin Sach

Lowest Common Ancestor(with a bit on on Range Minimum Queries)

Lowest common ancestor

Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . .

Lowest common ancestor

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,

Lowest common ancestor

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,

? ! !? ?! ?

Lowest common ancestor

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

Lowest common ancestor

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

i

root

Lowest common ancestor

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

i

root

Lowest common ancestor

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

i

ancestors of node iroot

Lowest common ancestor

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

i

ancestors of node i

- nodes on the path

from i to the root

root

Lowest common ancestor

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

Lowest common ancestor

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

j

Lowest common ancestor

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

j

Lowest common ancestor

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

j

ancestors of node j

Lowest common ancestor

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

j

ancestors of node j

- nodes on the path

from j to the root

Lowest common ancestor

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

j

Lowest common ancestor

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

i

j

root

Lowest common ancestor

Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . .

After preprocessing,

i

j

rootcommon ancestors of i and j

the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j

Lowest common ancestor

Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . .

After preprocessing,

i

j

rootcommon ancestors of i and j

- nodes which are

ancestors of both i

and j

the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j

Lowest common ancestor

Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . .

After preprocessing,

i

j

root

the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j

Lowest common ancestor

Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . .

After preprocessing,

i

j

root

the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j

4

5

3

0

1

2

Lowest common ancestor

Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . .

After preprocessing,

i

j

root

the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j

depth =

4

5

3

0

1

2

Lowest common ancestor

Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . .

After preprocessing,

i

j

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

4

5

3

0

1

2

Lowest common ancestor

Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . .

After preprocessing,

i

j

root depth =lowest common ancestor of i and j

- the common ancestor of i

and j furthest

from the root

the output to a query LCA(i, j) is the lowest common ancestor of nodes i and j

4

5

3

0

1

2

Lowest common ancestor

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

4

5

3

0

1

2

Lowest common ancestor

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

i

j 4

5

3

0

1

2LCA(i, j)

Lowest common ancestor

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

4

5

3

0

1

2

Lowest common ancestor

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

4

5

3

0

1

2

ij

LCA(i, j)

Lowest common ancestor

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

4

5

3

0

1

2

ij

LCA(i, j)

• Ideally, we would like O(n) space, O(n) prep. time and O(1) query time

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

7

0

9

6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

the nodes are numbered between

0 and (n− 1)

7

0

9

6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

7

0

9

6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

Write down every node you visit. . . and its depth

7

0

9

6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

0(node) N

Write down every node you visit. . . and its depth

(depth) D 0

7

0

9

6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

10(node) N

Write down every node you visit. . . and its depth

(depth) D 10

7

0

9

6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 50(node) N

Write down every node you visit. . . and its depth

(depth) D 210

7

0

9

6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 50(node) N

Write down every node you visit. . . and its depth

(depth) D 3210

7

0

9

6

9

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 50(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2

7

0

9

6

9

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 100(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3

7

0

9

6

9

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 50(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2

7

0

9

6

9

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 10(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1

7

0

9

6

9

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 10(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2

7

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 10(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1

7

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 00(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 20(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1 1

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 00(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1 1 0

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 30(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1 1 0 1

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 30(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1 1 0 1 2

7

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 30(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1

7

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 80(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2

7

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 30(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1

7

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 00(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0

7

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 40(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1

7

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

7

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

How long is the tour?

Write down every node you visit. . . and its depth

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

7

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

7

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

how do we find LCA(i,j)?

7

i j

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

how do we find LCA(i,j)?

7

i j

Find i and j in N

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

how do we find LCA(i,j)?

7

i j

Find i and j in N

7

i′ j′0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

how do we find LCA(i,j)?

7

i j

Find i and j in N

7

i′ j′

Compute RMQ(i′, j′) in D

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

how do we find LCA(i,j)?

7

i j

Find i and j in N

7

i′ j′

Compute RMQ(i′, j′) in D

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

how do we find LCA(i,j)?

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

how do we find LCA(i,j)?

ji

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

how do we find LCA(i,j)?

ji

9

6

Find i and j in N

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

how do we find LCA(i,j)?

ji

9

6

Find i and j in N

i′ j′

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

how do we find LCA(i,j)?

ji

9

6

Find i and j in N

i′ j′

Compute RMQ(i′, j′) in D

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

how do we find LCA(i,j)?

ji

9

6

Find i and j in N

i′ j′

Compute RMQ(i′, j′) in D

9 6

1

11

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

how do we find LCA(i,j)?

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

how do we find LCA(i,j)?

9

6

9 6

1

ji

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

how do we find LCA(i,j)?

9

6

9 6

1

ji

Find i and j in N . . .

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

how do we find LCA(i,j)?

9

6

9 6

1

ji

Find i and j in N . . . which copy of i?

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

Compute an Euler tour of T . . .(a depth first search with repeats)

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(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 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

how do we find LCA(i,j)?

9

6

9 6

1

ji

Find i and j in N . . . which copy of i? any copy is fine

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(n)

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(n)

O(?)

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(1)

O(n)

O(?)

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(1)

O(n)

O(?)

O(1)

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(1)

O(n)

O(?)

O(1)

O(?)

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(1)

O(n)

O(?)

O(1)

O(?)

O(1)

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(1)

O(n)

O(?)

O(1)

O(?)

O(1)

Prep. time O(n+ prepRMQ(n))

Query time O(1 + queryRMQ(n))

Space O(n+ spaceRMQ(n))

depends on the RMQ structure used

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(1)

O(n)

O(?)

O(1)

O(?)

O(1)

Prep. time O(n log logn)

Query time O(1)

Space O(n log logn)

using the best result from last lecture

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

why does this work?

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(1)

O(n)

O(?)

O(1)

O(?)

O(1)

Prep. time O(n log logn)

Query time O(1)

Space O(n log logn)

using the best result from last lecture

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d d

depth > d+ 1

S2

x

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

children

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d d

depth > d+ 1

S2

x

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

subtrees

children

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d d

depth > d+ 1

S2

x

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

subtrees

children

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d d

tour of S1 tour of S2 tour of S3 tour of Sk

depth > d+ 1

S2

x

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d d

tour of S1 tour of S2 tour of S3 tour of Sk

depth > d+ 1

S2

x

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d d

tour of S1 tour of S2 tour of S3 tour of Sk

depth > d+ 1

y yy y

d+ 1 d+ 1d+ 1 d+ 1

x

y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d d

tour of S1 tour of S2 tour of S3 tour of Sk

depth > d+ 1

y yy y

d+ 1 d+ 1d+ 1 d+ 1

x

y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

x

y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

i j

x

y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

i j imagine LCA(i, j) is not y

x

y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

i j imagine LCA(i, j) is not y

i′ and j′ are in here so RMQ does not return the location of a y

x

y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

i j imagine LCA(i, j) is not y

i′ and j′ are in here so RMQ does not return the location of a y

x

y

(all of the ys are out of range)

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

x

y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

i

j

x

y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

i

j

again, imagine LCA(i, j) is not y

x

y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

i

j

again, imagine LCA(i, j) is not y

i′ and j′ cross an x (which has smaller depth than y)

so the RMQ location isn’t a y

x

y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

x

y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

x

i j

y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

x

i j

y

now imagine LCA(i, j) is y

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

x

i j

y

now imagine LCA(i, j) is y

i′ and j′ cross a y (which is the smallest in the range)

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

x

i j

y

now imagine LCA(i, j) is y

i′ and j′ cross a y (which is the smallest in the range)

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

x

i j

y

now imagine LCA(i, j) is y

i′ and j′ cross a y (which is the smallest in the range)

Solving LCA using RMQ - correctness

We can also define a Euler tour of T recursively. . .

S1 S3 Sk

depth = d

depth = d+ 1

x x xx(node) N

(depth) D

x x

d d dd d 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

y yy y

d+ 1 d+ 1d+ 1 d+ 1

x

i j

y

now imagine LCA(i, j) is y

i′ and j′ cross a y (which is the smallest in the range)so the RMQ reports a y

Ongoing Summary

We have seen an O(n log logn) space, O(n log logn) prep. time and O(1) query time solution

for the Lowest Common Ancestor problemwhich uses solution 3 for RMQ from last lecture

Ongoing Summary

Can we do better?

We have seen an O(n log logn) space, O(n log logn) prep. time and O(1) query time solution

for the Lowest Common Ancestor problemwhich uses solution 3 for RMQ from last lecture

Solving LCAs using RMQs - efficiency

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

Solving LCAs using RMQs - efficiency

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

Notice anything interesting about D?

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

Solving LCAs using RMQs - efficiency

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

Notice anything interesting about D? D[i+ 1] = D[i]± 1

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

±1 Range minimum query

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

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]

i = 3 j = 7

RMQ(3, 7) = 5

• Can we exploit this±1 property to get a more efficient RMQ data structure?

• Ideally we would like O(n) space, O(n) prep. time and O(1) query time

where for all k, we have A[k + 1] = A[k]± 1

16 1715 16 15 14 15 16 17 18 19 20 21 20 19 20

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

(in a tie, report the leftmost)

Low-resolution RMQ (again)

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

i′ j′

Preprocess the array H(

which has length n = 2nlogn

)to answer RMQs. . .

in O(n) space/prep time

Preprocess each array Li (which has length (logn)/2) to answer RMQs. . .

in O(logn log logn) 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

n = 2nlogn

logn2

min of these

as there are O(n/ logn) Li arrays, we have O(n log logn) total space/prep time

L0L1

L2L3

L4L5 Ln

goes in here

all of thesego in here

Low-resolution RMQ (again)

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

i′ j′

Preprocess the array H(

which has length n = 2nlogn

)to answer RMQs. . .

in O(n) space/prep time

Preprocess each array Li (which has length (logn)/2) to answer RMQs. . .

in O(logn log logn) 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

n = 2nlogn

logn2

min of these

as there are O(n/ logn) Li arrays, we have O(n log logn) total space/prep time

too big and slow!

L0L1

L2L3

L4L5 Ln

goes in here

all of thesego in here

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4RMQx(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)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4

0100 0100

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4

0100 0100

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4

0100 0100dx = dy == 2 = 2

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4 Fact Lx is equivalent to Ly

iff dx = dy

0100 0100dx = dy == 2 = 2

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4 Fact Lx is equivalent to Ly

• We can precompute dx for each Lx in O(|Lx|) = O(logn) time.

iff dx = dy

0100 0100dx = dy == 2 = 2

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4 Fact Lx is equivalent to Ly

• We can precompute dx for each Lx in O(|Lx|) = O(logn) time.

• How many different values of d are there?

iff dx = dy

0100 0100dx = dy == 2 = 2

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4 Fact Lx is equivalent to Ly

• We can precompute dx for each Lx in O(|Lx|) = O(logn) time.

• How many different values of d are there?

iff dx = dy

0100 0100dx = dy == 2 = 2

d contains (logn)/2− 1 bits so . . .

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4 Fact Lx is equivalent to Ly

• We can precompute dx for each Lx in O(|Lx|) = O(logn) time.

• How many different values of d are there?

iff dx = dy

0100 0100dx = dy == 2 = 2

d contains (logn)/2− 1 bits so . . . at most 2(logn)/2

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4 Fact Lx is equivalent to Ly

• We can precompute dx for each Lx in O(|Lx|) = O(logn) time.

• How many different values of d are there?

iff dx = dy

0100 0100dx = dy == 2 = 2

d contains (logn)/2− 1 bits so . . . at most 2(logn)/2 =(2logn

)1/2

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4 Fact Lx is equivalent to Ly

• We can precompute dx for each Lx in O(|Lx|) = O(logn) time.

• How many different values of d are there?

iff dx = dy

0100 0100dx = dy == 2 = 2

d contains (logn)/2− 1 bits so . . . at most 2(logn)/2 =(2logn

)1/26√n

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4 Fact Lx is equivalent to Ly

• We can precompute dx for each Lx in O(|Lx|) = O(logn) time.

• How many different values of d are there?

• For each value of d we store RMQ(i, j) for all i, j

iff dx = dy

0100 0100dx = dy == 2 = 2

d contains (logn)/2− 1 bits so . . . at most 2(logn)/2 =(2logn

)1/26√n

(remember these are the locations of the minimum)

Counting ±1 RMQ arrays

L

logn2

How many different±1 RMQ arrays like this. . . are there?

Ly

logn2

We say that iff for all (i, j): RMQx(i, j) = RMQy(i, j)

Lx

logn2

is equivalent to

16 15 14 15 14 10 9 8 9 8is equivalent to

Lx Ly0 1 2 3 4 0 1 2 3 4

16 15 14 15 14 10 9 8 9 8

0 1 2 3 4 0 1 2 3 4 Fact Lx is equivalent to Ly

• We can precompute dx for each Lx in O(|Lx|) = O(logn) time.

• How many different values of d are there?

• For each value of d we store RMQ(i, j) for all i, j

iff dx = dy

. . . this requires O(√n log2 n) = O(n) total space and prep. time

0100 0100dx = dy == 2 = 2

d contains (logn)/2− 1 bits so . . . at most 2(logn)/2 =(2logn

)1/26√n

(remember these are the locations of the minimum)

RMQ on the L arrays in linear space

A

Key Idea replace A with a smaller, ‘low resolution’ array H

n

n = 2nlogn

logn2

L0L1

L2L3

L4L5 Ln

d0 d1 d2 d3 d4 d5 dn

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 6 d 6√n

in O(n) total space and prep. time

To perform a query within some Lx

• Look up dx

• Find the row dx in the table

• Find the entry giving RMQx(i, j)

row d3

This takes O(1) time

Optimal ±1 RMQKey Idea replace A with a smaller, ‘low resolution’ array H

and many small arrays L0, L1, L2 . . . ‘for the details’

Preprocess the array H (which has length n = 8nlogn ) to answer RMQs. . .

in O(n) space/prep time

Preprocess each array Li (which has length (logn)/2) to answer RMQs. . .

in O(logn) 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

n = 2nlogn

as there are O(n/ logn) Li arrays, we have O(n) total space/prep time

A

H

n

i jn

i′ j′

logn2

min of these

L0L1

L2L3

L4L5 Ln

goes in here

all of thesego in here

Ongoing Summary

for the Lowest Common Ancestor problem

We have seen an O(n) space, O(n) prep. time and O(1) query time solution

for the ±1 Range Minimum Query problemwhich 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 logn) space, O(n log logn) prep. time and O(1) query time solution

Ongoing Summary

How does this affect our LCA solution?

for the Lowest Common Ancestor problem

We have seen an O(n) space, O(n) prep. time and O(1) query time solution

for the ±1 Range Minimum Query problemwhich 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 logn) space, O(n log logn) prep. time and O(1) query time solution

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(1)

O(n)

O(n)

O(1)

O(1)

O(1)

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(1)

O(n)

O(n)

O(1)

O(1)

O(1)

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(1)

O(n)

O(n)

O(1)

O(1)

O(1)

This gives us O(n) space, O(n) prep. time and O(1) query time for the LCA problem

Solving LCAs using RMQs

3

0

1

2

2 3 4

5 8

10

1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 00(node) N

(depth) D 3210 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

0

0

9

6

9 6

1

Preprocessing Summary

1. Construct N and D from T

2. Add a pointer from each

3. Preprocess D for RMQs

node i to some N [i′] = i

Query Summary - LCA(i,j)

1. Find (any) i′ st. N [i′] = i

2. Find (any) j′ st. N [j′] = j

3. Compute RMQ(i′, j′) in D

4. LCA(i, j) = N [RMQ(i′, j′)]

O(n)

O(1)

O(n)

O(n)

O(1)

O(1)

O(1)

This gives us O(n) space, O(n) prep. time and O(1) query time for the LCA problemby using the solution to ±1RMQ

Ongoing Summary

We have seen an O(n) space, O(n) prep. time and O(1) query time solution

for the Lowest Common Ancestor problemwhich uses the solution to ±1RMQ

We have seen an O(n) space, O(n) prep. time and O(1) query time solution

for the ±1 Range Minimum Query problemwhich improves solution 3 for RMQ from last lecture

(but only for±1 inputs)

Ongoing Summary

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

for the Lowest Common Ancestor problemwhich uses the solution to ±1RMQ

We have seen an O(n) space, O(n) prep. time and O(1) query time solution

for the ±1 Range Minimum Query problemwhich improves solution 3 for RMQ from last lecture

(but only for±1 inputs)

(when the inputs aren’t±1)

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

Build the Cartesian tree, TA of the array A:

6 13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

6 13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

6 13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected locationpartitions the array in two

6 13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location

17 823 73 51 82 19 32 67 91 14 46 9 21 54

partitions the array in two

6 13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location

17 823 73 51 82 19 32 67 91 14 46 9 21 54

partitions the array in two

• The rest of the tree is given byrecursing left and right. . .

6 13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location

17 823 73 51 82 19 32 67 91 14 46 9 21 54

8 9

partitions the array in two

• The rest of the tree is given byrecursing left and right. . .

6 13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location 8 9

1723 73 51 82 19 32 67 91 14 46 21 54

partitions the array in two

• The rest of the tree is given byrecursing left and right. . .

6 13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location 8 9

1723 73 51 82 19 32 67 91 14 46 21 54

17 14 21

partitions the array in two

• The rest of the tree is given byrecursing left and right. . .

6

19

13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location 8 9

17 14 21

23 73 51 82 32 67 91 46 54

partitions the array in two

• The rest of the tree is given byrecursing left and right. . .

6

19

13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location 8 9

17 14 21

23 73 51 82 32 67 91 46 54

partitions the array in two

• The rest of the tree is given byrecursing left and right. . .

6

23 51 67 46 5432

19

13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location 8 9

17 14 21

73 82 91

partitions the array in two

• The rest of the tree is given byrecursing left and right. . .

6

23 51 67 46 5432

19

13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location 8 9

17 14 21

73 82 91

partitions the array in two

• The rest of the tree is given byrecursing left and right. . .

6

23 51 67 46 5432

19

13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location

This process isn’t very efficient. . .

a better one takes O(n) time

8 9

17 14 21

73 82 91

partitions the array in two

• The rest of the tree is given byrecursing left and right. . .

6

23 51 67 46 5432

19

13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location

This process isn’t very efficient. . .

a better one takes O(n) time

8 9

17 14 21

73 82 91

partitions the array in two

• The rest of the tree is given byrecursing left and right. . .

it’s not tricky but we don’t have

time to cover it

6

23 51 67 46 5432

19

13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location

This process isn’t very efficient. . .

a better one takes O(n) time

8 9

17 14 21

73 82 91

partitions the array in two

• The rest of the tree is given byrecursing 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 5432

19

13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46

i = 3 j = 7

RMQ(3, 7) = 6

9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location

This process isn’t very efficient. . .

a better one takes O(n) time

8 9

17 14 21

73 82 91

partitions the array in two

• The rest of the tree is given byrecursing 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

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location

This process isn’t very efficient. . .

a better one takes O(n) time

8 9

17 14 21

73 82 91

partitions the array in two

• The rest of the tree is given byrecursing 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 5432

19

13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location

This process isn’t very efficient. . .

a better one takes O(n) time

8 9

17 14 21

73 82 91

partitions the array in two

• The rest of the tree is given byrecursing 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 5432

19

9

91

54

i = 10 j = 15

13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location

This process isn’t very efficient. . .

a better one takes O(n) time

8 9

17 14 21

73 82 91

partitions the array in two

• The rest of the tree is given byrecursing 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 5432

19

9

91

54

i = 10 j = 15

13

Solving RMQs using LCAs

A

n

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 7 8 9 10 11 12 14 15

5

Build the Cartesian tree, TA of the array A:

• The root is the smallest value

• The selected location

This process isn’t very efficient. . .

a better one takes O(n) time

8 9

17 14 21

73 82 91

partitions the array in two

• The rest of the tree is given byrecursing 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 5432

19

9

91

54

i = 10 j = 15

13

by using the solution to LCA :)

Summary

We have seen an O(n) space, O(n) prep. time and O(1) query time solution

for the Range Minimum Query problemwhich uses the solution to LCA

We have seen an O(n) space, O(n) prep. time and O(1) query time solution

for the Lowest Common Ancestor problemwhich uses the solution to ±1RMQ

We have seen an O(n) space, O(n) prep. time and O(1) query time solution

for the ±1 Range Minimum Query problemwhich improves solution 3 for RMQ from last lecture

(but only for±1 inputs)

(which works for all inputs)

top related