0% found this document useful (0 votes)
28 views

Unit 2 Disjoint Sets

Uploaded by

androicons
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Unit 2 Disjoint Sets

Uploaded by

androicons
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Design and Analysis of Algorithms

UNIT-II DISJOINT SETS


Disjoint Sets – Disjoint set operations, Union and find algorithms, spanning trees, connected
components and biconnected components.
*-----------------*------------------*---------------------*-----------------*---------------------*
DISJOINT SETS
Consider a set S={1,2,3,4,5,6,7,8,9,10}. These elements can be partitioned into three disjoint
sets.
A = {1,2,3,6} B= {4,5,7} C= {8,9,10}
Each set can be represented as a tree. Notice that for each set we have linked the nodes from
the children to the parent, rather than our usual method of linking from parent to children.

(a) (b) (c)


Figure 1) Representation of the Sets A,B,C

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}

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 1


5) FIND(a)
Prints name of the set in which a is currently a member.
Example : FIND(4) this returns S2 since 4 belongs to S2.
6) SPLIT(a,S)
This operation partitions S into two sets S1 and S2 such that
S1={b|b<=a and b ε S}
S2={b|b>a and b ε S}
Example : S = { 1,2,4,6,9,12}
SPLIT(4,S) then S1={1,2,4} S2={6,9,12}

7) MIN(S) prints the smallest element of the set S.


Example S={2,4,10}
The function prints minimum element of set S i.e. 2.

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).

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 2


FIND Operation
FIND(i) implies that it finds the root node of ith node, in other words, it returns the name of
the set.

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;
}

Let us explore this algorithm with previous example.

Array representation is
FIND(5) i=5, j=i , j=5 P
0 1 1 0 2 0
1 2 3 4 5 6

While(p[j]>0) do condition true

j p[j] i.e., j=2

Again p[2]>0 do condition true

so j p[2] i.e., j=1

While(P[1]>0) do conidition false


Return(j) . so 1 is root node of node 5. We can observe same thing from above FIND

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).

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 3


Weighting Rule for UNION (i,j)
If the number of nodes in a tree i is less than the number in tree j, then make j the parent of i,
otherwise make i the parent of j. Both arguments of UNION must be roots.

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).

1 2 … ….. n there are n sets initially

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)

Figure : Trees obtained using the weighting rule

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 4


Algorithm UNION(i,j)
{
// UNION sets with roots i and j, i ≠ j, using weighting rule
// P[i] = -count[i] and P[j] = -count[j]

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);
}

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 5


UNION and FIND Operations

Implement the following UNION and FIND Operations

UNION(1,2) UNION(3,4) UNION(5,6) UNION(7,8)


UNION(1,3) UNION(5,7) UNION(1,5)

initially

UNION(1,2)
UNION(3,4)
UNION(5,6)
UNION(7,8)

UNION (1,3) UNION (5,7)

UNION(1,5)

Next we apply 8 FIND operations as follows

FIND(8) FIND(8) FIND(8) FIND(8)

FIND(8) FIND(8) FIND(8) FIND(8)

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.

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 6


S1= S3=
S2=

S1 U S2

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 7


DATA STRUCTURES
Data may be organised in many different ways. The logical or mathematical model of a
particular organisation of data is called a data structure.

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.

Algorithm to push an element into a stack.


Algorithm push (s,x)
// s is stack and x is the element to be pushed
{
if ( top = MAXSTACK)
print(“Error – Stack Overflow ”);
else
{
top = top+1;
s[top] = x;
}
}

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 8


Algorithm to pop an element from a stack.
Algorithm pop(s)
{
int x;
if (top = 0) then print(“Error – Stack Underflow”);
else
{
x = s[top];
top = top –1;
return (x);
}
}

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).

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 9


BINARY TREES
Binary tree is a tree in which no node can have more than two children.
Binary tree is either empty, or it consists of a node called the root together with two binary
trees called the left sub-tree and the right sub-tree.

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.

Consider the following binary tree.

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.

preorder (p) /* Function to traverse a binary tree in preorder */


treenode *p
{
if ( p != NULL)
{
printf (“%d”, p  data);
preorder (p  left);
preorder ( p  right);
}
}

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 10


inorder (p) /* Function to traverse a binary tree in inorder */
treenode *p;
{
if (p != NULL)
{
inorder (p  left);
printf(“%d”, p data);
inorder (p  right);
}
}

postorder (p) /* Function to traverse a binary tree in postorder */


treenode *p;
{
if (p!= NULL)
{
postorder(pleft);
postorder(pright);
printf(“%d”, pdata);
}
}
Problem:
The inorder and preorder traversals of a binary tree are
Inorder – DBACE
Preorder – ABDCE
Give the postorder traversal for the above tree.
Solution:
The first letter in preorder must be the root. So ‘A’ is our root. By the
definition of inorder, all nodes preceeding ‘A’ must occur in the left
subtree and all nodes succeeding ‘A’ must occur in right subtree.

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.

At this stage by seeing the preorder ‘C’


comes before ‘E’. So ‘C’ is the next
root and from the inorder we see ‘C’
has an empty left subtree and ‘E’ is in
its right subtree.

The Post order traversal is DBECA

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 11


/*Algorithm for preorder traversal using iterative method - Non-Recursive method */

Algorithm preorder(root)
{
treenode *p,*q;

top := 0; /* empty stack */


push(s,NULL);
p := root;
while (p != NULL)
{
q := p;
while (q!=NULL)
{
print(q->data);
if (q->right != NULL) then
push(s,q->right);
q = q->left;
}
p = pop(s);
}
}

/*----------Algorithm for inorder traversal using iterative method - Non-Recursive method */

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) );
}

Time complexity = O(n)

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 12


/*Algorithm for postorder traversal using iterative method - Non-recursive method */

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);
}
}

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 13


GRAPHS
A graph G consists of a set of V or vertices (nodes) and a set of edges (arcs).
We write G = (V, E).
V is a finite non empty set of vertices.
E is a set of pairs of vertices. These pairs are called edges.
An edge e=(v,w), is a pair of vertices v and w, and is said to be incident with v and w.

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.

Depth First Search:


Let us start with V1. Visit V1.
Its adjacent vertices are V2, V8, and V3.
Let us pick on V2. Visit V2.
Its adjacent vertices are V1, V4, V5.
V1 is already visited. Let us pick on V4. Visit V4.
Its adjacent vertices are V2, V8.
V2 is already visited. Let us pick on V8. Visit V8.
Its adjacent vertices are V4, V5, V1, V6, V7.
V4 and V1 are already visited. Let us pick on V5. Visit V5.
Its adjacent vertices are V2, V8.
Both are already visited. Therefore we back track.
We have V6 and V7 unvisited in the list of V8. We may visit any. We visit V6.
Its adjacent are V8 and V3. Obviously the choice is V3. Visit V3.
Its adjacent vertices are V1, V7.
We visit V7.
All the adjacent vertices of V7 are already visited, we backtrack and find that we have visited
all the vertices. Therefore the sequence of traversal is
V1, V2, V4, V8, V5, V6, V3, V7.
We may implement the depth first search method by using a stack, pushing all unvisited
vertices adjacent to the one just visited and popping the stack to find the next vertex to visit.

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 14


Algorithm for Depth First search:

Algorithm dfs (vertex V)


{
visited [V] = true;
for each w adjacent to V
if (!visited [w])
dfs(w);
}

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.

For each vertex, the loop will be calculated once


for each entry on the vertex’s adjacency list. The
total time to execute the loop for a single vertex V
is bounded by C1+(C2+C3).
Time complexity = O(|V|+|E|).

BREADTH FIRST SEARCH


In DFS we pick on one of the adjacent vertices;
visit all of the adjacent vertices and back track to
visit the unvisited adjacent vertices.
In BFS we first visit all the adjacent vertices of
the start vertex and then visit all the unvisited
vertices adjacent to these and so on.

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

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 15


V1
Algorithm bfs (vertex v)
{
vertex w;
V2 V8 V3
queue q;
visited [v] = true;
initialise (q);
addqueue (q,v) V4 V5 V6 V7
while (! Emptyqueue(q))
{
deletequeue (q,v);
for all vertices w adjacent to v
if (!visited [w])
{
addqueue (q,w);
visited[w] = true;
}
}
}
The sequence generated is V1 V2 V8 V3 V4 V5 V6 V7.

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

delete queue. Now V is V3


adjacent vertices of V3 are V1,V6,V7
V1,V6,V7 are already visited . There are no unvisited vertices of V3
Delete queue Now V is V4
Adjacent Vertices of V4 are V2 V8 both are already visited
And so on….

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).

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 16


DFS Vs BFS
DFS BFS
Method The DFS algorithm explores BFS is exactly the opposite
each possible path to its of DFS. In this method, each
conclusion before another node on the same level is
path is tried. checked before the search
proceeds to the next level.
Data structure used Stack (LIFO list) QUEUE(FIFO list)
Type of edges Back edges Cross edges
Complexity O(|V|+|E|) O(|V|+|E|)
Applications Spanning forest, connected Spanning forest, connected
components path and cycles, components path and cycles,
Biconnected components, shortest path, to produce a
articulation point reverse topological ordering
of nodes.

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,

