Unit 2 Disjoint Sets
Unit 2 Disjoint Sets
These sets can be represented by storing every element of the set in the same array. Since the
set elements are numbered 1 through n, we represent the tree nodes using an array P[1:n],
where n is the maximum number of elements. The ith element of this array represents the
tree node that contains element i. This array element gives the parent pointer of the
corresponding tree node. Notice that the root nodes have parent as -1.
i [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
parent -1 1 1 -1 4 1 4 -1 8 8
Array representation of sets A,B,C
OPERATIONS ON SETS
1) MEMBER (a,S)
Determine whether a is a member of set S, if so print “yes” otherwise print “no”
Example: S={1,2,3}
MEMBER (2,S) it means 2 belongs to S or not
The function prints Yes
2) INSERT(a,S) Replaces set S by S U {a}
Example: INSERT(4,s)
S=S U {4}
S={1,2,3,4}
3) DELETE(a,S) Replaces set S by S – {a}
Example DELETE(2,S)
S=S-{2}
S={1,3,4}
4) UNION(S1,S2,S3)
Calculates S3=S1 U S2
We assume s1 and s2 are disjoint
S1={1,2} s2={4,6}
S3=S1 U S2 = {1,2,4,6}
UNION operation
UNION(i,j) it means the elements of set i and elements of set j are combined. If we want to
represent UNION operation in the form of a tree; the UNION(i,j) i is parent ; j is the child.
UNION of S1 and S2 S1US2
To obtain the union of two sets, all that has to be done is to set the parent field of one of the
roots to the other root. This can be accomplished easily if with each set name, we keep a
pointer to the root of the tree representing the set.
Algorithm UNION(i,j)
{
//replace the disjoint sets with roots i and j.
integer i,j
PARENT(j):=i;
}
initially assume that the parent array contains all zeroes.
P
0 0 0 0 0 0
1 2 3 4 5 6
UNION(1,3)
P
0 0 1 0 0 0
1 2 3 4 5 6
UNION(2,5)
P
0 0 1 0 2 0
1 2 3 4 5 6
UNION(1,2)
P
0 1 1 0 2 0
1 2 3 4 5 6
Since the time taken for a UNION is constant, all the n-1 unions can be processed in time
O(n). However each find required following sequence of parent pointers from the element to
be found to the root. Since 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 finds is O(∑ i) = O(n2).
UNION(1,3)
FIND(1) ------------1
FIND(3)-------------1 since its parent is 1 (i.e root node is 1)
Algorithm FIND(i)
{
//find the root for the tree containing elements
integer i,j
j = i;
while (parent[j]>0) do
j:=parent[j];
return j;
}
Array representation is
FIND(5) i=5, j=i , j=5 P
0 1 1 0 2 0
1 2 3 4 5 6
Time Complexity : Each find requires following chain of parent links from node i to root.
The time required to process a FIND for an element at level i of a tree is O(i). Hence the total
time needed to process the n-2 finds is O(n2).
To implement the weighting rule, we need to know how many nodes are there in every tree.
To do this easily, we maintain a count field in the root of every tree. If i is a root node then
count[i] equals the number of nodes in that tree. Since all the nodes other than the roots of
trees have a positive number in the P field, we can maintain the count in the P field of roots
as a negative number.
Using this convention, the time required to perform a union has increased some what but still
bounded by a constant. i.e O(1).
UNION (1,2) the number of nodes in tree 1 is equal to the number of nodes in tree 2
2 3 … ….. n
UNION (2,3) the number of nodes in tree 2 is 2. The number of nodes in tree3 is 1
So 2>1 make parent as 2 and 3 as child
UNION(FIND(3),4)
UNION(FIND(n-1),n)
temp:=p[i]+p[j];
if (p[i]>p[j] then
{
// I has fewer nodes
p[i]:=j;
p[j]:=temp;
}
else
{
// j has fewer nodes
p[j]:=i;
p[i]:=temp;
}
}
COLLAPSING RULE
If j is a node on the path from i to its root then set Parent(j)←root(i). A more sophisticated
algorithm of FIND using collapsing rule is given below.
Algorithm Find(i)
{
j←i;
while (parent[j]>0)
{
j←parent[j];
}
k←i;
while(k≠j)
{
t←parent[k];
parent[k]←j;
k←t;
}
return(j);
}
initially
UNION(1,2)
UNION(3,4)
UNION(5,6)
UNION(7,8)
UNION(1,5)
FIND(8) = root node i.e.,, By using first FIND algorithm it will require 3 moves
upword to reach the root node. Similarly for 8 FIND operations it will require 8*3=24 moves.
If we use FIND algorithm with collapsing rule, the first FIND(8) requires going up 3 link
fields (moves) and then resetting 3 links. Each of the remaining 7 FINDs requires going up
only 1 move (link field), the total cost is now only 13 moves.
S1 U S2
Examples) Arrays, Linked lists, Trees, Structures, Stacks, Queues, Graphs, Algebraic
expressions
STACK
Def : A Stack is an ordered collection of data items, into which new items may be inserted
and from which items may be deleted at one end called Top of the Stack.
4 D
3 C Top
2 B
1 A
Unlike an array stack provides the insertion and deletion of items. So a stack is a
dynamic, constantly changing object.
New items may be put on top of the stack or items which are at the top of the stack
may be removed.
The two changes which can be made to a stack are given special names, those are
PUSH and POP. When an item is added to a stack it is pushed onto the stack and when an
item is removed it is popped from the stack.
Although an array cannot be a stack it can be the home of the stack i.e., an array can
be declared with a range that is large enough for the maximum size of the stack. During the
course of program execution the stack will grow and shrink within the space reserved for it.
One end of the array will be fixed bottom of the stack while the top of the stack will
constantly shift as items are popped and pushed. Thus another field is needed to keep track of
the current position of the top of the stack.
QUEUE
Def: A Queue is a ordered collection of items from which items may be deleted at one end
called the front of the Queue and into which items may be inserted at the other end called the
rear of the queue.
Function to insert an element into a queue.
Algorithm insert (q,x)
qtype *q;
int x;
{
if (q rear = maxQ ) then
printf(“Error – Q overflow\n”);
else
{
q rear = q rear +1;
q entry[qrear] = x;
}
}
Function to remove / delete an item from a queue.
Algorithm delete(q)
qtype *q;
{
int x;
x = q entry[q front];
q front = q front +1;
return (x);
}
Insertion and deletion for queues can be carried out in a fixed amount of time or O(1).
Traversal of binary tree:One of the most important operations on a binary tree is traversal,
moving through all the nodes of the binary tree, visiting each one in turn.
We can traverse the binary tree in many ways.
Preorder VLR
Inorder LVR
Postorder LRV
With preorder traversal, the node is visited before the subtrees.
With inorder traversal, the node is visited between the subtrees.
With postorder traversal, the node is visited after the subtrees.
In all cases left is visited before right.
Root
A Preorder A B C
Inorder B A C
B C Postorder B C A
To traverse a non-empty binary tree in preorder, we perform the following three operations.
(i) Visit the root
(ii) Traverse the left subtree in preorder
(iii) Traverse the right subtree in preorder
To traverse a non-empty binary tree in inorder, we perform the following three operations.
(i) Traverse the left subtree in inorder.
(ii) Visit the root.
(iii) Traverse the right subtree in inorder.
To traverse a non-empty binary tree in postorder, we perform the following three operations.
(i) Traverse the left subtree in postorder.
(ii) Traverse the right subtree in postorder.
(iii) Visit the root.
At this stage by seeing the preorder ‘B’ comes before ‘D’. So ‘B’ is
the next root and from the inorder we see ‘B’ has an empty right
subtree and ‘D’ is in it’s left subtree.
Algorithm preorder(root)
{
treenode *p,*q;
Algorithm inorder(root)
{
treenode *p;
top := 0;
p:=root;
do
{
while (p!=NULL)
{
push(s,p);
p:=p->left;
}
if(!empty(s)) then
{
p := pop(s);
print( p->data);
p := p->right;
}
} while( (!empty(s)) || (p!=NULL) );
}
Algorithm postorder(root)
{
treenode *p,*q,*a;
a=(treenode *)malloc(sizeof(treenode));
a->data := 1;
a->left := NULL;
a->right := NULL;
top:=0;
push(s,NULL);
push(s,NULL);
p:=root;
while(p!=NULL)
{
q:=p;
while (q!=NULL)
{
push(s,q);
if (q->right != NULL) then
{
push(s,q->right);
push(s,a);
}
q:=q->left;
}
while((q=pop(s))>0)
{
if (q=a) then
break;
else
print(q->data);
}
p=pop(s);
}
}
Graph Traversal
A graph traversal means visiting all the nodes of the graph. Two graph traversal methods are
commonly used. These are,
Depth First Search (DFS)
Breadth First Search (BFS)
DEPTH FIRST SEARCH: In graphs, we do not have any start vertex or any
special vertex signaled out to start traversal
from. Therefore the traversal may start from
any arbitrary vertex.
We start with vertex v. An adjacent
vertex is selected and a depth first search is
initiated from it. i.e. V1,V2,….Vk are adjacent
vertices to vertex v. We may select any vertex
from this list. Say we select v1. Now all the
adjacent vertices to v1 are identified and all of
those are visited. Next v2 is selected and all its
adjacent vertices visited and so on. This
process continues till all the vertices are visited.
Consider the following graph.
Complexity of DFS: The procedure DFS is called once for each vertex of G. We can
calculate the complexity of DFS by adding up the time taken by each of these calls to DFS.
There are two parts to each call, the marking process and the for loop. The marking process
takes a constant amount of time say C1. Each time through the loop, the for loop takes a
constant amount of time to test the condition say C2. And a constant amount of time say c3 as
an upper bound for the time to execute the body of the loop.
We start with V1. Its adjacent vertices are V2, V8, V3. We visit all one by one.
We pick on one of these, say V2. The unvisited adjacent vertices to V2 are V4, V5. We visit
both. We go back to the remaining visited vertices of V1 and pick on one of those say V3. The
unvisited adjacent vertices are V6, V7. There are no more unvisited adjacent vertices of V8,
V4, V5, V6 and V7. Thus the sequence so generated is V1 V2 V8 V3 V4 V5 V6 V7
Here we need a queue instead of a stack to implement it. We add unvisited vertices adjacent
to the one just visited at the rear and read at front to find the next vertex to visit.
V1 V1 V1
V2 V8 V3
V2 V3
V8 V4 V5
bfs(v1)
visit v1
add v1 to queue
delete queue now v is v1
adjacent vertices of V1 are v2, v8, v3
unvisited vertices are visited and added to queue. V2 v8 v3 are visited and added to queue
delete queue. Now v is v2
adjacent vertices of V2 are v1, v4, v5
v1 is already visited . unvisited vertices v4, v5 are visited and added to queue
delete queue. Now V is V8
adjacent vertices of V8 are V4, V5,V1,V6,V7
V4,V5,V1 are already visited . unvisited vertices V6, V7 are visited and added to queue
Complexity of BFS:The Operations of enqueing and dequeing take O(1) time. So the
total time devoted to queue operations is O(V).The total time spent in scanning
adjacency list is O(E). The total run time for BFS is O(V+E).
Graph Representation
Graph is a mathematical structure and finds its application in many areas of interest in
which problems need to be solved using computers. Thus this mathematical structure must
be represented as some kind of data structures. Two such representations are commonly
used. These are,
Adjacent matrix Representation and
Adjacency list representation
The choice of representation depends on the application and function to be performed on the
graph.
Adjacency Matrix:
The adjacency matrix A for graph G = (V,E) with n vertices, is an nxn matrix of d
bits, such that,
1 Vertice 1 2 3 4 5
1 0 1 1 0 1
2 1 0 1 0 0
3 1 1 0 1 1
4 0 0 1 0 1
5 1 0 1 1 0
2 5
3 4
You may observe that the adjacency matrix for an undirected graph is symmetric, as the
lower and upper triangles are same. Also all the diagonal elements are zero.
2 3 5 7
Vertex 1 2 3 4 5 6 7
1 0 1 1 1 0 0 0
2 0 0 0 0 0 0 0
3 0 0 0 0 1 0 0
4 0 0 0 0 0 1 0
5 0 0 0 1 0 0 1
6 0 0 0 0 0 0 0
7 0 0 0 0 0 1 0
The total number of 1’s account for the number of edges in the digraph. The number of 1’s in
each row tells the out degree of the corresponding vertex.
The adjacency list representation needs a list of all of its nodes. i.e.
The adjacency list representation needs a list of all of its nodes. i.e.
V1
V2
V3
V4
V5
V6
And for each node a linked list of its adjacent node. Therefore we shall have
Note that adjacent vertices may appear in the adjacency list in arbitrary order. Also an arrow
from v2 to v3 in the list linked to v1 does not mean that v2 and v3 are adjacent.
Connected Graph: In an undirected graph G, two vertices are said to be connected iff there
is a path from u to v ( since G is an undirected graph this means there must also be a path
from v to u). An undirected graph is said to be connected iff for every pair of distinct vertices
u and v in V(G), there is a path from u to v in G.
a)G1 b)G2
G1 and G2 are Connected graphs
G3
Graph G3 is not connected. It is one graph having two unconnected components. Since there
are unconnected components, it is an unconnected graph
1
A weakly connected graph
4 5
There does not exist a directed path from vertex 1 to vertex 4 also from vertex 5 to other
vertices and so on. Therefore it is a weakly connected graph.
2 Strongly Connected
Graph
4 5
An Un-Directed Graph
An Un-Directed Graph
After vertex B, if the Graph structure still remains connected, it is said to be a bi-connected
graph.
An Un-Directed Graph
• Given graph is not a bi-connected graph.
• The Articulation Points are 2,3,5
• To Transform the given graph into bi-connected graph, the new edges are included
corresponding to the Articulation point.
• Edges corresponding to the Articulation point 3-(4, 10) (10, 9)
• Edges corresponding to the Articulation point 2-(1, 5) (3, 8)
• Edges corresponding to the Articulation point 5-(6, 7).
3. Now we look for all the adjacent vertices excluding the just earlier
vertex of newly added vertex and the remaining adjacent vertices of earlier vertices, i.e.,
we have D,E and G (as adjacent vertices of F) and B (as remaining adjacent vertex of A).
Consider the following graph and some of the tree structures for the graph.
A A
B C
D E
G
B C D E F H
Spanning Tree
F
G H
A A
B C
B C D E
D
E
F
F
G H
G
SPANNING TREES
You may notice that all the spanning trees differ from each other significantly, however for
each structure
i) The vertex set is same as that of graph G
ii) The edge set is a subset of G(E) and
iii) There is no cycle.
Such a structure is called spanning tree of graph. Take any vertex V as an initial partial tree
and add edges one by one so that each edge joins a new vertex to the partial tree.
Frequently we encounter weighted graphs and we need to build a sub-graph that must include
every vertex in the graph. To construct such a graph with least weight or least cost we must
not have cycles in it.
Consider the above graph. The MST for this graph could be building a least cost
communication network.
We begin by first selecting an edge with least cost, it can between any
two vertices of graph G. Subsequently from the set of remaining edges,
we can select another least cost edge and so on. Each time an edge is
picked, we determine whether or not the inclusion of this edge into the
spanning tree being constructed creates a cycle. If it does this edge is
discarded. If no cycle is created, this edge is included in the spanning tree being constructed.
The minimum cost is BA.
Now we have
Vertices that may be EDGE COST
attached
BF 8
F
AF 7
G BG 9
C AC 4
D AD 5
The least cost is AC. Therefore we choose AC. Now we have
AF is the minimum cost edge. Therefore we add it to the partial tree. Now we have
Vertices that may EDGE COST
be attached
BG 9
G
CG 10
CE 8
E DE 9
FE 11
Obvious choice is CE.
The only vertes left is G and we have the minimum cost edge that connects it to the tree is
BG. Therefore we add it and the minimal spanning tree constructed would be of costs
9 + 3 + 7 + 5 + 4 + 8 = 36.
POSSIBLE QUESTIONS:
1)Write a non recursive algorithm of inorder traversal of a tree and also analyse its time
complexity.
2)Write a non recursive algorithm of preorder traversal of a tree.
3) Write a non recursive algorithm of postorder traversal of a tree.
4) The inorder and preorder traversals of a binary tree are
Inorder – D B A C E
Preorder– A B D C E
Give the postorder traversal for the above tree.
5)Show that the inorder and preorder sequences of a binary tree uniquely define binary tree.
Preorder : ABDIEHJCFKLGM
Inorder : DIBHJEAFLKCGM