SlideShare a Scribd company logo
1 of 29
Download to read offline
The LCA Problem Revisited

(Latin American Theoretical Informatics Symposium, 2000)

Michael A. Bender, Martín Farach-Colton

Presented by Minsung Hong, Junyong Choi
Lowest Common Ancestor(LCA)

• LCA 𝑇 𝑢, 𝑣 – given nodes 𝑢, 𝑣 in 𝑇, returns
the node furthest from the root that is an
ancestor of both 𝑢 and 𝑣.
𝑧

𝑢

𝑣

LCA 𝑇 𝑢, 𝑣 =𝑧
3
Range Minimum Query(RMQ)

• RMQ 𝐴 (𝑖, 𝑗) – given indices 𝑖, 𝑗 of array 𝐴,
returns the index of the smallest element in
the sub-array 𝐴[𝑖. . 𝑗].
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]

0

1

2

34 7

19

10

12

13

16

RMQ 𝐴 3,7 = 4
4
Complexity Notation
• Notation for the overall complexity of an algorithm
< 𝑓 𝑛 , 𝑔 𝑛 >

– Preprocessing time : 𝑓(𝑛)
– Query time : 𝑔 𝑛

5
Previous Work

• < 𝑂 𝑛 , 𝑂 1 > Algorithms for LCA
– Harel and Tarjan, 1984: first optimal

• very complicated and unimplementable

– Shieber and Vishkin, 1988

• simplified but not particularly implementable

– Berkman and Vishkin, 1993
• developed a new way using reduction to RMQ using Euler
tour.

6
Contents
• Introduction
• Reduction from LCA to RMQ
• RMQ Algorithms

– < 𝑂 𝑛2 , 𝑂 1 > Algorithm
– < 𝑂 𝑛 log 𝑛 , 𝑂 1 > Sparse Table(ST) Algorithm

• < 𝑂 𝑛 , 𝑂 1 > Algorithm for RMQ in LCA
• Conclusion
7
Reduction from LCA to RMQ

• Lemma
If there is an

< 𝑓 𝑛 , 𝑔 𝑛 >
solution for RMQ, then there is an
< 𝑓 2𝑛 − 1 + 𝑂 𝑛 , 𝑔 2𝑛 − 1 + 𝑂 1 >
solution for LCA.

8
Reduction from LCA to RMQ

• Observation
The LCA of nodes 𝑢 and 𝑣 is the shallowest node
encountered between the visits to 𝑢 and to 𝑣 during
a depth first search(DFS) traversal of 𝑇.
0

3

12

4
5 6

2

5

1

2

4

3

8 9

3

1

Shallowest node

Euler tour:

7

7

11
10

6

2

3

1

4

1

7

LCA 𝑇 1,5

1

3

=3

5

• Euler tour length 2𝑛 − 1

6

5

3

9
Reduction from LCA to RMQ
• Example

1
6

Euler Tour, 𝐸[1. . 2𝑛 − 1]
Level Array, 𝐿[1. . 2𝑛 − 1]
Representative Array, 𝑅[1. . 𝑛]

3

2
5

9
10

4

8

7

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
𝐸 1 6 3 6 5 6 1 2 1 9 10 9 4 7 4 9 8 9 1
𝐿 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0

1 2 3 4 5 6 7 8 9 10
𝑅 1 8 3 13 5 2 14 17 10 11

11
Reduction from LCA to RMQ
LCA 𝑇 (10,7)
= 𝐸 12 = 9

1

• Example

6

2

LCA 𝑻 (𝒙, 𝒚) = 𝑬[RMQ 𝑳 𝑹 𝒙 , 𝑹 𝒚 ]
3

5

9

𝐸[11 … 14]

10

4

8

7

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
𝐸 1 6 3 6 5 6 1 2 1 9 10 9 4 7 4 9 8 9 1
𝐿 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0

1 2 3 4 5 6 7 8 9 10
𝑅 1 8 3 13 5 2 14 17 10 11
𝑅[10]
𝑅[7]

RMQ 𝐿 11,14 = 12

13
Reduction from LCA to RMQ
• Preprocessing Complexity
– Building 3 arrays, 𝐿, 𝑅, 𝐸 : 𝑂(𝑛), during the DFS run.
– Preprocessing 𝐿 for RMQ : 𝑓(2𝑛 − 1)
• Query Complexity
– RMQ query on 𝐿 : 𝑔(2𝑛 − 1)
– Array references : 𝑂(1)
• Overall
< 𝑓 2𝑛 − 1 + 𝑂 𝑛 , 𝑔 2𝑛 − 1 + 𝑂 1 >
14
Contents
• Introduction
• Reduction from LCA to RMQ
• RMQ Algorithms

