SlideShare une entreprise Scribd logo
1  sur  46
Télécharger pour lire hors ligne
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.

Contenu connexe

Tendances

(8.5.1) soal dan pembahasan gradien, matematika sltp kelas 8
(8.5.1) soal dan pembahasan gradien, matematika sltp kelas 8(8.5.1) soal dan pembahasan gradien, matematika sltp kelas 8
(8.5.1) soal dan pembahasan gradien, matematika sltp kelas 8
kreasi_cerdik
 
fungsi, persamaan & pertidaksamaan kuadrat
 fungsi, persamaan & pertidaksamaan kuadrat fungsi, persamaan & pertidaksamaan kuadrat
fungsi, persamaan & pertidaksamaan kuadrat
mfebri26
 
(8.4.1) soal dan pembahasan nilai fungsi, matematika sltp kelas 8
(8.4.1) soal dan pembahasan nilai fungsi, matematika sltp kelas 8(8.4.1) soal dan pembahasan nilai fungsi, matematika sltp kelas 8
(8.4.1) soal dan pembahasan nilai fungsi, matematika sltp kelas 8
kreasi_cerdik
 
(8.7.1) soal dan pembahasan spldv, matematika sltp kelas 8
(8.7.1) soal dan pembahasan spldv, matematika sltp kelas 8(8.7.1) soal dan pembahasan spldv, matematika sltp kelas 8
(8.7.1) soal dan pembahasan spldv, matematika sltp kelas 8
kreasi_cerdik
 
Aljabar kelas 7
Aljabar kelas 7Aljabar kelas 7
Aljabar kelas 7
Eka Putra
 
Latihan soal uas matematika kelas 10 semester 1
Latihan soal uas matematika kelas 10 semester 1Latihan soal uas matematika kelas 10 semester 1
Latihan soal uas matematika kelas 10 semester 1
Kintan Azura
 
2 peluang-l-a-s-kaidah-pencacahan-dan-faktorial-hanya-soal
2 peluang-l-a-s-kaidah-pencacahan-dan-faktorial-hanya-soal2 peluang-l-a-s-kaidah-pencacahan-dan-faktorial-hanya-soal
2 peluang-l-a-s-kaidah-pencacahan-dan-faktorial-hanya-soal
Tri Bagus
 

Tendances (20)

(8.5.1) soal dan pembahasan gradien, matematika sltp kelas 8
(8.5.1) soal dan pembahasan gradien, matematika sltp kelas 8(8.5.1) soal dan pembahasan gradien, matematika sltp kelas 8
(8.5.1) soal dan pembahasan gradien, matematika sltp kelas 8
 
Phuong trinh va_he_phuong_trinh
Phuong trinh va_he_phuong_trinhPhuong trinh va_he_phuong_trinh
Phuong trinh va_he_phuong_trinh
 
Modul 1 functions
Modul 1 functionsModul 1 functions
Modul 1 functions
 
fungsi, persamaan & pertidaksamaan kuadrat
 fungsi, persamaan & pertidaksamaan kuadrat fungsi, persamaan & pertidaksamaan kuadrat
fungsi, persamaan & pertidaksamaan kuadrat
 
(8.4.1) soal dan pembahasan nilai fungsi, matematika sltp kelas 8
(8.4.1) soal dan pembahasan nilai fungsi, matematika sltp kelas 8(8.4.1) soal dan pembahasan nilai fungsi, matematika sltp kelas 8
(8.4.1) soal dan pembahasan nilai fungsi, matematika sltp kelas 8
 
(8.7.1) soal dan pembahasan spldv, matematika sltp kelas 8
(8.7.1) soal dan pembahasan spldv, matematika sltp kelas 8(8.7.1) soal dan pembahasan spldv, matematika sltp kelas 8
(8.7.1) soal dan pembahasan spldv, matematika sltp kelas 8
 
PPT Persamaan garis singgung lingkaran
PPT Persamaan garis singgung lingkaranPPT Persamaan garis singgung lingkaran
PPT Persamaan garis singgung lingkaran
 
Kelompok 1 relasi dan fungsi
Kelompok 1 relasi dan fungsiKelompok 1 relasi dan fungsi
Kelompok 1 relasi dan fungsi
 
1. kesebangunan
1. kesebangunan1. kesebangunan
1. kesebangunan
 
Aljabar kelas 7
Aljabar kelas 7Aljabar kelas 7
Aljabar kelas 7
 
Latihan soal uas matematika kelas 10 semester 1
Latihan soal uas matematika kelas 10 semester 1Latihan soal uas matematika kelas 10 semester 1
Latihan soal uas matematika kelas 10 semester 1
 
Lks spldv metode grafik ok
Lks spldv metode grafik okLks spldv metode grafik ok
Lks spldv metode grafik ok
 
Perbandingan trigonometri pada segitiga siki siku
Perbandingan trigonometri pada segitiga siki sikuPerbandingan trigonometri pada segitiga siki siku
Perbandingan trigonometri pada segitiga siki siku
 
2 peluang-l-a-s-kaidah-pencacahan-dan-faktorial-hanya-soal
2 peluang-l-a-s-kaidah-pencacahan-dan-faktorial-hanya-soal2 peluang-l-a-s-kaidah-pencacahan-dan-faktorial-hanya-soal
2 peluang-l-a-s-kaidah-pencacahan-dan-faktorial-hanya-soal
 
Ulangan harian matematika fpb kpk
Ulangan harian matematika fpb kpkUlangan harian matematika fpb kpk
Ulangan harian matematika fpb kpk
 
Identifying slope and y intercept
Identifying slope and y interceptIdentifying slope and y intercept
Identifying slope and y intercept
 
Ppt (lara yulia sastri)
Ppt (lara yulia sastri)Ppt (lara yulia sastri)
Ppt (lara yulia sastri)
 
Matematika Peminatan "Eksponen dan Logaritma"
Matematika Peminatan "Eksponen dan Logaritma"Matematika Peminatan "Eksponen dan Logaritma"
Matematika Peminatan "Eksponen dan Logaritma"
 
Integral Lipat Tiga dalam koordinat Tabung dan Bola
Integral Lipat Tiga dalam koordinat Tabung dan BolaIntegral Lipat Tiga dalam koordinat Tabung dan Bola
Integral Lipat Tiga dalam koordinat Tabung dan Bola
 
Fungsi Komposisi
Fungsi KomposisiFungsi Komposisi
Fungsi Komposisi
 

En vedette

Basic kinematic concepts
Basic kinematic conceptsBasic kinematic concepts
Basic kinematic concepts
Vaishnavichakka
 

En vedette (15)

7.4 formula p muestra
7.4 formula p muestra7.4 formula p muestra
7.4 formula p muestra
 
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)
 

Plus de Chih-Hsuan Kuo

[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
Chih-Hsuan Kuo
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm
Chih-Hsuan Kuo
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set
Chih-Hsuan Kuo
 

Plus de 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
 

Dernier

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Dernier (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

[ACM-ICPC] Tree Isomorphism