Daa Unit Ii
Daa Unit Ii
Shumama Ansa
Disjoint Set
Set:
• A set is a collection of distinct elements. The
Set can be represented, for example, as
S1={1,2,5,10}.
Disjoint Sets:
• The disjoints sets are those do not have any
common element.
• For example S1={1,7,8,9} and S2={2,5,10}, then
we can say that S1 and S2are two disjoint sets.
Disjoint Set Operations:
Example:
• S1={1,7,8,9}
• S2={2,5,10}
• s3={3,4,6}
• Find(4)=S3
• Find(5)=S2
• Find(7)=S1
Set Representation:
• The set will be represented as the tree
structure where all children will store the
address of parent / root node.
• The root node will store null at the place of
parent address.
• In the given set of elements any element can
be selected as the root node, generally we
select the first node as the root node.
Example:
• S1={1,7,8,9} S2={2,5,10} s3={3,4,6}
• Then these sets can be represented as
Disjoint Union:
• To perform disjoint set union between two
sets Si and Sj can take any one root and make
it sub-tree of the other.
• Consider the above example sets S1 and S2
then the union of S1 and S2 can be
represented as any one of the following.
Find:
• To perform find operation, along with the tree
structure we need to maintain the name of
each set.
• So, we require one more data structure to
store the set names.
• The data structure contains two fields.
• One is the set name and the other one is the
pointer to root.
Union and Find Algorithms:
• In presenting Union and Find algorithms, we
ignore the set names and identify sets just by the
roots of trees representing them.
• To represent the sets, we use an array of 1 to n
elements where n is the maximum value among
the elements of all sets.
• The index values represent the nodes (elements
of set) and the entries represent the parent node.
For the root value the entry will be‘-1’.
Array Representation
• Example: For the following sets the array
representation is as shown below.
I [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
p -1 -1 -1 3 2 3 1 1 1 2
Algorithm for Union operation
• To perform union the SimpleUnion(i,j) function
takes the inputs as the set roots i and j .
• Make the parent of i as j i.e, make the second
root as the parent of first root.
Algorithm SimpleUnion(i,j)
{
P[i]:=j;
}
Algorithm for find operation
• The SimpleFind(i) algorithm takes the element i and
finds the root node of i.
• It starts at i until it reaches a node with parent value-1.
Algorithm SimpleFind(i)
{
while( P[i]≥0)
i:=P[i];
return i;
}
Weighting rule for Union
• If the number of nodes in the tree with root i is less
than the number in the tree with the root j, then
make ‘j’ the parent of i; otherwise make ‘i' the parent
of j.
Algorithm WeightedUnion(i,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 for find:
• If j is a node on the path from i to its root and
p[i]≠root[i], then set P[j] to root[i].
• Consider the tree created by WeightedUnion()
on the sequence of1≤i≤8. Union(1,2),
Union(3,4), Union(5,6) and Union(7,8)
Algorithm CoIlapsingFind(i)
{
r:=i;
while(p[r] >0) do
r := p[r] ; / Find the root,
while(i!=r)do
s:=p[i];
p[i]=r;
i=s;
return r;
}
Graph Representations
Adjacency Matrix
Adjacency Lists
Adjacency multilists
Adjacency Matrix
Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
Examples for Adjacency Matrix
0 0 4
0
2 1 5
1 2
3 6
3 1
0 1 1 1 0 1 0
1 0 1 1 7
1 0 1
2 0 1 1 0 0 0 0 0
1 1 0 0 0 1 0
1 0
0 0 1 0 0 0
1 1 1 0
1 0
G2 0 0 1 0 0 0
G1
0 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 1 0 1 0
symmetric 0 0 0 0 0 1 0 1
undirected: n2/2 0 0 0 0 0 0 1 0
directed: n2
G4
Merits of Adjacency Matrix
From the adjacency matrix, to determine the
connection of vertices is easy n 1
G1 B1 G1 B1 B2
B2
B2 B2 B1
B2 B1
G1
• B1 B2 G1
• B1 G1 B2
• B2 B1 G1
• B2 G1 B1
• G1 B1 B2
• G1 B2 B1
• If there is a constraint such as girl should not sit in 2nd chair
• Then we will eliminate solutions B1 G1 B2 and B2 G1 B1.
• Hence such conditions are called as bounding function
conditions and 4 solutions remain.
• Definition 1: Explicit constraints are rules that restrict
each xi to take on values only from a given set. Explicit
constraints depend on the particular instance I of problem
being solved. All tuples that satisfy the explicit
constraints define a possible solution space for I.
• Definition 2: Implicit constraints are rules that determine
which of the tuples in the solution space of I satisfy the
criterion function. Thus, implicit constraints describe the
way in which the xi‟s must relate to each other.
• State space tree is the tree organization of the solution space.
• Live node is a node that has been generated but whose
children have not yet been generated.
• E-node is a live node whose children are currently being
explored. In other words, an E-node is a node currently being
expanded.
• Dead node is a generated node that is not to be expanded or
explored any further. All children of a dead node have already
been expanded.
• Depth first node generation with bounding functions is called
backtracking.
• Backtracking is the procedure where by, after determining that
a node can lead to nothing but dead end, we go back
(backtrack) to the nodes parent and proceed with the search on
the next child.
N- Queens Problem
• Let us understand n- Queens
• Queen can move in either horizontal, vertical
or diagonal direction
• n queens should be placed on a n*n board
such that no two queens are under attack
(same row, column and diagonal).
• Therefore there exists many solutions.
• Suppose two queens are placed at positions (i, j) and (k, l)
Then:
• Column Conflicts: Two queens conflict if their xi values are
identical.
• Diagonal conflict: Two queens i and j are on the same diagonal
if:
i – j = k – l.
This implies, j – l = i –k
i + j = k + l.
This implies, j – l = k –I
• Where, j be the column of object in row i for the ith queen and
l be the column of object in row „k‟ for the kth queen.
4 -Queens
• Here n- queens problem is generalized to 4-
queens where we have a 4*4 chessboard and
4 queens.
• So, 4 queens should be placed on the board
in such a way such that they are not under
attack.
4 – Queens Solution space tree
without bounding function(diagonals)
4 – Queens Solution space tree with
bounding function(diagonals)
8- Queens
• All solutions to the 8-queens problem can be
represented as 8-tuples (x1, . . . . , x8), where xi is the
column of the ith row where the ith queen is placed.
1 q
2 q
4
1 2 3 4
1 Q
4
1 2 3 4
4
1 2 3 4 5 6 7 8
1 q
2 q
3 q
4 Q
5 q
8
Sum of subsets problem
• Given positive numbers wi, 1 ≤ i ≤ n, and m, this
problem requires finding all subsets of wi whose sums
are “m‟.
• Suppose, we are given n distinct positive numbers
(called weights) and we desire to find all the
combinations of these numbers whose sums are m.
• This is called sum of subsets problem.
• If we consider backtracking procedure using fixed tuple
strategy, x(i) of solution vector is either 1 or 0 depending
on whether weight w(i) is included or not.
• If w[1:6]= {5, 10, 12, 13, 15, 18}
• n=6
• m=30
• Find the solutions where sum of subsets should be
equal to m
• Solution vector x where xi can be either 1 or 0 based
on whether the weight is included or not.
• The required solutions can be
– 110010
– 101100
– 001001
State space tree
Graph coloring
• Let G be a graph and m be a given positive
integer. We want to discover whether the nodes of
G can be colored in such a way that no two
adjacent nodes have the same color, yet only m
colors are used. This is termed the m - colorabiltiy
decision problem.
• The m - colorability optimization problem asks
for the smallest integer m for which the graph G
can be colored.
• Given any map, if the regions are to be colored in
such a way that no two adjacent regions have the
same color
1 2
M=3
{R,G,B}
4
3
X1=R X1=G
X1=B
X2=R
X2=B X2=G
de
ad
X3=R X3=B
X3=G
de
ad
X4=B
X4=G