SlideShare a Scribd company logo
1 of 58
The LCA Problem Revisited
Michael A.Bender & Martin Farach-Colton
Presented by: Dvir Halevi
Agenda
• Definitions
• Reduction from LCA to RMQ
• Trivial algorithms for RMQ
• ST algorithm for RMQ
• A faster algorithm for a private RMQ case
• General Solution for RMQ
Definitions – Least Common Ancestor
• LCAT(u,v) – given nodes u,v in T, returns the node
furthest from the root that is an ancestor of both u
and v.
u
v
Trivial solution:
Definitions – Range Minimum Query
• Given array A of length n.
• RMQA(i,j) – returns the index of the smallest element
in the subarray A[i..j].
0 1634 137 19 10 121 2
RMQ(3,7) =
4
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
Definitions – Complexity Notation
• Suppose an algorithm has:
• Preprocessing time –
• Query time –
• Notation for the overall complexity of an algorithm:
• Definitions
• Reduction from LCA to RMQ
• Trivial algorithms for RMQ
• ST algorithm for RMQ
• A faster algorithm for a private RMQ case
• General Solution for RMQ
Reduction from LCA to RMQ
• In order to solve LCA queries, we will reduce the problem
to RMQ.
• Lemma:
If there is an solution for RMQ, then there is an
Solution for LCA.
Reduction - proof
• Observation:
The LCA of nodes u and v is the shallowest node
encountered between the visits to u and to v during a depth
first search traversal of T.
3
4
5
1
672
Euler tour:
4 1 7 1 3 5 6 5 33 1231
2
3
4
6
9
10
8
5
7
11
12
0
LCAT(1,5) =
3
Shallowest node
Reduction (cont.)
• Remarks:
o Euler tour size: 2n-1
o We will use the first occurrence of i,j for the sake of
concreteness (any occurrence will suffice).
o Shallowest node must be the LCA, otherwise
contradiction to a DFS run.
3
4
5
1
672
Euler tour:
4 1 7 1 3 5 6 5 33 1231
2
3
4
6
9
10
8
5
7
11
12
0
LCA(1,5) =
3
Shallowest node
Reduction (cont.)
• On an input tree T, we build 3 arrays.
• Euler[1,..,2n-1] – The nodes visited in an Euler tour of T. Euler[i]
is the label of the i-th node visited in the tour.
• Level[1,..2n-1] – The level of the nodes we got in the tour.
Level[i] is the level of node Euler[i].
(level is defined to be the distance from the root)
• Representative[1,..n] – Representative[i] will hold the index of
the first occurrence of node i in Euler[].
Representative[v] =
Mark: Euler – E, Representative – R, Level – L
Reduction (cont.)
• Example:
7
1
6
3 5 10 8
92
4
E: 1 6 3 6 5 6 1 2 1 9 10 9 4 7 4 9 8 9 1
L: 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0
R: 1 8 3 13 5 2 14 17 10 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Reduction (cont.)
• To compute LCAT(x,y):
o All nodes in the Euler tour between the first visits to x
and y are E[R[x],..,R[y]] (assume R[x] < R[y])
o The shallowest node in this subtour is at index
RMQL(R[x],R[y]), since L[i] stores the level of the node
at E[i].
o RMQ will return the index, thus we output the node at
E[RMQL(R[x],R[y])] as LCAT(x,y).
Reduction (cont.)
• Example:
7
1
6
3 5 10 8
92
4
LCAT(10,7
)
R[10]R[7]
E[11,…,14]
RMQL(10,7) =
12
LCAT(10,7) = E[12]=9
E: 1 6 3 6 5 6 1 2 1 9 10 9 4 7 4 9 8 9 1
L: 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0
R: 1 8 3 13 5 2 14 17 10 11
Reduction (cont.)
• Preprocessing Complexity:
o L,R,E – Each is built in time, during the DFS run.
o Preprocessing L for RMQ -
• Query Complexity:
o RMQ query on L –
o Array references –
• Overall:
• Reduction proof is complete.
• We will only deal with RMQ solutions from this point on.
• Definitions
• Reduction from LCA to RMQ
• Trivial algorithms for RMQ
• ST algorithm for RMQ
• A faster algorithm for a private RMQ case
• General Solution for RMQ
RMQ
• Solution 1:
Given an array A of size n, compute the RQM for every pair of
indices and store in a table -
• Solution 2:
To calculate RMQ(i,j) use the already known value of RMQ(i,j-
1) .
Complexity reduced to -
• Definitions
• Reduction from LCA to RMQ
• Trivial algorithms for RMQ
• ST algorithm for RMQ
• A faster algorithm for a private RMQ case
• General Solution for RMQ
ST RMQ
• Preprocess sub arrays of length
• M(i,j) = index of min value in the sub array starting at index
i having length
10 167 2634 2 9 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
M(3,0)=3
M(3,1)=3
M(3,2)=5
ST RMQ
• Idea: precompute each query whose length is a power of n.
For every i between 1 and n and every j between 1 and
find the minimum element in the block starting at i and having
length .
• More precisely we build table M.
• Table M therefore has size O(nlogn).
ST RMQ
• Building M – using dynamic programming we can build M in
O(nlogn) time.
10 167 2634 9 2 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
M(3,1)=3 M(5,1)=6
M(3,2)=6
ST RMQ
• Using these blocks to compute arbitrary M[i,j]
• Select two blocks that entirely cover the subrange [i..j]
• Let ( is the largest block that fits [i..j])
• Compute RMQ(i,j):
a1 ... ...
i j
2k elements
2k elements
ST RMQ
• Query time is O(1).
• This algorithm is known as Sparse Table(ST)
algorithm for RMQ, with complexity:
• Our target: get rid of the log(n) factor from the
preprocessing.
• Definitions
• Reduction from LCA to RMQ
• Trivial algorithms for RMQ
• ST algorithm for RMQ
• A faster algorithm for a private RMQ case
• General Solution for RMQ
Faster RMQ
• Use a table-lookup technique to precompute answers on
small subarrays, thus removing the log factor from the
preprocessing.
• Partition A into blocks of size .
A
... ... .........
Faster RMQ
• A’[1,.., ] – A’[i] is the minimum element in the i-th block of A.
A
... ... .........
A’[0] A’[2n/logn]A’[i]
… ...
B[0] B[2n/logn]B[i]
• B[1,.., ] – B’[i] is the position (index) in which value A’[i] occurs.
• Example:
10 337 2634 9 2 1225 22
0 21 93 4 5 6 7 8 10 11 12 13 14 15
24 43 5 11 19 27
n=16
A[] :
10 25 22 7 34 9 … = 8
10 7 9
0 21A’[] :
… 0 3 5
0 21B[] :
…
Faster RMQ
• Recall RMQ queries return the position of the
minimum.
• LCA to RMQ reduction uses the position of the
minimum, rather than the minimum itself.
• Use array B to keep track of where minimas in A’
came from.
Faster RMQ
• Preprocess A’ for RMQ using ST algorithm.
• ST’s preprocessing time – O(nlogn).
• A’s size –
• ST’s preprocessing on A’:
• ST(A’) =
Faster RMQ
• Having preprocessed A’ for RMQ, how to answer RMQ(i,j) queries
on A?
• i and j might be in the same block -> preprocess every block.
• i < j on different blocks, answer the query as follows:
– Compute minima from i to end of its block.
– Compute minima of all blocks in between i’s and j’s blocks.
– Compute minima from the beginning of j’s block to j.
• Return the index of the minimum of these 3 values.
Faster RMQ
• i < j on different blocks, answer the query as follows:
– Compute minima from i to end of its block.
– Compute minima of all blocks in between i’s and j’s blocks.
– Compute minima from the beginning of j’s block to j.
• 2 – Takes O(1) time by RMQ on A’.
• 1 & 3 – Have to answer in-block RMQ queries
• We need in-block queries whether i and j are in the same block or
not.
Faster RMQ
• First Attempt: preprocess every block.
Per block :
All blocks –
• Second attempt: recall the LCA to RMQ reduction
• RMQ was performed on array L.
• What can we use to our advantage?
restriction
Faster RMQ
• Observation:
Let two arrays X & Y such that
Then
• There are normalized blocks.
3 46 55 4 5 64 5
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
0 13 22 1 2 31 2
B[0] B[2]B[1] B[9]B[3] B[4] B[5] B[6] B[7] B[8]
+1 -1 -1-1 +1 +1 -1+1 +1
Faster RMQ
• Preprocess:
o Create tables of size to answer all in block queries. Overall .
o For each block in A compute which normalized block table it
should use –
o Preprocess A’ using ST -
• Query:
o Query on A’ –
o Query on in-blocks –
• Overall RMQ complexity -
• Definitions
• Reduction from LCA to RMQ
• Trivial algorithms for RMQ
• ST algorithm for RMQ
• A faster algorithm for a private RMQ case
• General Solution for RMQ
General O(n) RMQ
• Reduction from RMQ to LCA
• General RMQ is solved by reducing RMQ to LCA, then
reducing LCA to RMQ.
• Lemma:
If there is a solution for LCA, then there is a
solution to RMQ.
• Proof: build a Cartesian tree of the array, activate LCA on
it.
General O(n) RMQ
• Cartesian tree of an array A:
• Root – minimum element of the array. Root node is labeled
with the position of the minimum.
• Root’s left & right children: the recursively constructed
Cartesian tress of the left & right subarrays, respectively.
10 1634 267 19 9 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
General O(n) RMQ
10 1634 267 19 9 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
0
2
1 3
5 7
6
9
8
Build Cartesian tree in O(n)
• Move from left to right in the array
• Suppose Ci is the Cartesian tree of A[1,..,i]
• Node i+1 (v) has to belong in the rightmost path of Ci
• Climb the rightmost path, find the first node (u) smaller than v
• Make v the right son of u, and previous right subtree of u left son of
v.
u
v
...
x
...
u
v
...
x
...
Build Cartesian tree in O(n)
10 1634 267 19 9 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
0
10
Build Cartesian tree in O(n)
10 1634 267 19 9 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
0
1
10
25
Build Cartesian tree in O(n)
10 1634 267 19 9 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
0
2
1
10
22
25
Build Cartesian tree in O(n)
3410 16267 19 9 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
0
2
1 33
73434
10
22
25 34
Build Cartesian tree in O(n)
10 1634 267 19 9 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
0
2
1 3
10
22
25 34
7
Build Cartesian tree in O(n)
10 1634 267 19 9 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
0
2
1 3
5 7
6
9
8
General O(n) RMQ
• How to answer RMQ queries on A?
• Build Cartesian tree C of array A.
• RMQA(i,j) = LCAC(i,j)
• Proof:
o let k = LCAC(i,j).
o In the recursive description of a Cartesian tree k is the first
element to split i and j.
o k is between i,j since it splits them and is minimal because it is
the first element to do so.
General O(n) RMQ
• Build Complexity:
• Every node enters the rightmost path once. Once it
leaves, will never return.
• O(n).
General O(n) RMQ
10 1634 267 19 9 1225 22
A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
4
0
2
1 3
5 7
6
9
8
The Level-Ancestor Problem
simplified
Michael A.Bender & Martin Farach-Colton
LA defined
• Depth(u) – distance from the root.
• Height(u) - # of nodes from u to its deepest descendant.
• LA(u,d) = v iff v is an ancestor of u where depth(v)=d.
• LA(u,0) = root LA(u,depth(u)) = u
• Natural solutions:
• Climbing up the tree from u – O(n) query time.
• Precompute all answers – O(n^2) preprocessing.
Table Algorithm
• Build a table storing answers to all possible queries.
• The lookup table can be filled in time.
• Queries require 1 table lookup.
• Overall complexity:
Jump Pointer algorithm
• Keep logn pointers in each vertex to ancestors having
distance of length power of 2.
• Follow these pointers each time narrowing the distance by
a power of 2, making query time
• Use the same building technique as in ST to achieve
preprocessing time
…
The Ladder Algorithm
• Motivation: LA problem on a path is easily solved in
using an array.
• Greedily decompose T into disjoint paths, removing a
longest root-leaf path recursively.
• To locate LA(u,d) we jump long paths from u, each time
jumping to a lengthier path.
• We may end up jumping long paths.
The Ladder Algorithm
• Jumping to top of paths yields
• Extend long paths to Ladders
• Ladder = path + length(path) immediate ancestors of the
top of the path.
• Ladders can overlap.
• A vertex’s ladder is the ladder derived from the path
containing it.
The Ladder Algorithm
• If v is of height h then v has at least h ancestors in its
ladder.
• Finding long path decomposition of T take time.
(compute the height of every node, each node picks one of its maximal
height children as its child on the long path.
Extending long paths to ladders take O(n) too)
• Queries: if u is of height h then vertex at the top of u’s
ladder is of height at least 2h. After I ladders we reach a
node of height at least , thus we travel ladders.
Jump Pointers + Ladder
• Jump pointer makes exponentially decreasing hops.
• Ladder makes exponentially increasing hops.
• Preprocess T for both jump pointers and ladder –
• Query – follow a single jump pointer + climb a single
ladder. (reason: the jump pointer gets us more than halfway there(node
v), v’s ladder must contain the ancestor ).
Macro-Micro Tree Algorithm
• The bottleneck is computing jump pointers.
• No need for each and every node to contain jump pointers.
• If w is a descendant of v then v can use w’s jump pointers.
• Jump nodes – vertices having jump pointers.
• We designate nodes to be jump nodes.
Macro-Micro Tree Algorithm
• Macro node – any ancestor of a jump node.
• Micro node – a node which is not a macro node.
• Macro tree – The macro nodes form a connected subtree.
• Micro trees – The connected components obtained from T
after removing macro nodes.
• Jump node – the maximally deep vertices having at least
log(n/4) descendants.
• There are at most O(n/logn) jump nodes.
Macro-Micro Tree Algorithm
• Computing all jump nodes pointers now takes O(n), leading
to an overall preprocess time O(n).
• By running a DFS we can find the jump node for every
macro node, making queries on macro nodes O(1).
• There are canonical forms of micro trees (very similar to
normalized blocks on the RMQ)
• Preprocessing all canonical forms – O(n).
• Micro trees queries take only O(1) too.
• Making an overall of:

