[acm-icpc] tree isomorphism

78

Click here to load reader

Upload: chih-hsuan-kuo

Post on 15-May-2015

374 views

Category:

Education


6 download

TRANSCRIPT

Page 3: [ACM-ICPC] Tree Isomorphism

IsomorphismThe structures of two trees are equal.

Page 4: [ACM-ICPC] Tree Isomorphism

so abstract...

Page 5: [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 6: [ACM-ICPC] Tree Isomorphism

How to judge two rooted tree are isomorphic?

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

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

Page 9: [ACM-ICPC] Tree Isomorphism

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

Page 10: [ACM-ICPC] Tree Isomorphism

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

Page 11: [ACM-ICPC] Tree Isomorphism

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

Page 12: [ACM-ICPC] Tree Isomorphism

Hash the tree

Page 13: [ACM-ICPC] Tree Isomorphism

Hash all sub-tree

Page 14: [ACM-ICPC] Tree Isomorphism

Hash all sub-treerecursively

Page 15: [ACM-ICPC] Tree Isomorphism

initial value = 191single vertex

Page 16: [ACM-ICPC] Tree Isomorphism

initial value = 191single vertex

Page 17: [ACM-ICPC] Tree Isomorphism

initial value = 191single vertex

191 191

191

191

Page 18: [ACM-ICPC] Tree Isomorphism

initial value = 191single vertex

191 191

191

191

define by yourself

Page 19: [ACM-ICPC] Tree Isomorphism

191 191

191

191

two-level sub-treechild = (191)

Page 20: [ACM-ICPC] Tree Isomorphism

191 191

191

191

two-level sub-treechild = (191)

Page 21: [ACM-ICPC] Tree Isomorphism

191 191

191

191

two-level sub-treechild = (191)

(191 × 701 xor 191) mod 34943 = 29247

29247

29247

Page 22: [ACM-ICPC] Tree Isomorphism

191 191

191

191

two-level sub-treechild = (191)

(191 × 701 xor 191) mod 34943 = 29247

29247

29247

define by yourself

Page 23: [ACM-ICPC] Tree Isomorphism

191 191

191

191

two-level sub-treechild = (191)

(191 × 701 xor 191) mod 34943 = 29247

29247

29247

define by yourself

Page 24: [ACM-ICPC] Tree Isomorphism

29247

29247191 191

191

191

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

Page 25: [ACM-ICPC] Tree Isomorphism

29247

29247191 191

191

191

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

Page 26: [ACM-ICPC] Tree Isomorphism

29247

29247191 191

191

191

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

child = (191,191,29247)sort

Page 27: [ACM-ICPC] Tree Isomorphism

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

33360

Page 28: [ACM-ICPC] Tree Isomorphism

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

33360

Page 29: [ACM-ICPC] Tree Isomorphism

33360 29247

29247191 191

191

191

the total treechild = (33360,29247)

Page 30: [ACM-ICPC] Tree Isomorphism

33360 29247

29247191 191

191

191

the total treechild = (33360,29247)

Page 31: [ACM-ICPC] Tree Isomorphism

33360 29247

29247191 191

191

191

the total treechild = (33360,29247)

child = (29247,33360)sort

Page 32: [ACM-ICPC] Tree Isomorphism

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

4687

Page 33: [ACM-ICPC] Tree Isomorphism

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

4687

Page 34: [ACM-ICPC] Tree Isomorphism

hash value of the tree is 4687

Page 35: [ACM-ICPC] Tree Isomorphism

initial value = 191single vertex

Page 36: [ACM-ICPC] Tree Isomorphism

initial value = 191single vertex

Page 37: [ACM-ICPC] Tree Isomorphism

initial value = 191single vertex

191191

191

191

Page 38: [ACM-ICPC] Tree Isomorphism

191191

191

191

two-level sub-treechild = (191)

Page 39: [ACM-ICPC] Tree Isomorphism

191191

191

191

two-level sub-treechild = (191)

Page 40: [ACM-ICPC] Tree Isomorphism

191191

191

191

two-level sub-treechild = (191)

(191 × 701 xor 191) mod 34943 = 29247

29247

29247

Page 41: [ACM-ICPC] Tree Isomorphism

191

29247

29247191

191

191

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

Page 42: [ACM-ICPC] Tree Isomorphism

191

29247

29247191

191

191

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

Page 43: [ACM-ICPC] Tree Isomorphism

191

29247

29247191

191

191

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

child = (191,191,29247)sort

Page 44: [ACM-ICPC] Tree Isomorphism

191

29247

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

33360

Page 45: [ACM-ICPC] Tree Isomorphism

191

3336029247

29247191

191

191

the total treechild = (33360,29247)

Page 46: [ACM-ICPC] Tree Isomorphism

191

3336029247

29247191

191

191

the total treechild = (33360,29247)

Page 47: [ACM-ICPC] Tree Isomorphism

191

3336029247

29247191

191

191

the total treechild = (33360,29247)

child = (29247,33360)sort

Page 48: [ACM-ICPC] Tree Isomorphism

191

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

4687

Page 49: [ACM-ICPC] Tree Isomorphism

hash value of the tree is 4687

Page 50: [ACM-ICPC] Tree Isomorphism
Page 51: [ACM-ICPC] Tree Isomorphism

Page 52: [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 53: [ACM-ICPC] Tree Isomorphism

Time Complexity

O(Nlog2N)N is number of verticesheight of tree

Page 54: [ACM-ICPC] Tree Isomorphism

Source Code

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;}

Page 55: [ACM-ICPC] Tree Isomorphism

Representation of Tree

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

Page 56: [ACM-ICPC] Tree Isomorphism

Representation of Tree

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

Page 57: [ACM-ICPC] Tree Isomorphism

4

3 2

121 1

1

Page 58: [ACM-ICPC] Tree Isomorphism

3

21 1

1

4

3 2

121 1

1

4

2

1

Page 59: [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 60: [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 61: [ACM-ICPC] Tree Isomorphism

How about unrooted tree?

Page 62: [ACM-ICPC] Tree Isomorphism

Find a Root

Page 63: [ACM-ICPC] Tree Isomorphism

eliminate leaves

Page 64: [ACM-ICPC] Tree Isomorphism

eliminate leaves

Page 65: [ACM-ICPC] Tree Isomorphism

eliminate leaves

Page 66: [ACM-ICPC] Tree Isomorphism

eliminate leaves

Page 67: [ACM-ICPC] Tree Isomorphism

eliminate leaves

Page 68: [ACM-ICPC] Tree Isomorphism

eliminate leaves

Page 69: [ACM-ICPC] Tree Isomorphism

Enumerate Possible Root(s)

Page 70: [ACM-ICPC] Tree Isomorphism

Rebuild The Tree

Page 71: [ACM-ICPC] Tree Isomorphism

Rebuild The Tree

Page 72: [ACM-ICPC] Tree Isomorphism

Rebuild The Tree

Page 73: [ACM-ICPC] Tree Isomorphism

Rebuild The Tree

Page 74: [ACM-ICPC] Tree Isomorphism

Rebuild The Tree

Page 75: [ACM-ICPC] Tree Isomorphism

Rebuild The Tree

Page 76: [ACM-ICPC] Tree Isomorphism

Apply the Isomorphism Detection

Page 77: [ACM-ICPC] Tree Isomorphism

[POJ] 1635 - Subway tree systems

Practice Now

Page 78: [ACM-ICPC] Tree Isomorphism

Thank You for Your Listening.