Design and Analysis of Algorithm Unit-V Backtracking The General method The 8-queens problem Sum of subsets Graph coloring Hamiltonian cycles Knapsack problem The General Method • The name backtrack was first coined by D.H. Lehmer in the 1950s • Backtracking is one of the most general techniques used to search the state space tree systematically to find solution to some problem. • The backtracking technique is similar to Depth first search in a directed graph. NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm • The solution in backtracking must be expressed as an n-tuple solution vector (x1,x2,…,xn) where Xi are chosen from some finite set Si • The solution vector must satisfy the criterion function P(x1,………,xn) (sometimes called bounding function). • For any problem we solve using backtracking require to satisfy complex set of constraints. These constraints can be divided into two categories: Explicit constraints Implicit constraints • Explicit constraints: Explicit constraints are rules that restrict each xi value to be chosen from a given set Si . e.g. xi } or xi = 0 or 1 or Si = {a : li • 0 a Si = { all nonnegative real numbers Si = { 0, 1 } li xi ui or ui } Implicit constraints: The implicit constraints are rules that determine which of the tuple in the solution space satisfy the criterion function. e.g 8-Queen problem No two queens are on the same row No two queens are on the same column No two queens are on the same diagonal Recursive backtracking algorithm NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm Iterative backtracking algorithm NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm Terminologies: The tree organization of the solution space is state space tree. Each node in the state space tree defines problem state All paths from the root node to other nodes defines the state space of the problem Solution states are those problem states s for which the path from the root to s defines a tuple in the solution space. Answer states are those solution states s for which the path from root to s defines a tuple that is a member of solutions (it satisfies the implicit constraints). A node which has been generated and all of whose children have not yet been generated 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 whose children have been generated Tree organization that are problem instance dependent are called dynamic tree. Bounding functions are used to kill live nodes without generating all their children Tree organization of 4-Queens problem (permutation tree) • In 4-Queens problem, 4-Queens are to be placed on 4x4 chessboard so that No two queens are on the same row NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm No two queens are on the same column No two queens on the same diagonal • The possible tree organization of 4-Queens problem is called a permutation tree. • In permutation tree, the solution space consists of 4! Permutation of 4-tuple solution vector (x1, x2, x3, x4) • The edges in a tree are labeled by possible value xi. Edges from level1 to level2 nodes specify the value for x1, edges from level2 to level3 nodes specify the value for x2 and so on. • The solution space is defined by all paths from root node to a leaf node. The 8-Queens problem • The problem is to place 8-Queens on 8x8 chessboard so that they don’t attach each other by being in the same row, same column and same diagonal. NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm • We can have solution (x1,x2,…,x8) in which each xi is the column of ith row where ith queen is placed. • Two Queens on the same diagonal that run from upper left to the lower right has the same (row - column) value. Also two Queens on the same diagonal that goes from the upper right to lower left has the same (row + column) value. • Suppose two queens are at position (i,j) and (k,l).Then they are on the same diagonal iff. i-j=k-l ………(1) or i+j=k+l ………(2) The first equation implies: j-l=i-k and the second equation implies: j-l=k-i So, two Queens placed on the same diagonal iff |j-l| = |i-k| Example: 4-Queens problem Q1→ Q2→ Q3→ Q4→ NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm The portion of State-space tree of 4-queens problem is 8-queen problem NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm The solution is (4, 6, 8, 2, 7, 1, 3, 5) Algorithm NQueens(k, n) // Using backtracking, this procedure prints all possible placements of n queens on // an nXn chessboard so that they are nonattacking. { for (int i=1; i<=n; i++) { if (Place(k, i)) { x[k] = i; if (k==n) then write (x[1…n]); else NQueens(k+1, n); } } } Algorithm Place( k, i) //Returns true if a queen can be placed in kth row and ith column. Otherwise it // returns false. x[] is a global array whose first (k-1) values have been set. abs(r) // returns the absolute value of r. { for (int j=1; j < k; j++) if ((x[j] == i) // Two in the same column || (abs(x[j]-i) == abs(j-k))) // or in the same diagonal return(false); return(true); } NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm Sum of subset • In a sum of subset problem given a set of n distinct positive numbers(usually called weights) and a positive integer m • The problem is to find all subset of the given set whose sum is equal to a positive integer m • We could formulate this problem using fixed-size or variable-sized tuple. • We consider a backtracking solution using fixed-size tuple strategy. In this case the element xi of the solution vector is either one or zero depending on whether the weight wi is included or not. • The root of tree represents the starting point. The children of any node are easily generated. For a node at level i the left child corresponds to xi=1 and right to xi=0 • The simple choice for the bounding function Bk(x1,..,xk)=true iff k n wi xi wi m i 1 i k 1 • Clearly x1,..,xk can’t lead to an answer node if this condition is not satisfy. • If we assume that wi’s are initially in nondecreasing order, (x1,..,xk) cannot lead to an answer node if k wi xi wk 1 m i 1 • So, the bounding function is k Bk (x1,...,xk ) trueiff wi xi NBNSCOE, SOLAPUR n wi m Prof. A V Mophare Design and Analysis of Algorithm i 1 i k 1 and wi xi wk 1 k m i 1 Algorithm SumOfSub(s, k, r) // Find all subsets of w[1:n] that sum to m. The values of x[j], 1<=j<k, have already //been determined. s= ∑j=1,k-1 w[j]*x[j] and r= ∑j=k,n w[j]. The w[j]'s are in // nondecreasing order. It is assumed that w[1]<=m and ∑i=1n w[i]>=m. { // Generate left child. Note that s+w[k] <= m sine Bk-1 is true. x[k] = 1; ;// Subset found if (s+w[k] == m) then write (x[1:k]) else if (s+w[k]+w[k+1] <= m) SumOfSub(s+w[k], k+1, r-w[k]); // Generate right child and evaluate Bk-1. if ((s+r-w[k] >= m) && (s+w[k+1] <= m)) { x[k] = 0; SumOfSub(s, k+1, r-w[k]); } } Example1: Given n=6, w[1:6]={5,10,12,13,15,18}. Find all possible subset whose sums are m=30 using sum of subset algorithm. Draw portion of state space tree that is generated 2011 10M Example2: Given n=4, (w1, w2, w3, w4)=(7,11,13, 24). Find all possible subset whose NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm sums are m=31 using sum of subset algorithm. 2015 5M Example2: Let w=[w1, w2, w3, w4,w5,w6]=(5, 7,10,12, 15, 18, 20]. Find all possible subset whose sums are m=35 using sum of subset algorithm. Draw portion of state space tree that is generated 2014 10M Solution to Example1: Given n=6, w[1:6]={5,10,12,13,15,18}, m=30. All possible subset whose sum=30 are: A = (5, 10, 15 ) i.e. ( 1, 1, 0 ,0, 1, 0) B = (5, 12, 13 ) i.e. ( 1, 0, 1, 1, 0, 0) C = (12, 18 ) i.e. ( 0, 0, 1, 0, 0, 1) Portion of state space tree is: NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm Graph Coloring • In this problem, for any given graph G and a positive integer m. We will have to color each of the vertices in G using at most m different colors in such a way that no two adjacent vertices have the same color. • This is called m-colorability decision problem. • The m-colorability problem is optimized when for the smaller integer m the graph G can be colored. • The smaller integer m for which the graph G can be colored is called the chromatic number of the graph. NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm • For example. The minimum number of colors required to color the graph shown below is 3. Hence we can say that graph’s chromatic number is 3. • Consider a simple graph containing four nodes. The graph can be colored using atmost 3 colors. • The possible choices to color each node using three colors are: NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm Planar Graph • A graph is said to be planar iff it can be drawn in a plane in such a way that no two edges cross each other. • Consider a Map with different regions be colored in such a way that no two adjacent regions have the same color. • The map can easily be transformed into a graph. Each region of the map becomes a node, and if two regions are adjacent, then the corresponding nodes are joined by an edge. • The following figure shows a map with five regions and its corresponding graph. This map requires four colors. NBNSCOE, SOLAPUR Prof. A V Mophare Design and Analysis of Algorithm Hamiltonian Cycles • Let G = (V, E) be a connected graph with n vertices. A Hamiltonian cycle is a path that visits each vertex exactly once and return to the starting vertex. • In other words, if a Hamiltonian cycle begins at some vertex v1Є G and the vertices of G are visited in the order v1, v2, ………… vn+1, then the edges (vi, vi+1) are in the E, 1 ≤ i ≤ n and the vi are distinct except for v1 and vn+1. Example 1: The graph contains the Hamiltonian cycle 1, 2, 8, 7, 6, 5, 4, 3, 1 Example 2: There is no way to visit all the vertices exactly once. Hence The graph Contains no Hamiltonian cycle NBNSCOE, SOLAPUR Prof. A V Mophare