More Related Content

What's hot

Programming workshop
Programming workshopProgramming workshop
Programming workshopSandeep Joshi
 
Dsoop (co 221) 1
Dsoop (co 221) 1Dsoop (co 221) 1
Dsoop (co 221) 1Puja Koch
 
Chapter 8 Root Locus Techniques
Chapter 8 Root Locus TechniquesChapter 8 Root Locus Techniques
Chapter 8 Root Locus Techniquesguesta0c38c3
 
Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5Traian Rebedea
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.Tariq Khan
 
Modern Control - Lec 04 - Analysis and Design of Control Systems using Root L...
Modern Control - Lec 04 - Analysis and Design of Control Systems using Root L...Modern Control - Lec 04 - Analysis and Design of Control Systems using Root L...
Modern Control - Lec 04 - Analysis and Design of Control Systems using Root L...Amr E. Mohamed
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problemsSumita Das
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsVenkatesh Iyer
 
Digital Signal Processing Tutorial:Chapt 3 frequency analysis
Digital Signal Processing Tutorial:Chapt 3 frequency analysisDigital Signal Processing Tutorial:Chapt 3 frequency analysis
Digital Signal Processing Tutorial:Chapt 3 frequency analysisChandrashekhar Padole
 
EC202 SIGNALS & SYSTEMS PREVIOUS ANSWER KEY
EC202 SIGNALS & SYSTEMS PREVIOUS ANSWER KEYEC202 SIGNALS & SYSTEMS PREVIOUS ANSWER KEY
EC202 SIGNALS & SYSTEMS PREVIOUS ANSWER KEYVISHNUPRABHANKAIMAL
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesjayavignesh86
 
