Sei sulla pagina 1di 35

SETS

• Sets are represented using trees.


Assumptions :
1.The elements of a set are numbered
1,2…11.The numbers are indices into a
symbol table where the actual names are
stored.
2.We assume that the sets are pair wise
disjoint i.e. if si and sj are two sets ij then si
 sj = .
1

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
SETS (Contd..)
• Two operations on sets are useful
(a) Disjoint set union
(b) To find the set containing element i
Let S1 = {1,7,8,9}, S2= {2,5,10}, S3= {3,4,6}

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
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)
SETS (Contd..)
• To take the union of two sets , we make one
of the trees as sub tree of the other .Union of
s1 and s2 could be
1 2
2 or 1
7 8 9 5 10
5 10 7 8 9

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
SETS (Contd..)
• In the algorithms UNION and FIND set
name is represented by the index of the
root, and nodes are numbered 1 through n
so 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)
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)
SETS (Contd..)
Procedure F(i)
// find the root of the tree //
// containing element i //
integer i, j // j is the temporary variable //
Ji
while parent (j) >0 do // parent(j)=0 if i is root //
jParent (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)
Analysis of UNION and FIND

• The time for a UNION is constant.


• The time to find a node at level i (i is the highest
level) with maximum n nodes is 0(log2n) because
n=2 i –1,
• Consider the worst case with both UNION and
FIND.
• 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).
8

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Analysis of UNION and FIND (Contd..)

• We are combining two sets ,and find the element


at lowest level ,Since the time for a union is
constant ,All (n-1) unions can be processed in
O(n) time.
• The time required to process a FIND for an
element 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)
1in-1
• The time can be improved using Weighting rule
for UNION(i,j).

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Analysis of UNION and FIND (Contd..)

n-1
:

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

10

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Weighting Rule
If the number of nodes in tree i is less
than the number in tree j, then make j the
parent of i, otherwise make i the parent of
j.
Example : Sets are 1 2 ….. n
2 3….. n
1 2 2 … 2
1 3 1 3 4 1 5 .. n
Union(1,2) Union(2,3) Union(3,4)
11

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Weighting Rule contd..
• The algorithm for UNION using weighting
rule is as below .
• The number of nodes in the tree is
maintained in the parent field as a negative
number to distinguish it from a pointer.

12

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Algorithm for UNION using weighting rule

Procedure UNION (i,j)


//union of sets with roots i and j ij 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
xParent (i)+Parent(j)
if parent(i)>parent(j) //i has less nodes//
then parent(i)j
parent(j)x
else parent(j)i //j has less nodes//
parent(i)x
end if
13
end UNION

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
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 <= log k+1 for all k <= n
• Consider the case k = n+1
• With increase in one node the level in the union may increase
by only one. Hence
• Level of any node <= log n+1 +1 <= log(n+1) +1 thus proved
(log1=0;log2=0.3; log3= 0.477; log4= 0.6

14

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

• The maximum time to process a find is at most


0(log n), Thus to process n finds the time is
O(nlog n).
• Further improvement is possible by modifying
FIND algorithm using the collapsing rule
• If j is a node on the path from i to its root then set
parent(j)root(i).
• This roughly doubles the time for an individual find.
• In the worst case this change is a considerable
improvement over just using the weighing rule.

15

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
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 //
ji // j changes to parent //
while parent(j)>0
j  parent(j)
Repeat // k is a node between //
ki // i and j //
while kj do // k is not the root //
tparent(k) // t is parent(i) //
parent(k)j // parent(k) is //
// find(i) //
kt //change k as parent of k// 16
// changes to next node //
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Worst case complexity of UNION-FIND
algorithm
• The time to process a sequence of UNIONS
and FINDS is stated as follows:
• Let T(m,n) be the worst case time required
to process an intermixed sequence of m  n
FINDS and n-1 UNIONS
Then
K1m(m,n)T(m,n)k2m(m,n)
for some ve constants k1 and k2
17

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Worst case complexity of UNION-FIND
algorithm contd..
where (m,n)=min{ z1 /A(z,4m/n>log2n}
A(p,q) is called Ackermann’s function and defined as below
A(p,q) =2q p=0 and q>=1
0 q=0 and p1
2 p1 and q=1
A(p,1).A(p,q-1) p1 and q2
A(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)

18

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Worst case complexity of UNION-FIND algorithm
contd..

• For m<>0, (m,n)<=3 for log n < A (3,4)


• For all practical purposes, we may assume
log n < A (3,4) and hence (m,n)<= 3
• So, K1m(m,n)T(m,n)k2m(m,n) becomes
K1m3T(m,n)k2m3
i.e. 3K1mT(m,n) 3k2m
i.e. C1mT(m,n) C2m
i.e. T(m,n) = o(m)
19

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Applications of UNION and FIND

Applications of UNION and FIND


• Processing equivalence statements in a
programming language.
• If ij 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.

20

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
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 of
vertices.
• Each pair in E is an edge of G.
• If the edges (i, j) are not ordered, it is
an undirected graph. 1
2
• If cost is attached, it is a network 3 21
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
GRAPHS (Contd..)

• Adjacency of vertices: Vertex i is


adjacent to vertex j if the edge (i,j)
exists.
• The degree of a vertex is the number
of its adjacent vertices.
• In-degree of a vertex i is the number
of edges for which i is the second
component in a directed graph.

22

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
GRAPHS (Contd..)
• Out-degree of a vertex i is the
number of edges with i as the first
component. 1 2
In-degree of 2 is 2
Out degree of 3 is 2 3

23

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

• A path from vertex 1 to vertex n is the


sequence 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 edges
in it.
• A simple path is a path in which all vertices
except possibly the first and last are distinct.
• A cycle is a simple path in which the first and
last vertices are the same.
• Connectedness: An undirected graph is
called connected if there exists a path
24
between every pair of vertices.
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
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 there
exists a path from i to j and a path from
j to i, the directed graph is called
strongly connected.
25

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Representation of graphs in memory

1. Sequential representation

2. Linked representation

26

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
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).

27

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

1 2

3 4

5
Figure - 1

28

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Sequential Representation
(Contd..)
1 2 3 4 5
1 0 1 1 1 0 Initializing the
2 1 0 1 0 0 adjacency matrix
3 1 1 0 0 0 takes o(n2)
4 1 0 0 0 1 operations
5 0 0 0 1 0

29

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Linked Representation
• Given a graph with n vertices its adjacency
list representation consists of n lists, one for
each vertex i.
• The list for vertex i contains those vertices
adjacent 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.
30

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Linked Representation
(Contd..)
1 Head nodes
2 2 3 4 0
3 1 3 0
4 1 2 0
1 5 0
5
4
1 2

3 4
31
5
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Sequential Adjacency Lists

• In case no insertions or deletions of


edges are to be performed on the
graph, the adjacency lists may be
represented sequentially in one
dimensional array V (1..p) where p= e
or p = 2e depending on the graph is
directed or undirected
• HEAD (i) for 1 i n gives the starting
point for the adjacency list for vertex i.
32

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

• If a list for vertex i is empty, then


HEAD(i)=HEAD (i+1)
• If we define HEAD (n+1)= p+1, (p =e or
2e) then the vertices on the adjacency list
for vertex i are stored in vertex (j) where
HEAD(i) <= j < HEAD(i+1)
(to take care of the last node HEAD (n+1) is
assigned p+1)

33

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

• The sequential adjacency list representation is shown for


the undirected graph given below.

1 2

3 4

34

Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Sequential Adjacency Lists
(Contd..)
Head (1) Head (2) Head (3) Head (4)
Head (5)

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

35

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

Potrebbero piacerti anche