Aij = 1 if there is an edge from vi to vj and


Aij = 0 if there is no such edge.

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.

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 17


1 4 6

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.

Adjacency List Representation: In this representation, we store a graph as a linked


structure. We store all the vertices in a list and then for each vertex, we have a linked list of
its adjacent vertices.

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

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 18


V1 V2 V3
V2 V3
V3 V4
V4 V1 V5 V6
V5 V4
V6

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

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 19


STRONGLY CONNECTED
A tree is a connected acyclic graph (which has no cycles). A directed graph is said to be
strongly connected iff for every pair of distinct vertices u and v in V(G), there is a directed
path from u to v and also from v to u.
A digraph is called strongly connected if there is a directed path from any vertex to
any other vertex.

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.

Let us make the above graph strongly connected as

2 Strongly Connected
Graph

4 5

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 20


BICONNECTED COMPONENTS
A connected undirected graph G is said to be biconnected if it remains connected after
removal of any one Vertex and the edges that are incident upon that vertex.
A biconnected component of an undirected graph is a maximal biconnected subgraph that is a
biconnected sub graph not contained in any larger biconnected sub graph.
Articulation point or cut point
A Vertex v is an articulation point or cut point for an undirected graph G if there are distinct
vertices w and x, distinct from v also such that v is in every path from w to x.

An Un-Directed Graph

Bi-Connected Components of Above Graph


Articulation Point:
• After deleting vertex B and its incident edges the given graph is divided into two non
empty components.

Graph Obtained after Deleting B from the Graph

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 21


After deleting vertex E and its incident edges the resulting non empty components are shown
below.

Graph Obtained after Deleting E from the Graph


After deleting vertex F and its incident edges the resulting non empty components are shown
below.

Graph Obtained after Deleting F from the Graph


From the above graphs we can say that B, E and F are the articulation points of the given
graph.
Bi-Connected Graph

An Un-Directed Graph
After vertex B, if the Graph structure still remains connected, it is said to be a bi-connected
graph.

Graph Obtained after Deleting B from the above Graph

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 22


Construction of Bi-Connected Graph:::
i) Check whether the given graph is bi-connected or not
ii) If the given graph is not bi-connected then identify all the Articulation points.
iii) If Articulation points exist, determine a set of edges whose inclusion makes the graph
bi-connected

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).

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 23


Single Source Shortest Path Problem

There are many paths from A to H.


For example length of path A F D E H = 1 + 3 + 4 + 6 = 14
A B C E H = 2 + 2 + 3 + 6 = 13
We may further look for a path with length shorter than 13 if exists.
Algorithm:
1. We start with source vertex A.
2. We locate the vertex closest to it. B F are adjacent vertices. Length
of AF < length of AB so we choose F.

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).

Vertices that may be attached Path from A Length


D AFD 4
E AFE 4
G AFG 6
B AB 2
We choose vertex B.

4) We go back to step 3 and continue till we exhaust all the vertices.


Vertices that may be attached Path from A Length
ABD 4
D
AFD 4
G AFG 6
C ABC 4
E ABE 6
B AFE 4
We may choose D, C or E.
We choose say D through B.

Vertices that may be Path from A Length


attached
G AFG 6
C ABC 4
AFE 4
E ABE 6
BDE 8
We may choose C or E, choose C.

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 24


Vertices that may be attached Path from A Length
G AFG 6
AFE 4
ABE 6
E
ABDE 8
ABCE 7
H ABCH 5
We choose E via AFE.

Vertices that may be attached Path from A Length


AFG 6
G
AFEG 11
ABCH 5
H AFEH 10

We choose H via ABCH.

Vertices that may be attached Path from A Length


AFG 6
AFEG 11
G

We choose path AFG.


Therefore the shortest paths from source vertex A to all the other vertices are
AB
ABC
ABD
ABCH
AF
AFE
AFG.

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 25


SPANNING TREE
A spanning tree for a connected undirected graph G=(V,E) is a sub group of G that is an
undirected tree and contains all the vertices of g. A spanning tree of a graph should include
all the vertices and a subset of edges.

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.

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 26


MINIMAL SPANNING 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

Vertices that may EDGE COST


be attached
BF 8
F
AF 7
BG 9
G
CG 10
D AD 5
E CE 8
The least cost is AD. Therefore we choose AD now we have

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 27


Vertices that may EDGE COST
be attached
BF 8
F AF 8
DF 10
BG 9
G
CG 10
CE 8
E DE 9

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

6)Differentiate between BFS and DFS.


7)Explain properties of DFS
8)Explain game tree with an example.

Dr.DSK DAA UNIT-II :: Disjoint Sets Page 28

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy