Ariel Rosenfeld Bar-Ilan Uni.

Slides:



Advertisements
Similar presentations
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Advertisements

Greedy Algorithms Amihood Amir Bar-Ilan University.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Constant-Time LCA Retrieval
296.3: Algorithms in the Real World
1 Huffman Codes. 2 Introduction Huffman codes are a very effective technique for compressing data; savings of 20% to 90% are typical, depending on the.
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):
Lowest common ancestors. Write an Euler tour of the tree LCA(1,5) = 3 Shallowest node.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
The LCA Problem Revisited Michael A.Bender & Martin Farach-Colton Presented by: Dvir Halevi.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
The LCA Problem Revisited
Constant-Time LCA Retrieval Presentation by Danny Hermelin, String Matching Algorithms Seminar, Haifa University.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
5.5.3 Rooted tree and binary tree  Definition 25: A directed graph is a directed tree if the graph is a tree in the underlying undirected graph.  Definition.
Foundation of Computing Systems
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Discrete Structures Li Tak Sing( 李德成 ) Lectures
Advanced Data Structures Lecture 8 Mingmin Xie. Agenda Overview Trie Suffix Tree Suffix Array, LCP Construction Applications.
Succinct Data Structures
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
BCA-II Data Structure Using C
Top 50 Data Structures Interview Questions
Chapter 5 : Trees.
Two equivalent problems
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
12. Graphs and Trees 2 Summary
B+-Trees.
B+-Trees.
Data Structures Review Session 2
Trees Another Abstract Data Type (ADT)
B+ Tree.
CMSC 341 Introduction to Trees.
Binary Trees, Binary Search Trees
Red-Black Trees Motivations
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Orthogonal Range Searching and Kd-Trees
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
Minimum Spanning Tree Verification
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
Trees Another Abstract Data Type (ADT)
Binary Trees.
Data Structures Review Session
Lectures on Graph Algorithms: searching, testing and sorting
Suffix trees.
Trees Another Abstract Data Type (ADT)
CMSC 202 Trees.
Data Structure and Algorithms
On the Range Maximum-Sum Segment Query Problem
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
CE 221 Data Structures and Algorithms
Trees.
Compact routing schemes with improved stretch
Binary Tree Properties & Representation
AVL-Trees (Part 1).
The LCA Problem Revisited
Binary SearchTrees [CLRS] – Chap 12.
Important Problem Types and Fundamental Data Structures
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Binary Trees, Binary Search Trees
Analysis of Algorithms CS 477/677
Presentation transcript:

Ariel Rosenfeld Bar-Ilan Uni. LCA Ariel Rosenfeld Bar-Ilan Uni.

The Lowest Common Ancestor In a rooted tree T, a node u is an ancestor of a node v if u is on the unique path from the root to v. In a rooted tree T, the Lowest Common Ancestor (LCA) of two nodes u and v is the deepest node in T that is the ancestor of both u and v.

For example… Node 3 is the LCA of nodes 4 and 6. 1 2 3 4 5 6 Node 3 is the LCA of nodes 4 and 6. Node 1 is the LCA of node 2 and 5.

The LCA Problem The LCA problem is then, given a rooted tree T for preprocessing, preprocess it in a way so that the LCA of any two given nodes in T can be retrieved in constant time.

Prepossessing vs. querying Restrictions\comments Querying Preprocessing O(n) None Naïve  (do it yourselves) O(1) O(n^3) DP O(n^2) When the tree is complete Using reduction. Article added.

Complete Binary Tree Let B denote a complete binary tree with n nodes. The key here is to encode the unique path from the root to a node in the node itself. We assign each node a path number, a logn bit number that encodes the unique path from the root to the node.

The Path Number For each node v in B we encode a path number in the following way: Counting from the left most bit, the i’th bit of the path number for v corresponds to the i’th edge on the path from the root to v. A 0 for the i’th bit from the left indicates that the i’th edge on the path goes to a left child, and a 1 indicates that it goes to a right child. Let k denote then number of edges on the path from the root to v, then we mark the k+1 bit (the height bit) of the path number 1, and the rest of the logn-k-1 bits 0.

For example… 1 1 1 1 Node i’s path number is Node j’s path number is node j 1 node i Node i’s path number is Node j’s path number is 1 1 1 1 The height bit is marked in blue Padded bits are marked in red.

1000 0100 1100 0010 0110 1010 1110 0001 0011 0101 0111 1001 1011 1101 1111 Path numbers can easily be assigned in a simple O(n) in-order traversal on B. (good programing exercise)

How do we solve LCA queries in B Suppose now that u and v are two nodes in B, and that path(u) and path(v) are their appropriate path numbers. We denote the lowest common ancestor of u and v as lca(u,v). We denote the prefix bits in the path number, those that correspond to edges on the path from the root, as the path bits of the path number.

First we calculate path(u) XOR path(v) and find the left most bit which equals 1. If there is no such bit than path(u) = path(v) and so u = v, so assume that the k’th bit of the result is 1. If both the k’th bit in path(u) and the k’th bit in path(v) are path bits, then this means that u and v agree on k-1 edges of their path from the root, meaning that the k-1 prefix of each node’s path number encodes within it the path from the root to lca(u,v).

For example… path(u) XOR path(v) = 0 0 1 0 XOR path(lca(u,v) = 0 1 1 1 0100 u 0010 v 0111 path(u) XOR path(v) = 0 0 1 0 XOR 0 1 1 1 0 1 0 1 path(lca(u,v) = 1 height bit padded bits

For example… path(u’) XOR path(v’) = 1 0 0 1 XOR 1 0 1 1 0 0 1 0 lca(u’,v’) 1010 u’ v’ 1001 1011 path(u’) XOR path(v’) = 1 0 0 1 XOR 1 0 1 1 0 0 1 0 path(lca(u,v) = 1 1 height bit padded bit

This concludes that if we take the prefix k- 1 bits of the result of path(u) XOR path(v), add 1 as the k’th bit, and pad logn-k 0 suffix bits, we get path(lca(u,v)). If either the k’th bit in path(u) or the k’th bit in path(v) (or both) is not a path bit then one node is ancestor to the other, and lca(u,v) can easily be retrieved by comparing path(u) and path(v)’s height bit.

The general LCA algorithm The following are the two stages of the general LCA algorithm for any arbitrary tree T: First, we reduce the LCA problem to the Restricted Range Minima {RRM} (simple case of Range Minimum Query {RMQ}) problem. Problem of finding the smallest number in an interval of a fixed list of numbers, where the difference between two successive numbers in the list is exactly one. We’ll solve the Restricted Range Minima problem and thus solve the LCA problem.

The Reduction Let T denote an arbitrary tree Let lca(u,v) denote the lowest common ancestor of nodes u and v in T. First we execute a depth-first traversal of T to label the nodes in the depth-first order they are encountered. In that same traversal we maintain a list L, of nodes of T, in the same order that they were visited. The only property of the depth-first numbering we need is that the number given to any node is smaller then the number given to any of it’s descendents.

For example… 000 001 010 011 100 101 110 111 The depth-first traversal creates these depth numbers and the following list L: L = { 0, 1, 0, 2, 3, 2, 4, 2, 5, 6, 5, 7, 5, 2, 0 }

Now if want to find lca(u,v), we find the first occurrence of the two nodes in L, this defines an interval I in L. Suppose u occurs in L before v. Now, I describes the part of the traversal, from the point we first discovered u to the point we first discovered v. lca(u,v) can be retrieved by finding the minimum number in I.

This is due to the following two simple facts: If u is an ancestor of v then all nodes visited between u and v are in u’s subtree, thus the depth- number assigned to u is minimal in I. If u is not an ancestor of v, then all those nodes visited between u and v are in lca(u,v)’s subtree, and the traversal must visit lca(u,v). Thus the minimum of I is the depth-number assigned to lca(u,v).

For example.. 000 001 010 011 100 101 110 111 L = { 0, 1, 0, 2, 3, 2, 4, 2, 5, 6, 5, 7, 5, 2, 0 } lca(3,7) = 2 lca(0,7) = 0

The Restricted Reduction So far we’ve shown how to reduce the LCA problem to the range minima problem. This next step shows how to achieve reduction to the restricted range minima problem. Denote level(u) as the number of edges in the unique path from the root to node u in T. If L = { l1, l2, … , lz } then we build the following list : L’={level(l1),level(l2),…level(lz)}.

We use L’ in the same manner we used L in the previous reduction scheme. This works because in every interval I = [u,v] in L, lca(u,v) is the lowest node in I for the same reasons mentioned earlier. The difference between two adjacent elements in L’ is exactly one. This completes the reduction to the restricted range minima problem.

The reduction complexity. Denote n as the number of nodes in T. Depth-first traversal can be done in O( n ) space and time complexity. L is of size O( n ) and thus it’s creation and initialization can be done in O( n ) space and time complexity. To find lca(u,v) we need the first occurrence of u and v in L. This could be stored in a table of size O( n ). Thus the creation and initialization of this table can be done in O( n ) space and time complexity. The total space and time complexity of the reduction is then O( n ).

The Range Minima Problem The Range Minima problem is the problem of finding the smallest number in an interval of a fixed list of numbers. The Restricted Range Minima problem is an instance of the Range Minima problem where the difference between two successive numbers is exactly one.

More Formally The Restricted Range Minima problem is stated formally in the following: Given a list L = { l1 , l2 , … , ln } of n real numbers, where for each i = 1… n-1 holds that | li - li+1 | = 1 preprocess the list so that for any interval [ li , li+1 , … , lj ] ,1  i < j  n, the minimum over the interval can be retrieved in constant time.

How to solve RRM? I added the article explaining how to solve RRM in O(n) time and space. Somewhat challenging..

Does it work the other way around? If LCA had a good solution, would it solve (restricted) Range Maxima? In other words, Can we reduce RM to LCA? Surprisingly easy.

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

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