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

UNIT-II

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

UNIT-II

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

SETS AND DISJOINT SET UNION

These numbers might, in practice, be indices into a symbol table in which the names of the
elements are store. The representation of sets We shall assume that the elements of the sets are
the numbers 1,2,3,...n. We assume that the sets being represented are pair wise disjoint (that is, if
S{and S j, i /j, are two sets, then there is no element that is in both Si and Sj.

For example, when n = 10, the elements can be partitioned into three disjoint sets,

S1= {0,6,7,8}, S2 = {1,4,9}, and S3= {2,3,5}.

Disjoint set union: If Si and Sj are two disjoint sets, then their union Si U Sj= all element sx
such that x is in Si or Sj. Thus, S1 U S2 = {1,7, 8,9,2,5,10}. Since we have assumed that all sets
are disjoint, we can assume that following the union of Si and Sj, the sets Si and Sj do not exist
independently; that is, they are replaced by Si U Sj in the collection of sets. If Si and Sj are two
disjoint sets, then their union Si U Sj= all elements x such that x is in Si or Sj. Thus, S1 U S2 =
{0, 1,4,6,7,8,9}.

Find (i). Given the element i, find the set containing i. Thus, 4 is in set S3, and 9 is in set Si.
The function Union (i, j) requires two trees with root s j and I be joined. Also, to simplify,
assume that the set elements are the numbers1throughn. Since the set elements are
numbered1throughn, 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

AlgorithmSimpleUnion(i, j)

p[i] :=j;

AlgorithmSimpleFind(j)

while (p[i\\ >0) doi:=p[i];

returni;

Backtracking: General method

Backtracking represents one of the most general techniques. Many problems, which deal with
searching for a set of solutions or which ask for an optimal solution satisfying some constraints
can be solved using the backtracking formulation.

In many applications of the backtrack method, the desired solution is expressible as an


n-tuple (xi,...,xn),where the %i fire chosen from some finite set Si. Suppose m is the size of set
Si. Then there is m = m1, m2, m3 …. Mn n tuple that are possible candidates for satisfying the
function P. The brute force approach would be to form all these n-tuples, evaluate each one with
P, and save those, which yield the optimum.

Many of the problems we solve using backtracking require that all the solutions satisfy a
complex set of constraint
8-QUEEN’S PROBLEM
8-queens: A classic combinatorial problem is to place eight queens on an 8 x 8 chessboard so
that no two "attack" that is, so that no two of them are on the same row, column, or diagonal.
Since each queen must be on a different row, we can without loss of generality assume queen i is
to be placed on row i. All solutions to the 8-queensproblemcan there fore be represented as 8-
tuples(x1, x2,...,xs), where X{ is the column on which queen i is placed. The explicit constraints
using this formulation are Si = {1,2,3,4, 5,6,7, 8}, 1< I < 8.

Given chess board of field 8 x 8. The 8-Queen problem is to place 8-Queen on the chessboard,
so that no two Queens can “attack” each other. A Queen can attack vertically, horizontally and
diagonally.

N-Queen Problem:

N-Queens Problem: It is a classic combinatorial problem. The eight queen’s puzzle is the
problem of placing eight queens puzzle is the problem of placing eight queens on an 8×8
chessboard so that no two queens attack each other. That is so that no two of them are on the
same row, column, or diagonal. The 8-queens puzzle is an example of the more general n-
queens problem of placing n queens on an n×n chessboard.
It is a generalized problem of 8-Queen problem. N Queens are placed on a chess board of size n
x n, without having attack each other.

In chess, queens can move all the way horizontally, vertically or diagonally (if there is no other
queen in the way).But, no two Queen can attack each other. So, due to this restriction, each
queen must be on a different row and column.

Backtracking strategy for 8-Queen problem is as follows-

1. Let us, in the chessboard, rows and columns are numbered from 1 to 8 and also
queens are numbered from 1 to 8.

2. Without loss of generality, assume that ith queen can be placed in ith row, because no
two queens can place in the same row.
3. All solution can represented as 8-tuple ( x1, x2,…. x8 ), where xi is the column number
of the ith row of ith queen placed.
4. Here explicit constraints are Si = {1, 2, 3, 4, 5, 6, 7, 8}, 1 ≤ i ≤ 8 and the solution
space will consist of 88 8-tuple.
5. According to the implicit constraints no two queen can on the same row.
6. So, all solution are permutation of 8-tuple(1, 2, 3, 4, 5, 6, 7, 8)
7. Thus the searches is reduce to 88 8-tuple to 8! tuple.

We know from the above that, in 8 -Queen problem all the solution can represented as 8-tuple
( x1, x2,….,x8 ), where xi is the column number of ith row where.ith queen placed. These all x i’s
are distinct because of the implicit constraint that no two queen can placed in same column. We
assume already in no. 2 that ith queen can be placed in ith row only. So, no two queen can placed
in same row. Now, we have only to decide whether two queens are on the same diagonal or not.
If chessboard square fields are numbered as two- d i m e n s i o n a l array, a [1:8] [1:8] then
we find that for all diagonal element “row- column” value is same. For example if 1st
queen is placed in a [1,3] square then the 2nd queen will placed diagonally if it placed in a
[2,4] square. Here “row-column” values are 2 and it is same for both queen.

Let two queen are placed in position (m, n) and (x, y) Then two queens can placed diagonally if
m-n=x-y (1) m + n = x + y----------(2)

(1) => n – y = m - x

(2) => n – y = x – m

Therefore two queens can be on the same diagonal if and only if | n – y | = | m – x |. This is an
implicit constraint.
Example:
Here is an example of 4-queen problem- The state space tree generated by 4-queen problem is as
follows- Here node at level i represent ith queen placed at ith row. i.e at level 1 it represents as 1st
queen places in 1st row. Xi in ith level represents column number of ith queen placed in ith row. i.e
x2 = 3 at level 2 means 2nd queen is in 3rd column of the 2nd row. Nodes are generated in depth
first search manner.

A the path from root to leaf will represent a tuple in solution space. All tuples are distinct and
some tuples may not lead to a feasible solution.

GRAPH COLORING PROBLEM

Let G be a graph and m be a given integer. Is there any way to color the vertices of graph G
using m color in such a way that no two adjacent vertices have same color? This is called as m-
color ability decision problem. According to the graph coloring theory if d is the degree of a
given graph, then it can be colored with d +1 color. The minimum number of color required in
graph coloring problem to color vertices is called chromatic number. The m-color ability
optimization problem is to determine the chromatic number of the graph G. This is called
graph coloring problem.
For example graph in the following fig can be colored with minimum 3 colors 1, 2, 3. So,
b 2
chromatic number is 3.
a
e

3c d 1
Solution using backtracking:

Suppose we represent graph G of n vertices by adjacency matrix G [1:n] [1:n], where G [i] [j]
=1,if there is an edge between vertex i and j in G and G [i] [j] =0, otherwise.
The colors of the graph can be numbered from 1 to m. The solution set are represented
by (x1, x2,…..,xn), where xi is the color of vertex i. The state space tree for this problem has
degree m and height n + 1. Each node at level i has m children correspond to m color and
represents ith vertex of graph G. The left most node has assigned color 1 and rightmost vertex
has assigned color m. Node at level n + 1 is leaf.

Example: G is a graph which has 3 vertices (n = 3) and we have to solve the graph coloring
problem for this graph G using 3 color (m = 3)

A C

The general state space tree for this problem is as follows-


There are possible three color to color vertex A. Hence x1 has three values, x1=1, x1=2, x1=3.
If we color the vertex A using color 1 then for 2nd vertex B there are three possible colors1,
2,3. Hence, x2=1,x2=2,x2=3 and similarly for vertex C . Now if we apply backtracking method
to solve the problem then it will use a bound in function to kill a node in the state space tree.
The bounding function for this problem is that no two adjacent vertexes have same color.
The pruned state space tree after applying bounding function is as follows
For the graph in fig vertex 1 is the adjacency of vertex 2.So, if we color vertex 1 using color 1
i.e x1=1 then we cannot color vertex 2 with color 2 i.e x2=1 because it will violet the bounding
condition. Hence, node 3 cannot lead to a feasible solution and it will bind. Other nodes are
bounded similarly.
Some feasible solution sets are
i) (1,2,3), where x1=1,x2=2,x3=3,

ii) (2,1,3), where x1=2,x2=1,x3=3

iii) (3,2,1), where x1=3,x2=2,x3=1 Here xi represents ith vertex color value.

Recursive algorithm for graph coloring problem:


/* finding color value of all vertex*/

void m coloring ( int k ) void Next Value ( int k )


{ do { do
{ {
Next Value ( k ); x [ k ] = x [ k + 1] % ( m + 1 );
if ( ! x [ k ] ) break; if ( k == n ) if ( ! x [ k ] ) return;
{ for ( int j = 1; j ≤ n; j++ )
for ( int i =1; i ≤ n; i+ {
+) if (G [ k ] [ j ]
cout << x[ i ] ; && (x [ k ]
cout << endl;
== x [ j ] ))
}
else break;
m coloring ( k + 1 ); }
} if ( j == n + 1 ) return;
while (1); }
} while(1);
}

Here k is the next vertex to color n is the number of vertices x [ i ] is the color of ith vertex. The
function m coloring first create the adjacency matrix G [ i ] [ j ] of the graph G and then
initialize x [ k ] = 0, for all 1 ≤ k ≤ n . First invoke the procedure by m coloring (1). Function
Next Value produces possible color for vertex k and assigns it to x k. Function m coloring
repeatedly picks a color value, assign it to xk, and then calls m coloring recursively.

SUM OF SUB SETS:

Sum of Subsets Problem: Given positive numbers wi 1 ≤ i ≤ n, & m, here sum of subsets
problem is finding all subsets of wi whose sums are m. Definition: Given n distinct +ve numbers
(usually called weights), desire (want) to find all combinations of these numbers whose sums are
m. this is called sum of subsets problem. To formulate this problem by using either fixed sized
tuples or variable sized tuples. Backtracking solution uses the fixed size tuple strategy.

For example: If n=4 (w1, w2, w3, w4)=(11,13,24,7) and m=31. Then desired subsets are (11, 13,
7) & (24, 7). The two solutions are described by the vectors (1, 2, 4) and (3, 4).

different sized tuples. Explicit constraints requires xi ∈ {j / j is an integer 1 ≤ j ≤ n }


In general all solution are k-tuples (x1, x2, x3---xk) 1 ≤ k ≤ n, different solutions may have

Implicit constraints requires: No two be the same & that the sum of the corresponding wi’s be
m i.e., (1, 2, 4) & (1, 4, 2) represents the same. Another constraint is xi

Weight of item-> Wi
Capacity of bag (subset)->M
The element of the solution vector is either one or zero.-> Xi
Xi Value depending on whether the weight wi is included or not.
If Xi=1 then wi is chosen.
If Xi=0 then wi is not chosen.
k k

∑ wixi + ∑ wi>¿ m ¿
i=0 i=k+1
Example: let n = 6, m = 30, and w[1 : 6] = {5,10,12,13,15,18}. The rectangular nodes list the
values of s, k, and r on each of the call s to Sum Of Sub. Circular nodes represent points at which
subsets with sums m are printed out. At nodes A, B, and C the output is respectively
P[1,2,…6]={5,10, 12, 13, 15, 18}
nodes The string Sum of sub sets
P1,p2,p5 (1,1,0,0,1,0), 30
P1,p3,p4 (1,0,1,1, 0, 0), 30
P3,p6 (0, 0,1,0,0, 1). 30

Subset Sum Problem using Backtracking


Given a set [] of non-negative integers and a value sum, the task is to print the subset of the
given set whose sum is equal to the given sum.
Examples:
Input: set[] = {1,2,1}, sum = 3
Output: [1,2],[2,1]
Explanation: There are subsets [1,2],[2,1] with sum 3.
Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 30
Output: []
Explanation: There is no subset that add up to 30.

Complexity analysis:
 Time Complexity: O(2n) The above solution may try all subsets of the given set in the
worst case. Therefore time complexity of the above solution is exponential.
 Auxiliary Space: O(n) where n is recursion stack space.
Examples
1. Let w = {5,7,10,12,15,18,20} and m \342\200\224 35.Find all possible subsets of w that
sum to r n. Do this using Sum Of Sub. Draw the portion of the state space tree that is
generated.
2. With m = 35,runSumOfSubonthedata(a)w = {5,7,10,12,15,18,20}, (b) w =
{20,18,15,12,10,7,5}, and (c) w = {15,7,20,5,18,10,12}. Are there any discernible differences
in the computing times

You might also like