– < 𝑂 𝑛2 , 𝑂 1 > Algorithm
– < 𝑂 𝑛 log 𝑛 , 𝑂 1 > Sparse Table(ST) Algorithm

• < 𝑂 𝑛 , 𝑂 1 > Algorithm for RMQ in LCA
• Conclusion

15
Easy RMQ Algorithm

• Given an array 𝐴 of size 𝑛

• Naïve < 𝑂 𝑛3 , 𝑂 1 > Algorithm
– compute the RMQ for every pair of indices and
store in a table.

• < 𝑂 𝑛2 , 𝑂 1 > Algorithm
– To calculate RMQ(i,j), use the already known
value of 𝑅𝑅𝑅 𝑖, 𝑗 − 1 . (Dynamic programming)

16
Sparse Table(ST) Algorithm for RMQ

• Preprocess sub-arrays of length 2 𝑘
•

𝑀(𝑖, 𝑗) = index of min value in the sub-array starting
at index 𝑖 having length 2 𝑗
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]

10 25

22

7

34

𝑀(3,0) = 3

2

9

12

26

16

𝑀(3,1) = 3

𝑀(3,2) = 5

17
Sparse Table(ST) Algorithm for RMQ

• Build table 𝑀
𝑀 𝑖, 𝑗 = argmin 𝑘=𝑖..𝑖+2 𝑗 −1 𝐴𝐴𝐴𝐴𝐴 𝑘

where
1 ≤ 𝑖 ≤ 𝑛, 1 ≤ 𝑗 ≤ log 𝑛
– size: 𝑂 𝑛 log 𝑛
– built in 𝑂 𝑛 log 𝑛 time using dynamic programming

𝑀[𝑖, 𝑗] = �

𝑀 𝑖, 𝑗 − 1 ,

𝑀[𝑖 + 2 𝑗−1 , 𝑗 − 1],

A[0]

10

A[1]

A[2]

25

22

A[3]

7

𝑖𝑖 𝐴 𝑀 𝑖, 𝑗 − 1 ≤ 𝐴 𝑀 𝑖 + 2 𝑗−1 , 𝑗 − 1

𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜

A[4]

A[5]

A[6]

A[7]

A[8]

A[9]

34

9

2

12

26

16

𝑀 3,1 = 3

𝑀 5,1 = 6

𝑀 3,2 = 6

18
Sparse Table(ST) Algorithm for RMQ

• Compute RMQ 𝑖, 𝑗

– Select two blocks that entirely cover the sub-range 𝑖. . 𝑗
– Let 𝑘 = log 𝑗 − 𝑖 + 1 .
• 2 𝑘 is the largest block that fits [𝑖. . 𝑗])

RMQ(𝑖, 𝑗) = �

𝑴 𝒊, 𝒌 ,

𝑴[𝒋 − 𝟐 𝒌 + 𝟏, 𝒌],

...

𝒊

𝑖𝑖 𝐴 𝑀 𝑖, 𝑘
𝑜𝑜ℎ𝑒𝑒𝑒𝑒𝑒𝑒

2 𝑘 elements

2 𝑘 elements

≤ 𝐴 𝑀 𝑗 − 2 𝑘 + 1, 𝑘
𝒋

...

20
Sparse Table(ST) Algorithm for RMQ
• ST algorithm’s time complexity
< 𝑂 𝑛 log 𝑛 , 𝑂 1 >

21
±1 RMQ

• In array 𝐿, adjacent elements differ by +1 or -1.
• For any two adjacent elements in an Euler tour,
one is always the parent of the other, and so their
levels differ by exactly one.
1
2

6
3

5

10

9
4
7

8

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
𝐸 1 6 3 6 5 6 1 2 1 9 10 9 4 7 4 9 8 9 1
𝐿 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 22 0
Contents
• Introduction
• Reduction from LCA to RMQ
• RMQ Algorithms

– < 𝑂 𝑛3 , 𝑂 1 > Algorithm
– < 𝑂 𝑛2 , 𝑂 1 > Algorithm
– < 𝑂 𝑛 log 𝑛 , 𝑂 1 > Sparse Table(ST) Algorithm

• < 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1RMQ
• Conclusion

23
< 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ

• Partition 𝐴 into

2𝑛
log 𝑛

blocks of size

log 𝑛
2

.

A
2𝑛
log 𝑛

blocks

log 𝑛
2

...

size

...

log 𝑛
2

...

size

...

log 𝑛
2

...

size

24
•
•

< 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ
𝐴′ 1. .
𝐵 1. .

2𝑛
log 𝑛

2𝑛
log 𝑛

2𝑛
log 𝑛

: 𝐴′ [𝑖] is the minimum element in the 𝑖-th block of 𝐴.

