Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[ACM-ICPC] Tree Isomorphism

[ACM-ICPC] Tree Isomorphism

KuoE0

May 01, 2013
Tweet

More Decks by KuoE0

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

  3. Isomorphism
    The structures of two trees are equal.

    View full-size slide

  4. so abstract...

    View full-size slide

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

    View full-size slide

  6. How to judge two rooted
    tree are isomorphic?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  11. Hash the tree

    View full-size slide

  12. Hash all sub-tree

    View full-size slide

  13. Hash all sub-tree
    recursively

    View full-size slide

  14. initial value = 191
    single vertex

    View full-size slide

  15. initial value = 191
    single vertex

    View full-size slide

  16. initial value = 191
    single vertex
    191 191
    191
    191

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  26. 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

    View full-size slide

  27. 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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  31. 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

    View full-size slide

  32. 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

    View full-size slide

  33. hash value of the
    tree is 4687

    View full-size slide

  34. initial value = 191
    single vertex

    View full-size slide

  35. initial value = 191
    single vertex

    View full-size slide

  36. initial value = 191
    single vertex
    191
    191
    191
    191

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  43. 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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  47. 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

    View full-size slide

  48. hash value of the
    tree is 4687

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  54. 3
    2
    1 1
    1
    4
    3 2
    1
    2
    1 1
    1
    4
    2
    1

    View full-size slide

  55. 2
    1
    3
    2
    1 1
    1
    4
    3 2
    1
    2
    1 1
    1
    4
    2
    1
    3
    1 1
    4
    2
    1

    View full-size slide

  56. 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

    View full-size slide

  57. How about unrooted tree?

    View full-size slide

  58. eliminate leaves

    View full-size slide

  59. eliminate leaves

    View full-size slide

  60. eliminate leaves

    View full-size slide

  61. eliminate leaves

    View full-size slide

  62. eliminate leaves

    View full-size slide

  63. eliminate leaves

    View full-size slide

  64. Enumerate Possible Root(s)

    View full-size slide

  65. Rebuild The Tree

    View full-size slide

  66. Rebuild The Tree

    View full-size slide

  67. Rebuild The Tree

    View full-size slide

  68. Rebuild The Tree

    View full-size slide

  69. Rebuild The Tree

    View full-size slide

  70. Rebuild The Tree

    View full-size slide

  71. Apply the Isomorphism Detection

    View full-size slide

  72. [POJ] 1635 - Subway tree systems
    Practice Now

    View full-size slide

  73. Thank You for Your Listening.

    View full-size slide