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

Slides:



Advertisements
Similar presentations
Mathematical Preliminaries
Advertisements

- 1 - Using an SMT Solver and Craig Interpolation to Detect and Remove Redundant Linear Constraints in Representations of Non-Convex Polyhedra Christoph.
1
1 Vorlesung Informatik 2 Algorithmen und Datenstrukturen (Parallel Algorithms) Robin Pomplun.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Principles & Applications
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 3 CPUs.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
Speaker: C. C. Lin Adviser: R. C. T. Lee
On-line Construction of Suffix Trees Chairman : Prof. R.C.T. Lee Speaker : C. S. Wu ( ) June 10, 2004 Dept. of CSIE National Chi Nan University.
UNITED NATIONS Shipment Details Report – January 2006.
David Burdett May 11, 2004 Package Binding for WS CDL.
and 6.855J Spanning Tree Algorithms. 2 The Greedy Algorithm in Action
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Create an Application Title 1A - Adult Chapter 3.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 10 second questions
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Break Time Remaining 10:00.
Monika Gope Lecturer IICT, KUET
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
Bright Futures Guidelines Priorities and Screening Tables
Discrete Mathematical Structures: Theory and Applications
Bellwork Do the following problem on a ½ sheet of paper and turn in.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
VOORBLAD.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
Adding Up In Chunks.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Artificial Intelligence
Binary Lesson 3 Hexadecimal. Counting to 15 Base Base Base 16 Base Base Base 16 Two Ten (Hex) Two Ten (Hex)
Binary Lesson 3 Hexadecimal. Counting to 15 Base Base Base 16 Base Base Base 16 Two Ten (Hex) Two Ten (Hex)
25 seconds left…...
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Analyzing Genes and Genomes
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Converting a Fraction to %
CSE Lecture 17 – Balanced trees
Clock will move after 1 minute
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Immunobiology: The Immune System in Health & Disease Sixth Edition
Physics for Scientists & Engineers, 3rd Edition
Energy Generation in Mitochondria and Chlorplasts
Select a time to count down from the clock above
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
and M-ary Quadrature Amplitude Modulation (M-QAM)
1/15/2015 Slide # 1 Binary, Octal and Hex Numbers Copyright Thaddeus Konar Introduction to Binary, Octal and Hexadecimal Numbers Thaddeus Konar.
Presentation transcript:

Fast Algorithms for Finding Nearest Common Ancestors Dov Harel and Robert Endre Tarjan Fast Algorithms for Finding Nearest Common Ancestors SIAM J. COMPUT. Vol. 13: , May 1984 Speaker : Chan Shuo Wu ( ) Dept. of CSIE National Chi Nan University

2 Source Dov Harel and Robert Endre Tarjan. Fast Algorithm for Finding Nearest Common Ancestors. SIAM J. Comput. Vol. 13: , May Dov Harel and Robert Endre Tarjan. Fast Algorithm for Finding Nearest Common Ancestors. SIAM J. Comput. Vol. 13: , May 1984.

3 Introduction l owest c ommon a ncestor l owest c ommon a ncestor Denote the nearest common ancestor of vertices x and y by nca ( x, y ).

4 This paper presents an algorithm for NCA that runs on a random access machine and uses O ( n ) preprocessing time, O (1) time per query, and O ( n ) space. This paper presents an algorithm for NCA that runs on a random access machine and uses O ( n ) preprocessing time, O (1) time per query, and O ( n ) space.

5 Idea Preprocessing Preprocessing For complete binary trees For complete binary trees For general rooted trees For general rooted trees The difficulty is on how to perform NCA query in constant time.

6 A Fast Algorithm for Complete Binary Trees

7 A Complete Binary Tree B

8 Path Number Each node v of B is assigned a number that encodes the unique path from the root to v. Each node v of B is assigned a number that encodes the unique path from the root to v

9 Set to

10 Computing nca ( x, y ) Compute XOR( x, y ) Compute XOR( x, y ) Find the position of the left-most 1-bit in XOR( x, y ). Let it be k. Find the position of the left-most 1-bit in XOR( x, y ). Let it be k. Let t be x (or y ). Set the bit in position k of t to be 1 and those bits right to k to be 0. Let t be x (or y ). Set the bit in position k of t to be 1 and those bits right to k to be 0. Set nca ( x, y )= t. Set nca ( x, y )= t.

11 Preprocessing of B Build an O ( n )-size table in O ( n ) time Build an O ( n )-size table in O ( n ) time With this table, the following operations on binary numbers can be done in constant time: With this table, the following operations on binary numbers can be done in constant time: find the position k of the left-most 1-bit find the position k of the left-most 1-bit set bits to the right of position k to zero set bits to the right of position k to zero

XOR XOR OR AND

13 Mapping General Tree to Complete Binary Tree

14 T B

15 Preprocessing of T

Depth-First Numbering

Definition For any number v, height of v, h ( v ) denotes the position of the least-significant 1-bit in the binary representation of v. Definition For any number v, height of v, h ( v ) denotes the position of the least-significant 1-bit in the binary representation of v. Definition For a node v of T, let I ( v ) be a node w in T such that h ( w ) is maximum over all nodes in the subtree of v. Definition For a node v of T, let I ( v ) be a node w in T such that h ( w ) is maximum over all nodes in the subtree of v.

v I ( v ) a run

19 Lemma For any node v in T, there is a unique node w in the subtree of v such that h ( w ) is maximum over all nodes in v 's subtree. Lemma For any node v in T, there is a unique node w in the subtree of v such that h ( w ) is maximum over all nodes in v 's subtree. For any node v in T, node I ( v ) is the deepest node in the run containing node v. For any node v in T, node I ( v ) is the deepest node in the run containing node v. The function v I ( v ) is well defined. The function v I ( v ) is well defined

