1 / 16

Constant-Time LCA Retrieval

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 .

Download Presentation

Constant-Time LCA Retrieval

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 ancestorof a node v if u is on the unique pathfrom theroot to v. • In a rooted tree T, the Lowest CommonAncestor (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… 1 • Node 3 is the LCA of nodes 4 and 6. • Node 1 is the LCA of node 2 and 5. 2 3 4 5 6

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

  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 logn bits can be done in constant time. • We assume that finding the left-most bit or the right-most bit of a logn 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 logn 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 logn-k-1bits 0.

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

  11. 1000 Path numbers can easily be assigned in a simple O(n) in-order traversal on B. 0100 1100 0010 0110 1010 1110 0001 0011 0101 0111 1001 1011 1101 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. For example… • path(u) XOR path(v) = lca(u,v) 0100 u 0010 v 0111 0 0 1 0 XOR 0 1 1 1 0 1 0 1 path(lca(u,v) = 0 1 0 0 height bit padded bits

  15. For example… • path(u’) XOR path(v’) = lca(u’,v’) 1010 u’ v’ 1001 1011 1 0 0 1 XOR 1 0 1 1 0 0 1 0 path(lca(u,v) = 1 0 1 0 height bit padded 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 logn-k0 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.

More Related