mca notes for students

Upload: rohit-chaudhary

Post on 10-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 MCA NOTES FOR STUDENTS

    1/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Data Structures For Disjoint

    Sets

  • 8/8/2019 MCA NOTES FOR STUDENTS

    2/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Disjoint Sets Some applications involve grouping n

    distinct elements into a collection of disjoint

    sets Two main operations needed

    Which set contains a given element?

    Uniting two sets

    Two different representations

    Linked-List

    Tree-based

  • 8/8/2019 MCA NOTES FOR STUDENTS

    3/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Disjoint Sets A disjoint data structure maintains a

    collection S = {S1,S

    2,....,S

    n}

    Each set is identified by a representative,which is some member of the set.

    Choice of representative is dependent on the

    application it could be any member,or theremight be certain criteria that determine which

    element to use.

  • 8/8/2019 MCA NOTES FOR STUDENTS

    4/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Required Operations

    Make_set(x)

    Creates a new set whose only member (and thus

    representative) is x.

    Union(x,y)

    Unions the sets that contain x and y. New

    representative needs to be chosen, and the old sets

    need to be removed.

    Find_set(x)

    Returns a pointer to the representative of the set

    containing x.

  • 8/8/2019 MCA NOTES FOR STUDENTS

    5/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Running Time Analysis

    For both methods, we will analyze the running time by

    looking at two parameters:

    n number of Make_set operations

    m number of Make_set, Union, and Find_set

    operations

    Maximum value of Union operations = n 1

    m n

  • 8/8/2019 MCA NOTES FOR STUDENTS

    6/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Linked List

    a b c

    /

    head

    tail

  • 8/8/2019 MCA NOTES FOR STUDENTS

    7/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Operations Make_set and Find_set are easy O(1)

    Union Appendxs list onto the end ofy.

    Use tail pointer to find the end ofy.

    New representative is the representative ofy

    Have to update every pointer inxto point to the new

    representative (linear in length ofx)

  • 8/8/2019 MCA NOTES FOR STUDENTS

    8/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Union Worst Case n Make_set operations followed by n1Union

    operations

    ith Union operation updates iobjects, so the totalnumber of objects updated is:

    T

    otal number of operations is 2n1, and so eachoperation on average requires 5(n) time.

    !

    !1

    1

    2)(

    n

    i

    ni

  • 8/8/2019 MCA NOTES FOR STUDENTS

    9/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Weighted Union

    Append the shorter list to the larger one.

    Union will still take ;(n) time if both sets have;(n) members.

    But normally,weighted union reduces the

    sequence ofm operations to O(m +n lg n) time.

  • 8/8/2019 MCA NOTES FOR STUDENTS

    10/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Disjoint-set Forests

  • 8/8/2019 MCA NOTES FOR STUDENTS

    11/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Disjoint Set Forests

    Make_set

    Create a tree with one node

    Find_set Follow parent pointers until root is reached (this

    path is called thefind path)

    Union Make root of one tree point to the root of the

    other

  • 8/8/2019 MCA NOTES FOR STUDENTS

    12/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Disjoint-set Forests

    Is this any better?

    No its possible to have a sequence ofn 1

    Union operations which creates a linear chain ofn nodes.

    However, there are two heuristics which can be

    used in conjunction with this representation to

    give a running time of almost O(m)

  • 8/8/2019 MCA NOTES FOR STUDENTS

    13/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    UnionB

    y Rank Similar to weighted-union

    Make the root of the tree with fewer nodes point

    to the root of the tree with more nodes For each node, maintain a rankthat is an upper

    bound on the height of the node.

    In union-by-rank, the root with the smaller rank

    is made to point to the other trees root.

  • 8/8/2019 MCA NOTES FOR STUDENTS

    14/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Path Compression

    During Find_set operations, make each node on

    the find path point to the root.

  • 8/8/2019 MCA NOTES FOR STUDENTS

    15/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Code MAKE_SET

    P[x]nx

    rank[x]n 0

    FIND_SET

    ifx { p[x]

    then p[x]n FIND_SET(p[x])

    return p[x]

  • 8/8/2019 MCA NOTES FOR STUDENTS

    16/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    CodeUnion

    LINK(FIND_SET(x), FIND_SET(y))

    LINKif rank[x] > rank[y]

    then p[y] nx

    else p[x] n yif rank[x] = rank[y]

    then rank[y] n rank[y] +1

  • 8/8/2019 MCA NOTES FOR STUDENTS

    17/17

    The UNIVERSITY ofNORTH CAROLINA atCHAPEL HILL

    Effects ofH

    euristics Separately

    Union-by-rank: O(m lg n)

    Path Compression: 5(n + f (1+ log2+f/n n))

    Together

    O(mE(n))

    E(n) is a slowly growing function (see 21.4 if

    interested)

    For most applications, E(n) 4, so running time

    is O(m)