[ACM-ICPC] Tree Isomorphism
Upcoming SlideShare
Loading in...5
×
 

[ACM-ICPC] Tree Isomorphism

on

  • 524 views

 

Statistics

Views

Total Views
524
Views on SlideShare
524
Embed Views
0

Actions

Likes
1
Downloads
7
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

[ACM-ICPC] Tree Isomorphism [ACM-ICPC] Tree Isomorphism Presentation Transcript

  • Tree Isomorphism郭至軒(KuoE0)KuoE0.tw@gmail.comKuoE0.ch
  • Latest update: May 1, 2013Attribution-ShareAlike 3.0 Unported(CC BY-SA 3.0)http://creativecommons.org/licenses/by-sa/3.0/
  • IsomorphismThe structures of two trees are equal.
  • so abstract...
  • 76 1 245 371624 5 343 1572 6isomorphism
  • How to judge two rootedtree are isomorphic?
  • If the trees are isomorphic, all their sub-trees arealso isomorphic.
  • If the trees are isomorphic, all their sub-trees arealso isomorphic.
  • If the trees are isomorphic, all their sub-trees arealso isomorphic.
  • If the trees are isomorphic, all their sub-trees arealso isomorphic.
  • Hash the tree
  • Hash all sub-tree
  • Hash all sub-treerecursively
  • initial value = 191single vertex
  • initial value = 191single vertex
  • initial value = 191single vertex191 191191191
  • initial value = 191single vertex191 191191191define by yourself
  • 191 191191191two-level sub-treechild = (191)
  • 191 191191191two-level sub-treechild = (191)
  • 191 191191191two-level sub-treechild = (191)(191 × 701 xor 191) mod 34943 = 292472924729247
  • 191 191191191two-level sub-treechild = (191)(191 × 701 xor 191) mod 34943 = 292472924729247define by yourself
  • 191 191191191two-level sub-treechild = (191)(191 × 701 xor 191) mod 34943 = 292472924729247define by yourself
  • 2924729247191 191191191three-level sub-treechild = (191,29247,191)
  • 2924729247191 191191191three-level sub-treechild = (191,29247,191)
  • 2924729247191 191191191three-level sub-treechild = (191,29247,191)child = (191,191,29247)sort
  • 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
  • 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
  • 33360 2924729247191 191191191the total treechild = (33360,29247)
  • 33360 2924729247191 191191191the total treechild = (33360,29247)
  • 33360 2924729247191 191191191the total treechild = (33360,29247)child = (29247,33360)sort
  • 33360 2924729247191 191191191the total treechild = (33360,29247)child = (29247,33360)sort(((191 × 701 xor 29247) mod 34943) × 701 xor 33360)mod 34943 = 46874687
  • 33360 2924729247191 191191191the total treechild = (33360,29247)child = (29247,33360)sort(((191 × 701 xor 29247) mod 34943) × 701 xor 33360)mod 34943 = 46874687
  • hash value of thetree is 4687
  • initial value = 191single vertex
  • initial value = 191single vertex
  • initial value = 191single vertex191191191191
  • 191191191191two-level sub-treechild = (191)
  • 191191191191two-level sub-treechild = (191)
  • 191191191191two-level sub-treechild = (191)(191 × 701 xor 191) mod 34943 = 292472924729247
  • 1912924729247191191191three-level sub-treechild = (191,191,29247)
  • 1912924729247191191191three-level sub-treechild = (191,191,29247)
  • 1912924729247191191191three-level sub-treechild = (191,191,29247)child = (191,191,29247)sort
  • 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
  • 191333602924729247191191191the total treechild = (33360,29247)
  • 191333602924729247191191191the total treechild = (33360,29247)
  • 191333602924729247191191191the total treechild = (33360,29247)child = (29247,33360)sort
  • 191333602924729247191191191the total treechild = (33360,29247)child = (29247,33360)sort(((191 × 701 xor 29247) mod 34943) × 701 xor 33360)mod 34943 = 46874687
  • hash value of thetree is 4687
  • AlgorithmHASH_TREE(T):1. hash all sub-trees2. sort hash value of sub-trees (unique)3. calculate hash value (any hash function)
  • Time ComplexityO(Nlog2N)N is number of verticesheight of tree
  • 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;}
  • 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.
  • 43 2121 11
  • 321 1143 2121 11421
  • 21321 1143 2121 1142131 1421
  • 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
  • How about unrooted tree?
  • Find a Root
  • eliminate leaves
  • eliminate leaves
  • eliminate leaves
  • eliminate leaves
  • eliminate leaves
  • eliminate leaves
  • Enumerate Possible Root(s)
  • Rebuild The Tree
  • Rebuild The Tree
  • Rebuild The Tree
  • Rebuild The Tree
  • Rebuild The Tree
  • Rebuild The Tree
  • Apply the Isomorphism Detection
  • [POJ] 1635 - Subway tree systemsPractice Now
  • ThankYou forYour Listening.