Sie sind auf Seite 1von 5

UNIT VI

Backtracking: General method, applications-8-queen problem, sum of subsets, graph coloring,


Hamiltonian cycles
Backtracking - General method: Backtracking is a general algorithm for finding all (or some) solutions to
some computational problem that incrementally builds candidates to the solutions, and abandons each
partial candidate c (back tracks) as soon as it determines that c cannot possibly be completed to a valid
solution.
Back tracking algorithms try each possibility until they find the right one. It is a depth-first search of
the set of possible solutions. During the search, if an alternative does not work, the search backtracks to
the choice point, the place which presented different alternatives, and tries the next alternative. When the
alternatives are exhausted, the search returns to the previous choice point and tries the next alternative
there. If there are no more choice points, the search fails .
Applications: Back

tracking method is used to solve the following problems:

8-queen problem, sum of subsets, graph coloring, Hamiltonian cycles


8-queen problem (n-Queens

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.

How to solve Sum of Subsets Problem using backtracking:


Let W= Weight added so far to the subset and
T= Total remaining weight in the given set, then,
Every node in the partial solution state space tree is represented as (W,i,T)
A node at level i, is promising, iff,
W+wi m and
W+T m
If at any node, W=m, then the path from root to that node is the solution.
If a node is not promising, then, we backtrack from that node to its previous node
Graph Coloring: Graph coloring is a way of coloring the vertices of a graph such that no two adjacent
vertices share the same color.The minimum number of colors required to color a graph in this way is
called its chromatic number.
How to solve graph coloring problem using backtracking::
Chromatic number of a graph can be obtained by using the following algorithm.
1. Initialize cn to 1. // cn = chromatic number
2. Apply cn to vertex 1
3. cc=cn; // cc=current color
4. while(cc cant be applied to next un colored vertex) do
cc=cc-1. //backtrack
5. If(cc 0) {Apply cc to that un colored vertex; goto 3;}
6. Else If(there are any uncolored vertices) {cn=cn+1; goto 3;}
7. Print( Chromatic number =, cn)
8. Stop.
NOTE: A particular color cant be applied to a particular vertex, if that vertex is adjacent to any of the
vertices that are already colored with that color.
Hamiltonian cycles:

How to solve Hamiltonian path problem using backtracking:


Proceed in any possible path, if Hamiltonian cycle is not obtained, then backtrack and take diversion.
PROBLEMS
n Queens Problem
1. Solve n queens problem when
(i) n=4 (ii) n=5 (iii) n=8
Sum of Subsets
1. Let w={1,2,3} and m=3. Find all subsets of the set w that sum to m.
2. Let w={7,11,13,24} and m=31. Find all subsets of the set w that sum to m.
3. Let w={5,10,12,13,15,18} and m=30. Find all subsets of the set w that sum to m.
Graph Coloring
1 .Find chromatic number for the following graphs, and show how those graphs can be colored.

Fig(i): Map
Its corresponding graph
Ans: chromatic number(cn)=Minimum number of colors required to color the given graph=4

Fig(iii) Ans: cn=3


Fig(ii) Ans: cn=2.
Explanation: For A,D,E color 1.
For B,C,F color 2.
Hamiltonian cycles
1. Find Hamiltonian cycles for the following graphs.

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.

Das könnte Ihnen auch gefallen