Download presentation
Presentation is loading. Please wait.
1
1 Nearest Commen Ancestors (NCA) Discrete Range Maximum 1234567891011 4716 6213583 4 7 1 6 11 6 8 2 35 Cartesian Tree [Vuillemin 1980] i j max(i, j) i j nca(i, j) 1 2 11 13 Preprocessing Time vs Query Time ? Org. [D. Harel, R.E. Tarjan, Fast algorithms for finding nearest common ancestors, SIAM J. on Comp. 13 (2): 338–355, 1984]
2
Incremental construction left-to-right O(n) time 2 Cartesian Tree Construction 18∙∙∙13∙∙∙8 210 18 8 13 2 10 + 18 8 13 2 START HERE
3
A B C D E F J G KI 3 Reduction: NCA Discrete Range Maximum Euler Tour HEBABDCDBEFGFEHJIJKJH 123434543234321232321 node depth i j nca(i, j) E G C ji 2 minimum depth nca(i, j) H
4
4 NCA on Perfect Binary Trees i 2i2i2i+1 1 23 45 10 11 20212223 = nca(11,21) 11 = 1011 2 21 = 10101 2 nca(21,21) = 5 = 101 2 = lcp( 1011 2, 10101 2 ) longest common prefix proc lcp(x, y) if y < x then swap (x, y) return x >> (msb(x XOR (y >> (msb(y)-msb(x))) position of most significant bit ≠ 0
5
5 Discrete Range Mimimum - O(n∙log n) Space 2343454322112343 11233234 1 1 1 1 123 1234345432343212 1 33 2 33 3 33 d 32 5 11 j 0 1 i ∙ ∙ ∙ right i left j drm(i, j) = min(right i (d), left j (d)) d = msb(i XOR j) nca(i, j)
6
6 Blocked solution – O(n) space 1234345414343232 1212 block of O(log n) elements 01011 54321 j i W j (One for each j) Block query: j+1-msb(W j AND ((1 << (j-i+1))-1)) General query: 1 top query + 2 bottom queries Top structure O(n/log n) elements Space O(n) Query O(1) O(n) Preprocessing Time O(1) Query Time
7
7 Summary... General Discrete Range Searching Cartesian Tree NCA Discrete Range Max on Depth Array ”O(n∙log n)” solution on O(n/log n) blocks O(log n) size blocks 12345678 47168629 i j 4 7 1 6 8 6 2 9 i j nca(i, j) 123434543234321 O(n) Preprocessing Time O(1) Query Time
8
Preprocess a string Query Length of longest common substring starting a positions i and j 8 An NCA application... 123456789 TATTATTAA$ j i nca(i, j) 123456789 TATTATTAA$ i j Build suffix tree + NCA query Suffix tree
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.