: 𝐵[𝑖] is the index in which value 𝐴′ [𝑖] occurs.

blocks

...
size

log 𝑛
2

𝐴’[0]

...
𝐵[0]

𝐴𝐴[𝑖]

𝐴

...

...

…

𝐵[𝑖]

...

𝐵

2𝑛
log 𝑛

2𝑛
𝐴’
log 𝑛

...

25
< 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ

• Example: 𝑛 = 16
𝐴[] ∶ 0

1

2

6

7

10 25 22 7 34 9

2

12 26 33 24 43 5

22 7

10 25

1

10 7

4

8

5

𝐴𝐴[] ∶ 0

3

2

9

34 9

7

…

9

10

2𝑛
log 𝑛

…

size:

𝐵[] ∶ 0

0

11 12

13

14

15

11 19 27

= 8 blocks

log 𝑛
2

1

2

3

5

=2

7

…
26
< 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ

• Build A’, B : 𝑂(𝑛)
• Preprocess 𝐴’ for RMQ using ST algorithm.
– " < 𝑂 𝑛 log 𝑛 , 𝑂 1 > " algorithm
– 𝐴’ size,

𝑛′

=

2𝑛
log 𝑛

– ST’s preprocessing on 𝐴’ = 𝑛′ log 𝑛′
=

2𝑛
2𝑛
log
log 𝑛
log 𝑛

– Total time complexity : < 𝑂 𝑛 , 𝑂 1 >

= 𝑂 𝑛
27
< 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ

• Compute RMQ 𝑖, 𝑗

– 𝑖 and 𝑗 might be in the same block, in-block RMQ query.
– 𝑖 < 𝑗 on different blocks
𝒊

1

𝒋

2

3

log 𝑛
2

Return the index of the minimum of these 3 values.
• 2: 𝑂(1) time by RMQ on 𝐴’
• 1 & 3 : in-block RMQ queries.

29
< 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ

• Observation
Let two arrays 𝑋 & 𝑌 such that ∀𝑖, 𝑋 𝑖 = 𝑌 𝑖 + 𝐶
Then ∀𝑖, 𝑗 RMQ 𝑋 𝑖, 𝑗 = RMQ 𝑌 (𝑖, 𝑗)

X[0] X[1] X[2] X[3] X[4] X[5] X[6] X[7] X[8] X[9]

3

4

5

6

5

4

5

6

5

4

Y[0] Y[1] Y[2] Y[3] Y[4] Y[5] Y[6] Y[7] Y[8] Y[9]

𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁

0

1

2

+1 +1

3

2

1

2

3

2

+1 -1 -1 +1 +1 -1

2

log 𝑛
−1
2

= 𝑂( 𝑛)

• There are 𝑂( 𝑛) normalized blocks.

1
-1

30
< 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ

• In-block queries
– " < 𝑂 𝑛2 , 𝑂 1 > " RMQ algorithm for each of 𝑂( 𝑛) normalized
blocks.
– Create 𝑂( 𝑛) tables of size 𝑂 log 2 𝑛 .
• table lookup for all queries 𝑖, 𝑗 where 1 ≤ 𝑖, 𝑗 ≤

– Preprocessing time: 𝑂

𝑛 log 2 𝑛 = 𝑂(𝑛).

+1 +1 +1 +1 +1 +1 +1 +1 +1

...
+1 +1 +1 -1 -1 +1 +1 -1 -1

...
-1 -1 -1 -1 -1 -1 -1 -1 -1

log 𝑛
2

𝑇0 𝑖, 𝑗 for ∀𝑖, 𝑗 where 1 ≤ 𝑖, 𝑗 ≤

𝑇𝑖 𝑖, 𝑗 for ∀𝑖, 𝑗 where 1 ≤ 𝑖, 𝑗 ≤

log 𝑛
2

log 𝑛
2

𝑇 𝑚 𝑖, 𝑗 for ∀𝑖, 𝑗 where 1 ≤ 𝑖, 𝑗 ≤

log 𝑛
2

31
< 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ

• Preprocess
– In-block queries: 𝑂(𝑛).
– For each block in 𝐴, compute which normalized block table
it should use : 𝑂(𝑛)
– Preprocess 𝐴’ using ST : 𝑂(𝑛)
• Query
– Query on 𝐴’ : 𝑂(1)
– Query on in-blocks : 𝑂(1)
• Overall complexity

< 𝑂 𝑛 , 𝑂 1 >

32
Conclusion

𝑂 𝑛 reduction using
Cartesian Tree

RMQ

𝑂 𝑛 reduction using
Euler Tour

±1 RMQ

LCA

A

2𝑛
blocks
log 𝑛

Use " < 𝑂 𝑛 log 𝑛 , 𝑂 1 > " algorithm

Use " < 𝑂 𝑛2 , 𝑂 1 > " algorithm

