Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Constant-Time LCA Retrieval Presentation by Danny Hermelin, String Matching Algorithms Seminar, Haifa University."— Presentation transcript:

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

2 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.

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

4 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

5 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.

6  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.

7 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.

8 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.

9 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.

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

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

12 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.

13 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 ).

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

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

16 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.


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

Similar presentations


Ads by Google