Slide 1

Slide 1 text

Tree Isomorphism ֲࢸݢʢKuoE0ʣ [email protected] KuoE0.ch

Slide 2

Slide 2 text

Latest update: May 1, 2013 Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/

Slide 3

Slide 3 text

Isomorphism The structures of two trees are equal.

Slide 4

Slide 4 text

so abstract...

Slide 5

Slide 5 text

7 6 1 2 4 5 3 7 1 6 2 4 5 3 4 3 1 5 7 2 6 isomorphism

Slide 6

Slide 6 text

How to judge two rooted tree are isomorphic?

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

If the trees are isomorphic, all their sub-trees are also isomorphic.

Slide 9

Slide 9 text

If the trees are isomorphic, all their sub-trees are also isomorphic.

Slide 10

Slide 10 text

If the trees are isomorphic, all their sub-trees are also isomorphic.

Slide 11

Slide 11 text

If the trees are isomorphic, all their sub-trees are also isomorphic.

Slide 12

Slide 12 text

Hash the tree

Slide 13

Slide 13 text

Hash all sub-tree

Slide 14

Slide 14 text

Hash all sub-tree recursively

Slide 15

Slide 15 text

initial value = 191 single vertex

Slide 16

Slide 16 text

initial value = 191 single vertex

Slide 17

Slide 17 text

initial value = 191 single vertex 191 191 191 191

Slide 18

Slide 18 text

initial value = 191 single vertex 191 191 191 191 define by yourself

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

191 191 191 191 two-level sub-tree child = (191) (191 × 701 xor 191) mod 34943 = 29247 29247 29247

Slide 22

Slide 22 text

191 191 191 191 two-level sub-tree child = (191) (191 × 701 xor 191) mod 34943 = 29247 29247 29247 define by yourself

Slide 23

Slide 23 text

191 191 191 191 two-level sub-tree child = (191) (191 × 701 xor 191) mod 34943 = 29247 29247 29247 define by yourself

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

29247 29247 191 191 191 191 three-level sub-tree child = (191,29247,191) child = (191,191,29247) sort (((((191 × 701 xor 191) mod 34943) × 701 xor 191) mod 34943) × 701 xor 29247) mod 34943 = 33360 33360

Slide 28

Slide 28 text

29247 29247 191 191 191 191 three-level sub-tree child = (191,29247,191) child = (191,191,29247) sort (((((191 × 701 xor 191) mod 34943) × 701 xor 191) mod 34943) × 701 xor 29247) mod 34943 = 33360 33360

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

33360 29247 29247 191 191 191 191 the total tree child = (33360,29247) child = (29247,33360) sort (((191 × 701 xor 29247) mod 34943) × 701 xor 33360) mod 34943 = 4687 4687

Slide 33

Slide 33 text

33360 29247 29247 191 191 191 191 the total tree child = (33360,29247) child = (29247,33360) sort (((191 × 701 xor 29247) mod 34943) × 701 xor 33360) mod 34943 = 4687 4687

Slide 34

Slide 34 text

hash value of the tree is 4687

Slide 35

Slide 35 text

initial value = 191 single vertex

Slide 36

Slide 36 text

initial value = 191 single vertex

Slide 37

Slide 37 text

initial value = 191 single vertex 191 191 191 191

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

191 191 191 191 two-level sub-tree child = (191) (191 × 701 xor 191) mod 34943 = 29247 29247 29247

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

191 29247 29247 191 191 191 three-level sub-tree child = (191,191,29247) child = (191,191,29247) (((((191 × 701 xor 191) mod 34943) × 701 xor 191) mod 34943) × 701 xor 29247) mod 34943 = 33360 sort 33360

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

191 33360 29247 29247 191 191 191 the total tree child = (33360,29247) child = (29247,33360) sort (((191 × 701 xor 29247) mod 34943) × 701 xor 33360) mod 34943 = 4687 4687

Slide 49

Slide 49 text

hash value of the tree is 4687

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Slide 52

Slide 52 text

Algorithm HASH_TREE(T): 1. hash all sub-trees 2. sort hash value of sub-trees (unique) 3. calculate hash value (any hash function)

Slide 53

Slide 53 text

Time Complexity O(Nlog2N) N is number of vertices height of tree

Slide 54

Slide 54 text

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; }

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

4 3 2 1 2 1 1 1

Slide 58

Slide 58 text

3 2 1 1 1 4 3 2 1 2 1 1 1 4 2 1

Slide 59

Slide 59 text

2 1 3 2 1 1 1 4 3 2 1 2 1 1 1 4 2 1 3 1 1 4 2 1

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

How about unrooted tree?

Slide 62

Slide 62 text

Find a Root

Slide 63

Slide 63 text

eliminate leaves

Slide 64

Slide 64 text

eliminate leaves

Slide 65

Slide 65 text

eliminate leaves

Slide 66

Slide 66 text

eliminate leaves

Slide 67

Slide 67 text

eliminate leaves

Slide 68

Slide 68 text

eliminate leaves

Slide 69

Slide 69 text

Enumerate Possible Root(s)

Slide 70

Slide 70 text

Rebuild The Tree

Slide 71

Slide 71 text

Rebuild The Tree

Slide 72

Slide 72 text

Rebuild The Tree

Slide 73

Slide 73 text

Rebuild The Tree

Slide 74

Slide 74 text

Rebuild The Tree

Slide 75

Slide 75 text

Rebuild The Tree

Slide 76

Slide 76 text

Apply the Isomorphism Detection

Slide 77

Slide 77 text

[POJ] 1635 - Subway tree systems Practice Now

Slide 78

Slide 78 text

Thank You for Your Listening.