[acm-icpc] tree isomorphism

46
Tree Isomorphism 郭至軒(KuoE0[email protected] KuoE0.ch

Upload: chih-hsuan-kuo

Post on 15-May-2015

985 views

Category:

Education


4 download

TRANSCRIPT

Page 2: [ACM-ICPC] Tree Isomorphism

IsomorphismThe structures of two trees are equal.

Page 3: [ACM-ICPC] Tree Isomorphism

so abstract...

Page 4: [ACM-ICPC] Tree Isomorphism

7

6 1 2

45 3

7

1

6

2

4 5 3

4

3 1

5

72 6

isomorphism

Page 5: [ACM-ICPC] Tree Isomorphism

How to judge two rooted tree are isomorphic?

Page 6: [ACM-ICPC] Tree Isomorphism
Page 7: [ACM-ICPC] Tree Isomorphism

If the trees are isomorphic, all their sub-trees are also isomorphic.

Page 8: [ACM-ICPC] Tree Isomorphism

If the trees are isomorphic, all their sub-trees are also isomorphic.

Page 9: [ACM-ICPC] Tree Isomorphism

Hash the tree

Page 10: [ACM-ICPC] Tree Isomorphism

Hash all sub-treerecursively

Page 11: [ACM-ICPC] Tree Isomorphism

191 191

191

191 initial value = 191single vertex

Page 12: [ACM-ICPC] Tree Isomorphism

29247

29247191 191

191

191

two-level sub-treechild = (191)

(191 × 701 xor 191) mod 34943 = 29247

Page 13: [ACM-ICPC] Tree Isomorphism

33360 29247

29247191 191

191

191

three-level sub-treechild = (191,29247,191)

child = (191,191,29247)sort

(((((191 × 701 xor 191) mod 34943) × 701 xor 191) mod 34943) × 701 xor 29247) mod 34943 = 33360

Page 14: [ACM-ICPC] Tree Isomorphism

4687

33360 29247

29247191 191

191

191

the total treechild = (33360,29247)

child = (29247,33360)sort

(((191 × 701 xor 29247) mod 34943) × 701 xor 33360) mod 34943 = 4687

Page 15: [ACM-ICPC] Tree Isomorphism

hash value of the tree is 4687

Page 16: [ACM-ICPC] Tree Isomorphism

191191

191

191initial value = 191single vertex

Page 17: [ACM-ICPC] Tree Isomorphism

191

29247

29247191

191

191

two-level sub-treechild = (191)

(191 × 701 xor 191) mod 34943 = 29247

Page 18: [ACM-ICPC] Tree Isomorphism

191

3336029247

29247191

191

191

three-level sub-treechild = (191,191,29247)

child = (191,191,29247)

(((((191 × 701 xor 191) mod 34943) × 701 xor 191) mod 34943) × 701 xor 29247) mod 34943 = 33360

sort

Page 19: [ACM-ICPC] Tree Isomorphism

191

4687

3336029247

29247191

191

191

the total treechild = (33360,29247)

child = (29247,33360)sort

(((191 × 701 xor 29247) mod 34943) × 701 xor 33360) mod 34943 = 4687

Page 20: [ACM-ICPC] Tree Isomorphism

hash value of the tree is 4687

Page 21: [ACM-ICPC] Tree Isomorphism

Page 22: [ACM-ICPC] Tree Isomorphism

Algorithm

HASH_TREE(T):

1. hash all sub-trees2. sort hash value of sub-trees (unique)3. calculate hash value (any hash function)

Page 23: [ACM-ICPC] Tree Isomorphism

Time Complexity

O(Nlog2N)

number of verticesheight of tree

Page 24: [ACM-ICPC] Tree Isomorphism

int hash( TREE &now, int root ) { int value = INIT; vector< int > sub; //get all hash value of subtree for ( int i = 0; i < now[ root ].size(); ++i ) sub.push_back( hash( now, now[ root ][ i ] ) ); //sort them to keep unique order sort( sub.begin(), sub.end() ); //hash this this tree for ( int i = 0; i < sub.size(); ++i ) value = ( ( value * P1 ) ^ sub[ i ] ) % P2; return value % P2;}

Source Code

Page 25: [ACM-ICPC] Tree Isomorphism

Representation of Tree

Let the height of left child less than the right one.

Page 26: [ACM-ICPC] Tree Isomorphism

Representation of Tree

Let the height of left child less than the right one.

Page 27: [ACM-ICPC] Tree Isomorphism

4

3 2

121 1

1

Page 28: [ACM-ICPC] Tree Isomorphism

3

21 1

1

4

3 2

121 1

1

4

2

1

Page 29: [ACM-ICPC] Tree Isomorphism

2

1

3

21 1

1

4

3 2

121 1

1

4

2

1

3

1 1

4

2

1

Page 30: [ACM-ICPC] Tree Isomorphism

AlgorithmSORT_CHILD(T):

1. sort all sub-trees2. compare the height3. if height is equal, compare child

recursively4. put the lower at left and the higher at

right

Page 31: [ACM-ICPC] Tree Isomorphism

How about unrooted tree?

Page 32: [ACM-ICPC] Tree Isomorphism

find a root

Page 33: [ACM-ICPC] Tree Isomorphism

eliminate leaves

Page 34: [ACM-ICPC] Tree Isomorphism

eliminate leaves

Page 35: [ACM-ICPC] Tree Isomorphism

eliminate leaves

Page 36: [ACM-ICPC] Tree Isomorphism

eliminate leaves

Page 37: [ACM-ICPC] Tree Isomorphism

try each root

Page 38: [ACM-ICPC] Tree Isomorphism

rebuild the tree

Page 39: [ACM-ICPC] Tree Isomorphism

rebuild the tree

Page 40: [ACM-ICPC] Tree Isomorphism

rebuild the tree

Page 41: [ACM-ICPC] Tree Isomorphism

rebuild the tree

Page 42: [ACM-ICPC] Tree Isomorphism

rebuild the tree

Page 43: [ACM-ICPC] Tree Isomorphism

rebuild the tree

Page 44: [ACM-ICPC] Tree Isomorphism

apply the isomorphism detection

Page 45: [ACM-ICPC] Tree Isomorphism

POJ 1635 - Subway tree systems

Practice Now

Page 46: [ACM-ICPC] Tree Isomorphism

Thank You for Your Listening.