Advanced Algorithms COMS31900 Lowest Common Ancestor (with a bit - - PowerPoint PPT Presentation

advanced algorithms coms31900 lowest common ancestor
SMART_READER_LITE
LIVE PREVIEW

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)


slide-1
SLIDE 1

Advanced Algorithms – COMS31900 Lowest Common Ancestor

(with a bit on on Range Minimum Queries)

Rapha¨ el Clifford

Slides by Benjamin Sach

slide-2
SLIDE 2

Advanced Algorithms – COMS31900 Rapha¨ el Clifford Lowest Common Ancestor

(with a bit on on Range Minimum Queries)

slide-3
SLIDE 3

Lowest common ancestor

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

slide-4
SLIDE 4

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,

slide-5
SLIDE 5

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,

?! !

? ?

!?

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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 root

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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

slide-18
SLIDE 18

Lowest common ancestor

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

i j

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

slide-19
SLIDE 19

Lowest common ancestor

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

i j

root common 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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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 1 2

slide-22
SLIDE 22

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 1 2

slide-23
SLIDE 23

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 1 2

slide-24
SLIDE 24

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 1 2

slide-25
SLIDE 25

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 1 2

slide-26
SLIDE 26

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 1 2

LCA(i, j)

slide-27
SLIDE 27

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 1 2

slide-28
SLIDE 28

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 1 2

i j

LCA(i, j)

slide-29
SLIDE 29

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 1 2

i j

LCA(i, j)

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

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10

7 9 6

1

slide-31
SLIDE 31

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10

the nodes are numbered between

0 and (n − 1)

7 9 6

1

slide-32
SLIDE 32

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10

7 9 6

1

slide-33
SLIDE 33

Solving LCAs using RMQs

3 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 9 6

1

slide-34
SLIDE 34

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10

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

7 9 6

1

slide-35
SLIDE 35

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10

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

1

(node) N

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

(depth) D

1

7 9 6

1

slide-36
SLIDE 36

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10

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

1 5

(node) N

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

(depth) D

2 1

7 9 6

1

slide-37
SLIDE 37

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10

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

1 5

(node) N

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

(depth) D

3 2 1

7 9 6

9 1

slide-38
SLIDE 38

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10

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

1 5 5

(node) N

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

(depth) D

3 2 1 2

7 9 6

9 1

slide-39
SLIDE 39

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10

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

1 5 5 10

(node) N

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

(depth) D

3 2 1 2 3

7 9 6

9 1

slide-40
SLIDE 40

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2

7 9 6

9 1

slide-41
SLIDE 41

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1

7 9 6

9 1

slide-42
SLIDE 42

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1 2

7 9 6

9 6 1

slide-43
SLIDE 43

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1 2 1

7 9 6

9 6 1

slide-44
SLIDE 44

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1 2 1

7 9 6

9 6 1

slide-45
SLIDE 45

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1 2 1 1

7 9 6

9 6 1

slide-46
SLIDE 46

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1 2 1 1 0

7 9 6

9 6 1

slide-47
SLIDE 47

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1 2 1 1 0 1

7 9 6

9 6 1

slide-48
SLIDE 48

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2

7

7

9 6

9 6 1

slide-49
SLIDE 49

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1

7

7

9 6

9 6 1

slide-50
SLIDE 50

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2

7

7

9 6

9 6 1

slide-51
SLIDE 51

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1

7

7

9 6

9 6 1

slide-52
SLIDE 52

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0

7

7

9 6

9 6 1

slide-53
SLIDE 53

Solving LCAs using RMQs

3 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

(node) N

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

(depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1

7

7

9 6

9 6 1

slide-54
SLIDE 54

Solving LCAs using RMQs

3 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 0

(node) N

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

(depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

7

7

9 6

9 6 1

slide-55
SLIDE 55

Solving LCAs using RMQs

3 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 0

(node) N

How long is the tour? Write down every node you visit . . . and its depth

(depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

7

7

9 6

9 6 1

slide-56
SLIDE 56

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

7

7

9 6

9 6 1

slide-57
SLIDE 57

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

9 6

9 6 1

slide-58
SLIDE 58

Solving LCAs using RMQs

3 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 0

(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

3 2 1 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

9 6

9 6 1

slide-59
SLIDE 59

Solving LCAs using RMQs

3 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 0

(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

3 2 1 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

9 6

9 6 1

slide-60
SLIDE 60

Solving LCAs using RMQs

3 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 0

(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

3 2 1 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′

9 6

9 6 1

slide-61
SLIDE 61

Solving LCAs using RMQs

3 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 0

(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

3 2 1 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

9 6

9 6 1

slide-62
SLIDE 62

Solving LCAs using RMQs

3 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 0

(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

3 2 1 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

9 6

9 6 1

slide-63
SLIDE 63

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

how do we find LCA(i,j)?

9 6

9 6 1

slide-64
SLIDE 64

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

how do we find LCA(i,j)?

j i 9 6

9 6 1

slide-65
SLIDE 65

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

how do we find LCA(i,j)?

j i 9 6

Find i and j in N

9 6 1

slide-66
SLIDE 66

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

how do we find LCA(i,j)?

j i 9 6

Find i and j in N

i′ j′ 9 6 1

slide-67
SLIDE 67

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

how do we find LCA(i,j)?

j i 9 6

Find i and j in N

i′ j′

Compute RMQ(i′, j′) in D

9 6 1

slide-68
SLIDE 68

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

how do we find LCA(i,j)?

j i 9 6

Find i and j in N

i′ j′

Compute RMQ(i′, j′) in D

9 6 1 1 1

slide-69
SLIDE 69

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

how do we find LCA(i,j)?

9 6

9 6 1

slide-70
SLIDE 70

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

how do we find LCA(i,j)?

9 6

9 6 1

j i

slide-71
SLIDE 71

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

how do we find LCA(i,j)?

9 6

9 6 1

j i

Find i and j in N. . .

slide-72
SLIDE 72

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

how do we find LCA(i,j)?

9 6

9 6 1

j i

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

slide-73
SLIDE 73

Solving LCAs using RMQs

3 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 0

(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

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

how do we find LCA(i,j)?

9 6

9 6 1

j i

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

any copy is fine

slide-74
SLIDE 74

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

9 6

9 6 1

slide-75
SLIDE 75

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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

slide-76
SLIDE 76

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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′)]
slide-77
SLIDE 77

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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)

slide-78
SLIDE 78

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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)

slide-79
SLIDE 79

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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(?)

slide-80
SLIDE 80

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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(?)

slide-81
SLIDE 81

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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)

slide-82
SLIDE 82

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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(?)

slide-83
SLIDE 83

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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)

slide-84
SLIDE 84

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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

slide-85
SLIDE 85

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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 log n)

Query time O(1) Space O(n log log n) using the best result from last lecture

slide-86
SLIDE 86

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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 log n)

Query time O(1) Space O(n log log n) using the best result from last lecture

slide-87
SLIDE 87

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 x x

(node) N (depth) D

x x d d d d d d

depth > d + 1

S2

x

slide-88
SLIDE 88

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 x x

(node) N (depth) D

x x d d d d d d

depth > d + 1

S2

x

slide-89
SLIDE 89

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 x x

(node) N (depth) D

x x d d d d d d

depth > d + 1

S2

x

slide-90
SLIDE 90

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 x x

(node) N (depth) D

x x d d d d d d

tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1

S2

x

slide-91
SLIDE 91

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 x x

(node) N (depth) D

x x d d d d d d

tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1

S2

x

slide-92
SLIDE 92

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 x x

(node) N (depth) D

x x d d d d d d

tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1

y y y y

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

x y

slide-93
SLIDE 93

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 x x

(node) N (depth) D

x x d d d d d d

tour of S1 tour of S2 tour of S3 tour of Sk depth > d + 1

y y y y

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

x y

slide-94
SLIDE 94

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 x x

(node) N (depth) D

x x d d d d 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 y y y

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

x y

slide-95
SLIDE 95

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 x x

(node) N (depth) D

x x d d d d 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 y y y

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

i j x y

slide-96
SLIDE 96

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 x x

(node) N (depth) D

x x d d d d 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 y y y

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

i j

imagine LCA(i, j) is not y

x y

slide-97
SLIDE 97

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 x x

(node) N (depth) D

x x d d d d 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 y y y

d + 1 d + 1 d + 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

slide-98
SLIDE 98

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 x x

(node) N (depth) D

x x d d d d 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 y y y

d + 1 d + 1 d + 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)

