Constant-Time LCA Retrieval Presentation by Danny Hermelin, String Matching Algorithms Seminar, Haifa University.

Slides:



Advertisements
Similar presentations
Fast Algorithms for Finding Nearest Common Ancestors Dov Harel and Robert Endre Tarjan Fast Algorithms for Finding Nearest Common Ancestors SIAM J. COMPUT.
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Fast Algorithms For Hierarchical Range Histogram Constructions
QuickSort Average Case Analysis An Incompressibility Approach Brendan Lucier August 2, 2005.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Constant-Time LCA Retrieval
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
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.
Lecture 5: Linear Time Sorting Shang-Hua Teng. Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of 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):
Priority queues CS310 – Data Structures Professor Roch Weiss, Chapter 6.9, 21 All figures marked with a chapter and section number are copyrighted © 2006.
Suffix trees and suffix arrays presentation by Haim Kaplan.
Lecture 5: Master Theorem and Linear Time Sorting
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
CSIT 402 Data Structures II
The LCA Problem Revisited
Succinct Data Structures Ian Munro University of Waterloo Joint work with David Benoit, Andrej Brodnik, D, Clark, F. Fich, M. He, J. Horton, A. López-Ortiz,
Complexity 20-1 Complexity Andrei Bulatov Parallel Arithmetic.
Compiled by: Dr. Mohammad Omar Alhawarat
A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.
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.
AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
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 }
Sets of Digital Data CSCI 2720 Fall 2005 Kraemer.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Foundation of Computing Systems
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tries 4/16/2018 8:59 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Succinct Data Structures
CSCE 210 Data Structures and Algorithms
Succinct Data Structures
Tries 07/28/16 11:04 Text Compression
Tries 5/27/2018 3:08 AM Tries Tries.
Ariel Rosenfeld Bar-Ilan Uni.
Tries 9/14/ :13 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Binary Trees, Binary Search Trees
Math 221 Huffman Codes.
Binary Trees.
Trees.
CE 221 Data Structures and Algorithms
Binary Trees, Binary Search Trees
Advanced Implementation of Tables
CE 221 Data Structures and Algorithms
Binary Tree Properties & Representation
Switching Lemmas and Proof Complexity
Binary Trees, Binary Search Trees
Presentation transcript:

Constant-Time LCA Retrieval Presentation by Danny Hermelin, String Matching Algorithms Seminar, Haifa University.

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. Node 1 is the LCA of node 2 and

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. There exists a preprocessing algorithm that requires no more then linear time and space complexity. The LCA Problem

The assumed machine model We make the following two assumptions on our computational model. Let n denote the size of our input in unary representation :  All arithmetic, comparative and logical operations on numbers whose binary representation is of size no more then log n bits can be done in constant time.  We assume that finding the left-most bit or the right-most bit of a log n sized number can be done in constant time.

 The first assumption is a very reasonable straightforward assumption considering most machines on the market today.  The second can be achieved assuming constant time logical word operations and constant time shift/rotate word operations.  These assumptions helps our discussion focus on the more interesting parts of the algorithm solving the LCA problem.

The Simple case: Complete Binary Tree Our discussion begins with a particularly simple instance of the LCA problem, LCA queries on complete binary trees. We will use our knowledge of solving the LCA problem on complete binary trees. It can be expanded to solve the LCA problem on any arbitrary rooted tree T.

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 log n 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 log n - k- 1 bits 0.

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

Path numbers can easily be assigned in a simple O( n ) in-order traversal on B

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 then 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 ) = u v lca( u, v ) XOR path(lca(u,v) = 0100 height bitpadded bits

For example… path( u’ ) XOR path( v’ ) = u’v’ lca( u’, v’ ) XOR path(lca(u,v) = 1010 height bitpadded 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 log n - 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.