...

…

...

...

…

< 𝐎 𝐧 , 𝐎 𝟏 > Algorithm

...

… …

log 𝑛
2

...

...

+1 +1 +1 -1 -1 +1 +1 -1

-1
33
QnA
Thank You.

More Related Content

What's hot

Paper study: Learning to solve circuit sat
Paper study: Learning to solve circuit satPaper study: Learning to solve circuit sat
Paper study: Learning to solve circuit satChenYiHuang5
 
Ch.02 modeling in frequency domain
Ch.02 modeling in frequency domainCh.02 modeling in frequency domain
Ch.02 modeling in frequency domainNguyen_Tan_Tien
 
Control Systems Lab 2
Control Systems Lab 2Control Systems Lab 2
Control Systems Lab 2Julia London
 
Design and Analysis of a Control System Using Root Locus and Frequency Respon...
Design and Analysis of a Control System Using Root Locus and Frequency Respon...Design and Analysis of a Control System Using Root Locus and Frequency Respon...
Design and Analysis of a Control System Using Root Locus and Frequency Respon...Umair Shahzad
 
Paper Study: Melding the data decision pipeline
Paper Study: Melding the data decision pipelinePaper Study: Melding the data decision pipeline
Paper Study: Melding the data decision pipelineChenYiHuang5
 
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
 
Paper study: Attention, learn to solve routing problems!
Paper study: Attention, learn to solve routing problems!Paper study: Attention, learn to solve routing problems!
Paper study: Attention, learn to solve routing problems!ChenYiHuang5
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
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
 
Paper Study: A learning based iterative method for solving vehicle routing
Paper Study: A learning based iterative method for solving vehicle routingPaper Study: A learning based iterative method for solving vehicle routing
Paper Study: A learning based iterative method for solving vehicle routingChenYiHuang5
 
Control system Lab record
Control system Lab record Control system Lab record
Control system Lab record Yuvraj Singh
 
Constrained state feedback control
Constrained state feedback controlConstrained state feedback control
Constrained state feedback controlDivya Goel
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02Krish_ver2
 
control system Lab 01-introduction to transfer functions
control system Lab 01-introduction to transfer functionscontrol system Lab 01-introduction to transfer functions
control system Lab 01-introduction to transfer functionsnalan karunanayake
 
Transfer fn mech. systm
Transfer fn mech. systmTransfer fn mech. systm
Transfer fn mech. systmSyed Saeed
 

What's hot (20)

Paper study: Learning to solve circuit sat
Paper study: Learning to solve circuit satPaper study: Learning to solve circuit sat
Paper study: Learning to solve circuit sat
 
Ch.02 modeling in frequency domain
Ch.02 modeling in frequency domainCh.02 modeling in frequency domain
Ch.02 modeling in frequency domain
 
Control Systems Lab 2
Control Systems Lab 2Control Systems Lab 2
Control Systems Lab 2
 
Design and Analysis of a Control System Using Root Locus and Frequency Respon...
Design and Analysis of a Control System Using Root Locus and Frequency Respon...Design and Analysis of a Control System Using Root Locus and Frequency Respon...
Design and Analysis of a Control System Using Root Locus and Frequency Respon...
 
Paper Study: Melding the data decision pipeline
Paper Study: Melding the data decision pipelinePaper Study: Melding the data decision pipeline
Paper Study: Melding the data decision pipeline
 
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
 
Paper study: Attention, learn to solve routing problems!
Paper study: Attention, learn to solve routing problems!Paper study: Attention, learn to solve routing problems!
Paper study: Attention, learn to solve routing problems!
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
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...
 
Mergesort
MergesortMergesort
Mergesort
 
Quicksort
QuicksortQuicksort
Quicksort
 
Paper Study: A learning based iterative method for solving vehicle routing
Paper Study: A learning based iterative method for solving vehicle routingPaper Study: A learning based iterative method for solving vehicle routing
Paper Study: A learning based iterative method for solving vehicle routing
 
Seminar9
Seminar9Seminar9
Seminar9
 
Control system Lab record
Control system Lab record Control system Lab record
Control system Lab record
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Constrained state feedback control
Constrained state feedback controlConstrained state feedback control
Constrained state feedback control
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
control system Lab 01-introduction to transfer functions
control system Lab 01-introduction to transfer functionscontrol system Lab 01-introduction to transfer functions
control system Lab 01-introduction to transfer functions
 
Transfer fn mech. systm
Transfer fn mech. systmTransfer fn mech. systm
Transfer fn mech. systm
 
Assignment2 control
Assignment2 controlAssignment2 control
Assignment2 control
 

Similar to LCA problem revisited algorithm

