Você está na página 1de 5

MODULE IV MCA403 ALGORITHM ANALYSIS AND DESIGN ADMN 2009- 10 Dept.

of Computer Science And Applications, SJCET, Palai 77 MODULE IV 4.1 BACKTRACKING Many problems with searching a set of solutions or which ask for an optimal solu tion satisfying some constraints can be solved using the backtracking formulatio n. In many applications of the backtrack method, the desired solution is express ible as an n-tuple(x1, ..,xn), where the xi are chosen from some from finite set S i. Often the problem to be solved calls for finding one vector that maximizes (o r minimizes or satisfies) a criterion function P(x1, ..,xn). Sometimes it seeks al l vectors that satisfy P. The criterion function P is the inequality a[xi]=a[xi+ 1] for 1 = i < n. The set Si is finite and includes the integers 1 through n. Many of the problems we solve using backtracking require that all the solutions satisfy a complex set of constraints. For any problem these constraints can be c lassified into two categories: explicit and implicit. The explicit constraints d epend on the particular instance I of the problem being solved. All tuples that satisfy the explicit constraints define a possible solution space for I. the imp licit constraints are the rules that determine which of the tuples in the soluti on space of I satisfy the criterion function. Thus implicit constraints describe the way in which xi must relate to each other. CONTROL ABSTRACTION (i) Recursive backtracking algorithm Algorithm Backtrack(k) //This schema describes the backtracking process using recursion. On entering, t he first //k-1 values x[1], x[2], ..,x[k-1] of the solution vector x[1:n] have bee n assigned. x[ ] and //n are global. { for (each x[k] ? T(x[1], ..,x[k-1]) do { if (Bk(x[1],x[2], ..,x[k])!=0) then { if (x[1],x[2], ..,x[k] is a path to an answer node) then write (x[1:k]); if (k<n) then backtrack(k+1); } } } MODULE IV MCA403 ALGORITHM ANALYSIS AND DESIGN ADMN 2009- 10 Dept. of Computer Science And Applications, SJCET, Palai 78 Explanation Let (x1,x2, ..xi) be a path from the root to a node in a state space tree. Let T(x 1,x2, ..xi) be the set of all possible values for xi+1 such that (x1,x2, ..xi+1) is also a path to a problem state. T(x1,x2, ..xn) =?. We assume the existence of boun ding function Bi+1 (expressed as predicates) such that if Bi+1(x1,x2, ..xi+1) is f alse for a path (x1,x2, ..xi+1) from the root node to a problem state, then the pa th cannot be extended to reach an answer node. Thus the candidates for position i+1 of the solution vector (x1,x2, ..xn) are those values which are generated by T and satisfy Bi+1. The above recursive algorithm is initially invoked by Backtra ck(1);. The solution vector (x1,x2, ..xn), is treated as a global array x[1:n]. All the po ssible elements for the kth position of the tuple that satisfy Bk are generated, one by one, and adjoined to the current vector (x1,x2, ..xk-1). Each time xk is a ttached, a check is made to determine whether a solution has been found. Then th e algorithm is recursively invoked. When the for? loop is exited, no more values for xk exist and the current copy of Backtrack ends. The last unresolved call no w resumes. (ii)General iterative backtracking method Algorithm IBacktrack(n) //This schema describes the backtracking process. //All solutions are generated in x[1:n] and printed as soon as they are determin

ed. { k:= 1; while(k?0) do { if (there remains an untried x[k] ? T(x[1],x[2], ..,x[k-1]) and Bk (x[1], ..,x[k]) i s true) then { if (x[1], .x[k] is a path to answer node) then write (x[1:k]); k:=k+1;//Consider the next set. } else k:=k-1; //Backtrack to the previous set. } } Explanation T() will yield the set of possible values that can be placed as the first compon ent x[1] of the solution vector. The component x[1] will take on those for which the bounding function B1x(1) is true. The variable k is continually incremented and a solution vector is grown until either a solution in found or no untried v alue of xk MODULE IV MCA403 ALGORITHM ANALYSIS AND DESIGN ADMN 2009- 10 Dept. of Computer Science And Applications, SJCET, Palai 79 remains. When k is decremented, the algorithm must resume the generation of poss ible elements for the kth position that have not yet been tried. Conclusion The efficiency of both algorithms depends very much on four factors: 1. The time to generate next xk 2. The number of xk satisfying the explicit constraints 3. The time for the bounding functions Bk 4. The number of xk satisfying the Bk Constraints Solutions must satisfy a set of constraints Explicit vs. implicit Definition 1: Explicit constraints are rules that restrict each xi to take on va lues only from a given set. eg) xi ? 0 or Si = { all nonnegative real numbers } xi = 0 or 1 or Si = { 0, 1 } li ? xi ? ui or Si = {a : li ? a ? ui } All tuples satisfying the explicit constraints define a possible solution space for I (I=problem instance) Definition 2: The implicit constraints are rules that determine which of the tup les in the solution space of I satisfy the criterion function. Thus implicit con strains describe the way in which the xi must relate to each other. Classic combinatorial problem Place eight queens on an 8*8 chessboard so that no two attack Without loss of generality, assume that queen i is placed on row i All solutions represented as 8-tuples (x1,x2, ,x8) where xi is the column on which queen i is placed Constraints Explicit constraints Si={1,2, ,8} So, solution space consists of 88 8-tuples Implicit constraints No two xi?s can be the same (By this, solution space reduced from 88 to 8!) No two queens can be on the same diagonal MODULE IV MCA403 ALGORITHM ANALYSIS AND DESIGN ADMN 2009- 10 Dept. of Computer Science And Applications, SJCET, Palai 80 BOUNDING FUNCTIONS

The backtrack algorithm as its virtue the ability to yield the same answer with far fewer than m trials. Its basic idea is to build solution vector, one compete nt at a time and to use modified criterion functions Pi(x1, ,xn) called bounding fu nctions, to test whether the vector being formed has any chance of success. The major advantage of this method is this: if it is realized that the partial vecto r (x1, x2, .., xi) can in no way lead to an optimal solution, then mi+1 mn possible t est vectors can be ignored entirely. 4.1.1 N-QUEENS PROBLEM The n-queens problem is a generalization of the 8-queens problem. Here n queens are to be placed on an nxn chessboard so that no two attack, that is no two quee ns are on the same row, column, or diagonal. The solution space consists of all n! permutations of the n-tuple (1,2, ,n). The tree is called permutation tree. The edges are labeled by possible values of xi. Edges from level 1 to level 2 nodes specify the values for x1. Edges from level i to i+1 are labeled with values of xi. The solution space is defined by all the paths from the root node to a leaf node. For eg. If n=4, then there will be 4! =24 leaf nodes in the tree. Eg: 8-queens problem A classic problem is to place eight queens on an 8x8 chessboard so that no two at tack, that is, so that no two of them are on the same row, column, or diagonal. MODULE IV MCA403 ALGORITHM ANALYSIS AND DESIGN ADMN 2009- 10 Dept. of Computer Science And Applications, SJCET, Palai 81 Bounding function No two queens placed on the same column ? xi s are distinct No two queens placed on the same diagonal ? how to test? The same value of row-column or row+column Supposing two queens on (i,j) and (k,l) i-j=k-l (i.e., j-l=i-k) or i+j=k+l (i.e., j-l=k-i) So, two queens placed on the same diagonal iff |j-l| = |i-k| This example shows how the backtracking works(4-Queens) TREE STRUCTURE The n-queens problem is a generalization of the 8-queens problem. Now n queens a re to be placed on an nn chess board so that no two attack; that is, no two queen s are on the same row, column or diagonal.Generalising our discussions, the solu tion space consists of all n! permutations of n-tuple (1, 2.., n).The above figu re shows a possible tree organization for the case n=4. A tree such as this is c alled a permutation tree. The edges are labeled by possible values of xi. Edges from level 1 to level 2 nodes specify the value for x1.Thus the leftmost sub tre e contains all solutions with x1=1 and x2=2,and so on. Edges from level to level i+1are labeled with the value of xi.the solution space is defined by all paths from the root node to a leaf node. There are 4! =24 nodes in the tree. MODULE IV MCA403 ALGORITHM ANALYSIS AND DESIGN ADMN 2009- 10 Dept. of Computer Science And Applications, SJCET, Palai 82 Tree organization of the 4-queens solution space. Nodes are numbered as in depth first search. ALGORITHM Algorithm Place (k, ?) // Returns true if a queen can be placed in the kth row and //ith column. Otherwise it returns false. x[] is a //global array whose first (k 1) values have been set. //Abs (p) returns the absolute value of p. { for j: =1 to k-1 do if ((x [j] = ?) // Two in the same column or (Abs (x [j] ? ) = Abs (j-k)) ) // or in the same diagonal then return false; return true; } Algorithm Nqueens (k, n)

// Using backtracking, this procedure prints all // possible placements of n queens on an n x n // chessboard so that they are nonattacking. { for ?: =1 to n do { if Place (k, ?) then MODULE IV MCA403 ALGORITHM ANALYSIS AND DESIGN ADMN 2009- 10 Dept. of Computer Science And Applications, SJCET, Palai 83 { x [k] : = ?; if (k=n) then write (x[1:n]); else Nqueens (k+1,n); } } } 4.1.2 SUM OF SUBSETS Given positive numbers wi, 1=i=n, and m, find all subsets of the wi whose sum is m eg) (w1,w2,w3,w4) = (11,13,24,7) and m=31 Solutions are (11,13,7) and (24,7) Representation of solution vector Variable-sized By giving indices In the above example, (1,2,4) and (3,4) Explicit constraints: xi ? { j | j is integer and 1=j=n} Implicit constraints: no two be the same, the sums are m Fixed-sized n-tuple (x1,x2, ,xn) where xi?{0,1}, 1=i=n In the above example, (1,1,0,1) and (0,0,1,1) Variable tuple size Suppose we are given n distinct positive numbers (usually called weights) and we desire to find all combinations of these numbers whose sums are m.This is calle d sum of subsets problem. We could formulate this problem using either fixed-or variable-sized tuple Given positive numbers wi,1<=i<=n, and m, this problem calls for finding all sub sets of the wi whose sums are m.For example if n=4,( w1,w2,w3,w4)=(11,13,24,7), and m=31,then the desired subsets are (11,13,7) and (24,7).Rather than represent the solution vector by the wi which sum to m we could represent the solution ve ctor by giving the indices of these wi.Now the two solutions are described by th e vectors (1,2,4) and (3,4).In general all solutions are k-tuples (x1,x2,.....xk ), 1<=k<=n and different solutions may have different sized tuples. MODULE IV MCA403 ALGORITHM ANALYSIS AND DESIGN ADMN 2009- 10 Dept. of Computer Science And Applications, SJCET, Palai 84 Solution space defined by all paths from the root to any node Fixed tuple size: Solution space defined by all paths from the root to a leaf node Bounding function Bk(x1,..,xk)=true iff Assuming wi s in nondecreasing order, (x1,..,xk) cannot lead to an answer node if MODULE IV MCA403 ALGORITHM ANALYSIS AND DESIGN ADMN 2009- 10 Dept. of Computer Science And Applications, SJCET, Palai 85 So, the bounding function is Backtracking algorithm for the sum of sunsets Invoked by SumOfSubsets(0,1,Si=1,nwi) Terminology A node which has been generated and all of whose children have not yet been gene rated is called a live node The live node whose children are currently being generated is called E-node A dead node is a generated node which is not to be expanded further or all of wh

ose children have been generated Two different ways of tree generation Backtracking MODULE IV MCA403 ALGORITHM ANALYSIS AND DESIGN ADMN 2009- 10 Dept. of Computer Science And Applications, SJCET, Palai 86 Depth first node generation with bounding functions Branch-and-bound E-node remains E-node until it is dead Example: n=6, w[1:6]={5,10,12,13,15,18}, m=30 Four factors for efficiency of the backtracking algorithms Time to generate the next xk Number of xk satisfying the explicit constraints Time for the bounding function Bk Number of xk satisfying Bk

Você também pode gostar