slide-99
SLIDE 99

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 x x

(node) N (depth) D

x x d d d d 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 y y y

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

x y

slide-100
SLIDE 100

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 x x

(node) N (depth) D

x x d d d d 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 y y y

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

i j x y

slide-101
SLIDE 101

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 x x

(node) N (depth) D

x x d d d d 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 y y y

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

i j

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

x y

slide-102
SLIDE 102

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 x x

(node) N (depth) D

x x d d d d 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 y y y

d + 1 d + 1 d + 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

slide-103
SLIDE 103

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 x x

(node) N (depth) D

x x d d d d 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 y y y

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

x y

slide-104
SLIDE 104

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 x x

(node) N (depth) D

x x d d d d 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 y y y

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

x i j y

slide-105
SLIDE 105

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 x x

(node) N (depth) D

x x d d d d 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 y y y

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

x i j y

now imagine LCA(i, j) is y

slide-106
SLIDE 106

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 x x

(node) N (depth) D

x x d d d d 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 y y y

d + 1 d + 1 d + 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)

slide-107
SLIDE 107

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 x x

(node) N (depth) D

x x d d d d 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 y y y

d + 1 d + 1 d + 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)

slide-108
SLIDE 108

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 x x

(node) N (depth) D

x x d d d d 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 y y y

d + 1 d + 1 d + 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)

slide-109
SLIDE 109

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 x x

(node) N (depth) D

x x d d d d 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 y y y

d + 1 d + 1 d + 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

slide-110
SLIDE 110

Ongoing Summary

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

for the Lowest Common Ancestor problem

which uses solution 3 for RMQ from last lecture

slide-111
SLIDE 111

Ongoing Summary Can we do better?

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

for the Lowest Common Ancestor problem

which uses solution 3 for RMQ from last lecture

slide-112
SLIDE 112

Solving LCAs using RMQs - efficiency

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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′)]
slide-113
SLIDE 113

Solving LCAs using RMQs - efficiency

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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′)]
slide-114
SLIDE 114

Solving LCAs using RMQs - efficiency

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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′)]
slide-115
SLIDE 115

±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 17 15 16 15 14 15 16 17 18 19 20 21 20 19 20

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

(in a tie, report the leftmost)

slide-116
SLIDE 116

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 =

2n log n

  • to answer RMQs. . .

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

˜ n =

2n log n

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

L0 L1 L2 L3 L4 L5 L˜

n

goes in here all of these go in here

slide-117
SLIDE 117

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 =

2n log n

  • to answer RMQs. . .

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

˜ n =

2n log n

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!

L0 L1 L2 L3 L4 L5 L˜

n

goes in here all of these go in here

slide-118
SLIDE 118

Counting ±1 RMQ arrays

L

log n 2

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

slide-119
SLIDE 119

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

slide-120
SLIDE 120

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to (remember these are the locations of the minimum)

slide-121
SLIDE 121

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

(remember these are the locations of the minimum)

slide-122
SLIDE 122

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

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)

slide-123
SLIDE 123

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

(remember these are the locations of the minimum)

slide-124
SLIDE 124

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

(remember these are the locations of the minimum)

slide-125
SLIDE 125

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

1 1

(remember these are the locations of the minimum)

slide-126
SLIDE 126

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

1 1

(remember these are the locations of the minimum)

slide-127
SLIDE 127

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

1 1

dx = dy = = 2 = 2

(remember these are the locations of the minimum)

slide-128
SLIDE 128

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

