advanced algorithms coms31900 lowest common ancestor
play

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

Advanced Algorithms COMS31900 Lowest Common Ancestor (with a bit on on Range Minimum Queries) Rapha el Clifford Slides by Benjamin Sach Advanced Algorithms COMS31900 Lowest Common Ancestor (with a bit on on Range Minimum Queries)


  1. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 (depth) D 0 1 2 3 2 3 2 1 2 1

  2. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 (depth) D 0 1 2 3 2 3 2 1 2 1 0

  3. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1

  4. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0

  5. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1

  6. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2

  7. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1

  8. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2

  9. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1

  10. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0

  11. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1

  12. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  13. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  14. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  15. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  16. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . i j and there are ( n − 1) edges 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 how do we find LCA(i,j)?

  17. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . i j and there are ( n − 1) edges 9 10 3 Find i and j in N (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 how do we find LCA(i,j)?

  18. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . i j and there are ( n − 1) edges 9 10 3 Find i and j in N (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 i ′ j ′ how do we find LCA(i,j)?

  19. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . i j and there are ( n − 1) edges 9 10 3 Find i and j in N (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 i ′ j ′ Compute RMQ ( i ′ , j ′ ) in D how do we find LCA(i,j)?

  20. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . i j and there are ( n − 1) edges 9 10 3 Find i and j in N (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 i ′ j ′ Compute RMQ ( i ′ , j ′ ) in D how do we find LCA(i,j)?

  21. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 how do we find LCA(i,j)?

  22. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? j i We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 how do we find LCA(i,j)?

  23. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? j i We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 Find i and j in N (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 how do we find LCA(i,j)?

  24. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? j i We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 Find i and j in N (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 i ′ j ′ how do we find LCA(i,j)?

  25. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? j i We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 Find i and j in N (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 i ′ j ′ Compute RMQ ( i ′ , j ′ ) in D how do we find LCA(i,j)?

  26. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? j i We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 Find i and j in N (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 1 0 1 0 1 2 1 2 1 0 1 0 i ′ j ′ Compute RMQ ( i ′ , j ′ ) in D how do we find LCA(i,j)?

  27. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 how do we find LCA(i,j)?

  28. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit j i . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 how do we find LCA(i,j)?

  29. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit j i . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 Find i and j in N . . . (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 how do we find LCA(i,j)?

  30. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit j i . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 Find i and j in N . . . which copy of i ? (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 how do we find LCA(i,j)?

  31. Solving LCAs using RMQs 0 0 Compute an Euler tour of T . . . (a depth first search with repeats) 4 1 3 1 2 Write down every node you visit j i . . . and its depth 6 7 8 5 2 How long is the tour? We follow each edge twice. . . and there are ( n − 1) edges 9 10 3 Find i and j in N . . . which copy of i ? any copy is fine (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 how do we find LCA(i,j)?

  32. Solving LCAs using RMQs 0 0 4 1 3 1 2 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  33. Solving LCAs using RMQs 0 0 Preprocessing Summary 1. Construct N and D from T 2. Add a pointer from each node i to some N [ i ′ ] = i 4 1 3 1 2 3. Preprocess D for RMQs 6 7 8 5 2 9 10 3 (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  34. Solving LCAs using RMQs 0 0 Preprocessing Summary 1. Construct N and D from T 2. Add a pointer from each node i to some N [ i ′ ] = i 4 1 3 1 2 3. Preprocess D for RMQs 6 7 8 5 2 Query Summary - LCA(i,j) 1. Find (any) i ′ st. N [ i ′ ] = i 2. Find (any) j ′ st. N [ j ′ ] = j 9 10 3. Compute RMQ ( i ′ , j ′ ) in D 3 4. LCA ( i, j ) = N [ RMQ ( i ′ , j ′ )] (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  35. Solving LCAs using RMQs 0 0 Preprocessing Summary O ( n ) 1. Construct N and D from T 2. Add a pointer from each node i to some N [ i ′ ] = i 4 1 3 1 2 3. Preprocess D for RMQs 6 7 8 5 2 Query Summary - LCA(i,j) 1. Find (any) i ′ st. N [ i ′ ] = i 2. Find (any) j ′ st. N [ j ′ ] = j 9 10 3. Compute RMQ ( i ′ , j ′ ) in D 3 4. LCA ( i, j ) = N [ RMQ ( i ′ , j ′ )] (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  36. Solving LCAs using RMQs 0 0 Preprocessing Summary O ( n ) 1. Construct N and D from T O ( n ) 2. Add a pointer from each node i to some N [ i ′ ] = i 4 1 3 1 2 3. Preprocess D for RMQs 6 7 8 5 2 Query Summary - LCA(i,j) 1. Find (any) i ′ st. N [ i ′ ] = i 2. Find (any) j ′ st. N [ j ′ ] = j 9 10 3. Compute RMQ ( i ′ , j ′ ) in D 3 4. LCA ( i, j ) = N [ RMQ ( i ′ , j ′ )] (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  37. Solving LCAs using RMQs 0 0 Preprocessing Summary O ( n ) 1. Construct N and D from T O ( n ) 2. Add a pointer from each node i to some N [ i ′ ] = i 4 1 3 1 2 O (?) 3. Preprocess D for RMQs 6 7 8 5 2 Query Summary - LCA(i,j) 1. Find (any) i ′ st. N [ i ′ ] = i 2. Find (any) j ′ st. N [ j ′ ] = j 9 10 3. Compute RMQ ( i ′ , j ′ ) in D 3 4. LCA ( i, j ) = N [ RMQ ( i ′ , j ′ )] (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  38. Solving LCAs using RMQs 0 0 Preprocessing Summary O ( n ) 1. Construct N and D from T O ( n ) 2. Add a pointer from each node i to some N [ i ′ ] = i 4 1 3 1 2 O (?) 3. Preprocess D for RMQs 6 7 8 5 2 Query Summary - LCA(i,j) 1. Find (any) i ′ st. N [ i ′ ] = i O (1) 2. Find (any) j ′ st. N [ j ′ ] = j 9 10 3. Compute RMQ ( i ′ , j ′ ) in D 3 4. LCA ( i, j ) = N [ RMQ ( i ′ , j ′ )] (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  39. Solving LCAs using RMQs 0 0 Preprocessing Summary O ( n ) 1. Construct N and D from T O ( n ) 2. Add a pointer from each node i to some N [ i ′ ] = i 4 1 3 1 2 O (?) 3. Preprocess D for RMQs 6 7 8 5 2 Query Summary - LCA(i,j) 1. Find (any) i ′ st. N [ i ′ ] = i O (1) 2. Find (any) j ′ st. N [ j ′ ] = j O (1) 9 10 3. Compute RMQ ( i ′ , j ′ ) in D 3 4. LCA ( i, j ) = N [ RMQ ( i ′ , j ′ )] (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  40. Solving LCAs using RMQs 0 0 Preprocessing Summary O ( n ) 1. Construct N and D from T O ( n ) 2. Add a pointer from each node i to some N [ i ′ ] = i 4 1 3 1 2 O (?) 3. Preprocess D for RMQs 6 7 8 5 2 Query Summary - LCA(i,j) 1. Find (any) i ′ st. N [ i ′ ] = i O (1) 2. Find (any) j ′ st. N [ j ′ ] = j O (1) 9 10 O (?) 3. Compute RMQ ( i ′ , j ′ ) in D 3 4. LCA ( i, j ) = N [ RMQ ( i ′ , j ′ )] (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  41. Solving LCAs using RMQs 0 0 Preprocessing Summary O ( n ) 1. Construct N and D from T O ( n ) 2. Add a pointer from each node i to some N [ i ′ ] = i 4 1 3 1 2 O (?) 3. Preprocess D for RMQs 6 7 8 5 2 Query Summary - LCA(i,j) 1. Find (any) i ′ st. N [ i ′ ] = i O (1) 2. Find (any) j ′ st. N [ j ′ ] = j O (1) 9 10 O (?) 3. Compute RMQ ( i ′ , j ′ ) in D 3 O (1) 4. LCA ( i, j ) = N [ RMQ ( i ′ , j ′ )] (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0

  42. Solving LCAs using RMQs 0 0 Preprocessing Summary O ( n ) 1. Construct N and D from T O ( n ) 2. Add a pointer from each node i to some N [ i ′ ] = i 4 1 3 1 2 O (?) 3. Preprocess D for RMQs 6 7 8 5 2 Query Summary - LCA(i,j) 1. Find (any) i ′ st. N [ i ′ ] = i O (1) 2. Find (any) j ′ st. N [ j ′ ] = j O (1) 9 10 O (?) 3. Compute RMQ ( i ′ , j ′ ) in D 3 O (1) 4. LCA ( i, j ) = N [ RMQ ( i ′ , j ′ )] (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 Prep. time O ( n + prepRMQ ( n )) Space O ( n + spaceRMQ ( n )) Query time O (1 + queryRMQ ( n )) depends on the RMQ structure used

  43. Solving LCAs using RMQs 0 0 Preprocessing Summary O ( n ) 1. Construct N and D from T O ( n ) 2. Add a pointer from each node i to some N [ i ′ ] = i 4 1 3 1 2 O (?) 3. Preprocess D for RMQs 6 7 8 5 2 Query Summary - LCA(i,j) 1. Find (any) i ′ st. N [ i ′ ] = i O (1) 2. Find (any) j ′ st. N [ j ′ ] = j O (1) 9 10 O (?) 3. Compute RMQ ( i ′ , j ′ ) in D 3 O (1) 4. LCA ( i, j ) = N [ RMQ ( i ′ , j ′ )] (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 Prep. time O ( n log log n ) Space O ( n log log n ) Query time O (1) using the best result from last lecture

  44. Solving LCAs using RMQs 0 0 Preprocessing Summary O ( n ) 1. Construct N and D from T O ( n ) 2. Add a pointer from each node i to some N [ i ′ ] = i 4 1 3 1 2 O (?) 3. Preprocess D for RMQs 6 7 8 5 2 Query Summary - LCA(i,j) 1. Find (any) i ′ st. N [ i ′ ] = i O (1) 2. Find (any) j ′ st. N [ j ′ ] = j O (1) 9 10 O (?) 3. Compute RMQ ( i ′ , j ′ ) in D 3 O (1) 4. LCA ( i, j ) = N [ RMQ ( i ′ , j ′ )] why does this work? (node) N 0 1 5 9 5 10 5 1 6 1 0 2 0 3 7 3 8 3 0 4 0 2 n − 1 (depth) D 0 1 2 3 2 3 2 1 2 1 0 1 0 1 2 1 2 1 0 1 0 Prep. time O ( n log log n ) Space O ( n log log n ) Query time O (1) using the best result from last lecture

  45. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . x depth = d depth = d + 1 depth > d + 1 S 1 S 2 S 3 S k (node) N x x x x x x (depth) D d d d d d d

  46. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . x depth = d children depth = d + 1 depth > d + 1 S 1 S 2 S 3 S k (node) N x x x x x x (depth) D d d d d d d

  47. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . x depth = d children depth = d + 1 depth > d + 1 S 1 S 2 S 3 S k subtrees (node) N x x x x x x (depth) D d d d d d d

  48. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . x depth = d children depth = d + 1 depth > d + 1 S 1 S 2 S 3 S k subtrees (node) N x x x x x x (depth) D d d d d d d tour of S 1 tour of S 2 tour of S 3 tour of S k

  49. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . x depth = d depth = d + 1 depth > d + 1 S 1 S 2 S 3 S k (node) N x x x x x x (depth) D d d d d d d tour of S 1 tour of S 2 tour of S 3 tour of S k

  50. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . x depth = d depth = d + 1 y depth > d + 1 S 1 S 3 S k y y y y (node) N x x x x x x (depth) D d d d d d d d + 1 d + 1 d + 1 d + 1 tour of S 1 tour of S 2 tour of S 3 tour of S k

  51. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . x depth = d depth = d + 1 y depth > d + 1 S 1 S 3 S k y y y y (node) N x x x x x x (depth) D d d d d d d d + 1 d + 1 d + 1 d + 1 tour of S 1 tour of S 2 tour of S 3 tour of S k

  52. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . Claim the RMQ reports the location of x depth = d some y in N iff LCA ( i, j ) = y depth = d + 1 y depth > d + 1 S 1 S 3 S k y y y y (node) N x x x x x x (depth) D d d d d d d d + 1 d + 1 d + 1 d + 1 tour of S 1 tour of S 2 tour of S 3 tour of S k

  53. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . Claim the RMQ reports the location of x depth = d some y in N iff LCA ( i, j ) = y depth = d + 1 y depth > d + 1 S 1 S 3 S k i j y y y y (node) N x x x x x x (depth) D d d d d d d d + 1 d + 1 d + 1 d + 1 tour of S 1 tour of S 2 tour of S 3 tour of S k

  54. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . Claim the RMQ reports the location of x depth = d some y in N iff LCA ( i, j ) = y depth = d + 1 y depth > d + 1 S 1 S 3 S k i j imagine LCA ( i, j ) is not y y y y y (node) N x x x x x x (depth) D d d d d d d d + 1 d + 1 d + 1 d + 1 tour of S 1 tour of S 2 tour of S 3 tour of S k

  55. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . Claim the RMQ reports the location of x depth = d some y in N iff LCA ( i, j ) = y depth = d + 1 y depth > d + 1 S 1 S 3 S k 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 y y y y (node) N x x x x x x (depth) D d d d d d d d + 1 d + 1 d + 1 d + 1 tour of S 1 tour of S 2 tour of S 3 tour of S k

  56. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . Claim the RMQ reports the location of x depth = d some y in N iff LCA ( i, j ) = y depth = d + 1 y depth > d + 1 S 1 S 3 S k 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 (all of the y s are out of range) y y y y (node) N x x x x x x (depth) D d d d d d d d + 1 d + 1 d + 1 d + 1 tour of S 1 tour of S 2 tour of S 3 tour of S k

  57. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . Claim the RMQ reports the location of x depth = d some y in N iff LCA ( i, j ) = y depth = d + 1 y depth > d + 1 S 1 S 3 S k y y y y (node) N x x x x x x (depth) D d d d d d d d + 1 d + 1 d + 1 d + 1 tour of S 1 tour of S 2 tour of S 3 tour of S k

  58. Solving LCA using RMQ - correctness We can also define a Euler tour of T recursively. . . Claim the RMQ reports the location of x depth = d some y in N iff LCA ( i, j ) = y depth = d + 1 y depth > d + 1 S 1 S 3 S k j i y y y y (node) N x x x x x x (depth) D d d d d d d d + 1 d + 1 d + 1 d + 1 tour of S 1 tour of S 2 tour of S 3 tour of S k

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend