The LCA Problem Revisited

Slides:



Advertisements
Similar presentations
The LCA Problem Revisited Michael A. BenderMartin Farach-Colton Latin American Theoretical Informatics Symposium, pages 8894, Speaker:
Advertisements

WSPD Applications.
Longest Common Subsequence
Efficient Keyword Search for Smallest LCAs in XML Database Yu Xu Department of Computer Science & Engineering University of California, San Diego Yannis.
Dynamic Graph Algorithms - I
Union-Find structure Mikko Malinen School of Computing University of Eastern Finland.
Lecture 3: Parallel Algorithm Design
Constant-Time LCA Retrieval
Suffix Sorting & Related Algoritmics Martin Farach-Colton Rutgers University USA.
Suffix Sorting & Related Algoritmics Martin Farach-Colton Rutgers University USA.
Rapid Global Alignments How to align genomic sequences in (more or less) linear time.
Comp 122, Fall 2004 Dynamic Programming. dynprog - 2 Lin / Devi Comp 122, Spring 2004 Longest Common Subsequence  Problem: Given 2 sequences, X =  x.
1 Reduction between Transitive Closure & Boolean Matrix Multiplication Presented by Rotem Mairon.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Advanced Topics in Algorithms and Data Structures 1 Rooting a tree For doing any tree computation, we need to know the parent p ( v ) for each node v.
Computability and Complexity 23-1 Computability and Complexity Andrei Bulatov Search and Optimization.
Complexity 15-1 Complexity Andrei Bulatov Hierarchy Theorem.
1 Nearest Commen Ancestors (NCA) Discrete Range Maximum Cartesian Tree [Vuillemin 1980] i j max(i, j) i.
Lowest Common Ancestors Two vertices (u, v) Lowest common ancestors, lca (u, v) Example lca (5, 6) = 4 lca (3, 7) = 2 lca (7, 8) = 1 l(v):
1 Algorithms for Large Data Sets Ziv Bar-Yossef Lecture 8 May 4, 2005
Lowest common ancestors. Write an Euler tour of the tree LCA(1,5) = 3 Shallowest node.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Improvements on the Range-Minimum-Query- Problem
E.G.M. Petrakissearching1 Searching  Find an element in a collection in the main memory or on the disk  collection: (K 1,I 1 ),(K 2,I 2 )…(K N,I N )
CS 307 Fundamentals of Computer Science 1 Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
11-1 Matrix-chain Multiplication Suppose we have a sequence or chain A 1, A 2, …, A n of n matrices to be multiplied –That is, we want to compute the product.
ECE669 L10: Graph Applications March 2, 2004 ECE 669 Parallel Computer Architecture Lecture 10 Graph Applications.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Basic PRAM algorithms Problem 1. Min of n numbers Problem 2. Computing a position of the first one in the sequence of 0’s and 1’s.
The Encoding Complexity of Two Dimensional Range Minimum Data Structures MADALGO, October 2, 2013 (Work presented at ESA 2013) 1234  n
The Encoding Complexity of Two Dimensional Range Minimum Data Structures European Symposium on Algorithms, Inria, Sophia Antipolis, France, September 3,
Data Structures Using C++ 2E
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
“On an Algorithm of Zemlyachenko for Subtree Isomorphism” Yefim Dinitz, Alon Itai, Michael Rodeh (1998) Presented by: Masha Igra, Merav Bukra.
The LCA Problem Revisited Michael A.Bender & Martin Farach-Colton Presented by: Dvir Halevi.
Constant-Time LCA Retrieval Presentation by Danny Hermelin, String Matching Algorithms Seminar, Haifa University.
Tries1. 2 Outline and Reading Standard tries (§9.2.1) Compressed tries (§9.2.2) Suffix tries (§9.2.3)
Lectures on Greedy Algorithms and Dynamic Programming
Suffix trees. Trie A tree representing a set of strings. a b c e e f d b f e g { aeef ad bbfe bbfg c }
COSC 3101NJ. Elder Announcements Midterms are marked Assignment 2: –Still analyzing.
Lecture 14 Lower Bounds Decision tree model Linear-time reduction.
Interval S = [3,10]  {x | 3 ≤ x ≤ 10} Closed segment S = (3,10)  {x | 3 < x < 10} Opened segment S = [3,3]  {3} Point.
Cartesian Trees Amihood Amir Bar-Ilan University.
LOWEST COMMON ANCESTOR IN TREE CPSC 490 – FEBRUARY 24 TH – MICHAEL HILAND.
Section 9.3. Section Summary Representing Relations using Matrices Representing Relations using Digraphs.
Succinct Data Structures
All-pairs Shortest paths Transitive Closure
Succinct Data Structures
Lecture 3: Parallel Algorithm Design
Discrete Methods in Mathematical Informatics
Two equivalent problems
Chapter 8 Dynamic Programming
Ariel Rosenfeld Bar-Ilan Uni.
Data Structures Review Session 2
Orthogonal Range Searching and Kd-Trees
Minimum Spanning Tree Verification
Unit-5 Dynamic Programming
Suffix trees.
Approximating Points by A Piecewise Linear Function: I
On the Range Maximum-Sum Segment Query Problem
CMPS 3120: Computational Geometry Spring 2013
DYNAMIC PROGRAMMING.
COMP108 Algorithmic Foundations Dynamic Programming
The LCA Problem Revisited
Presentation transcript:

The LCA Problem Revisited Michael A.Bender & Martin Farach-Colton

Outline Definitions. Reduction from LCA to RMQ. Trivial Solutions for RMQ Faster solution for sub-problem of RMQ. Solution for general RMQ.

LCA - Lowest Common Ancestor LCAT(u,v) The LCA of nodes u and v in a tree T is the shared ancestor of u and v that is located farthest from the root. u v

