3.Disjoint Sets

Embed Size (px)

Citation preview

  • 7/30/2019 3.Disjoint Sets

    1/40

    Disjoint Sets

    1

  • 7/30/2019 3.Disjoint Sets

    2/40

    Prepared By

    AnithaPadmanaban

    Equivalence Relation

    An equivalence relation is a relation operator that

    observes three properties:

    Reflexive: (a R a), for all a

    Symmetric: (a R b) if and only if (b R a)

    Transitive: (a R b) and (b R c) implies (a R c)

    2

  • 7/30/2019 3.Disjoint Sets

    3/40

    Prepared By

    AnithaPadmanaban

    Example

    The relation

  • 7/30/2019 3.Disjoint Sets

    4/40

    Prepared By

    AnithaPadmanaban

    Dynamic Equivalence Problem

    The equivalence class of an element a S is the

    subset of S that contains all the elements related

    to a.

    The equivalence classes form a partition of S (Si

    Sj =); i.e., the sets are disjoint.

    Find (X) returns the name of the set (equivalence

    class) containing a given element X.

    Union (X, Y) returns the new root4

  • 7/30/2019 3.Disjoint Sets

    5/40

    Prepared By

    AnithaPadmanaban

    Basic Data Structure

    Tree data structure is used to implement the set.

    The name of a set is given by the node at the

    root.

    The array implementation of tree represents that

    P[i] is the parent of ith element.

    Ifi is the root P[i]=0.

    5

  • 7/30/2019 3.Disjoint Sets

    6/40

    Prepared By

    AnithaPadmanaban

    Operations on Disjoint Sets

    Union (Add operation (e.g., add relation a~b))

    Check if a and b are already related: if they are in

    the same equivalence class.

    If not, merge the two equivalence classes

    containing a and b into a new equivalence class.

    Find

    Return the name (pointer or index of representative)

    of the set containing a given element

    6

  • 7/30/2019 3.Disjoint Sets

    7/40

    Prepared By

    AnithaPadmanabanDynamic Equivalence Problem

    Eight elements, initially in different sets.

    1 2 3 4 5 6 7 8

    7

  • 7/30/2019 3.Disjoint Sets

    8/40

    Prepared By

    AnithaPadmanabanDynamic Equivalence Problem

    After union(5,6)

    1 2 3 4 5

    6

    7 8

    8

  • 7/30/2019 3.Disjoint Sets

    9/40

    Prepared By

    AnithaPadmanabanDynamic Equivalence Problem

    After union(7,8)

    1 2 3 4 5

    6

    7

    8

    9

  • 7/30/2019 3.Disjoint Sets

    10/40

    Prepared By

    AnithaPadmanabanDynamic Equivalence Problem

    After union(5,7)

    1 2 3 4 5

    6 7

    8

    10

  • 7/30/2019 3.Disjoint Sets

    11/40

    Prepared By

    AnithaPadmanabanDynamic Equivalence Problem

    After union(3,4)

    1 2 3

    4

    5

    6 7

    8

    11

  • 7/30/2019 3.Disjoint Sets

    12/40

    Prepared By

    AnithaPadmanabanDynamic Equivalence Problem

    After union(4,5)

    1 2 3

    4 5

    6 7

    8

    12

  • 7/30/2019 3.Disjoint Sets

    13/40

    Prepared By

    AnithaPadmanaban

    Smart Union Algorithms

    The previous union algorithm arbitrarily makes

    the second tree a subtree of the first.

    Heuristics to reduce the depth of the tree.

    Union operations is performed by three ways:

    Arbitrary Union

    Union by Size

    Union by Height

    13

  • 7/30/2019 3.Disjoint Sets

    14/40

    Prepared By

    AnithaPadmanaban

    Arbitrary Union

    This can be performed by making the second tree

    a subtree of the first

    1 2 3 4 5 6 7

    Arbitrary Union14

  • 7/30/2019 3.Disjoint Sets

    15/40

    Prepared By

    AnithaPadmanaban

    Union (2,3) and Union (6,7)

    1 2 3 4 5 6 7

    1 2

    3

    4 5 6

    7

    0 0 2 0 0 0 6

    1 2 3 4 5 6 7

    Arbitrary Union15

  • 7/30/2019 3.Disjoint Sets

    16/40

    Prepared By

    AnithaPadmanabanUnion (2,6)

    1 2

    3

    4 5

    6

    7

    0 0 2 0 0 2 6

    1 2 3 4 5 6 7

    Arbitrary Union16

  • 7/30/2019 3.Disjoint Sets

    17/40

    Prepared By

    AnithaPadmanaban

    Union by Size

    This can be performed by making the smaller tree

    a subtree of the larger

    1 2 3 4 5 6 7

    Union by Size17

  • 7/30/2019 3.Disjoint Sets

    18/40

    Prepared By

    AnithaPadmanaban

    Union (3,4) and Union(5,6)

    1 2 3 4 5 6 7

    1 2 3

    4

    5

    6

    7

    Union by Size18

  • 7/30/2019 3.Disjoint Sets

    19/40

    Prepared By

    AnithaPadmanaban

    Union (3,5)

    1 2 3

    4

    5

    6

    7

    1 2 3

    4

    5

    6

    7

    Union by Size19

  • 7/30/2019 3.Disjoint Sets

    20/40

    Prepared By

    AnithaPadmanaban

    Union (2,3)

    Here first tree is smaller, therefore it becomessubtree of the second tree

    1

    2

    3

    4

    5

    6

    7

    Union by Size20

  • 7/30/2019 3.Disjoint Sets

    21/40

    Prepared By

    AnithaPadmanaban

    Implementation of Union by Size

    Initially, the size of the tree is -1 for all tree

    Parent Array

    Eight elements, initially in different sets.

    1 2 3 4 5 6 7 8

    -1 -1 -1 -1 -1 -1 -1 -1

    1 2 3 4 5 6 7 8

    Union by Size21

  • 7/30/2019 3.Disjoint Sets

    22/40

    Prepared By

    AnithaPadmanabanParent Array

    After union(5,6)

    1 2 3 4 5

    6

    7 8

    -1 -1 -1 -1 -1 5 -1 -1

    1 2 3 4 5 6 7 8

    Union by Size22

  • 7/30/2019 3.Disjoint Sets

    23/40

    Prepared By

    AnithaPadmanabanParent Array

    After union(7,8)

    1 2 3 4 5

    6

    7

    8

    -1 -1 -1 -1 -1 5 -1 7

    1 2 3 4 5 6 7 8

    Union by Size23

  • 7/30/2019 3.Disjoint Sets

    24/40

    Prepared By

    AnithaPadmanabanParent Array

    After union(5,7)

    1 2 3 4 5

    6 7

    8

    -1 -1 -1 -1 -1 5 5 7

    1 2 3 4 5 6 7 8

    Union by Size24

  • 7/30/2019 3.Disjoint Sets

    25/40

    Prepared By

    AnithaPadmanabanParent Array

    After union(3,4)

    1 2 3

    4

    5

    6 7

    8

    -1 -1 -1 3 -1 5 5 7

    1 2 3 4 5 6 7 8

    Union by Size25

  • 7/30/2019 3.Disjoint Sets

    26/40

    Prepared By

    AnithaPadmanabanParent Array

    After union(4,5)

    1 2 3

    4 5

    6 7

    8

    -1 -1 -1 3 3 5 5 7

    1 2 3 4 5 6 7 8

    Union by Size26

  • 7/30/2019 3.Disjoint Sets

    27/40

    Prepared By

    AnithaPadmanabanParent Array

    Find(8) 1 2 3

    4 5

    6 7

    8

    -1 -1 -1 3 3 5 5 7

    1 2 3 4 5 6 7 8

    Union by Size27

    E l

  • 7/30/2019 3.Disjoint Sets

    28/40

    Prepared By

    AnithaPadmanabanExample

    Union (3,4)

    P[3]=P[3]+P[4]

    = -1 + -1

    = -2 (Parent)

    P[4]= 3 (Parent)

    -1 -1 -1 -1 -1 -1 -1 -1

    1 2 3 4 5 6 7 8

    -1 -1 -2 3 -1 -1 -1 -1

    1 2 3 4 5 6 7 8

    Union by Size28

    E l

  • 7/30/2019 3.Disjoint Sets

    29/40

    Prepared By

    AnithaPadmanabanExample

    Union (5,6)

    P[5]=P[5]+P[6]

    = -1 + -1

    = -2 (Parent)

    P[6]= 5 (Parent)

    -1 -1 -2 3 -2 5 -1 -1

    1 2 3 4 5 6 7 8

    -1 -1 -2 3 -1 -1 -1 -1

    1 2 3 4 5 6 7 8

    Union by Size29

    E l

  • 7/30/2019 3.Disjoint Sets

    30/40

    Prepared By

    AnithaPadmanabanExample

    Union(3,5)

    P[3]=P[3]+P[5]

    = -2 + -2

    = -4 (Parent)

    P[5]= 3 (Parent)

    -1 -1 -4 3 3 5 -1 -1

    1 2 3 4 5 6 7 8

    -1 -1 -2 3 -2 5 -1 -1

    1 2 3 4 5 6 7 8

    Union by Size30

    P d BExample

  • 7/30/2019 3.Disjoint Sets

    31/40

    Prepared By

    AnithaPadmanabanExample

    Union(2,3)

    P[3]=P[2]+P[3]

    = -1 + -4

    = -5 (Parent)

    P[2]= 3 (Parent)

    -1 3 -5 3 3 5 -1 -1

    1 2 3 4 5 6 7 8

    -1 -1 -4 3 3 5 -1 -1

    1 2 3 4 5 6 7 8

    Union by Size31

    P d B

  • 7/30/2019 3.Disjoint Sets

    32/40

    Prepared By

    AnithaPadmanaban

    Advantages Union by Size

    Easy to implement

    Requires no extra spaces

    Fast execution

    Sequence of M operations requires O(M)average

    time

    32

    P d B

  • 7/30/2019 3.Disjoint Sets

    33/40

    Prepared By

    AnithaPadmanaban

    Union by Height

    Instead of size, here the height of the tree is

    considered

    This can be implemented by making the shallowtree a subtree of the deeper tree

    Initially the array is set to 0

    0 0 0 0 0 0 0 0

    1 2 3 4 5 6 7 8

    33

    Prepared By

  • 7/30/2019 3.Disjoint Sets

    34/40

    Prepared By

    AnithaPadmanaban

    Example

    Union(3,4)

    1 2 3 4 5 6 7

    1 2 3

    4

    5 6 7

    0 0 -1 3 0 0 0 01 2 3 4 5 6 7 8

    If (S[R1] = = S[R2]) then

    S[R] = S[R1]-1

    S[3] = S[4] then

    S[3] = 0 1

    S[3] = 134

    Prepared By

  • 7/30/2019 3.Disjoint Sets

    35/40

    Prepared By

    AnithaPadmanabanUnion (5,6)

    Here,

    If (S[R1] = = S[R2]) then

    S[R] = S[R1]-1

    ie., S[5] = = S[6]

    S[5] = S[5] 1

    = 0 1

    = 1

    0 0 -1 3 -1 5 0 0

    1 2 3 4 5 6 7 8

    0 0 -1 3 0 0 0 0

    1 2 3 4 5 6 7 8

    35

    Prepared By

  • 7/30/2019 3.Disjoint Sets

    36/40

    Prepared By

    AnithaPadmanabanUnion (3,5)

    Here,

    If (S[R1] = = S[R2]) then

    S[R] = S[R1]-1

    ie., S[3] = = S[5]

    S[3] = S[3] 1

    = 1 1

    = 2

    0 0 -1 3 -1 5 0 0

    1 2 3 4 5 6 7 8

    0 0 -2 3 -1 5 0 0

    1 2 3 4 5 6 7 8

    36

    Prepared By

  • 7/30/2019 3.Disjoint Sets

    37/40

    Prepared By

    AnithaPadmanabanUnion (2,3)

    Here,

    S[R2] < S[R1]

    i.e, S[3] < S[2]

    therefore, S[2] = 3

    0 0 -2 3 -1 5 0 0

    1 2 3 4 5 6 7 8

    0 3 -2 3 -1 5 0 0

    1 2 3 4 5 6 7 8

    If S[R1] is equal to S[R2] thenboth S[R1] and S[R2] is updated

    else

    S[R1] is alone updated

    37

    Prepared By

  • 7/30/2019 3.Disjoint Sets

    38/40

    Prepared By

    AnithaPadmanaban

    Path Compression

    Path compression is performed during a Find

    operation and is independent of the strategy used

    to perform unions

    Modify Find (X) such that every node on the path

    from X to the root has its parent changed to the

    root

    Effectively reduce the length of the path

    38

    Prepared By

  • 7/30/2019 3.Disjoint Sets

    39/40

    Prepared By

    AnithaPadmanaban

    39

    Prepared By

  • 7/30/2019 3.Disjoint Sets

    40/40

    Prepared By

    AnithaPadmanaban

    40