Sets Graphs DS

Embed Size (px)

Citation preview

  • 8/6/2019 Sets Graphs DS

    1/35

    1

    SETS

    Sets are represented using trees.

    Assumptions :

    1.The elements of a set are numbered1,211.The numbers are indices into asymbol table where the actual names arestored.

    2.We assume that the sets are pair wisedisjoint i.e. if si and sj are two sets ij then si sj = .

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    2/35

    2

    SETS (Contd..)

    Two operations on sets are useful

    (a) Disjoint set union

    (b) To find the set containing element iLet S

    1= {1,7,8,9}, S

    2= {2,5,10}, S

    3= {3,4,6}

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    3/35

    3

    SETS (Contd..)

    The tree representation of these sets are:

    1 2 3

    7 8 9 5 10 4 6

    S1 S2 S3

    Child nodes are linked to parent/root node.

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    4/35

    4

    SETS (Contd..)

    To take the union of two sets , we make one

    of the trees as sub tree of the other .Union ofs1 and s2 could be

    1 2

    2 or 1

    7 8 9 5 105 10 7 8 9

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    5/35

    5

    SETS (Contd..)

    In the algorithms UNION and FIND set

    name is represented by the index of the

    root, and nodes are numbered 1 through nso that the node index corresponds to the

    element index .

    Each node needs only one field, the parent

    field to link to its parent.

    Root nodes have a parent field of zero.

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    6/35

    6

    SETS (Contd..)

    Procedure U(i,j)

    //replace the disjoint sets with roots i and

    j by their union//integer i,j

    Parent(i)j

    end U

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    7/35

    7

    SETS (Contd..)

    Procedure F(i)

    // find the root of the tree //

    // containing element i //

    integer i, j // j is the temporary variable //J iwhile parent (j) >0 do // parent(j)=0 if i is root //

    jParent (j)repeat // while loop is executed //

    return(j)

    end F // maximum levels 1 times //

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    8/35

    8

    Analysis of UNION and FIND

    The time for a UNION is constant.

    The time to find a node at level i (i is the highestlevel) with maximum n nodes is 0(log2n) because

    n=2 i 1,

    Consider the worst case with both UNION andFIND.

    Suppose n elements are in n sets i.e. Si {i}

    1 I n Parent(i) = 0 1 I n Suppose we require

    U(1,2), F(1), U(2,3), F(1), U(3,4). F(1)..U((N-1),N).

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    9/35

    9

    Analysis of UNION and FIND (Contd..)

    We are combining two sets ,and find the elementat lowest level ,Since the time for a union isconstant ,All (n-1) unions can be processed inO(n) time.

    The time required to process a FIND for anelement at level i of a tree is O(i).

    The total time needed to process the n-2 finds is

    i 1+2+3+..+n-2=(n-2)(n-1)/2=O(n2)1in-1

    The time can be improved using Weighting rulefor UNION(i,j).

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    10/35

    10

    Analysis of UNION and FIND (Contd..)

    n

    n-1:

    1

    A worst case tree n-1 unions n-2 finds in all.

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    11/35

    11

    Weighting Rule

    If the number of nodes in tree i is lessthan the number in tree j, then make j theparent of i, otherwise make i the parent of

    j.Example : Sets are 1 2 .. n

    2 3.. n

    1 2 2 21 3 1 3 4 1 5 .. n

    Union(1,2) Union(2,3) Union(3,4)

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    12/35

    12

    Weighting Rule contd..

    The algorithm for UNION using weightingrule is as below .

    The number of nodes in the tree ismaintained in the parent field as a negativenumber to distinguish it from a pointer.

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    13/35

    13

    Algorithm for UNION using weighting rule

    Procedure UNION (i,j)

    //union of sets with roots i and j ij using weighing rule////count(i) is the number of nodes in tree with root i//

    //parent (i) =-count(i)//

    //parent(j)=-count(j)//

    integer i,j,k

    xParent (i)+Parent(j)if parent(i)>parent(j) //i has less nodes//

    then parent(i)

    jparent(j)xelse parent(j)i //j has less nodes//

    parent(i)xend if

    end UNION

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    14/35

    14

    Complexity of Union using weighting rule

    The time of union is still bounded by a constant. Lemma: Let T be a tree with n nodes created using the

    algo.UNION.

    No node in T has level greater than log n+1 Proof by induction: Let n=1; level =1

    Let it be true for k = n i.e. Level of any node

  • 8/6/2019 Sets Graphs DS

    15/35

    15

    Collapsing rule for FIND

    The maximum time to process a find is at most0(log n), Thus to process n finds the time is

    O(nlog n).

    Further improvement is possible by modifyingFIND algorithm using the collapsing rule

    If j is a node on the path from i to its root then setparent(j)root(i).

    This roughly doubles the time for an individual find.

    In the worst case this change is a considerableimprovement over just using the weighing rule.

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    16/35

    16

    Algorithm FIND using collapsing rule

    The algorithm FIND using collapsing rule is as follows

    Procedure FIND(i)

    // collapse all nodes from i //

    // to the root j //

    ji // j changes to parent //while parent(j)>0

    j parent(j)Repeat // k is a node between //

    ki // i and j //while k j do // k is not the root //tparent(k) // t is parent(i) //parent(k) j // parent(k) is //

    // find(i) //

    kt //change k as parent of k//// changes to next node //

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    17/35

    17

    Worst case complexity of UNION-FIND

    algorithm

    The time to process a sequence of UNIONSand FINDS is stated as follows:

    Let T(m,n) be the worst case time requiredto process an intermixed sequence of m n

    FINDS and n-1 UNIONS

    Then

    K1m(m,n)T(m,n)k

    2m(m,n)

    for some ve constants k1

    and k2

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    18/35

    18

    Worst case complexity of UNION-FIND

    algorithm contd..

    where (m,n)=min{ z1 /A(z,4m/n>log2n}A(p,q) is called Ackermanns function and defined as below

    A(p,q) =2q p=0 and q>=1

    0 q=0 and p12 p1 and q=1A(p,1).A(p,q-1) p1 and q2A(p,q) is a very rapidly growing function satisfying

    (a) (a) A(p,q+1)> A (p, q)(b) A(p+1,q)>A(p,q)

    (c) A(3,4)2 2.2 (65536 twos)

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    19/35

    19

    Worst case complexity of UNION-FIND algorithm

    contd..

    For m0, (m,n)

  • 8/6/2019 Sets Graphs DS

    20/35

    20

    Applications of UNION and FIND

    Applications of UNION and FIND

    Processing equivalence statements in a

    programming language. If ij is to be processed , we must determine

    the sets containing i and j, if they are

    different, then the two sets are to be

    replaced by their union.

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    21/35

    21

    GRAPHS

    A graph G is a set of two sets V and E; V is a finite non-empty set of

    vertices; E is a finite set of pairs ofvertices.

    Each pair in E is an edge of G.

    If the edges (i, j) are not ordered, it isan undirected graph. 1

    2

    If cost is attached, it is a network 3Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    22/35

    22

    GRAPHS (Contd..)

    Adjacency of vertices: Vertex i isadjacent to vertex j if the edge (i,j)exists.

    The degree of a vertex is the numberof its adjacent vertices.

    In-degree of a vertex i is the number

    of edges for which i is the secondcomponent in a directed graph.

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    23/35

    23

    GRAPHS (Contd..)

    Out-degree of a vertex i is thenumber of edges with i as the firstcomponent.

    In-degree of 2 is 2

    Out degree of 3 is 2

    1 2

    3

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    24/35

    24

    GRAPHS (Contd..)

    A path from vertex 1 to vertex n is thesequence of vertices such that ( 1,2) ( 2,3) (n-1, n) are edges in the graph.

    The length of a path is the number of edgesin it.

    A simple path is a path in which all verticesexcept possibly the first and last are distinct.

    A cycle is a simple path in which the first andlast vertices are the same.

    Connectedness: An undirected graph iscalled connected if there exists a path

    between every pair of vertices.Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    25/35

    25

    GRAPHS (Contd..)

    Sub-graph of a graph G is a subset VB

    of vertices in G and the subset of the

    edges which connect vertices of VB. A connected component is a maximal

    connected sub-graph

    If for every pair of vertices i, j thereexists a path from i to j and a path from

    j to i, the directed graph is called

    strongly connected.

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    26/35

    26

    Representation of graphs in memory

    1. Sequential representation

    2. Linked representation

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    27/35

    27

    Sequential Representation

    Graph is represented as a two

    dimensional array

    For an undirected graph, graph(i,j)=1 if (i,j)is an edge otherwise graph(i,j)=0

    If the graph is a network graph (i,j)=cost of

    edge (i,j).

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    28/35

    28

    Sequential Representation

    (Contd..)

    Figure - 1

    1

    3

    2

    4

    5

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    29/35

    29

    Sequential Representation

    (Contd..)1 2 3 4 5

    1 0 1 1 1 0 Initializing the

    2 1 0 1 0 0 adjacency matrix3 1 1 0 0 0 takes o(n2)

    4 1 0 0 0 1 operations

    5 0 0 0 1 0

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    30/35

    30

    Linked Representation

    Given a graph with n vertices its adjacencylist representation consists of n lists, one foreach vertex i.

    The list for vertex i contains those verticesadjacent from i.

    The heads of the lists are stored sequentially.

    A directed graph requires n locations (for

    vertices) and e nodes for edges. An undirected graph requires n locations plus

    2e nodes.

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    31/35

    31

    Linked Representation

    (Contd..)1

    2

    3

    4

    54

    1

    1

    1

    2 3

    3 0

    2 0

    5 0

    4 0

    Head nodes

    21

    34

    5

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    32/35

    32

    Sequential Adjacency Lists

    In case no insertions or deletions ofedges are to be performed on the

    graph, the adjacency lists may berepresented sequentially in onedimensional array V (1..p) where p= eor p = 2e depending on the graph is

    directed or undirected HEAD (i) for 1 i n gives the startingpoint for the adjacency list for vertex i.

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    33/35

    33

    Sequential Adjacency Lists contd..

    If a list for vertex i is empty, thenHEAD(i)=HEAD (i+1)

    If we define HEAD (n+1)= p+1, (p =e or2e) then the vertices on the adjacency listfor vertex i are stored in vertex (j) where

    HEAD(i)

  • 8/6/2019 Sets Graphs DS

    34/35

    34

    Sequential Adjacency Lists (Contd..)

    The sequential adjacency list representation is shown for

    the undirected graph given below.

    1 2

    3 4

    5

    Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)

    http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/http://www.novapdf.com/
  • 8/6/2019 Sets Graphs DS

    35/35

    35

    Sequential Adjacency Lists

    (Contd..)Head (1) Head (2) Head (3) Head (4)Head (5)

    Vertex

    (10)(9)(8)(7)(6)(5)(4)(3)(2)(1)

    4512131432