RangRange Minimum Query and Lowest Common Ancestore Minimum Query and Lowest Common Ancestor
Lowest Common Ancestor Advanced Algorithms...
Transcript of Lowest Common Ancestor Advanced Algorithms...
![Page 1: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/1.jpg)
Advanced Algorithms – COMS31900
Lowest Common Ancestor(with a bit on on Range Minimum Queries)
Raphael Clifford
Slides by Benjamin Sach
![Page 2: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/2.jpg)
Advanced Algorithms – COMS31900
Raphael Clifford
Lowest Common Ancestor(with a bit on on Range Minimum Queries)
![Page 3: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/3.jpg)
Lowest common ancestor
Preprocess a tree T (with n nodes) to answer lowest common ancestor queries. . .
![Page 4: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/4.jpg)
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,
![Page 5: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/5.jpg)
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,
? ! !? ?! ?
![Page 6: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/6.jpg)
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
![Page 7: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/7.jpg)
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
![Page 8: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/8.jpg)
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
![Page 9: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/9.jpg)
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
![Page 10: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/10.jpg)
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
![Page 11: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/11.jpg)
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
![Page 12: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/12.jpg)
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
![Page 13: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/13.jpg)
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
![Page 14: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/14.jpg)
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
![Page 15: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/15.jpg)
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
![Page 16: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/16.jpg)
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
![Page 17: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/17.jpg)
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
![Page 18: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/18.jpg)
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
![Page 19: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/19.jpg)
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
![Page 20: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/20.jpg)
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
![Page 21: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/21.jpg)
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
![Page 22: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/22.jpg)
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
![Page 23: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/23.jpg)
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
![Page 24: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/24.jpg)
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
![Page 25: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/25.jpg)
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
![Page 26: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/26.jpg)
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)
![Page 27: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/27.jpg)
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
![Page 28: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/28.jpg)
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)
![Page 29: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/29.jpg)
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
![Page 30: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/30.jpg)
Solving LCAs using RMQs
3
0
1
2
2 3 4
5 8
10
7
0
9
6
1
![Page 31: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/31.jpg)
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
![Page 32: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/32.jpg)
Solving LCAs using RMQs
3
0
1
2
2 3 4
5 8
10
7
0
9
6
1
![Page 33: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/33.jpg)
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
![Page 34: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/34.jpg)
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
![Page 35: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/35.jpg)
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
![Page 36: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/36.jpg)
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
![Page 37: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/37.jpg)
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
![Page 38: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/38.jpg)
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
![Page 39: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/39.jpg)
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
![Page 40: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/40.jpg)
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
![Page 41: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/41.jpg)
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
![Page 42: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/42.jpg)
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
![Page 43: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/43.jpg)
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
![Page 44: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/44.jpg)
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
![Page 45: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/45.jpg)
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
![Page 46: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/46.jpg)
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
![Page 47: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/47.jpg)
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
![Page 48: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/48.jpg)
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
![Page 49: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/49.jpg)
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
![Page 50: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/50.jpg)
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
![Page 51: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/51.jpg)
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
![Page 52: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/52.jpg)
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
![Page 53: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/53.jpg)
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
![Page 54: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/54.jpg)
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
![Page 55: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/55.jpg)
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
![Page 56: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/56.jpg)
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
![Page 57: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/57.jpg)
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
![Page 58: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/58.jpg)
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
![Page 59: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/59.jpg)
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
![Page 60: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/60.jpg)
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
![Page 61: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/61.jpg)
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
![Page 62: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/62.jpg)
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
![Page 63: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/63.jpg)
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
![Page 64: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/64.jpg)
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
![Page 65: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/65.jpg)
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
![Page 66: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/66.jpg)
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
![Page 67: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/67.jpg)
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
![Page 68: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/68.jpg)
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
![Page 69: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/69.jpg)
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
![Page 70: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/70.jpg)
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
![Page 71: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/71.jpg)
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 . . .
![Page 72: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/72.jpg)
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?
![Page 73: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/73.jpg)
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
![Page 74: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/74.jpg)
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
![Page 75: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/75.jpg)
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
![Page 76: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/76.jpg)
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′)]
![Page 77: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/77.jpg)
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)
![Page 78: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/78.jpg)
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)
![Page 79: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/79.jpg)
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(?)
![Page 80: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/80.jpg)
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(?)
![Page 81: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/81.jpg)
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)
![Page 82: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/82.jpg)
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(?)
![Page 83: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/83.jpg)
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)
![Page 84: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/84.jpg)
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
![Page 85: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/85.jpg)
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
![Page 86: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/86.jpg)
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
![Page 87: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/87.jpg)
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
![Page 88: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/88.jpg)
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
![Page 89: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/89.jpg)
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
![Page 90: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/90.jpg)
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
![Page 91: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/91.jpg)
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
![Page 92: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/92.jpg)
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
![Page 93: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/93.jpg)
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
![Page 94: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/94.jpg)
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
![Page 95: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/95.jpg)
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
![Page 96: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/96.jpg)
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
![Page 97: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/97.jpg)
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
![Page 98: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/98.jpg)
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)
![Page 99: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/99.jpg)
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
![Page 100: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/100.jpg)
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
![Page 101: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/101.jpg)
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
![Page 102: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/102.jpg)
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
![Page 103: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/103.jpg)
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
![Page 104: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/104.jpg)
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
![Page 105: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/105.jpg)
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
![Page 106: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/106.jpg)
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)
![Page 107: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/107.jpg)
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)
![Page 108: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/108.jpg)
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)
![Page 109: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/109.jpg)
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
![Page 110: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/110.jpg)
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
![Page 111: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/111.jpg)
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
![Page 112: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/112.jpg)
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′)]
![Page 113: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/113.jpg)
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′)]
![Page 114: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/114.jpg)
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′)]
![Page 115: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/115.jpg)
±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)
![Page 116: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/116.jpg)
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
![Page 117: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/117.jpg)
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
![Page 118: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/118.jpg)
Counting ±1 RMQ arrays
L
logn2
How many different±1 RMQ arrays like this. . . are there?
![Page 119: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/119.jpg)
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
![Page 120: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/120.jpg)
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)
![Page 121: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/121.jpg)
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)
![Page 122: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/122.jpg)
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)
![Page 123: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/123.jpg)
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)
![Page 124: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/124.jpg)
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)
![Page 125: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/125.jpg)
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)
![Page 126: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/126.jpg)
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)
![Page 127: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/127.jpg)
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)
![Page 128: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/128.jpg)
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)
![Page 129: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/129.jpg)
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)
![Page 130: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/130.jpg)
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)
![Page 131: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/131.jpg)
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)
![Page 132: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/132.jpg)
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)
![Page 133: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/133.jpg)
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)
![Page 134: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/134.jpg)
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)
![Page 135: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/135.jpg)
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)
![Page 136: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/136.jpg)
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)
![Page 137: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/137.jpg)
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
![Page 138: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/138.jpg)
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 to answer RMQs. . .
in O(n) space/prep time
Preprocess each array Li (which has length (logn)/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 = 2nlogn
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
![Page 139: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/139.jpg)
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
![Page 140: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/140.jpg)
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
![Page 141: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/141.jpg)
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)
![Page 142: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/142.jpg)
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)
![Page 143: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/143.jpg)
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
![Page 144: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/144.jpg)
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
![Page 145: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/145.jpg)
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)
![Page 146: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/146.jpg)
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)
![Page 147: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/147.jpg)
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
![Page 148: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/148.jpg)
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
![Page 149: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/149.jpg)
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
![Page 150: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/150.jpg)
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
![Page 151: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/151.jpg)
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
![Page 152: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/152.jpg)
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
![Page 153: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/153.jpg)
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
![Page 154: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/154.jpg)
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
![Page 155: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/155.jpg)
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
![Page 156: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/156.jpg)
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
![Page 157: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/157.jpg)
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
![Page 158: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/158.jpg)
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
![Page 159: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/159.jpg)
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
![Page 160: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/160.jpg)
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
![Page 161: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/161.jpg)
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
![Page 162: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/162.jpg)
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
![Page 163: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/163.jpg)
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
![Page 164: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/164.jpg)
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
![Page 165: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/165.jpg)
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
![Page 166: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/166.jpg)
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
![Page 167: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/167.jpg)
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 :)
![Page 168: Lowest Common Ancestor Advanced Algorithms COMS31900people.cs.bris.ac.uk/~clifford/coms31900-2020/slides/lca.pdf · 2020. 1. 9. · Lowest common ancestor Preprocess a tree T (with](https://reader035.fdocuments.us/reader035/viewer/2022071015/5fce487bea9cb112e12c03d2/html5/thumbnails/168.jpg)
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)