Lca seminar modified
Lca seminar modifiedLca seminar modified
Lca seminar modifiedInbok Lee
 
Design and Implementation of Parallel and Randomized Approximation Algorithms
Design and Implementation of Parallel and Randomized Approximation AlgorithmsDesign and Implementation of Parallel and Randomized Approximation Algorithms
Design and Implementation of Parallel and Randomized Approximation AlgorithmsAjay Bidyarthy
 
Direct solution of sparse network equations by optimally ordered triangular f...
Direct solution of sparse network equations by optimally ordered triangular f...Direct solution of sparse network equations by optimally ordered triangular f...
Direct solution of sparse network equations by optimally ordered triangular f...Dimas Ruliandi
 
A Mathematical Introduction to Robotic Manipulation 輪講 第三回.pdf
A Mathematical Introduction to Robotic Manipulation 輪講 第三回.pdfA Mathematical Introduction to Robotic Manipulation 輪講 第三回.pdf
A Mathematical Introduction to Robotic Manipulation 輪講 第三回.pdfssuserbaad54
 
Algorithim lec1.pptx
Algorithim lec1.pptxAlgorithim lec1.pptx
Algorithim lec1.pptxrediet43
 
A framework for practical fast matrix multiplication
A framework for practical fast matrix multiplication�A framework for practical fast matrix multiplication�
A framework for practical fast matrix multiplicationAustin Benson
 
Unit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptxUnit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptxssuser01e301
 
Efficient anomaly detection via matrix sketching
Efficient anomaly detection via matrix sketchingEfficient anomaly detection via matrix sketching
Efficient anomaly detection via matrix sketchingHsing-chuan Hsieh
 
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural Networks
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural NetworksPaper Study: OptNet: Differentiable Optimization as a Layer in Neural Networks
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural NetworksChenYiHuang5
 
Parallel R in snow (english after 2nd slide)
Parallel R in snow (english after 2nd slide)Parallel R in snow (english after 2nd slide)
Parallel R in snow (english after 2nd slide)Cdiscount
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c eraMauro Pagano
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisAnindita Kundu
 
Linear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent SetLinear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent SetDhaval Shukla
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 

Similar to LCA problem revisited algorithm (20)

Lca seminar modified
Lca seminar modifiedLca seminar modified
Lca seminar modified
 
Design and Implementation of Parallel and Randomized Approximation Algorithms
Design and Implementation of Parallel and Randomized Approximation AlgorithmsDesign and Implementation of Parallel and Randomized Approximation Algorithms
Design and Implementation of Parallel and Randomized Approximation Algorithms
 
Direct solution of sparse network equations by optimally ordered triangular f...
Direct solution of sparse network equations by optimally ordered triangular f...Direct solution of sparse network equations by optimally ordered triangular f...
Direct solution of sparse network equations by optimally ordered triangular f...
 
A Mathematical Introduction to Robotic Manipulation 輪講 第三回.pdf
A Mathematical Introduction to Robotic Manipulation 輪講 第三回.pdfA Mathematical Introduction to Robotic Manipulation 輪講 第三回.pdf
A Mathematical Introduction to Robotic Manipulation 輪講 第三回.pdf
 
Algorithim lec1.pptx
Algorithim lec1.pptxAlgorithim lec1.pptx
Algorithim lec1.pptx
 
pattern mining
pattern miningpattern mining
pattern mining
 
Merge sort
Merge sortMerge sort
Merge sort
 
A framework for practical fast matrix multiplication
A framework for practical fast matrix multiplication�A framework for practical fast matrix multiplication�
A framework for practical fast matrix multiplication
 
Unit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptxUnit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptx
 
Data Mining Lecture_8(b).pptx
Data Mining Lecture_8(b).pptxData Mining Lecture_8(b).pptx
Data Mining Lecture_8(b).pptx
 
Efficient anomaly detection via matrix sketching
Efficient anomaly detection via matrix sketchingEfficient anomaly detection via matrix sketching
Efficient anomaly detection via matrix sketching
 
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural Networks
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural NetworksPaper Study: OptNet: Differentiable Optimization as a Layer in Neural Networks
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural Networks
 
Parallel R in snow (english after 2nd slide)
Parallel R in snow (english after 2nd slide)Parallel R in snow (english after 2nd slide)
Parallel R in snow (english after 2nd slide)
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c era
 
Parameter estimation
Parameter estimationParameter estimation
Parameter estimation
 
Merge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering studentsMerge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering students
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
 
Linear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent SetLinear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent Set
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 

Recently uploaded

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

