[ACM-ICPC] Tree Isomorphism

  • 852 views
Uploaded on

 

More in: Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
852
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
38
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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