Download presentation

Presentation is loading. Please wait.

Published byAllison Fisher Modified over 4 years ago

1
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:338--55, May 1984 Speaker : Chan Shuo Wu ( ) Dept. of CSIE National Chi Nan University

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

3
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
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
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
6 A Fast Algorithm for Complete Binary Trees

7
7 A Complete Binary Tree B

8
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. 8 4 2 13 1000 0011 0100 0010 0001 6 57 0111 0110 0101 12 10 911 1011 1100 1010 1001 14 1315 1111 1110 1101

9
9 Set to 0 1001 1011 ---- 0010 00 0 1001 ---- 1011 ---- 1010

10
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
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 123456789101112131415... 000100100011010001010110011110001001101010111100110111101111... 122333344444444 11111110 1100 1000... 124816

12
12 1001 XOR 1011 ---- 0010 1001 XOR 1011 ---- 0010 OR 1001 ---- 1011 AND 1110 ---- 1010...1000 1100 1110 1111...444444443333221 111111101101110010111010100110000111011001010100001100100001...151413121110987654321

13
13 Mapping General Tree to Complete Binary Tree

14
14 T B

15
15 Preprocessing of T

16
16 1 2 34 5 678 910 0001 0010 00110100 0101 01100111 1000 10011010 Depth-First Numbering

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

18
18 1 2 34 5 678 910 0001 0010 00110100 0101 01100111 1000 10011010 v I ( v ) a run

19
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. 0010 0100 1000 1100

20
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

21
21...010100...000110...010000 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 = 010100 000110 010000

22
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
23 Preprocessing of T ( cont. )

24
24 1 2 34 5 678 910 1000 0100 1100 0011 1101 0110 1010 0111 1001 1010 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
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.123456789101000110011011100100010101001100010011010

26
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. 1 2 34 5 678 910 0001 0010 00110100 0101 01100111 1000 10011010

27
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. 1 2 34 5 678 910 0001 0010 00110100 0101 01100111 1000 10011010

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

31
31 0011 XOR 0110 ---- 0101 OR 0011 ---- 0111 AND 1100 ---- 0100

32
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

33
33123456789101000110011011100100010101001100010011010 A 1101 AND 1100 ---- 1100 1010 AND 1100 ---- 1000 1100 AND 1000 ---- 1000 h ( I ( z )) = h (1000) = 4

34
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

35
35123456789101000110011011100100010101001100010011010 A h ( I ( z )) = 4 1101 AND 0111 ---- 0101 OR 0011 ---- 0111 AND 1100 ---- 0100 = NOT 1000

36
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

37
37 1 2 34 5 678 910 0001 0010 00110100 0101 01100111 1000 10011010

38
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
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
40

Similar presentations

Presentation is loading. Please wait....

OK

Chapter 1 The Study of Body Function Image PowerPoint

Chapter 1 The Study of Body Function Image PowerPoint

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google