RMQ - Range Minimum Query RMQA(i,j) For indices i and j between 1 and n, query RMQA(i,j) returns the index of the smallest element in the sub array A[i…j]. A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 1 2 34 7 19 10 12 13 16 RMQA(3,7) = 4

Complexity Notation Preprocessing Time Query Time

LCA - Trivial Algorithm For each pair of vertices, follow both paths toward the root until the first shared vertex is found. Complexity =

Reduction From LCA to RMQ If there is an -time solution for RMQ, then there is an -time solution for LCA. The reduction - We build three array from the input tree T. 1. E[1,…,2n-1] stores the nodes visited in an Euler Tour of T. 2. L[1,…,2n-1], where L[i] is the level of E[i] in T. 3. R[1,…,n], where R[i] is the index of the first occurrence of the node i in the array E.

Example 1 4 5 6 7 9 2 3 8 E: 0 1 2 1 3 1 0 4 0 5 6 5 7 8 7 5 9 5 0 L: 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0 R: 0 1 2 4 7 9 10 12 13 16

Computing LCAT(u,v) The nodes in the Euler Tour between the first visits to u and to v are E[R[u],…,R[v]]. The shallowest node is at index RMQL(R[u],R[v]). The node E[RMQL(R[u],R[v])] is LCAT(u,v).

Example - LCAT(6,9) 1 4 5 6 7 9 2 3 8 E[10,…,16] E: 0 1 2 1 3 1 0 4 0 5 6 5 7 8 7 5 9 5 0 L: 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0 R: 0 1 2 4 7 9 10 12 13 16 RMQL(10,16) = 11 LCAT(6,9) = E[11]=5 R[6] R[9]

Complexity Analysis Preprocessing Array Construction : O(n). Preprocessing of the array L : f(2n-1). Query Three Array references : O(1). RMQ query in L : g(2n-1). Overall :

Naive Solutions for RMQ From now on, we will focus on the RMQ problem. Computing the RMQ for every pair of indices - Trivial dynamic programming -

ST Algorithm for RMQ Preprocessing Time: O(nlogn). an a1 ... ... i i+2j-1-1 i+2j-1 an a1 ... ...

ST Algorithm for RMQ Cont. Arbitrary RMQ(i,j) Query Time: O(1). ST Algorithm Complexity: i j 2k elements an a1 ... ... 2k elements

RMQ Solution A’: block min value … ... B : block min index … ... ... A’[i] A’[2n/logn] A’: block min value … ... B[0] B[i] B[2n/logn] B : block min index … ... ... ... ... ... ... A

RMQ Solution Cont. Size(A’) = 2n / logn. ST_Preprocessing(A’) ST(A’)=

RMQ Solution Cont. Arbitrary RMQ(i,j) Query We should compute the following values: 1. The minimum from i to the end of its block. 2. The minimum of all the blocks in between i’s block and j’s block. 3. The minimum from the beginning of j’s block to j. A[i] A[j] ... 1 2 3

RMQ Solution Cont. ST Preprocessing of a block Per Block All Blocks A[i] A[j] ... 1 2 3

Observation Let two arrays X & Y such that 3 4 5 6 5 4 5 6 5 4 1 2 3 2 1 2 3 2 1 2 3 2 1 +1 +1 +1 -1 -1 +1 +1 -1 -1

 1RMQ Block size = 1 sequence block size = Number of possible sequences = Preprocessing all possible 1 blocks: Determining which table to use for each block: O(n). Overall time complexity =

General RMQ Solution Reduction from RMQ to LCA. Claim: If there is an -time solution for LCA, then there is an -time solution for RMQ.

Cartesian Tree A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 10 25 22 34 7 19 10 12 26 16 Given an array, we can build a Cartesian tree. The root of a Cartesian tree is the minimum element of the array. The root is labeled with the position of this minimum.

Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 A[0] A[1] A[2] A[3] A[4]

Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 6 A[0] A[1] A[2] A[3]

Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 6 7 A[0] A[1] A[2] A[3]

Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 6 7 9 A[0] A[1] A[2]

Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 6 7 9 8 A[0] A[1] A[2]

Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 6 5 7 9 8 A[0] A[1] A[2]

Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 6 5 7 9 8 A[0] A[1] A[2] 6 5 7 9 8

Cartesian Tree A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 10 25 22 34 7 19 10 12 26 16 4 6 2 5 7 1 3 9 8

Linear Cartesian Tree Construction 10 25 22 34 7 19 10 12 26 16 Suppose Ci is the Cartesian tree of A[0,…,i]. The i+1 node belongs to the rightmost path of Ci+1. We climb up the rightmost path of Ci until finding the position where i+1 node belongs.

Linear Cartesian Tree Construction 10 25 22 34 7 19 10 12 26 16 10

Linear Cartesian Tree Construction 10 25 22 34 7 19 10 12 26 16 10 25 1

Linear Cartesian Tree Construction 10 25 22 34 7 19 10 12 26 16 10 22 2 25 1

Linear Cartesian Tree Construction 10 25 22 34 34 34 7 7 19 10 12 26 16 10 22 2 34 25 1 3 3

Linear Cartesian Tree Construction 10 25 22 34 7 19 10 12 26 16 7 4 10 22 2 34 25 1 3

Linear Cartesian Tree Construction 10 25 22 34 7 19 10 12 26 16 4 6 2 5 7 1 3 9 8

Reduction from RMQ to LCA If there is an -time solution for LCA, then there is an -time solution for RMQ. Let A be the input array to the RMQ problem. Let C be the Cartesian tree of A. RMQA(i,j) = LCAC(i,j)

Linear Cartesian Tree Construction 10 25 22 34 7 19 10 12 26 16 4 6 2 5 7 1 3 9 8