KuoE0
May 01, 2013
180

# [ACM-ICPC] Tree Isomorphism

May 01, 2013

## Transcript

1. Tree Isomorphism
ֲࢸݢʢKuoE0ʣ
[email protected]
KuoE0.ch

2. Latest update: May 1, 2013
(CC BY-SA 3.0)

3. Isomorphism
The structures of two trees are equal.

4. so abstract...

5. 7
6 1 2
4
5 3
7
1
6
2
4 5 3
4
3 1
5
7
2 6
isomorphism

6. How to judge two rooted
tree are isomorphic?

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

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

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

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

11. Hash the tree

12. Hash all sub-tree

13. Hash all sub-tree
recursively

14. initial value = 191
single vertex

15. initial value = 191
single vertex

16. initial value = 191
single vertex
191 191
191
191

17. initial value = 191
single vertex
191 191
191
191
deﬁne by yourself

18. 191 191
191
191
two-level sub-tree
child = (191)

19. 191 191
191
191
two-level sub-tree
child = (191)

20. 191 191
191
191
two-level sub-tree
child = (191)
(191 × 701 xor 191) mod 34943 = 29247
29247
29247

21. 191 191
191
191
two-level sub-tree
child = (191)
(191 × 701 xor 191) mod 34943 = 29247
29247
29247
deﬁne by yourself

22. 191 191
191
191
two-level sub-tree
child = (191)
(191 × 701 xor 191) mod 34943 = 29247
29247
29247
deﬁne by yourself

23. 29247
29247
191 191
191
191
three-level sub-tree
child = (191,29247,191)

24. 29247
29247
191 191
191
191
three-level sub-tree
child = (191,29247,191)

25. 29247
29247
191 191
191
191
three-level sub-tree
child = (191,29247,191)
child = (191,191,29247)
sort

26. 29247
29247
191 191
191
191
three-level sub-tree
child = (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

27. 29247
29247
191 191
191
191
three-level sub-tree
child = (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

28. 33360 29247
29247
191 191
191
191
the total tree
child = (33360,29247)

29. 33360 29247
29247
191 191
191
191
the total tree
child = (33360,29247)

30. 33360 29247
29247
191 191
191
191
the total tree
child = (33360,29247)
child = (29247,33360)
sort

31. 33360 29247
29247
191 191
191
191
the total tree
child = (33360,29247)
child = (29247,33360)
sort
(((191 × 701 xor 29247) mod 34943) × 701 xor 33360)
mod 34943 = 4687
4687

32. 33360 29247
29247
191 191
191
191
the total tree
child = (33360,29247)
child = (29247,33360)
sort
(((191 × 701 xor 29247) mod 34943) × 701 xor 33360)
mod 34943 = 4687
4687

33. hash value of the
tree is 4687

34. initial value = 191
single vertex

35. initial value = 191
single vertex

36. initial value = 191
single vertex
191
191
191
191

37. 191
191
191
191
two-level sub-tree
child = (191)

38. 191
191
191
191
two-level sub-tree
child = (191)

39. 191
191
191
191
two-level sub-tree
child = (191)
(191 × 701 xor 191) mod 34943 = 29247
29247
29247

40. 191
29247
29247
191
191
191
three-level sub-tree
child = (191,191,29247)

41. 191
29247
29247
191
191
191
three-level sub-tree
child = (191,191,29247)

42. 191
29247
29247
191
191
191
three-level sub-tree
child = (191,191,29247)
child = (191,191,29247)
sort

43. 191
29247
29247
191
191
191
three-level sub-tree
child = (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

44. 191
33360
29247
29247
191
191
191
the total tree
child = (33360,29247)

45. 191
33360
29247
29247
191
191
191
the total tree
child = (33360,29247)

46. 191
33360
29247
29247
191
191
191
the total tree
child = (33360,29247)
child = (29247,33360)
sort

47. 191
33360
29247
29247
191
191
191
the total tree
child = (33360,29247)
child = (29247,33360)
sort
(((191 × 701 xor 29247) mod 34943) × 701 xor 33360)
mod 34943 = 4687
4687

48. hash value of the
tree is 4687

49. Algorithm
HASH_TREE(T):
1. hash all sub-trees
2. sort hash value of sub-trees (unique)
3. calculate hash value (any hash function)

50. Time Complexity
O(Nlog2N)
N is number of vertices
height of tree

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

52. Representation of Tree
Let the height of left child less than the right one.

53. Representation of Tree
Let the height of left child less than the right one.

54. 4
3 2
1
2
1 1
1

55. 3
2
1 1
1
4
3 2
1
2
1 1
1
4
2
1

56. 2
1
3
2
1 1
1
4
3 2
1
2
1 1
1
4
2
1
3
1 1
4
2
1

57. Algorithm
SORT_CHILD(T):
1. sort all sub-trees
2. compare the height
3. if height is equal, compare child
recursively
4. put the lower at left and the higher at
right

59. Find a Root

60. eliminate leaves

61. eliminate leaves

62. eliminate leaves

63. eliminate leaves

64. eliminate leaves

65. eliminate leaves

66. Enumerate Possible Root(s)

67. Rebuild The Tree

68. Rebuild The Tree

69. Rebuild The Tree

70. Rebuild The Tree

71. Rebuild The Tree

72. Rebuild The Tree

73. Apply the Isomorphism Detection

74. [POJ] 1635 - Subway tree systems
Practice Now

75. Thank You for Your Listening.