SlideShare a Scribd company logo
Tree Isomorphism
     郭至軒(KuoE0)
    KuoE0.tw@gmail.com
         KuoE0.ch
Isomorphism
The structures of two trees are equal.
so abstract...
7                       7


    6     1   2               1       2


    5     4   3           4   5       3


isomorphism   4       5       6


              3   1


              2   7   6
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 vertex
191         191   191
                        initial value = 191

      191
29247

                              two-level sub-tree
191   29247   191      191
                              child = (191)

      191




                    (191 × 701 xor 191) mod 34943 = 29247
33360         29247

                            three-level sub-tree
191   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
4687




      33360          29247

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


 (((191 × 701 xor 29247) mod 34943) × 701 xor 33360)
                                 mod 34943 = 4687
hash value of
the tree is 4687
single vertex
initial value = 191   191   191   191




                                        191
29247

two-level sub-tree
child = (191)                 191    191   191   29247




                                                 191




                (191 × 701 xor 191) mod 34943 = 29247
29247         33360

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


              sort
child = (191,191,29247)                           191




(((((191 × 701 xor 191) mod 34943) × 701 xor 191) mod
           34943) × 701 xor 29247) mod 34943 = 33360
4687




                            29247          33360

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


              sort
child = (29247,33360)                              191




 (((191 × 701 xor 29247) mod 34943) × 701 xor 33360)
                                 mod 34943 = 4687
hash value of
the tree is 4687
≡
Algorithm
HASH_TREE(T):

1. hash all sub-trees
2. sort hash value of sub-trees (unique)
3. calculate hash value (any hash function)
Time Complexity

         O(Nlog2N)
         number of vertices
         height of tree
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;
}
Representation of Tree




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




Let the height of left child less than the right one.
4


    3       2


1   2   1   1


    1
4           4


    3       2   2       3


1   2   1   1   1   1   2   1


    1                   1
4           4               4


    3       2   2       3       2       3


1   2   1   1   1   1   2   1   1   1   1   2


    1                   1                   1
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
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 Now
POJ 1635 - Subway tree systems
Thank You for Your
    Listening.

More Related Content

Viewers also liked

Efficiency of Hamming Matrix Method as a Test for Isomorphism: A Comparative ...
Efficiency of Hamming Matrix Method as a Test for Isomorphism: A Comparative ...Efficiency of Hamming Matrix Method as a Test for Isomorphism: A Comparative ...
Efficiency of Hamming Matrix Method as a Test for Isomorphism: A Comparative ...
ijsrd.com
 
Bases teoricas del muestreo expo
Bases teoricas del muestreo expoBases teoricas del muestreo expo
Bases teoricas del muestreo expo
k4rol1n4
 
Presentation - Gear train
Presentation - Gear trainPresentation - Gear train
Presentation - Gear train
Amit Biswas
 
Kinematic Inversions 2
Kinematic Inversions 2Kinematic Inversions 2
Kinematic Inversions 2
Rohit Singla
 
Compound Gear train
 Compound Gear train Compound Gear train
Compound Gear train
Avinash Navin
 
Gear Trains
Gear TrainsGear Trains
Gear Trains
Abhishek Attal
 
A presentation On Epicycle Gear Train
A presentation On Epicycle Gear TrainA presentation On Epicycle Gear Train
A presentation On Epicycle Gear Train
Manthan Thakkar
 
Basic kinematic concepts
Basic kinematic conceptsBasic kinematic concepts
Basic kinematic conceptsVaishnavichakka
 
Epicyclic gear train
Epicyclic gear train Epicyclic gear train
Epicyclic gear train
kamallalu
 
Gear
GearGear
Theory of Machine and Mechanisms (Gears)
Theory of Machine and Mechanisms (Gears)Theory of Machine and Mechanisms (Gears)
Theory of Machine and Mechanisms (Gears)
Abhishek Attal
 
Gears and Gear Trains
Gears and Gear Trains Gears and Gear Trains
Gears and Gear Trains
Deepak Sharma
 
Kinemetic chains, Pairs, Joints, Four bar Mechanisms (KOM)
Kinemetic chains, Pairs, Joints, Four bar Mechanisms (KOM)Kinemetic chains, Pairs, Joints, Four bar Mechanisms (KOM)
Kinemetic chains, Pairs, Joints, Four bar Mechanisms (KOM)
University of Windsor
 

