1 Nearest Commen Ancestors (NCA) Discrete Range Maximum Cartesian Tree [Vuillemin 1980] i j max(i, j) i j nca(i, j) 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]
Incremental construction left-to-right O(n) time 2 Cartesian Tree Construction 18∙∙∙13∙∙∙ START HERE
A B C D E F J G KI 3 Reduction: NCA Discrete Range Maximum Euler Tour HEBABDCDBEFGFEHJIJKJH node depth i j nca(i, j) E G C ji 2 minimum depth nca(i, j) H
4 NCA on Perfect Binary Trees i 2i2i2i = nca(11,21) 11 = = nca(21,21) = 5 = = lcp( , ) 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 Discrete Range Mimimum - O(n∙log n) Space d 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 Blocked solution – O(n) space block of O(log n) elements 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 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 i j i j nca(i, j) O(n) Preprocessing Time O(1) Query Time
Preprocess a string Query Length of longest common substring starting a positions i and j 8 An NCA application TATTATTAA$ j i nca(i, j) TATTATTAA$ i j Build suffix tree + NCA query Suffix tree