Fact Lx is equivalent to Ly iff dx = dy

1 1

dx = dy = = 2 = 2

(remember these are the locations of the minimum)

slide-129
SLIDE 129

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

Fact Lx is equivalent to Ly

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

iff dx = dy

1 1

dx = dy = = 2 = 2

(remember these are the locations of the minimum)

slide-130
SLIDE 130

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

Fact Lx is equivalent to Ly

  • We can precompute dx for each Lx in O(|Lx|) = O(log n) time.
  • How many different values of d are there?

iff dx = dy

1 1

dx = dy = = 2 = 2

(remember these are the locations of the minimum)

slide-131
SLIDE 131

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

Fact Lx is equivalent to Ly

  • We can precompute dx for each Lx in O(|Lx|) = O(log n) time.
  • How many different values of d are there?

iff dx = dy

1 1

dx = dy = = 2 = 2 d contains (log n)/2 − 1 bits so . . .

(remember these are the locations of the minimum)

slide-132
SLIDE 132

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

Fact Lx is equivalent to Ly

  • We can precompute dx for each Lx in O(|Lx|) = O(log n) time.
  • How many different values of d are there?

iff dx = dy

1 1

dx = dy = = 2 = 2 d contains (log n)/2 − 1 bits so . . .

at most 2(log n)/2 (remember these are the locations of the minimum)

slide-133
SLIDE 133

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

Fact Lx is equivalent to Ly

  • We can precompute dx for each Lx in O(|Lx|) = O(log n) time.
  • How many different values of d are there?

iff dx = dy

1 1

dx = dy = = 2 = 2 d contains (log n)/2 − 1 bits so . . .

at most 2(log n)/2

=

  • 2log n1/2

(remember these are the locations of the minimum)

slide-134
SLIDE 134

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

Fact Lx is equivalent to Ly

  • We can precompute dx for each Lx in O(|Lx|) = O(log n) time.
  • How many different values of d are there?

iff dx = dy

1 1

dx = dy = = 2 = 2 d contains (log n)/2 − 1 bits so . . .

at most 2(log n)/2

=

  • 2log n1/2 √n

(remember these are the locations of the minimum)

slide-135
SLIDE 135

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

Fact Lx is equivalent to Ly

  • We can precompute dx for each Lx in O(|Lx|) = O(log n) 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

1 1

dx = dy = = 2 = 2 d contains (log n)/2 − 1 bits so . . .

at most 2(log n)/2

=

  • 2log n1/2 √n

(remember these are the locations of the minimum)

slide-136
SLIDE 136

Counting ±1 RMQ arrays

L

log n 2

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

Ly

log n 2

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

Lx

log n 2

is equivalent to

16 15 14 15 14 10 9 8 9 8

is equivalent to

Lx Ly

1 2 3 4 1 2 3 4

16 15 14 15 14 10 9 8 9 8

1 2 3 4 1 2 3 4

Fact Lx is equivalent to Ly

  • We can precompute dx for each Lx in O(|Lx|) = O(log n) 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

1 1

dx = dy = = 2 = 2 d contains (log n)/2 − 1 bits so . . .

at most 2(log n)/2

=

  • 2log n1/2 √n

(remember these are the locations of the minimum)

slide-137
SLIDE 137

RMQ on the L arrays in linear space A

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

n ˜ n =

2n log n

log n 2

L0 L1 L2 L3 L4 L5 L˜

n

d0 d1 d2 d3 d4 d5 d˜

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

  • Look up dx
  • Find the row dx in the table
  • Find the entry giving RMQx(i, j)

row d3

This takes O(1) time

slide-138
SLIDE 138

Optimal ±1 RMQ

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

˜ n =

2n log n O(n) total space/prep time

A H ˜ n

i j

n

i′ j′

log n 2

min of these

L0 L1 L2 L3 L4 L5 L˜

n

goes in here all of these go in here

slide-139
SLIDE 139

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 problem

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

slide-140
SLIDE 140

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 problem

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

slide-141
SLIDE 141

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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)

slide-142
SLIDE 142

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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)

