Tree Isomorphism     郭至軒(KuoE0)    KuoE0.tw@gmail.com         KuoE0.ch
IsomorphismThe structures of two trees are equal.
so abstract...
7                       7    6     1   2               1       2    5     4   3           4   5       3isomorphism   4    ...
How to judge two rooted  tree are isomorphic?
If the trees are isomorphic, all their sub-trees               are also isomorphic.
If the trees are isomorphic, all their sub-trees               are also isomorphic.
Hash the tree
Hash all sub-tree           recursively
single vertex191         191   191                        initial value = 191      191
29247                              two-level sub-tree191   29247   191      191                              child = (191)...
33360         29247                            three-level sub-tree191   29247   191   191                            chil...
4687      33360          29247                             the total tree191   29247   191    191                         ...
hash value ofthe tree is 4687
single vertexinitial value = 191   191   191   191                                        191
29247two-level sub-treechild = (191)                 191    191   191   29247                                             ...
29247         33360three-level sub-treechild = (191,191,29247)      191    191   191     29247              sortchild = (1...
4687                            29247          33360the total treechild = (33360,29247)       191     191    191     29247...
hash value ofthe tree is 4687
≡
AlgorithmHASH_TREE(T):1. hash all sub-trees2. sort hash value of sub-trees (unique)3. calculate hash value (any hash funct...
Time Complexity         O(Nlog2N)         number of vertices         height of tree
Source Codeint hash( TREE &now, int root ) {	 int value = INIT;	 vector< int > sub;	 //get all hash value of subtree	 for ...
Representation of TreeLet the height of left child less than the right one.
Representation of TreeLet the height of left child less than the right one.
4    3       21   2   1   1    1
4           4    3       2   2       31   2   1   1   1   1   2   1    1                   1
4           4               4    3       2   2       3       2       31   2   1   1   1   1   2   1   1   1   1   2    1  ...
AlgorithmSORT_CHILD(T):1. sort all sub-trees2. compare the height3. if height is equal, compare child   recursively4. put ...
How about unrooted tree?
find a root
eliminate leaves
eliminate leaves
eliminate leaves
eliminate leaves
try each root
rebuild the tree
rebuild the tree
rebuild the tree
rebuild the tree
rebuild the tree
rebuild the tree
apply the isomorphism detection
Practice NowPOJ 1635 - Subway tree systems
Thank You for Your    Listening.
[ACM-ICPC] Tree Isomorphism
Upcoming SlideShare
Loading in …5
×

[ACM-ICPC] Tree Isomorphism

1,780 views

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,780
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
64
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

[ACM-ICPC] Tree Isomorphism

  1. 1. Tree Isomorphism 郭至軒(KuoE0) KuoE0.tw@gmail.com KuoE0.ch
  2. 2. IsomorphismThe structures of two trees are equal.
  3. 3. so abstract...
  4. 4. 7 7 6 1 2 1 2 5 4 3 4 5 3isomorphism 4 5 6 3 1 2 7 6
  5. 5. How to judge two rooted tree are isomorphic?
  6. 6. If the trees are isomorphic, all their sub-trees are also isomorphic.
  7. 7. If the trees are isomorphic, all their sub-trees are also isomorphic.
  8. 8. Hash the tree
  9. 9. Hash all sub-tree recursively
  10. 10. single vertex191 191 191 initial value = 191 191
  11. 11. 29247 two-level sub-tree191 29247 191 191 child = (191) 191 (191 × 701 xor 191) mod 34943 = 29247
  12. 12. 33360 29247 three-level sub-tree191 29247 191 191 child = (191,29247,191) sort 191 child = (191,191,29247)(((((191 × 701 xor 191) mod 34943) × 701 xor 191) mod 34943) × 701 xor 29247) mod 34943 = 33360
  13. 13. 4687 33360 29247 the total tree191 29247 191 191 child = (33360,29247) sort 191 child = (29247,33360) (((191 × 701 xor 29247) mod 34943) × 701 xor 33360) mod 34943 = 4687
  14. 14. hash value ofthe tree is 4687
  15. 15. single vertexinitial value = 191 191 191 191 191
  16. 16. 29247two-level sub-treechild = (191) 191 191 191 29247 191 (191 × 701 xor 191) mod 34943 = 29247
  17. 17. 29247 33360three-level sub-treechild = (191,191,29247) 191 191 191 29247 sortchild = (191,191,29247) 191(((((191 × 701 xor 191) mod 34943) × 701 xor 191) mod 34943) × 701 xor 29247) mod 34943 = 33360
  18. 18. 4687 29247 33360the total treechild = (33360,29247) 191 191 191 29247 sortchild = (29247,33360) 191 (((191 × 701 xor 29247) mod 34943) × 701 xor 33360) mod 34943 = 4687
  19. 19. hash value ofthe tree is 4687
  20. 20.
  21. 21. AlgorithmHASH_TREE(T):1. hash all sub-trees2. sort hash value of sub-trees (unique)3. calculate hash value (any hash function)
  22. 22. Time Complexity O(Nlog2N) number of vertices height of tree
  23. 23. 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;}
  24. 24. Representation of TreeLet the height of left child less than the right one.
  25. 25. Representation of TreeLet the height of left child less than the right one.
  26. 26. 4 3 21 2 1 1 1
  27. 27. 4 4 3 2 2 31 2 1 1 1 1 2 1 1 1
  28. 28. 4 4 4 3 2 2 3 2 31 2 1 1 1 1 2 1 1 1 1 2 1 1 1
  29. 29. 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
  30. 30. How about unrooted tree?
  31. 31. find a root
  32. 32. eliminate leaves
  33. 33. eliminate leaves
  34. 34. eliminate leaves
  35. 35. eliminate leaves
  36. 36. try each root
  37. 37. rebuild the tree
  38. 38. rebuild the tree
  39. 39. rebuild the tree
  40. 40. rebuild the tree
  41. 41. rebuild the tree
  42. 42. rebuild the tree
  43. 43. apply the isomorphism detection
  44. 44. Practice NowPOJ 1635 - Subway tree systems
  45. 45. Thank You for Your Listening.

×