Upcoming SlideShare
×

[ACM-ICPC] Tree Isomorphism

744 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

[ACM-ICPC] Tree Isomorphism

1. 1. Tree Isomorphism郭至軒（KuoE0）KuoE0.tw@gmail.comKuoE0.ch
3. 3. IsomorphismThe structures of two trees are equal.
4. 4. so abstract...
5. 5. 76 1 245 371624 5 343 1572 6isomorphism
6. 6. How to judge two rootedtree are isomorphic?
7. 7. If the trees are isomorphic, all their sub-trees arealso isomorphic.
8. 8. If the trees are isomorphic, all their sub-trees arealso isomorphic.
9. 9. If the trees are isomorphic, all their sub-trees arealso isomorphic.
10. 10. If the trees are isomorphic, all their sub-trees arealso isomorphic.
11. 11. Hash the tree
12. 12. Hash all sub-tree
13. 13. Hash all sub-treerecursively
14. 14. initial value = 191single vertex
15. 15. initial value = 191single vertex
16. 16. initial value = 191single vertex191 191191191
17. 17. initial value = 191single vertex191 191191191deﬁne by yourself
18. 18. 191 191191191two-level sub-treechild = (191)
19. 19. 191 191191191two-level sub-treechild = (191)
20. 20. 191 191191191two-level sub-treechild = (191)(191 × 701 xor 191) mod 34943 = 292472924729247
21. 21. 191 191191191two-level sub-treechild = (191)(191 × 701 xor 191) mod 34943 = 292472924729247deﬁne by yourself
22. 22. 191 191191191two-level sub-treechild = (191)(191 × 701 xor 191) mod 34943 = 292472924729247deﬁne by yourself
23. 23. 2924729247191 191191191three-level sub-treechild = (191,29247,191)
24. 24. 2924729247191 191191191three-level sub-treechild = (191,29247,191)
25. 25. 2924729247191 191191191three-level sub-treechild = (191,29247,191)child = (191,191,29247)sort
26. 26. 2924729247191 191191191three-level sub-treechild = (191,29247,191)child = (191,191,29247)sort(((((191 × 701 xor 191) mod 34943) × 701 xor 191) mod34943) × 701 xor 29247) mod 34943 = 3336033360
27. 27. 2924729247191 191191191three-level sub-treechild = (191,29247,191)child = (191,191,29247)sort(((((191 × 701 xor 191) mod 34943) × 701 xor 191) mod34943) × 701 xor 29247) mod 34943 = 3336033360
28. 28. 33360 2924729247191 191191191the total treechild = (33360,29247)
29. 29. 33360 2924729247191 191191191the total treechild = (33360,29247)
30. 30. 33360 2924729247191 191191191the total treechild = (33360,29247)child = (29247,33360)sort
31. 31. 33360 2924729247191 191191191the total treechild = (33360,29247)child = (29247,33360)sort(((191 × 701 xor 29247) mod 34943) × 701 xor 33360)mod 34943 = 46874687
32. 32. 33360 2924729247191 191191191the total treechild = (33360,29247)child = (29247,33360)sort(((191 × 701 xor 29247) mod 34943) × 701 xor 33360)mod 34943 = 46874687
33. 33. hash value of thetree is 4687
34. 34. initial value = 191single vertex
35. 35. initial value = 191single vertex
36. 36. initial value = 191single vertex191191191191
37. 37. 191191191191two-level sub-treechild = (191)
38. 38. 191191191191two-level sub-treechild = (191)
39. 39. 191191191191two-level sub-treechild = (191)(191 × 701 xor 191) mod 34943 = 292472924729247
40. 40. 1912924729247191191191three-level sub-treechild = (191,191,29247)
41. 41. 1912924729247191191191three-level sub-treechild = (191,191,29247)
42. 42. 1912924729247191191191three-level sub-treechild = (191,191,29247)child = (191,191,29247)sort
43. 43. 1912924729247191191191three-level sub-treechild = (191,191,29247)child = (191,191,29247)(((((191 × 701 xor 191) mod 34943) × 701 xor 191) mod34943) × 701 xor 29247) mod 34943 = 33360sort33360
44. 44. 191333602924729247191191191the total treechild = (33360,29247)
45. 45. 191333602924729247191191191the total treechild = (33360,29247)
46. 46. 191333602924729247191191191the total treechild = (33360,29247)child = (29247,33360)sort
47. 47. 191333602924729247191191191the total treechild = (33360,29247)child = (29247,33360)sort(((191 × 701 xor 29247) mod 34943) × 701 xor 33360)mod 34943 = 46874687
48. 48. hash value of thetree is 4687
49. 49.
50. 50. AlgorithmHASH_TREE(T):1. hash all sub-trees2. sort hash value of sub-trees (unique)3. calculate hash value (any hash function)
51. 51. Time ComplexityO(Nlog2N)N is number of verticesheight of tree
52. 52. Source Codeint 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;}
53. 53. Representation of TreeLet the height of left child less than the right one.
54. 54. Representation of TreeLet the height of left child less than the right one.
55. 55. 43 2121 11
56. 56. 321 1143 2121 11421
57. 57. 21321 1143 2121 1142131 1421
58. 58. AlgorithmSORT_CHILD(T):1. sort all sub-trees2. compare the height3. if height is equal, compare childrecursively4. put the lower at left and the higher atright
59. 59. How about unrooted tree?
60. 60. Find a Root
61. 61. eliminate leaves
62. 62. eliminate leaves
63. 63. eliminate leaves
64. 64. eliminate leaves
65. 65. eliminate leaves
66. 66. eliminate leaves
67. 67. Enumerate Possible Root(s)
68. 68. Rebuild The Tree
69. 69. Rebuild The Tree
70. 70. Rebuild The Tree
71. 71. Rebuild The Tree
72. 72. Rebuild The Tree
73. 73. Rebuild The Tree
74. 74. Apply the Isomorphism Detection
75. 75. [POJ] 1635 - Subway tree systemsPractice Now