LCA problem revisited algorithm

  • 1. The LCA Problem Revisited (Latin American Theoretical Informatics Symposium, 2000) Michael A. Bender, Martín Farach-Colton Presented by Minsung Hong, Junyong Choi
  • 2. Lowest Common Ancestor(LCA) • LCA 𝑇 𝑢, 𝑣 – given nodes 𝑢, 𝑣 in 𝑇, returns the node furthest from the root that is an ancestor of both 𝑢 and 𝑣. 𝑧 𝑢 𝑣 LCA 𝑇 𝑢, 𝑣 =𝑧 3
  • 3. Range Minimum Query(RMQ) • RMQ 𝐴 (𝑖, 𝑗) – given indices 𝑖, 𝑗 of array 𝐴, returns the index of the smallest element in the sub-array 𝐴[𝑖. . 𝑗]. A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 0 1 2 34 7 19 10 12 13 16 RMQ 𝐴 3,7 = 4 4
  • 4. Complexity Notation • Notation for the overall complexity of an algorithm < 𝑓 𝑛 , 𝑔 𝑛 > – Preprocessing time : 𝑓(𝑛) – Query time : 𝑔 𝑛 5
  • 5. Previous Work • < 𝑂 𝑛 , 𝑂 1 > Algorithms for LCA – Harel and Tarjan, 1984: first optimal • very complicated and unimplementable – Shieber and Vishkin, 1988 • simplified but not particularly implementable – Berkman and Vishkin, 1993 • developed a new way using reduction to RMQ using Euler tour. 6
  • 6. Contents • Introduction • Reduction from LCA to RMQ • RMQ Algorithms – < 𝑂 𝑛2 , 𝑂 1 > Algorithm – < 𝑂 𝑛 log 𝑛 , 𝑂 1 > Sparse Table(ST) Algorithm • < 𝑂 𝑛 , 𝑂 1 > Algorithm for RMQ in LCA • Conclusion 7
  • 7. Reduction from LCA to RMQ • Lemma If there is an < 𝑓 𝑛 , 𝑔 𝑛 > solution for RMQ, then there is an < 𝑓 2𝑛 − 1 + 𝑂 𝑛 , 𝑔 2𝑛 − 1 + 𝑂 1 > solution for LCA. 8
  • 8. Reduction from LCA to RMQ • Observation The LCA of nodes 𝑢 and 𝑣 is the shallowest node encountered between the visits to 𝑢 and to 𝑣 during a depth first search(DFS) traversal of 𝑇. 0 3 12 4 5 6 2 5 1 2 4 3 8 9 3 1 Shallowest node Euler tour: 7 7 11 10 6 2 3 1 4 1 7 LCA 𝑇 1,5 1 3 =3 5 • Euler tour length 2𝑛 − 1 6 5 3 9
  • 9. Reduction from LCA to RMQ • Example 1 6 Euler Tour, 𝐸[1. . 2𝑛 − 1] Level Array, 𝐿[1. . 2𝑛 − 1] Representative Array, 𝑅[1. . 𝑛] 3 2 5 9 10 4 8 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 𝐸 1 6 3 6 5 6 1 2 1 9 10 9 4 7 4 9 8 9 1 𝐿 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0 1 2 3 4 5 6 7 8 9 10 𝑅 1 8 3 13 5 2 14 17 10 11 11
  • 10. Reduction from LCA to RMQ LCA 𝑇 (10,7) = 𝐸 12 = 9 1 • Example 6 2 LCA 𝑻 (𝒙, 𝒚) = 𝑬[RMQ 𝑳 𝑹 𝒙 , 𝑹 𝒚 ] 3 5 9 𝐸[11 … 14] 10 4 8 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 𝐸 1 6 3 6 5 6 1 2 1 9 10 9 4 7 4 9 8 9 1 𝐿 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0 1 2 3 4 5 6 7 8 9 10 𝑅 1 8 3 13 5 2 14 17 10 11 𝑅[10] 𝑅[7] RMQ 𝐿 11,14 = 12 13
  • 11. Reduction from LCA to RMQ • Preprocessing Complexity – Building 3 arrays, 𝐿, 𝑅, 𝐸 : 𝑂(𝑛), during the DFS run. – Preprocessing 𝐿 for RMQ : 𝑓(2𝑛 − 1) • Query Complexity – RMQ query on 𝐿 : 𝑔(2𝑛 − 1) – Array references : 𝑂(1) • Overall < 𝑓 2𝑛 − 1 + 𝑂 𝑛 , 𝑔 2𝑛 − 1 + 𝑂 1 > 14
  • 12. Contents • Introduction • Reduction from LCA to RMQ • RMQ Algorithms – < 𝑂 𝑛2 , 𝑂 1 > Algorithm – < 𝑂 𝑛 log 𝑛 , 𝑂 1 > Sparse Table(ST) Algorithm • < 𝑂 𝑛 , 𝑂 1 > Algorithm for RMQ in LCA • Conclusion 15
  • 13. Easy RMQ Algorithm • Given an array 𝐴 of size 𝑛 • Naïve < 𝑂 𝑛3 , 𝑂 1 > Algorithm – compute the RMQ for every pair of indices and store in a table. • < 𝑂 𝑛2 , 𝑂 1 > Algorithm – To calculate RMQ(i,j), use the already known value of 𝑅𝑅𝑅 𝑖, 𝑗 − 1 . (Dynamic programming) 16
  • 14. Sparse Table(ST) Algorithm for RMQ • Preprocess sub-arrays of length 2 𝑘 • 𝑀(𝑖, 𝑗) = index of min value in the sub-array starting at index 𝑖 having length 2 𝑗 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 10 25 22 7 34 𝑀(3,0) = 3 2 9 12 26 16 𝑀(3,1) = 3 𝑀(3,2) = 5 17
  • 15. Sparse Table(ST) Algorithm for RMQ • Build table 𝑀 𝑀 𝑖, 𝑗 = argmin 𝑘=𝑖..𝑖+2 𝑗 −1 𝐴𝐴𝐴𝐴𝐴 𝑘 where 1 ≤ 𝑖 ≤ 𝑛, 1 ≤ 𝑗 ≤ log 𝑛 – size: 𝑂 𝑛 log 𝑛 – built in 𝑂 𝑛 log 𝑛 time using dynamic programming 𝑀[𝑖, 𝑗] = � 𝑀 𝑖, 𝑗 − 1 , 𝑀[𝑖 + 2 𝑗−1 , 𝑗 − 1], A[0] 10 A[1] A[2] 25 22 A[3] 7 𝑖𝑖 𝐴 𝑀 𝑖, 𝑗 − 1 ≤ 𝐴 𝑀 𝑖 + 2 𝑗−1 , 𝑗 − 1 𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜 A[4] A[5] A[6] A[7] A[8] A[9] 34 9 2 12 26 16 𝑀 3,1 = 3 𝑀 5,1 = 6 𝑀 3,2 = 6 18
  • 16. Sparse Table(ST) Algorithm for RMQ • Compute RMQ 𝑖, 𝑗 – Select two blocks that entirely cover the sub-range 𝑖. . 𝑗 – Let 𝑘 = log 𝑗 − 𝑖 + 1 . • 2 𝑘 is the largest block that fits [𝑖. . 𝑗]) RMQ(𝑖, 𝑗) = � 𝑴 𝒊, 𝒌 , 𝑴[𝒋 − 𝟐 𝒌 + 𝟏, 𝒌], ... 𝒊 𝑖𝑖 𝐴 𝑀 𝑖, 𝑘 𝑜𝑜ℎ𝑒𝑒𝑒𝑒𝑒𝑒 2 𝑘 elements 2 𝑘 elements ≤ 𝐴 𝑀 𝑗 − 2 𝑘 + 1, 𝑘 𝒋 ... 20
  • 17. Sparse Table(ST) Algorithm for RMQ • ST algorithm’s time complexity < 𝑂 𝑛 log 𝑛 , 𝑂 1 > 21
  • 18. ±1 RMQ • In array 𝐿, adjacent elements differ by +1 or -1. • For any two adjacent elements in an Euler tour, one is always the parent of the other, and so their levels differ by exactly one. 1 2 6 3 5 10 9 4 7 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 𝐸 1 6 3 6 5 6 1 2 1 9 10 9 4 7 4 9 8 9 1 𝐿 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 22 0
  • 19. Contents • Introduction • Reduction from LCA to RMQ • RMQ Algorithms – < 𝑂 𝑛3 , 𝑂 1 > Algorithm – < 𝑂 𝑛2 , 𝑂 1 > Algorithm – < 𝑂 𝑛 log 𝑛 , 𝑂 1 > Sparse Table(ST) Algorithm • < 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1RMQ • Conclusion 23
  • 20. < 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ • Partition 𝐴 into 2𝑛 log 𝑛 blocks of size log 𝑛 2 . A 2𝑛 log 𝑛 blocks log 𝑛 2 ... size ... log 𝑛 2 ... size ... log 𝑛 2 ... size 24
  • 21. • • < 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ 𝐴′ 1. . 𝐵 1. . 2𝑛 log 𝑛 2𝑛 log 𝑛 2𝑛 log 𝑛 : 𝐴′ [𝑖] is the minimum element in the 𝑖-th block of 𝐴. : 𝐵[𝑖] is the index in which value 𝐴′ [𝑖] occurs. blocks ... size log 𝑛 2 𝐴’[0] ... 𝐵[0] 𝐴𝐴[𝑖] 𝐴 ... ... … 𝐵[𝑖] ... 𝐵 2𝑛 log 𝑛 2𝑛 𝐴’ log 𝑛 ... 25
  • 22. < 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ • Example: 𝑛 = 16 𝐴[] ∶ 0 1 2 6 7 10 25 22 7 34 9 2 12 26 33 24 43 5 22 7 10 25 1 10 7 4 8 5 𝐴𝐴[] ∶ 0 3 2 9 34 9 7 … 9 10 2𝑛 log 𝑛 … size: 𝐵[] ∶ 0 0 11 12 13 14 15 11 19 27 = 8 blocks log 𝑛 2 1 2 3 5 =2 7 … 26
  • 23. < 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ • Build A’, B : 𝑂(𝑛) • Preprocess 𝐴’ for RMQ using ST algorithm. – " < 𝑂 𝑛 log 𝑛 , 𝑂 1 > " algorithm – 𝐴’ size, 𝑛′ = 2𝑛 log 𝑛 – ST’s preprocessing on 𝐴’ = 𝑛′ log 𝑛′ = 2𝑛 2𝑛 log log 𝑛 log 𝑛 – Total time complexity : < 𝑂 𝑛 , 𝑂 1 > = 𝑂 𝑛 27
  • 24. < 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ • Compute RMQ 𝑖, 𝑗 – 𝑖 and 𝑗 might be in the same block, in-block RMQ query. – 𝑖 < 𝑗 on different blocks 𝒊 1 𝒋 2 3 log 𝑛 2 Return the index of the minimum of these 3 values. • 2: 𝑂(1) time by RMQ on 𝐴’ • 1 & 3 : in-block RMQ queries. 29
  • 25. < 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ • Observation Let two arrays 𝑋 & 𝑌 such that ∀𝑖, 𝑋 𝑖 = 𝑌 𝑖 + 𝐶 Then ∀𝑖, 𝑗 RMQ 𝑋 𝑖, 𝑗 = RMQ 𝑌 (𝑖, 𝑗) X[0] X[1] X[2] X[3] X[4] X[5] X[6] X[7] X[8] X[9] 3 4 5 6 5 4 5 6 5 4 Y[0] Y[1] Y[2] Y[3] Y[4] Y[5] Y[6] Y[7] Y[8] Y[9] 𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁 0 1 2 +1 +1 3 2 1 2 3 2 +1 -1 -1 +1 +1 -1 2 log 𝑛 −1 2 = 𝑂( 𝑛) • There are 𝑂( 𝑛) normalized blocks. 1 -1 30
  • 26. < 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ • In-block queries – " < 𝑂 𝑛2 , 𝑂 1 > " RMQ algorithm for each of 𝑂( 𝑛) normalized blocks. – Create 𝑂( 𝑛) tables of size 𝑂 log 2 𝑛 . • table lookup for all queries 𝑖, 𝑗 where 1 ≤ 𝑖, 𝑗 ≤ – Preprocessing time: 𝑂 𝑛 log 2 𝑛 = 𝑂(𝑛). +1 +1 +1 +1 +1 +1 +1 +1 +1 ... +1 +1 +1 -1 -1 +1 +1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 log 𝑛 2 𝑇0 𝑖, 𝑗 for ∀𝑖, 𝑗 where 1 ≤ 𝑖, 𝑗 ≤ 𝑇𝑖 𝑖, 𝑗 for ∀𝑖, 𝑗 where 1 ≤ 𝑖, 𝑗 ≤ log 𝑛 2 log 𝑛 2 𝑇 𝑚 𝑖, 𝑗 for ∀𝑖, 𝑗 where 1 ≤ 𝑖, 𝑗 ≤ log 𝑛 2 31
  • 27. < 𝑂 𝑛 , 𝑂 1 > Algorithm for ±1 RMQ • Preprocess – In-block queries: 𝑂(𝑛). – For each block in 𝐴, compute which normalized block table it should use : 𝑂(𝑛) – Preprocess 𝐴’ using ST : 𝑂(𝑛) • Query – Query on 𝐴’ : 𝑂(1) – Query on in-blocks : 𝑂(1) • Overall complexity < 𝑂 𝑛 , 𝑂 1 > 32
  • 28. Conclusion 𝑂 𝑛 reduction using Cartesian Tree RMQ 𝑂 𝑛 reduction using Euler Tour ±1 RMQ LCA A 2𝑛 blocks log 𝑛 Use " < 𝑂 𝑛 log 𝑛 , 𝑂 1 > " algorithm Use " < 𝑂 𝑛2 , 𝑂 1 > " algorithm ... … ... ... … < 𝐎 𝐧 , 𝐎 𝟏 > Algorithm ... … … log 𝑛 2 ... ... +1 +1 +1 -1 -1 +1 +1 -1 -1 33