Ada UNIT2
Ada UNIT2
DISJOINT SETS
Set:
A set is a collection of distinct elements. The Set can be represented, for examples, 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 S2 are two disjoint sets.
1. 1. A tree is used to represent each set and the root to name a set.
2. Each node points upwards to its parent
3. Two sets A and B are said to be disjoint if A— B = f. i.e., there is no common element in
both A and B
4. A collection of disjoint sets is called a disjoint set forest
5. An array can be used to store parent of each element
1. Union
2. Find
Union:
If Si and Sj are two disjoint sets, then their union Si U Sj consists of all the elements x such that
x is in Si or Sj.
Example:
S1 U S2 = {1, 2, 5, 7, 8, 9, 10}
Find:
Example:
S1 = {1, 7, 8, 9}
Then,
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:
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.
Disjoint 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 ALGORITHM
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’.
Example:
To perform union the SimpleUnion(i,j) function takes the inputs as the set roots i and j . And
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;
Algorithms SimpleFind(i)
return i;
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)
//Union sets with roots i and j , i≠j using the weighted rule
temp:= P[i]+P[j];
if (P[i]>P[j]) then
P[i]:=j;
P[j]:=temp;
else
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 of 1≤i≤8. Union(1,2), Union(3,4), Union(5,6)
and Union(7,8).
Algorithm CollapsingFind(i)
r:=i;
while(i≠r)
s:=P[i];
P[i]:=r;
i:=s;
BACKTRACKING
General Method
Backtracking is a technique based on algorithm to solve problem. It uses recursive calling to find
the solution by building a solution step by step increasing values with time. It removes the
solutions that doesn't give rise to the solution of the problem based on the constraints given to
solve the problem.
Optimization problem used to find the best solution that can be applied.
Enumeration problem used to find the set of all feasible solutions of the problem.
In backtracking problem, the algorithm tries to find a sequence path to the solution which has
some small checkpoints from where the problem can backtrack if no feasible solution is found
for the problem.
All solutions require a set of constraints divided into two categories: explicitand implicit
constraints.
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.
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 x i’s
must relate to each other.
Applications Of Backtracking
1. N-queens problem
2. Sum of subsets problem
3. Graph coloring
N-Queen’s Problem
In N-Queen problem, we are given an NxN chessboard and we have to place n queens on the
board in such a way that no two queens attack each other. A queen will attack another queen if it
is placed in horizontal, vertical or diagonal points in its way.
For solving n queens problem, we will try placing queen into different positions of one row. And
checks if it clashes with other queens. In current positioning of queens if there are any two
queens attacking each other. If they are attacking, we will backtrack to previous location of the
queen and change its positions. And check clash of queen again.
1. Board[r][c] will be used to check safe positions on the board and place the queen.
2. On encountering safe position increment the column value by 1.
3. On encountering unsafe position change the row to next row.
b[3][2] = unsafe, we reached the last row of column 2 but no safe position we found to place the
third queen. Now we have to backtrack and remove the last positioned queen i.e., at position b[2]
[1] and place that queen by increasing the row value by 1.
B[3][3] = unsafe
We have reached the end of rows and columns but didn’t find safe place to position fourth queen.
Again backtrack and remive the last placed queen i.e., b[1][2]
B[3][2]= unsafe.
Again backtrack as we cannot place the third queen in its position. Remove both the queens i.e.,
b[3][1] and b[0][0]. And increment the row value by 1 i.e., b[1][0]
B[0][2] = safe, place the third queen and increment the value of column by 1
Matrix representation
0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
place (int k)
{
int i;
return (0);
return (1);
nqueen (int n)
int m, k, i = 0;
x [1] = 0;
k = 1;
while (k > 0)
x [k] = x [k] + 1;
if (k == n)
else
}
else
k--;
i++;
getch();
k++;
x [k]=0;
return (0);
main ()
int n;
clrscr ();
nqueen (n);
Sum of Subsets
Subset sum problem is to find subset of elements that are selected from a given set whose sum
adds up to a given number K. The backtracking approach generates all permutations in the worst
case but in general, performs better than the recursive approach towards subset sum problem.
In Backtracking algorithm as we go down along depth of tree we add elements so far, and if the
added sum is satisfying explicit constraints, we will continue to generate child nodes further.
Whenever the constraints are not met, we stop further generation of sub-trees of that node, and
backtrack to previous node to explore the nodes not yet explored.We need to explore the nodes
along the breadth and depth of the tree. Generating nodes along breadth is controlled by loop and
nodes along the depth are generated using recursion.
For example, n = 4, w = (11, 13, 24, 7) and m = 31, the desired subsets are
S1 = (11, 13, 7)
S2 = (24, 7)
In the above tree which ever weight are included that edge will be written as 1 or will be written
as 0. I.e., xi = 0/1.
Problem: 1 2 3 4 5 6
W [1:6] = {5, 10, 12, 13, 15, 18} n = 6 and m = 30
As there are 6 weights there will be 7 level of state space tree and its height will be 6.
Total weight is 73
0, 73
x1=1
x2=1 5, 68
x3=1 15, 58 x3 = 0
27, 46 15, 46
Graph Coloring
Graph coloring is the procedure of assignment of colors to each vertex of a graph G such that no
adjacent vertices get same color. The objective is to minimize the number of colors while
coloring a graph. The smallest number of colors required to color a graph G is called its
chromatic number of that graph. Graph coloring problem is a NP Complete problem.
The steps required to color a graph G with n number of vertices are as follows −
In the above figure, at first vertex a is colored red. As the adjacent vertices of vertex a are again
adjacent, vertex b and vertex d are colored with different color, green and blue respectively.
Then vertex c is colored as red as no adjacent vertex of c is colored red. Hence, we could color
the graph by 3 colors. Hence, the chromatic number of the graph is 3.
1. Register Allocation
2. Map Coloring
3. Bipartite Graph Checking
4. Mobile Radio Frequency Assignment
5. Making time table, etc.
Example:
Color the graph given below with minimum number of colors by backtracking using state space
tree.
Lets assume 1= red, 2= green and 3= blue