slide-143
SLIDE 143

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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

slide-144
SLIDE 144

Solving LCAs using RMQs

3 1 2 2 3

4

5

8

10 1 5 5 10 5 1 1 0 2 0 3 3 8 3 0 4 0

(node) N (depth) D

3 2 1 2 3 2 1 2 1 1 0 1 2 1 2 1 0 1 0

2n−1

7

7

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

by using the solution to ±1RMQ

slide-145
SLIDE 145

Ongoing Summary

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

for the Lowest Common Ancestor problem

which 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 problem

which improves solution 3 for RMQ from last lecture (but only for ±1 inputs)

slide-146
SLIDE 146

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 problem

which 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 problem

which improves solution 3 for RMQ from last lecture (but only for ±1 inputs) (when the inputs aren’t ±1)

slide-147
SLIDE 147

Solving RMQs using LCAs A n

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

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

Build the Cartesian tree, TA of the array A:

6 13

slide-148
SLIDE 148

Solving RMQs using LCAs A n

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

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

slide-149
SLIDE 149

Solving RMQs using LCAs A n

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

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

slide-150
SLIDE 150

Solving RMQs using LCAs A n

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

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

partitions the array in two

6 13

slide-151
SLIDE 151

Solving RMQs using LCAs A n

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

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 8 23 73 51 82 19 32 67 91 14 46 9 21 54

partitions the array in two

6 13

slide-152
SLIDE 152

Solving RMQs using LCAs A n

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

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 8 23 73 51 82 19 32 67 91 14 46 9 21 54

partitions the array in two

  • The rest of the tree is given by

recursing left and right. . .

6 13

slide-153
SLIDE 153

Solving RMQs using LCAs A n

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

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 8 23 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 by

recursing left and right. . .

6 13

slide-154
SLIDE 154

Solving RMQs using LCAs A n

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

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 23 73 51 82 19 32 67 91 14 46 21 54

partitions the array in two

  • The rest of the tree is given by

recursing left and right. . .

6 13

slide-155
SLIDE 155

Solving RMQs using LCAs A n

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

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 23 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 by

recursing left and right. . .

6

19

13

slide-156
SLIDE 156

Solving RMQs using LCAs A n

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

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 by

recursing left and right. . .

6

19

13

slide-157
SLIDE 157

Solving RMQs using LCAs A n

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

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 by

recursing left and right. . .

6

23 51 67 46 54 32 19

13

slide-158
SLIDE 158

Solving RMQs using LCAs A n

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

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 by

recursing left and right. . .

6

23 51 67 46 54 32 19

13

slide-159
SLIDE 159

Solving RMQs using LCAs A n

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

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 by

recursing left and right. . .

6

23 51 67 46 54 32 19

13

slide-160
SLIDE 160

Solving RMQs using LCAs A n

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

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 by

recursing left and right. . .

6

23 51 67 46 54 32 19

13

slide-161
SLIDE 161

Solving RMQs using LCAs A n

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

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 by

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

slide-162
SLIDE 162

Solving RMQs using LCAs A n

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

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 by

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

slide-163
SLIDE 163

Solving RMQs using LCAs A n

17 8 23 73 51 82 19 32 5 67 91 14 46 i = 3 j = 7

RMQ(3, 7) = 6

9 21 54

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 by

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

slide-164
SLIDE 164

Solving RMQs using LCAs A n

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

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 by

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

slide-165
SLIDE 165

Solving RMQs using LCAs A n

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

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 by

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

9

91 54

i = 10 j = 15

13

slide-166
SLIDE 166

Solving RMQs using LCAs A n

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

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 by

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

9

91 54

i = 10 j = 15

13

slide-167
SLIDE 167

Solving RMQs using LCAs A n

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

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 by

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

9

91 54

i = 10 j = 15

13

by using the solution to LCA :)

slide-168
SLIDE 168

Summary

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

for the Range Minimum Query problem

which 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 problem

which 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 problem

which improves solution 3 for RMQ from last lecture (but only for ±1 inputs) (which works for all inputs)