Partial preview of the text
Download The doc explores the knight tour problem, solution based on backtracking and more Schemes and Mind Maps Design and Analysis of Algorithms in PDF only on Docsity!
The Knight’s Tour Problem: Given a N*N board with the Knight placed on the first block of an empty board. Moving according to the rules of chess knight must visit each square exactly once. Print the order of each the cell in which they are visited What is Backtracking: Backtracking is basically recursion with undoing the recursive step if a solution for the given path does not exist. Solving one piece at a time, and removing those solutions that fail to satisfy the constraints of the problem at any point of time. Backtracking works in an incremental way to attack problems. Typically, we start from an empty solution vector and one by one add items (In this context of Knight’s tour problem, an item is a Knight's move). When we add an item, we check if adding the current item violates the problem constraint, if it does then we remove the item and try other alternatives. If none of the alternatives works out then we go to the previous stage and remove the item added in the previous stage. If we reach the initial stage back then we say that no solution exists. If adding an item doesn’t violate constraints then we recursively add items one by one. If the solution vector becomes complete then we print the solution. Bae er cas 3. Do following for each move number from 2 to the number of Teer! eS eeed eer ue Set P to be the position in S with minimum accessibility Mark the board at P with the current move number /4. Return the marked board — each square will be marked with the fiona ena RS) SEN ea aes 59 34 1 43 37 50 43 46 TS-VALID(i, j, sol) Af (dp=1 and iceN and jo=1 and j<=N and sol[i]{J] return TRUE return FALSE We will start by checking if the solution is found, If the solution is found (step_count then we will just return true, NON), KNIGHT-TOUR(sol, i, j, step_count, x_move, y move) if step_count == N*N return TRUE ‘Our next task is to move to the next possible knight's move and check if this will lead us to the solution. If not, then we will select the different move and if none of the moves are leading us to the solution, then we will return false. ‘As mentioned above, to find the possible moves, we will iterate over the x_move and the y_move arrays. Now, we have to check if the cell (itx_movefkl, j+y_movetk}) is valid or not. Ifitis valid then we will move to that cell - sol [i+x_move[k]][J+y_movelk]] = step_count and check if this path is leading us to the solution ot not by calling the function again by recursion from the new position. We start by moving the knight and if the knight reaches to a cell from where there is no further cell available to move and we have not reached to the solution yet (not all colls are covered), then we backtrack and change our decision and choose a different path. So from a cell, we choose a move of the knight from all the moves available, and then recursively check if this will ead us to the solution or not. If not, then we choose a different path, We keep the track of the moves in a matrix. This matrix stores the step number in which we visited a cell. For example, if we visit a cell in the second step, it will have a value of 2. This matrix also helps us to know whether we have covered all the cells or not. If the last visited cell has a value of N*N, it means we have covered all the cells. ‘Thus, our approach includes starting from a cell and then choosing a move from all the available moves. Then we check if this move will lead us to the solution or not. If not, we choose a different move. Also, we store all the steps in which we are moving ina matrix ns: Tee eee Rue a ge UR LY) Bi ear near en ke Messe ak The accessibility of a position P is the number of positions accessible from P. next_i = itx move[k] next_j = j+y_move[k] Af IS-VALID(next_i, next_j, sol) sol[next_i][next_j] = step_count Af KNIGHT-TOUR(sol, next_i, next_j, step_count+1, x_move, y move) return TRUE sol[next_1, next_j] = -1 return FALSE Ifthe move (i#x_move[k}, j+y_movefk]) is leading us to the solution - if KNIGHT- TOUR(sol, i+x move[k], j+y_nove[k], step_count+1, x move, y move), then We are returning true. If this move is not leading us to the solution, then we will choose a different move {loop will iterate to a different move). Also, we will again make the cell (}#x_movelk], i+y_movelki) of solution matrix-1 as we have checked this path and itis not leading Us to the solution, so leave it and thus itis backtracking. SSTART-KNIGHT-TOUR() sol =[1(] for i in 1 toN for j in 1 toN sol[4](3] = 1 xmove = [2, 1, -1, -2, -2, -1, 1, 2] yomove = [1, 2, 2) 1 ~ -1] sol[1, 1] = @ if KNIGHT-TOUR(sol, 1, 1, 1, xmove, y_move) return TRUE return FALSE At last, if none the possible move retums us false, then we will just retum false.