range minimum and lowest common ancestor queries
play

Range Minimum and Lowest Common Ancestor Queries Slides by Solon P. - PowerPoint PPT Presentation

Range Minimum and Lowest Common Ancestor Queries Slides by Solon P. Pissis November 15, 2019 RMQs and LCA queries Range Minimum Queries problem RMQs and LCA queries Range Minimum Queries problem Definition Given an array A [1 . . . n ],


  1. Range Minimum and Lowest Common Ancestor Queries Slides by Solon P. Pissis November 15, 2019 RMQs and LCA queries

  2. Range Minimum Queries problem RMQs and LCA queries

  3. Range Minimum Queries problem Definition Given an array A [1 . . . n ], preprocess A so that a minimum of any fragment A [ i . . . j ] can be computed efficiently: RMQ A ( i , j ) = arg min A [ k ] with 1 ≤ i ≤ k ≤ j ≤ n . RMQs and LCA queries

  4. Range Minimum Queries problem Definition Given an array A [1 . . . n ], preprocess A so that a minimum of any fragment A [ i . . . j ] can be computed efficiently: RMQ A ( i , j ) = arg min A [ k ] with 1 ≤ i ≤ k ≤ j ≤ n . 1 i k j n RMQs and LCA queries

  5. Range Minimum Queries problem Definition Given an array A [1 . . . n ], preprocess A so that a minimum of any fragment A [ i . . . j ] can be computed efficiently: RMQ A ( i , j ) = arg min A [ k ] with 1 ≤ i ≤ k ≤ j ≤ n . 1 i k j n Answering any query in O (1) time is trivial if we allow O ( n 2 ) time and space preprocessing. RMQs and LCA queries

  6. O ( n log n ) time and space and O (1)-time queries RMQs and LCA queries

  7. O ( n log n ) time and space and O (1)-time queries Let us show the following lemma. RMQs and LCA queries

  8. O ( n log n ) time and space and O (1)-time queries Let us show the following lemma. Lemma The RMQ problem can be solved in O (1) time after O ( n log n ) time and space preprocessing. RMQs and LCA queries

  9. O ( n log n ) time and space and O (1)-time queries Let us show the following lemma. Lemma The RMQ problem can be solved in O (1) time after O ( n log n ) time and space preprocessing. We apply the well-known doubling technique for preprocessing . RMQs and LCA queries

  10. O ( n log n ) time and space and O (1)-time queries Let us show the following lemma. Lemma The RMQ problem can be solved in O (1) time after O ( n log n ) time and space preprocessing. We apply the well-known doubling technique for preprocessing . For all k = 0 , 1 , . . . , log n construct: B k [ i ] = min { A [ i ] , A [ i + 1] , . . . , A [ i + 2 k − 1] } . RMQs and LCA queries

  11. O ( n log n ) time and space and O (1)-time queries Let us show the following lemma. Lemma The RMQ problem can be solved in O (1) time after O ( n log n ) time and space preprocessing. We apply the well-known doubling technique for preprocessing . For all k = 0 , 1 , . . . , log n construct: B k [ i ] = min { A [ i ] , A [ i + 1] , . . . , A [ i + 2 k − 1] } . How? RMQs and LCA queries

  12. O ( n log n ) time and space and O (1)-time queries Let us show the following lemma. Lemma The RMQ problem can be solved in O (1) time after O ( n log n ) time and space preprocessing. We apply the well-known doubling technique for preprocessing . For all k = 0 , 1 , . . . , log n construct: B k [ i ] = min { A [ i ] , A [ i + 1] , . . . , A [ i + 2 k − 1] } . How? B 0 [ i ] = A [ i ] and B k +1 [ i ] = min { B k [ i ] , B k [ i + 2 k ] } , for all i . RMQs and LCA queries

  13. O ( n log n ) time and space and O (1)-time queries And now the querying part. RMQs and LCA queries

  14. O ( n log n ) time and space and O (1)-time queries And now the querying part. Two cases: RMQs and LCA queries

  15. O ( n log n ) time and space and O (1)-time queries And now the querying part. Two cases: ◮ Fragment of length 2 k : Trivial, use B ; RMQs and LCA queries

  16. O ( n log n ) time and space and O (1)-time queries And now the querying part. Two cases: ◮ Fragment of length 2 k : Trivial, use B ; ◮ Otherwise, we can cover any range with two power-of-2 ranges. RMQs and LCA queries

  17. O ( n log n ) time and space and O (1)-time queries And now the querying part. Two cases: ◮ Fragment of length 2 k : Trivial, use B ; ◮ Otherwise, we can cover any range with two power-of-2 ranges. Compute k = ⌊ log( j − i + 1) ⌋ . 1 i j n RMQs and LCA queries

  18. O ( n log n ) time and space and O (1)-time queries And now the querying part. Two cases: ◮ Fragment of length 2 k : Trivial, use B ; ◮ Otherwise, we can cover any range with two power-of-2 ranges. Compute k = ⌊ log( j − i + 1) ⌋ . 1 i j n Return min { B k [ i ] , B k [ j − 2 k + 1] } in O (1) time! RMQs and LCA queries

  19. O ( n ) time and space and O (log n )-time queries RMQs and LCA queries

  20. O ( n ) time and space and O (log n )-time queries Let us show the following lemma. RMQs and LCA queries

  21. O ( n ) time and space and O (log n )-time queries Let us show the following lemma. Lemma The RMQ problem can be solved in O (log n ) time after O ( n ) time and space preprocessing. RMQs and LCA queries

  22. O ( n ) time and space and O (log n )-time queries Let us show the following lemma. Lemma The RMQ problem can be solved in O (log n ) time after O ( n ) time and space preprocessing. (Segment tree.) RMQs and LCA queries

  23. O ( n ) time and space and O (log n )-time queries Let us show the following lemma. Lemma The RMQ problem can be solved in O (log n ) time after O ( n ) time and space preprocessing. (Segment tree.) We apply the well-known micro-macro decomposition technique. RMQs and LCA queries

  24. O ( n ) time and space and O (log n )-time queries Let us show the following lemma. Lemma The RMQ problem can be solved in O (log n ) time after O ( n ) time and space preprocessing. (Segment tree.) We apply the well-known micro-macro decomposition technique. b = log n 1 n RMQs and LCA queries

  25. O ( n ) time and space and O (log n )-time queries Let us show the following lemma. Lemma The RMQ problem can be solved in O (log n ) time after O ( n ) time and space preprocessing. (Segment tree.) We apply the well-known micro-macro decomposition technique. b = log n 1 n Decompose array A into blocks of length b = log n . RMQs and LCA queries

  26. O ( n ) time and space and O (log n )-time queries b = log n 1 n RMQs and LCA queries

  27. O ( n ) time and space and O (log n )-time queries b = log n 1 n ◮ Construct a new array A ′ : A ′ [ i ] = min { A [ i · b + 1] , A [ i · b + 2] , . . . , A [( i + 1) · b ] } . RMQs and LCA queries

  28. O ( n ) time and space and O (log n )-time queries b = log n 1 n ◮ Construct a new array A ′ : A ′ [ i ] = min { A [ i · b + 1] , A [ i · b + 2] , . . . , A [( i + 1) · b ] } . ◮ Build the previously described structure for A ′ [1 . . . n log n ]. RMQs and LCA queries

  29. O ( n ) time and space and O (log n )-time queries b = log n 1 n ◮ Construct a new array A ′ : A ′ [ i ] = min { A [ i · b + 1] , A [ i · b + 2] , . . . , A [( i + 1) · b ] } . ◮ Build the previously described structure for A ′ [1 . . . n log n ]. How much time and space do we need? RMQs and LCA queries

  30. O ( n ) time and space and O (log n )-time queries b = log n 1 n ◮ Construct a new array A ′ : A ′ [ i ] = min { A [ i · b + 1] , A [ i · b + 2] , . . . , A [( i + 1) · b ] } . ◮ Build the previously described structure for A ′ [1 . . . n log n ]. n n How much time and space do we need? O ( log n log( log n )) = O ( n ). RMQs and LCA queries

  31. O ( n ) time and space and O (log n )-time queries Let i , j be a query (Notice: endpoints are inside blocks). 1 n i j RMQs and LCA queries

  32. O ( n ) time and space and O (log n )-time queries Let i , j be a query (Notice: endpoints are inside blocks). 1 n i j ◮ We do not have the min in the prefix and suffix of every block. RMQs and LCA queries

  33. O ( n ) time and space and O (log n )-time queries Let i , j be a query (Notice: endpoints are inside blocks). 1 n i j ◮ We do not have the min in the prefix and suffix of every block. ◮ For each block, precompute it: O ( n ) time and space! RMQs and LCA queries

  34. O ( n ) time and space and O (log n )-time queries Let i , j be a query (Notice: endpoints are inside blocks). 1 n i j ◮ We do not have the min in the prefix and suffix of every block. ◮ For each block, precompute it: O ( n ) time and space! ◮ For the rest: use the structure we have built for A ′ . RMQs and LCA queries

  35. O ( n ) time and space and O (log n )-time queries Let i , j be a query (Notice: endpoints are inside blocks). 1 n i j ◮ We do not have the min in the prefix and suffix of every block. ◮ For each block, precompute it: O ( n ) time and space! ◮ For the rest: use the structure we have built for A ′ . ◮ Now any such query takes O (1) time. RMQs and LCA queries

  36. O ( n ) time and space and O (log n )-time queries Let i , j be a query (Notice: endpoints are inside blocks). 1 n i j ◮ We do not have the min in the prefix and suffix of every block. ◮ For each block, precompute it: O ( n ) time and space! ◮ For the rest: use the structure we have built for A ′ . ◮ Now any such query takes O (1) time. Any problems? RMQs and LCA queries

  37. O ( n ) time and space and O (log n )-time queries Let i , j be a query (Notice: endpoints are inside blocks). 1 n i j ◮ We do not have the min in the prefix and suffix of every block. ◮ For each block, precompute it: O ( n ) time and space! ◮ For the rest: use the structure we have built for A ′ . ◮ Now any such query takes O (1) time. Any problems? Yes! RMQs and LCA queries

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