Beruflich Dokumente
Kultur Dokumente
Problem):
n queens should be placed in an nxn chess board, in such a way that no queen can attack
another queen.
The following algorithm computes solution to n-Queens problem.
1. First suppose that every new queen will be placed in a new row. This ensures that no queen can
attack another queen row-wise. Thus n queens can be placed in n different row positions.
2. Now, we have to calculate the column positions of these n queens. This can be done in the
following way.
2.1 First place, first queen in first row and first column.
2.2 Calculate the column position of the next queen in next row, in the following
way.
Place it in the first column of the new row. If it collides with any of the
previously placed queens (either column-wise or diagonal-wise) , then, move it one
step right. If it goes out of the board, move previous queen one step right, and goto
step 2.2.
If queen can be placed in a particular column in that row, without any
collision, then goto step 2.2.
If all n queens are placed in n different column positions, then STOP.
Otherwise goto step 2.2.
A queen collides with another queen, column-wise, if the column positions of the both queens are same.
A queen collides with another queen, diagonal-wise, if
|Difference of column positions|=|Difference of row positions|
How to solve n Queens Problem using backtracking:
1. Place ith queen in first possible column position in ith row.
2. If you are not able to place ith queen in any column position in ith row, then, backtrack and move
(i-1)th queen in (i-1)th row one column right.
3. Continue this (steps 1 and 2) until all n queens are placed in n different column positions.
Sum of Subsets:
Given a set S of n objects with weights (w1,w2,,wn) and a positive integer M,we have to find a subset S'
of the given set S, such that, the sum of the elements of subset S' is equal to M.
For example, if a given set S = (1, 2, 3, 4) and M = 5, then there exists sets S= (3, 2) and S=(1, 4)
whose sum is equal to M.
It can also be noted that some instance of the problem does not have any solution.
For example, if a given set S = (1, 3, 5) and M = 7, then no subset occurs for which the sum is equal
to M = 7.
Fig(i): Map
Its corresponding graph
Ans: chromatic number(cn)=Minimum number of colors required to color the given graph=4
Fig(i)
Fig(ii)
Fig(iii)
Fig(iv)
Fig(v)
UNIT VII
Branch and Bound: General method, applications - Travelling sales person, 0/1 knapsack problem- LC Branch
and Bound solution, FIFO Branch and Bound solution
General Method:
E-node is the node that is being explored currently.
Exploring a node means generating all its children (branches or live-nodes).
In Branch and Bound Technique, all branches(live nodes) of an E-node are generated, before one of
the branches(live node) becomes an E-node
There are three types of Branch and Bound techniques. They are:
1. LCBB 2.LIFOBB 3.FIFOBB
Applications: BB can be used to solve optimization problems such as 0/1 knapsack problem, traveling sales
person problem etc.
Travelling sales person problem(TSPP): TSPP can be solved by using BB, in the following way:
1. Obtain reduced matrix for the given cost matrix.
2. Compute bound(b) for that reduced matrix.
3. Root (E-node) consists of all possible paths.
4. Left child can be obtained by including a particular edge in the solution set.
5. Right child can be obtained by excluding that particular edge from the solution set.
6. Reduce left and right childs and compute bounds.
7. The child with minimum bound becomes E-node for the next iteration.
8. Continue this until either left or right child becomes empty.
9. Then, solution can be obtained by backtracking from that empty child to root.
Note:
1. If you include a particular edge(i,j) in the solution set, then,
-delete ith row and jth colunmn.
-put in jth row and ith column
2. If you exclude a particular edge(i,j) in the solution set, then,
-put in ith row and jth column.
0/1 knapsack problem:
Branch and Bound technique is designed for minimization problem.
But, 0/1 knapsack problem is a maximization problem(as we have to maximize profit). So, we have
to multiply bounds(lower and upper) with -1.
Do not consider fractions while computing upper bound(UB)
Consider fractions while computing lower bound(LB)
LCBB(Least Cost Branch and Bound):
The live node with minimum(UB-LB) value becomes E-node in the next iteration.
If two live nodes have same (UB-LB) value, then the node with least UB value becomes E-node in
the next iteration.
FIFOBB(Last In First Out Branch and Bound):
Let, LUB= Least Upper Bound, CUB = Current Upper Bound, & CLB = Current Lower Bound,
Initialize LUB to infinity.
If (CUB<LUB) then LUB=CUB
If(CLB>LUB) then kill that node
Use Queue to hold all live nodes of E-node.
LIFOBB(First in First Out Branch and Bound): This is same as LIFOBB, except that, here, we use Stack to
hold all live nodes of E-node.