Reduction of multiple subsystem [compatibility mode]
Reduction of multiple subsystem [compatibility mode]Reduction of multiple subsystem [compatibility mode]
Reduction of multiple subsystem [compatibility mode]azroyyazid
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Deepak John
 
Grovers Algorithm
Grovers Algorithm Grovers Algorithm
Grovers Algorithm CaseyHaaland
 

What's hot (20)

Programming workshop
Programming workshopProgramming workshop
Programming workshop
 
Dsoop (co 221) 1
Dsoop (co 221) 1Dsoop (co 221) 1
Dsoop (co 221) 1
 
Chapter 8 Root Locus Techniques
Chapter 8 Root Locus TechniquesChapter 8 Root Locus Techniques
Chapter 8 Root Locus Techniques
 
Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5Algorithm Design and Complexity - Course 5
Algorithm Design and Complexity - Course 5
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
Modern Control - Lec 04 - Analysis and Design of Control Systems using Root L...
Modern Control - Lec 04 - Analysis and Design of Control Systems using Root L...Modern Control - Lec 04 - Analysis and Design of Control Systems using Root L...
Modern Control - Lec 04 - Analysis and Design of Control Systems using Root L...
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
 
DSP 05 _ Sheet Five
DSP 05 _ Sheet FiveDSP 05 _ Sheet Five
DSP 05 _ Sheet Five
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Lecture23
Lecture23Lecture23
Lecture23
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Digital Signal Processing Tutorial:Chapt 3 frequency analysis
Digital Signal Processing Tutorial:Chapt 3 frequency analysisDigital Signal Processing Tutorial:Chapt 3 frequency analysis
Digital Signal Processing Tutorial:Chapt 3 frequency analysis
 
