fast algorithms for finding nearest common ancestors dov harel and robert endre tarjan fast...

40
Dov Harel and Robert Endre Tarjan Fast Algorithms for Findi Fast Algorithms for Findi ng Nearest Common Ancesto ng Nearest Common Ancesto rs rs SIAM J. COMPUT. Vol. 13:338--55, May 1984 Speaker : Chan Shuo Wu ( 吳吳吳 ) Dept. of CSIE National Chi Nan University

Upload: allison-fisher

Post on 27-Mar-2015

225 views

Category:

Documents


3 download

TRANSCRIPT

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

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

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

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.

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

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

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

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.

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

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.

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

66

A Fast Algorithm for A Fast Algorithm for Complete Binary TreesComplete Binary Trees

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

77

A Complete Binary Tree A Complete Binary Tree BB

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

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

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

99

Set to 0

10011011----00100001001----1011----1010

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

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

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

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

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

1212

1001XOR1011----0010

1001XOR1011----0010OR

1001----1011AND1110----1010

...100010001000100010001000100010001100110011001100111011101111...444444443333221...111111101101110010111010100110000111011001010100001100100001

...151413121110987654321

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

1313

Mapping General Tree to Mapping General Tree to Complete Binary TreeComplete Binary Tree

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

1414

TT

BB

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

1515

Preprocessing of Preprocessing of TT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2323

Preprocessing of Preprocessing of T T ((cont.cont.))

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

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

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

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

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

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

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

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

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

2828

II((zz))

II((xx))

BB

zz

xx

II((xx)) II((zz))

TTyy

II((yy))

II((yy))

Constant-Time NCA Constant-Time NCA RetrievalRetrieval

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

2929

Constant-Time Constant-Time ncanca Retrieval Retrieval

xx

II((xx))II((zz))

TT

yy

II((yy))

x’x’

y’y’

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

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

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

3131

0011XOR0110----0101OR

0011----0111AND1100----0100

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

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

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

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

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

3434

Constant-Time NCA Constant-Time NCA RetrievalRetrieval

xx

II((xx))II((zz))

TT

yy

II((yy))

x’x’

y’y’

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

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

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

3636

Constant-Time NCA Constant-Time NCA RetrievalRetrieval

xx

II((xx))II((zz))

TT

yy

II((yy))

x’x’

y’y’

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

3737

1

2

3 4

5

6 7 8

9 10

0001

0010

0011 0100

0101

0110 0111

1000

1001 1010

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

3838

Constant-Time NCA Constant-Time NCA RetrievalRetrieval

xx

II((xx))II((zz))

TT

yy

II((yy))

x’x’

y’y’

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

3939

Constant-Time NCA Constant-Time NCA RetrievalRetrieval

xx

II((xx))II((zz))

TT

yy

II((yy))

y’y’

zz

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

4040