Upload
allison-fisher
View
223
Download
3
Tags:
Embed Size (px)
Citation preview
Dov Harel and Robert Endre Tarjan
Fast Algorithms for Finding NeFast Algorithms for Finding Nearest Common Ancestorsarest Common AncestorsSIAM J. COMPUT. Vol. 13:338--55, May 1984
Speaker : Chan Shuo Wu ( 吳展碩 )
Dept. of CSIENational Chi Nan University
22
SourceSource Dov Harel and Robert Endre TarjanDov Harel and Robert Endre Tarjan. . Fast Fast
Algorithm for Finding Nearest Common AncestorsAlgorithm for Finding Nearest Common Ancestors. . SIAM J. Comput. Vol. 13:338--55, May 1984.SIAM J. Comput. Vol. 13:338--55, May 1984.
33
IntroductionIntroduction llowest owest ccommon ommon aancestorncestor
Denote the nearest common ancestor of vertices Denote the nearest common ancestor of vertices xx and and yy by by ncanca((xx, , yy).).
44
This paper presents an algorithm for NCA that runs on This paper presents an algorithm for NCA that runs on a random access machine and uses a random access machine and uses OO((nn) preprocessin) preprocessing time, g time, OO(1) time per query(1) time per query, and , and OO((nn) space.) space.
55
IdeaIdea
PreprocessingPreprocessingFor complete binary treesFor complete binary treesFor general rooted treesFor general rooted trees
The difficulty is on how to perform NCA query in constant The difficulty is on how to perform NCA query in constant time.time.
66
A Fast Algorithm for A Fast Algorithm for Complete Binary TreesComplete Binary Trees
77
A Complete Binary Tree A Complete Binary Tree BB
88
Path NumberPath Number Each node Each node vv of of BB is assigned a number that is assigned a number that
encodes the unique path from the root to encodes the unique path from the root to vv..
8
4
2
1 3
1000
0011
0100
0010
0001
6
5 7
0111
0110
0101
12
10
9 11
1011
1100
1010
1001
14
13 15
1111
1110
1101
99
Set to 0
10011011----00100001001----1011----1010
1010
Computing Computing ncanca((xx, , yy)) Compute XOR(Compute XOR(xx, , yy)) Find the position of the left-most 1-bit in XOR(Find the position of the left-most 1-bit in XOR(xx, , yy). Let ). Let
it be it be k.k. Let Let tt be be xx (or (or yy ). Set the bit in position ). Set the bit in position kk of of tt to be 1 an to be 1 an
d those bits right to d those bits right to kk to be 0. to be 0. Set Set ncanca((xx, , yy)=)=tt..
1111
Preprocessing of Preprocessing of BB Build an Build an OO((nn)-size table in )-size table in OO((nn) time) time
With this table, the following operations on binary nuWith this table, the following operations on binary numbers can be done in constant time:mbers can be done in constant time: find the position find the position kk of the left-most 1-bit of the left-most 1-bit set bits to the right of position set bits to the right of position k k to zero to zero
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 ...1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 ...
1111 1110 1110 1100 1100 1100 1100 1000 1000 1000 1000 1000 1000 1000 1000 ...1 2 4 8 16
1212
1001XOR1011----0010
1001XOR1011----0010OR
1001----1011AND1110----1010
...100010001000100010001000100010001100110011001100111011101111...444444443333221...111111101101110010111010100110000111011001010100001100100001
...151413121110987654321
1313
Mapping General Tree to Mapping General Tree to Complete Binary TreeComplete Binary Tree
1414
TT
BB
1515
Preprocessing of Preprocessing of TT
1616
1
2
3 4
5
6 7 8
9 10
0001
0010
0011 0100
0101
0110 0111
1000
1001 1010
Depth-First NumberingDepth-First Numbering
DefinitionDefinition For any number For any number vv, , heightheight of of vv, , hh((vv) denotes the position of ) denotes the position of the least-significant 1-bit in the binary representation of the least-significant 1-bit in the binary representation of vv..
Definition Definition For a node For a node vv of of TT, let , let II((vv) be a node ) be a node ww in in TT such that such that hh((ww) is ) is maximum over all nodes in the subtree of maximum over all nodes in the subtree of vv..
1818
1
2
3 4
5
6 7 8
9 10
0001
0010
0011 0100
0101
0110 0111
1000
1001 1010
vv I I ((vv))
a runa run
1919
LemmaLemma For any node For any node v v in in TT, there is a unique node , there is a unique node ww in t in the subtree of he subtree of vv such that such that hh((ww) is maximum over all nodes ) is maximum over all nodes in in vv's subtree.'s subtree.
For any node For any node v v in in TT, node , node II((vv) is the deepest node in the run containin) is the deepest node in the run containing node g node vv..
The function The function vv II((vv) is well defined.) is well defined.
0010
0100:
1000:
1100
2020
LemmaLemma If If zz is an ancestor of is an ancestor of xx in in TT then then II((zz) is an ancestor ) is an ancestor of of II((xx) in ) in BB..
zz
xx
II((xx)) II((zz))
TT
II((zz))
II((xx))
BB
2121
...010...010110000
...0001...00011100
...0...01100000000
zz
xx
II((xx)) II((zz))
TT
LemmaLemma If If zz is an ancestor of is an ancestor of xx in in TT then then II((zz) is an ancestor ) is an ancestor of of II((xx) in ) in BB..
ProofProof
NN
II((zz) =) =II((xx) =) =NN = =
010100000110
010000
2222
LemmaLemma If If zz is an ancestor of is an ancestor of xx in in TT then then II((zz) is an ancestor ) is an ancestor of of II((xx) in ) in BB..
II((zz))
II((xx))
BB
zz
xx
II((xx)) II((zz))
TTyy
II((yy))
II((yy))
2323
Preprocessing of Preprocessing of T T ((cont.cont.))
2424
1
2
3 4
5
6 7 8
9 10
10001000
01001100
00111101
01101010
01111001
10011001
10101010
For each node For each node v v inin T T, create an , create an OO(log (log nn)-bit number )-bit number AAvv. Bit . Bit AAvv((ii) is set to 1 if and only if node ) is set to 1 if and only if node vv has some ancestor in has some ancestor in TT that maps to height that maps to height i i in in BB..
2525
For each node For each node v v inin T T, create an , create an OO(log (log nn) bit number ) bit number AAvv. Bit . Bit AAvv((ii) is set to 1 if and only if node ) is set to 1 if and only if node vv has some ancestor in has some ancestor in TT that maps to height that maps to height i i in in BB..
11 22 33 44 55 66 77 88 99 1010
10001000 11001100 11011101 11001100 10001000 10101010 10011001 10001000 10011001 10101010
2626
For each node inFor each node in T T, set a pointer to its parent , set a pointer to its parent node in node in TT..
1
2
3 4
5
6 7 8
9 10
0001
0010
0011 0100
0101
0110 0111
1000
1001 1010
2727
For each node For each node vv, set a pointer to the , set a pointer to the rootroot of of the the run run containing node containing node vv..
1
2
3 4
5
6 7 8
9 10
0001
0010
0011 0100
0101
0110 0111
1000
1001 1010
2828
II((zz))
II((xx))
BB
zz
xx
II((xx)) II((zz))
TTyy
II((yy))
II((yy))
Constant-Time NCA Constant-Time NCA RetrievalRetrieval
2929
Constant-Time Constant-Time ncanca Retrieval Retrieval
xx
II((xx))II((zz))
TT
yy
II((yy))
x’x’
y’y’
3030
1.1. Find the lowest common ancestor Find the lowest common ancestor bb in in BB of nodes of nodes II((xx) and ) and II((yy).).2.2. Find the smallest position Find the smallest position jj greater than or equal to greater than or equal to hh((bb) such that both ) such that both
numbers numbers AAxx and and AAyy have 1-bits in position have 1-bits in position jj. . jj is then is then hh((II((zz)).)).3.3. Find node Find node xx’, the closet node to ’, the closet node to xx on the same run as on the same run as zz as follow: as follow:
Find the positionFind the position l l of the right-most 1-bit in of the right-most 1-bit in AAxx.. If If l l = = jj, then set , then set xx’ = ’ = xx ( (xx and and zz are on the same run in are on the same run in TT) and go to step 4. Othe) and go to step 4. Othe
rwise (when rwise (when ll < < jj)) Find the position Find the position kk of the left-most 1-bit in of the left-most 1-bit in AAxx that is to the right of position that is to the right of position jj. F. F
rom the number consisting of the bits of rom the number consisting of the bits of II((xx) to the left of position ) to the left of position kk, followed , followed by a 1-bit in position by a 1-bit in position kk, followed by all zeros. (That number will be , followed by all zeros. (That number will be II((ww), even t), even though we don’t yet know hough we don’t yet know ww.) Look up node .) Look up node LL((II((ww)), which must be node )), which must be node ww. S. Set node et node xx’ to be the parent of node ’ to be the parent of node ww in in TT..
4.4. Find node Find node yy’, the closest node to ’, the closest node to yy on the same run as on the same run as zz, by the same a, by the same approach as in step 3.pproach as in step 3.
5.5. If If xx’ < ’ < yy’ then set ’ then set zz to to xx’, else set ’, else set zz to to yy’.’.
3131
0011XOR0110----0101OR
0011----0111AND1100----0100
3232
II((zz))
II((xx))
BB
zz
xx
II((xx)) II((zz))
TTyy
II((yy))
II((yy))
LemmaLemma Let Let bb be the NCA of be the NCA of II((xx) and ) and II((yy) in ) in BB. Let . Let jj be the smallest pos be the smallest position ition hh((bb) such that both ) such that both AAxx and and AAyy have 1-bits in position have 1-bits in position jj. Then no. Then node de II((zz) is at height) is at height j j in in BB..
b
3333
11 22 33 44 55 66 77 88 99 1010
10001000 11001100 11011101 11001100 10001000 10101010 10011001 10001000 10011001 10101010A
1101AND1100----1100
1010AND1100----1000
1100AND1000----1000
hh((I I ((zz)) = )) = hh(1000) = 4(1000) = 4
3434
Constant-Time NCA Constant-Time NCA RetrievalRetrieval
xx
II((xx))II((zz))
TT
yy
II((yy))
x’x’
y’y’
3535
11 22 33 44 55 66 77 88 99 1010
10001000 11001100 11011101 11001100 10001000 10101010 10011001 10001000 10011001 10101010A
hh((I I ((zz)) = 4)) = 4
1101AND0111 ----0101OR
0011----0111AND1100----0100
= NOT 1000
3636
Constant-Time NCA Constant-Time NCA RetrievalRetrieval
xx
II((xx))II((zz))
TT
yy
II((yy))
x’x’
y’y’
3737
1
2
3 4
5
6 7 8
9 10
0001
0010
0011 0100
0101
0110 0111
1000
1001 1010
3838
Constant-Time NCA Constant-Time NCA RetrievalRetrieval
xx
II((xx))II((zz))
TT
yy
II((yy))
x’x’
y’y’
3939
Constant-Time NCA Constant-Time NCA RetrievalRetrieval
xx
II((xx))II((zz))
TT
yy
II((yy))
y’y’
zz
4040