Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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 //
Ji
while 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)
Analysis of UNION and FIND
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Analysis of UNION and FIND (Contd..)
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Analysis of UNION and FIND (Contd..)
n-1
:
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
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
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 //
ji // j changes to parent //
while parent(j)>0
j parent(j)
Repeat // k is a node between //
ki // i and j //
while kj 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// 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{ z1 /A(z,4m/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 p1
2 p1 and q=1
A(p,1).A(p,q-1) p1 and q2
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..
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Applications of UNION and FIND
20
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
GRAPHS
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..)
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
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Sequential Adjacency Lists contd..
33
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Sequential Adjacency Lists (Contd..)
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)