EC202 SIGNALS & SYSTEMS PREVIOUS ANSWER KEY
EC202 SIGNALS & SYSTEMS PREVIOUS ANSWER KEYEC202 SIGNALS & SYSTEMS PREVIOUS ANSWER KEY
EC202 SIGNALS & SYSTEMS PREVIOUS ANSWER KEY
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Reduction of multiple subsystem [compatibility mode]
Reduction of multiple subsystem [compatibility mode]Reduction of multiple subsystem [compatibility mode]
Reduction of multiple subsystem [compatibility mode]
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
Digitalcontrolsystems
DigitalcontrolsystemsDigitalcontrolsystems
Digitalcontrolsystems
 
Grovers Algorithm
Grovers Algorithm Grovers Algorithm
Grovers Algorithm
 

Viewers also liked

Integrating Lucene into a Transactional XML Database
Integrating Lucene into a Transactional XML DatabaseIntegrating Lucene into a Transactional XML Database
Integrating Lucene into a Transactional XML Databaselucenerevolution
 
Gesture-aware remote controls: guidelines and interaction techniques
Gesture-aware remote controls: guidelines and interaction techniquesGesture-aware remote controls: guidelines and interaction techniques
Gesture-aware remote controls: guidelines and interaction techniquesDong-Bach Vo
 
Java Search Engine Framework
Java Search Engine FrameworkJava Search Engine Framework
Java Search Engine FrameworkAppsterdam Milan
 
Comparing the Multimodal Interaction Technique Design of MINT with NiMMiT
Comparing the Multimodal Interaction Technique Design of MINT with NiMMiTComparing the Multimodal Interaction Technique Design of MINT with NiMMiT
Comparing the Multimodal Interaction Technique Design of MINT with NiMMiTSebastian Feuerstack
 
[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)
[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)
[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)Yusuke Iwasawa
 
Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...go_oh
 
Masterizing php data structure 102
Masterizing php data structure 102Masterizing php data structure 102
Masterizing php data structure 102Patrick Allaert
 
Building and deploying PHP applications with Phing
Building and deploying PHP applications with PhingBuilding and deploying PHP applications with Phing
Building and deploying PHP applications with PhingMichiel Rook
 
Semi supervised learning
Semi supervised learningSemi supervised learning
Semi supervised learningAhmed Taha
 
Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...
Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...
Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...Spark Summit
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)Nikita Popov
 
Biomolecular interaction analysis (BIA) techniques
Biomolecular interaction analysis (BIA) techniquesBiomolecular interaction analysis (BIA) techniques
Biomolecular interaction analysis (BIA) techniquesN Poorin
 
HTTP cookie hijacking in the wild: security and privacy implications
HTTP cookie hijacking in the wild: security and privacy implicationsHTTP cookie hijacking in the wild: security and privacy implications
HTTP cookie hijacking in the wild: security and privacy implicationsPriyanka Aash
 
