Presentation is loading. Please wait.

Presentation is loading. Please wait.

Analysis and design of algorithm

Similar presentations


Presentation on theme: "Analysis and design of algorithm"— Presentation transcript:

1 Analysis and design of algorithm
Unit-6 BackTracking Analysis and design of algorithm

2 Outline Overview and mechanism
Applications - 8-queen problem algorithm 8-queen problem algorithm analysis.

3 Overview Backtracking is a systematic method to iterate through all the possible configurations of a search space. It is a general algorithm/technique which must be customized for each individual application. Recursion is the key in backtracking programming. As the name sug­gests we back­track to find the solution.

4 Mechanism start with one possible move out of many available moves and try to solve the problem. If we are able to solve the problem with the selected move then we will print the solu­tion else we will back­track and select some other move and try to solve it. If none if the moves work out we will claim that there is no solution for the problem.

5 Model our solution as a vector a = (a1, a2,
Model our solution as a vector a = (a1, a2, ..., an), where each element ai is selected from a finite ordered set Si . Such a vector might represent an arrangement where ai contains the ith element of the permutation. Or the vector might represent a given subset S, where ai is true if and only if the ith element of the universe is in S. At each step in the backtracking algorithm, we start from a given partial solution, say, a = (a1, a2, ..., ak), and try to extend it by adding another element at the end. After extending it, we must test whether what we have so far is a solution. If not, we must then check whether the partial solution is still potentially extendible to some complete solution. If so, recur and continue. If not, we delete the last element from a and try another possibility for that position, if one exists.

6 Applications It is basically used to solve the constraint satisfaction problems like N queen problem Sudoku Knight problem crossword

7 Algorithm general backtracking
Pick a starting point. while(Problem is not solved) For each path from the starting point. check if selected path is safe, if yes select it and make recursive call to rest of the problem If recursive calls returns true, then return true. else undo the current move and return false. End For If none of the move works out, return false, NO SOLUTON.

8 8 queen problem by The 8 queen problem is a case of more general set of problems namely “n queen problem”. The problem is : How to place n queen on n by n board, so that they don’t attack each other.

9 General Idea Q1 attacks some positions, therefore Q2 has to comply with these constraints and take place, not directly attacked by Q1. Placing Q3 is harder, since we have to satisfy constraints of Q1and Q2. Going the same way we may reach point, where the constraints make the placement of the next queen impossible. Therefore we need to relax the constraints and find new solution. To do this we are going backwards and finding new admissible solution. To keep everything in order we keep the simple rule: last placed, first displaced. In other words if we place successfully queen on the ith column but cannot find solution for (i+1)th queen, then going backwards we will try to find other admissible solution for the ith queen first. This process is called backtracking.

10 Algorithm for N queen Problem:
1) Start in the leftmost column 2) If all queens are placed return true 3) Try all rows in the current column. Do following for every tried row. a) If the queen can be placed safely in this row then mark this [row, column] as part of the solution and recursively check if placing queen here leads to a solution. b) If placing queen in [row, column] leads to a solution then return true. c) If placing queen doesn't lead to a solution then umark this [row, column] (Backtrack) and go to step (a) to try other rows. d) If all rows have been tried and nothing worked, return false to trigger backtracking.

11 Example: 4 queen problem.

12

13

14

15


Download ppt "Analysis and design of algorithm"

Similar presentations


Ads by Google