20 Lemma If z is an ancestor of x in T then I ( z ) is an ancestor of I ( x ) in B. Lemma If z is an ancestor of x in T then I ( z ) is an ancestor of I ( x ) in B. z x I(x)I(x)I(x)I(x) I(z)I(z)I(z)I(z) T I(z)I(z)I(z)I(z) I(x)I(x)I(x)I(x) B

z x I(x)I(x)I(x)I(x) I(z)I(z)I(z)I(z) T Lemma If z is an ancestor of x in T then I ( z ) is an ancestor of I ( x ) in B. Lemma If z is an ancestor of x in T then I ( z ) is an ancestor of I ( x ) in B. Proof Proof N I ( z ) = I ( x ) = N =

22 Lemma If z is an ancestor of x in T then I ( z ) is an ancestor of I ( x ) in B. Lemma If z is an ancestor of x in T then I ( z ) is an ancestor of I ( x ) in B. I(z)I(z)I(z)I(z) I(x)I(x)I(x)I(x) B z x I(x)I(x)I(x)I(x) I(z)I(z)I(z)I(z) T y I(y)I(y)I(y)I(y) I(y)I(y)I(y)I(y)

23 Preprocessing of T ( cont. )

For each node v in T, create an O (log n )-bit number A v. Bit A v ( i ) is set to 1 if and only if node v has some ancestor in T that maps to height i in B. For each node v in T, create an O (log n )-bit number A v. Bit A v ( i ) is set to 1 if and only if node v has some ancestor in T that maps to height i in B.

25 For each node v in T, create an O (log n ) bit number A v. Bit A v ( i ) is set to 1 if and only if node v has some ancestor in T that maps to height i in B. For each node v in T, create an O (log n ) bit number A v. Bit A v ( i ) is set to 1 if and only if node v has some ancestor in T that maps to height i in B

26 For each node in T, set a pointer to its parent node in T. For each node in T, set a pointer to its parent node in T

27 For each node v, set a pointer to the root of the run containing node v. For each node v, set a pointer to the root of the run containing node v

28 I(z)I(z)I(z)I(z) I(x)I(x)I(x)I(x) B z x I(x)I(x)I(x)I(x) I(z)I(z)I(z)I(z) T y I(y)I(y)I(y)I(y) I(y)I(y)I(y)I(y) Constant-Time NCA Retrieval

29 Constant-Time nca Retrieval x I(x)I(x)I(x)I(x) I(z)I(z)I(z)I(z) T y I(y)I(y)I(y)I(y) x y

30 1.Find the lowest common ancestor b in B of nodes I ( x ) and I ( y ). 2.Find the smallest position j greater than or equal to h ( b ) such that both numbers A x and A y have 1-bits in position j. j is then h ( I ( z )). 3.Find node x, the closet node to x on the same run as z as follow: Find the position l of the right-most 1-bit in A x. Find the position l of the right-most 1-bit in A x. If l = j, then set x = x ( x and z are on the same run in T ) and go to step 4. Otherwise (when l < j ) If l = j, then set x = x ( x and z are on the same run in T ) and go to step 4. Otherwise (when l < j ) Find the position k of the left-most 1-bit in A x that is to the right of position j. From the number consisting of the bits of I ( x ) to the left of position k, followed by a 1-bit in position k, followed by all zeros. (That number will be I ( w ), even though we dont yet know w.) Look up node L ( I ( w )), which must be node w. Set node x to be the parent of node w in T. Find the position k of the left-most 1-bit in A x that is to the right of position j. From the number consisting of the bits of I ( x ) to the left of position k, followed by a 1-bit in position k, followed by all zeros. (That number will be I ( w ), even though we dont yet know w.) Look up node L ( I ( w )), which must be node w. Set node x to be the parent of node w in T. 4.Find node y, the closest node to y on the same run as z, by the same approach as in step 3. 5.If x < y then set z to x, else set z to y.

XOR OR AND

32 I(z)I(z)I(z)I(z) I(x)I(x)I(x)I(x) B z x I(x)I(x)I(x)I(x) I(z)I(z)I(z)I(z) T y I(y)I(y)I(y)I(y) I(y)I(y)I(y)I(y) Lemma Let b be the NCA of I ( x ) and I ( y ) in B. Let j be the smallest position h ( b ) such that both A x and A y have 1-bits in position j. Then node I ( z ) is at height j in B. Lemma Let b be the NCA of I ( x ) and I ( y ) in B. Let j be the smallest position h ( b ) such that both A x and A y have 1-bits in position j. Then node I ( z ) is at height j in B. b

A 1101 AND AND AND h ( I ( z )) = h (1000) = 4

34 Constant-Time NCA Retrieval x I(x)I(x)I(x)I(x) I(z)I(z)I(z)I(z) T y I(y)I(y)I(y)I(y) x y

A h ( I ( z )) = AND OR AND = NOT 1000

36 Constant-Time NCA Retrieval x I(x)I(x)I(x)I(x) I(z)I(z)I(z)I(z) T y I(y)I(y)I(y)I(y) x y

38 Constant-Time NCA Retrieval x I(x)I(x)I(x)I(x) I(z)I(z)I(z)I(z) T y I(y)I(y)I(y)I(y) x y

39 Constant-Time NCA Retrieval x I(x)I(x)I(x)I(x) I(z)I(z)I(z)I(z) T y I(y)I(y)I(y)I(y) y z

40