Viewers also liked (14)

Efficiency of Hamming Matrix Method as a Test for Isomorphism: A Comparative ...
Efficiency of Hamming Matrix Method as a Test for Isomorphism: A Comparative ...Efficiency of Hamming Matrix Method as a Test for Isomorphism: A Comparative ...
Efficiency of Hamming Matrix Method as a Test for Isomorphism: A Comparative ...
 
Bases teoricas del muestreo expo
Bases teoricas del muestreo expoBases teoricas del muestreo expo
Bases teoricas del muestreo expo
 
Presentation - Gear train
Presentation - Gear trainPresentation - Gear train
Presentation - Gear train
 
Kinematic Inversions 2
Kinematic Inversions 2Kinematic Inversions 2
Kinematic Inversions 2
 
Compound Gear train
 Compound Gear train Compound Gear train
Compound Gear train
 
Gear Trains
Gear TrainsGear Trains
Gear Trains
 
A presentation On Epicycle Gear Train
A presentation On Epicycle Gear TrainA presentation On Epicycle Gear Train
A presentation On Epicycle Gear Train
 
Basic kinematic concepts
Basic kinematic conceptsBasic kinematic concepts
Basic kinematic concepts
 
Epicyclic gear train
Epicyclic gear train Epicyclic gear train
Epicyclic gear train
 
Gear
GearGear
Gear
 
Theory of Machine and Mechanisms (Gears)
Theory of Machine and Mechanisms (Gears)Theory of Machine and Mechanisms (Gears)
Theory of Machine and Mechanisms (Gears)
 
Gear train
Gear trainGear train
Gear train
 
Gears and Gear Trains
Gears and Gear Trains Gears and Gear Trains
Gears and Gear Trains
 
Kinemetic chains, Pairs, Joints, Four bar Mechanisms (KOM)
Kinemetic chains, Pairs, Joints, Four bar Mechanisms (KOM)Kinemetic chains, Pairs, Joints, Four bar Mechanisms (KOM)
Kinemetic chains, Pairs, Joints, Four bar Mechanisms (KOM)
 

More from Chih-Hsuan Kuo

Rust
RustRust
[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-select
Chih-Hsuan Kuo
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in Rust
Chih-Hsuan Kuo
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。
Chih-Hsuan Kuo
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36
Chih-Hsuan Kuo
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
Chih-Hsuan Kuo
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OS
Chih-Hsuan Kuo
 
Necko walkthrough
Necko walkthroughNecko walkthrough
Necko walkthrough
Chih-Hsuan Kuo
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in Gecko
Chih-Hsuan Kuo
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
Chih-Hsuan Kuo
 
面試心得分享
面試心得分享面試心得分享
面試心得分享
Chih-Hsuan Kuo
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...
Chih-Hsuan Kuo
 
Python @Wheel Lab
Python @Wheel LabPython @Wheel Lab
Python @Wheel Lab
Chih-Hsuan Kuo
 
Introduction to VP8
Introduction to VP8Introduction to VP8
Introduction to VP8
Chih-Hsuan Kuo
 
Python @NCKU CSIE
Python @NCKU CSIEPython @NCKU CSIE
Python @NCKU CSIE
Chih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree IsomorphismChih-Hsuan Kuo
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's AlgorithmChih-Hsuan Kuo
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint SetChih-Hsuan Kuo
 

More from Chih-Hsuan Kuo (20)

Rust
RustRust
Rust
 
[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-select
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in Rust
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OS
 
Necko walkthrough
Necko walkthroughNecko walkthrough
Necko walkthrough
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in Gecko
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
 
應徵軟體工程師
應徵軟體工程師應徵軟體工程師
應徵軟體工程師
 
面試心得分享
面試心得分享面試心得分享
面試心得分享
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...
 
Python @Wheel Lab
Python @Wheel LabPython @Wheel Lab
Python @Wheel Lab
 
Introduction to VP8
Introduction to VP8Introduction to VP8
Introduction to VP8
 
Python @NCKU CSIE
Python @NCKU CSIEPython @NCKU CSIE
Python @NCKU CSIE
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set
 
[ACM-ICPC] Traversal
[ACM-ICPC] Traversal[ACM-ICPC] Traversal
[ACM-ICPC] Traversal
 

Recently uploaded

The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 

Recently uploaded (20)

The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 

[ACM-ICPC] Tree Isomorphism