LCA and RMQ ~簡潔もあるよ!~
LCA and RMQ ~簡潔もあるよ!~LCA and RMQ ~簡潔もあるよ!~
LCA and RMQ ~簡潔もあるよ!~Yuma Inoue
 
Cookies and browser exploits
Cookies and browser exploitsCookies and browser exploits
Cookies and browser exploitsIftach Ian Amit
 

Viewers also liked (20)

Integrating Lucene into a Transactional XML Database
Integrating Lucene into a Transactional XML DatabaseIntegrating Lucene into a Transactional XML Database
Integrating Lucene into a Transactional XML Database
 
Gesture-aware remote controls: guidelines and interaction techniques
Gesture-aware remote controls: guidelines and interaction techniquesGesture-aware remote controls: guidelines and interaction techniques
Gesture-aware remote controls: guidelines and interaction techniques
 
Java Search Engine Framework
Java Search Engine FrameworkJava Search Engine Framework
Java Search Engine Framework
 
Encoding survey
Encoding surveyEncoding survey
Encoding survey
 
Comparing the Multimodal Interaction Technique Design of MINT with NiMMiT
Comparing the Multimodal Interaction Technique Design of MINT with NiMMiTComparing the Multimodal Interaction Technique Design of MINT with NiMMiT
Comparing the Multimodal Interaction Technique Design of MINT with NiMMiT
 
Xml databases
Xml databasesXml databases
Xml databases
 
Algorithms and Data Structures~hmftj
Algorithms and Data Structures~hmftjAlgorithms and Data Structures~hmftj
Algorithms and Data Structures~hmftj
 
XML In My Database!
XML In My Database!XML In My Database!
XML In My Database!
 
[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)
[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)
[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)
 
Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...
 
XML Databases
XML DatabasesXML Databases
XML Databases
 
Masterizing php data structure 102
Masterizing php data structure 102Masterizing php data structure 102
Masterizing php data structure 102
 
Building and deploying PHP applications with Phing
Building and deploying PHP applications with PhingBuilding and deploying PHP applications with Phing
Building and deploying PHP applications with Phing
 
Semi supervised learning
Semi supervised learningSemi supervised learning
Semi supervised learning
 
Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...
Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...
Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)
 
Biomolecular interaction analysis (BIA) techniques
Biomolecular interaction analysis (BIA) techniquesBiomolecular interaction analysis (BIA) techniques
Biomolecular interaction analysis (BIA) techniques
 
HTTP cookie hijacking in the wild: security and privacy implications
HTTP cookie hijacking in the wild: security and privacy implicationsHTTP cookie hijacking in the wild: security and privacy implications
HTTP cookie hijacking in the wild: security and privacy implications
 
LCA and RMQ ~簡潔もあるよ!~
LCA and RMQ ~簡潔もあるよ!~LCA and RMQ ~簡潔もあるよ!~
LCA and RMQ ~簡潔もあるよ!~
 
Cookies and browser exploits
Cookies and browser exploitsCookies and browser exploits
Cookies and browser exploits
 

Similar to Lca seminar modified

The LCA problem revisited
The LCA problem revisitedThe LCA problem revisited
The LCA problem revisitedMinsung Hong
 
ICPC 2015, Tsukuba : Unofficial Commentary
ICPC 2015, Tsukuba: Unofficial CommentaryICPC 2015, Tsukuba: Unofficial Commentary
ICPC 2015, Tsukuba : Unofficial Commentaryirrrrr
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxLecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxFazlullah28
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programminghodcsencet
 
IOEfficientParalleMatrixMultiplication_present
IOEfficientParalleMatrixMultiplication_presentIOEfficientParalleMatrixMultiplication_present
IOEfficientParalleMatrixMultiplication_presentShubham Joshi
 
Branch and bounding : Data structures
Branch and bounding : Data structuresBranch and bounding : Data structures
Branch and bounding : Data structuresKàŕtheek Jåvvàjí
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptxDeepakM509554
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptxTekle12
 
Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graphgetacew
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdfabay golla
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesSreedhar Chowdam
 
Mit15 082 jf10_lec01
Mit15 082 jf10_lec01Mit15 082 jf10_lec01
Mit15 082 jf10_lec01Saad Liaqat
 

Similar to Lca seminar modified (20)

The LCA problem revisited
The LCA problem revisitedThe LCA problem revisited
The LCA problem revisited
 
Ch07 linearspacealignment
Ch07 linearspacealignmentCh07 linearspacealignment
Ch07 linearspacealignment
 
ICPC 2015, Tsukuba : Unofficial Commentary
ICPC 2015, Tsukuba: Unofficial CommentaryICPC 2015, Tsukuba: Unofficial Commentary
ICPC 2015, Tsukuba : Unofficial Commentary
 
Lec35
Lec35Lec35
Lec35
 
DAA Notes.pdf
DAA Notes.pdfDAA Notes.pdf
DAA Notes.pdf
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxLecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptx
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 
IOEfficientParalleMatrixMultiplication_present
IOEfficientParalleMatrixMultiplication_presentIOEfficientParalleMatrixMultiplication_present
IOEfficientParalleMatrixMultiplication_present
 
Branch and bounding : Data structures
Branch and bounding : Data structuresBranch and bounding : Data structures
Branch and bounding : Data structures
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Data Mining Lecture_8(b).pptx
Data Mining Lecture_8(b).pptxData Mining Lecture_8(b).pptx
Data Mining Lecture_8(b).pptx
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graph
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
 
Data structure-question-bank
Data structure-question-bankData structure-question-bank
Data structure-question-bank
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
Linear Sorting
Linear SortingLinear Sorting
Linear Sorting
 
Mit15 082 jf10_lec01
Mit15 082 jf10_lec01Mit15 082 jf10_lec01
Mit15 082 jf10_lec01
 
sorting
sortingsorting
sorting
 
JMM2016PosterFinal
JMM2016PosterFinalJMM2016PosterFinal
JMM2016PosterFinal
 

Lca seminar modified

  • 1. The LCA Problem Revisited Michael A.Bender & Martin Farach-Colton Presented by: Dvir Halevi
  • 2. Agenda • Definitions • Reduction from LCA to RMQ • Trivial algorithms for RMQ • ST algorithm for RMQ • A faster algorithm for a private RMQ case • General Solution for RMQ
  • 3. Definitions – Least Common Ancestor • LCAT(u,v) – given nodes u,v in T, returns the node furthest from the root that is an ancestor of both u and v. u v Trivial solution:
  • 4. Definitions – Range Minimum Query • Given array A of length n. • RMQA(i,j) – returns the index of the smallest element in the subarray A[i..j]. 0 1634 137 19 10 121 2 RMQ(3,7) = 4 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
  • 5. Definitions – Complexity Notation • Suppose an algorithm has: • Preprocessing time – • Query time – • Notation for the overall complexity of an algorithm:
  • 6. • Definitions • Reduction from LCA to RMQ • Trivial algorithms for RMQ • ST algorithm for RMQ • A faster algorithm for a private RMQ case • General Solution for RMQ
  • 7. Reduction from LCA to RMQ • In order to solve LCA queries, we will reduce the problem to RMQ. • Lemma: If there is an solution for RMQ, then there is an Solution for LCA.
  • 8. Reduction - proof • Observation: The LCA of nodes u and v is the shallowest node encountered between the visits to u and to v during a depth first search traversal of T. 3 4 5 1 672 Euler tour: 4 1 7 1 3 5 6 5 33 1231 2 3 4 6 9 10 8 5 7 11 12 0 LCAT(1,5) = 3 Shallowest node
  • 9. Reduction (cont.) • Remarks: o Euler tour size: 2n-1 o We will use the first occurrence of i,j for the sake of concreteness (any occurrence will suffice). o Shallowest node must be the LCA, otherwise contradiction to a DFS run. 3 4 5 1 672 Euler tour: 4 1 7 1 3 5 6 5 33 1231 2 3 4 6 9 10 8 5 7 11 12 0 LCA(1,5) = 3 Shallowest node
  • 10. Reduction (cont.) • On an input tree T, we build 3 arrays. • Euler[1,..,2n-1] – The nodes visited in an Euler tour of T. Euler[i] is the label of the i-th node visited in the tour. • Level[1,..2n-1] – The level of the nodes we got in the tour. Level[i] is the level of node Euler[i]. (level is defined to be the distance from the root) • Representative[1,..n] – Representative[i] will hold the index of the first occurrence of node i in Euler[]. Representative[v] = Mark: Euler – E, Representative – R, Level – L
  • 11. Reduction (cont.) • Example: 7 1 6 3 5 10 8 92 4 E: 1 6 3 6 5 6 1 2 1 9 10 9 4 7 4 9 8 9 1 L: 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0 R: 1 8 3 13 5 2 14 17 10 11 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  • 12. Reduction (cont.) • To compute LCAT(x,y): o All nodes in the Euler tour between the first visits to x and y are E[R[x],..,R[y]] (assume R[x] < R[y]) o The shallowest node in this subtour is at index RMQL(R[x],R[y]), since L[i] stores the level of the node at E[i]. o RMQ will return the index, thus we output the node at E[RMQL(R[x],R[y])] as LCAT(x,y).
  • 13. Reduction (cont.) • Example: 7 1 6 3 5 10 8 92 4 LCAT(10,7 ) R[10]R[7] E[11,…,14] RMQL(10,7) = 12 LCAT(10,7) = E[12]=9 E: 1 6 3 6 5 6 1 2 1 9 10 9 4 7 4 9 8 9 1 L: 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0 R: 1 8 3 13 5 2 14 17 10 11
  • 14. Reduction (cont.) • Preprocessing Complexity: o L,R,E – Each is built in time, during the DFS run. o Preprocessing L for RMQ - • Query Complexity: o RMQ query on L – o Array references – • Overall: • Reduction proof is complete. • We will only deal with RMQ solutions from this point on.
  • 15. • Definitions • Reduction from LCA to RMQ • Trivial algorithms for RMQ • ST algorithm for RMQ • A faster algorithm for a private RMQ case • General Solution for RMQ
  • 16. RMQ • Solution 1: Given an array A of size n, compute the RQM for every pair of indices and store in a table - • Solution 2: To calculate RMQ(i,j) use the already known value of RMQ(i,j- 1) . Complexity reduced to -
  • 17. • Definitions • Reduction from LCA to RMQ • Trivial algorithms for RMQ • ST algorithm for RMQ • A faster algorithm for a private RMQ case • General Solution for RMQ
  • 18. ST RMQ • Preprocess sub arrays of length • M(i,j) = index of min value in the sub array starting at index i having length 10 167 2634 2 9 1225 22 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8] M(3,0)=3 M(3,1)=3 M(3,2)=5
  • 19. ST RMQ • Idea: precompute each query whose length is a power of n. For every i between 1 and n and every j between 1 and find the minimum element in the block starting at i and having length . • More precisely we build table M. • Table M therefore has size O(nlogn).
  • 20. ST RMQ • Building M – using dynamic programming we can build M in O(nlogn) time. 10 167 2634 9 2 1225 22 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8] M(3,1)=3 M(5,1)=6 M(3,2)=6
  • 21. ST RMQ • Using these blocks to compute arbitrary M[i,j] • Select two blocks that entirely cover the subrange [i..j] • Let ( is the largest block that fits [i..j]) • Compute RMQ(i,j): a1 ... ... i j 2k elements 2k elements
  • 22. ST RMQ • Query time is O(1). • This algorithm is known as Sparse Table(ST) algorithm for RMQ, with complexity: • Our target: get rid of the log(n) factor from the preprocessing.
  • 23. • Definitions • Reduction from LCA to RMQ • Trivial algorithms for RMQ • ST algorithm for RMQ • A faster algorithm for a private RMQ case • General Solution for RMQ
  • 24. Faster RMQ • Use a table-lookup technique to precompute answers on small subarrays, thus removing the log factor from the preprocessing. • Partition A into blocks of size . A ... ... .........
  • 25. Faster RMQ • A’[1,.., ] – A’[i] is the minimum element in the i-th block of A. A ... ... ......... A’[0] A’[2n/logn]A’[i] … ... B[0] B[2n/logn]B[i] • B[1,.., ] – B’[i] is the position (index) in which value A’[i] occurs.
  • 26. • Example: 10 337 2634 9 2 1225 22 0 21 93 4 5 6 7 8 10 11 12 13 14 15 24 43 5 11 19 27 n=16 A[] : 10 25 22 7 34 9 … = 8 10 7 9 0 21A’[] : … 0 3 5 0 21B[] : …
  • 27. Faster RMQ • Recall RMQ queries return the position of the minimum. • LCA to RMQ reduction uses the position of the minimum, rather than the minimum itself. • Use array B to keep track of where minimas in A’ came from.
  • 28. Faster RMQ • Preprocess A’ for RMQ using ST algorithm. • ST’s preprocessing time – O(nlogn). • A’s size – • ST’s preprocessing on A’: • ST(A’) =
  • 29. Faster RMQ • Having preprocessed A’ for RMQ, how to answer RMQ(i,j) queries on A? • i and j might be in the same block -> preprocess every block. • i < j on different blocks, answer the query as follows: – Compute minima from i to end of its block. – Compute minima of all blocks in between i’s and j’s blocks. – Compute minima from the beginning of j’s block to j. • Return the index of the minimum of these 3 values.
  • 30. Faster RMQ • i < j on different blocks, answer the query as follows: – Compute minima from i to end of its block. – Compute minima of all blocks in between i’s and j’s blocks. – Compute minima from the beginning of j’s block to j. • 2 – Takes O(1) time by RMQ on A’. • 1 & 3 – Have to answer in-block RMQ queries • We need in-block queries whether i and j are in the same block or not.
  • 31. Faster RMQ • First Attempt: preprocess every block. Per block : All blocks – • Second attempt: recall the LCA to RMQ reduction • RMQ was performed on array L. • What can we use to our advantage? restriction
  • 32. Faster RMQ • Observation: Let two arrays X & Y such that Then • There are normalized blocks. 3 46 55 4 5 64 5 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8] 0 13 22 1 2 31 2 B[0] B[2]B[1] B[9]B[3] B[4] B[5] B[6] B[7] B[8] +1 -1 -1-1 +1 +1 -1+1 +1
  • 33. Faster RMQ • Preprocess: o Create tables of size to answer all in block queries. Overall . o For each block in A compute which normalized block table it should use – o Preprocess A’ using ST - • Query: o Query on A’ – o Query on in-blocks – • Overall RMQ complexity -
  • 34. • Definitions • Reduction from LCA to RMQ • Trivial algorithms for RMQ • ST algorithm for RMQ • A faster algorithm for a private RMQ case • General Solution for RMQ
  • 35. General O(n) RMQ • Reduction from RMQ to LCA • General RMQ is solved by reducing RMQ to LCA, then reducing LCA to RMQ. • Lemma: If there is a solution for LCA, then there is a solution to RMQ. • Proof: build a Cartesian tree of the array, activate LCA on it.
  • 36. General O(n) RMQ • Cartesian tree of an array A: • Root – minimum element of the array. Root node is labeled with the position of the minimum. • Root’s left & right children: the recursively constructed Cartesian tress of the left & right subarrays, respectively. 10 1634 267 19 9 1225 22 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8]
  • 37. General O(n) RMQ 10 1634 267 19 9 1225 22 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8] 4 0 2 1 3 5 7 6 9 8
  • 38. Build Cartesian tree in O(n) • Move from left to right in the array • Suppose Ci is the Cartesian tree of A[1,..,i] • Node i+1 (v) has to belong in the rightmost path of Ci • Climb the rightmost path, find the first node (u) smaller than v • Make v the right son of u, and previous right subtree of u left son of v. u v ... x ... u v ... x ...
  • 39. Build Cartesian tree in O(n) 10 1634 267 19 9 1225 22 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8] 0 10
  • 40. Build Cartesian tree in O(n) 10 1634 267 19 9 1225 22 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8] 0 1 10 25
  • 41. Build Cartesian tree in O(n) 10 1634 267 19 9 1225 22 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8] 0 2 1 10 22 25
  • 42. Build Cartesian tree in O(n) 3410 16267 19 9 1225 22 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8] 0 2 1 33 73434 10 22 25 34
  • 43. Build Cartesian tree in O(n) 10 1634 267 19 9 1225 22 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8] 4 0 2 1 3 10 22 25 34 7
  • 44. Build Cartesian tree in O(n) 10 1634 267 19 9 1225 22 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8] 4 0 2 1 3 5 7 6 9 8
  • 45. General O(n) RMQ • How to answer RMQ queries on A? • Build Cartesian tree C of array A. • RMQA(i,j) = LCAC(i,j) • Proof: o let k = LCAC(i,j). o In the recursive description of a Cartesian tree k is the first element to split i and j. o k is between i,j since it splits them and is minimal because it is the first element to do so.
  • 46. General O(n) RMQ • Build Complexity: • Every node enters the rightmost path once. Once it leaves, will never return. • O(n).
  • 47. General O(n) RMQ 10 1634 267 19 9 1225 22 A[0] A[2]A[1] A[9]A[3] A[4] A[5] A[6] A[7] A[8] 4 0 2 1 3 5 7 6 9 8
  • 48. The Level-Ancestor Problem simplified Michael A.Bender & Martin Farach-Colton
  • 49. LA defined • Depth(u) – distance from the root. • Height(u) - # of nodes from u to its deepest descendant. • LA(u,d) = v iff v is an ancestor of u where depth(v)=d. • LA(u,0) = root LA(u,depth(u)) = u • Natural solutions: • Climbing up the tree from u – O(n) query time. • Precompute all answers – O(n^2) preprocessing.
  • 50. Table Algorithm • Build a table storing answers to all possible queries. • The lookup table can be filled in time. • Queries require 1 table lookup. • Overall complexity:
  • 51. Jump Pointer algorithm • Keep logn pointers in each vertex to ancestors having distance of length power of 2. • Follow these pointers each time narrowing the distance by a power of 2, making query time • Use the same building technique as in ST to achieve preprocessing time …
  • 52. The Ladder Algorithm • Motivation: LA problem on a path is easily solved in using an array. • Greedily decompose T into disjoint paths, removing a longest root-leaf path recursively. • To locate LA(u,d) we jump long paths from u, each time jumping to a lengthier path. • We may end up jumping long paths.
  • 53. The Ladder Algorithm • Jumping to top of paths yields • Extend long paths to Ladders • Ladder = path + length(path) immediate ancestors of the top of the path. • Ladders can overlap. • A vertex’s ladder is the ladder derived from the path containing it.
  • 54. The Ladder Algorithm • If v is of height h then v has at least h ancestors in its ladder. • Finding long path decomposition of T take time. (compute the height of every node, each node picks one of its maximal height children as its child on the long path. Extending long paths to ladders take O(n) too) • Queries: if u is of height h then vertex at the top of u’s ladder is of height at least 2h. After I ladders we reach a node of height at least , thus we travel ladders.
  • 55. Jump Pointers + Ladder • Jump pointer makes exponentially decreasing hops. • Ladder makes exponentially increasing hops. • Preprocess T for both jump pointers and ladder – • Query – follow a single jump pointer + climb a single ladder. (reason: the jump pointer gets us more than halfway there(node v), v’s ladder must contain the ancestor ).
  • 56. Macro-Micro Tree Algorithm • The bottleneck is computing jump pointers. • No need for each and every node to contain jump pointers. • If w is a descendant of v then v can use w’s jump pointers. • Jump nodes – vertices having jump pointers. • We designate nodes to be jump nodes.
  • 57. Macro-Micro Tree Algorithm • Macro node – any ancestor of a jump node. • Micro node – a node which is not a macro node. • Macro tree – The macro nodes form a connected subtree. • Micro trees – The connected components obtained from T after removing macro nodes. • Jump node – the maximally deep vertices having at least log(n/4) descendants. • There are at most O(n/logn) jump nodes.
  • 58. Macro-Micro Tree Algorithm • Computing all jump nodes pointers now takes O(n), leading to an overall preprocess time O(n). • By running a DFS we can find the jump node for every macro node, making queries on macro nodes O(1). • There are canonical forms of micro trees (very similar to normalized blocks on the RMQ) • Preprocessing all canonical forms – O(n). • Micro trees queries take only O(1) too. • Making an overall of: