
Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP’S BSD COE ,DHULE ADA Unit -3 I.S Borse 1 Outline – Chapter 3 1. Backtracking i) Eight Queens Problem ii) Graph Coloring iii) Hamilton Cycles iv) Knapsack Problem 2. Branch and Bound i) Traveling salesman’s problem ii) lower bound theory-comparison trees for sorting /searching iii) lower bound on parallel computation. ADA Unit -3 I.S Borse 2 A short list of categories • Algorithm types we will consider include: – Simple recursive algorithms – Backtracking algorithms – Divide and conquer algorithms – Dynamic programming algorithms – Greedy algorithms – Branch and bound algorithms – Brute force algorithms – Randomized algorithms 3 ADA Unit -3 I.S Borse Backtracking • Suppose you have to make a series of decisions, among various choices, where – You don’t have enough information to know what to choose – Each decision leads to a new set of choices – Some sequence of choices (possibly more than one) may be a solution to your problem • Backtracking is a methodical way of trying out various sequences of decisions, until you find one that “works” 4 ADA Unit -3 I.S Borse Backtracking • Problem: Find out all 3-bit binary numbers for which the sum of the 1's is greater than or equal to 2. • The only way to solve this problem is to check all the possibilities: (000, 001, 010, ....,111) • The 8 possibilities are called the search space of the problem. They can be organized into a tree. ADA Unit -3 I.S Borse 5 Backtracking: Illustration 0 _ _ 1 _ _ 0 0 _ 0 1 _ 1 0 _ 1 1 _ 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 ADA Unit -3 I.S Borse 6 Backtracking • For some problems, the only way to solve is to check all possibilities. • Backtracking is a systematic way to go through all the possible configurations of a search space. • We assume our solution is a vector (a(1),a(2), a(3), ..a(n)) where each element a(i) is selected from a finite ordered set S. ADA Unit -3 I.S Borse 7 Backtracking and Recursion • Backtracking is easily implemented with recursion because: • The run-time stack takes care of keeping track of the choices that got us to a given point. • Upon failure we can get to the previous choice simply by returning a failure code from the recursive call. ADA Unit -3 I.S Borse 8 Improving Backtracking: Search Pruning • It help us to reduce the search space and hence get a solution faster. • The idea is to a void those paths that may not lead to a solutions as early as possible by finding contradictions so that we can backtrack immediately without the need to build a hopeless solution vector. ADA Unit -3 I.S Borse 9 Solving a maze • Given a maze, find a path from start to finish • At each intersection, you have to decide between three or fewer choices: – Go straight – Go left – Go right • You don’t have enough information to choose correctly • Each choice leads to another set of choices • One or more sequences of choices may (or may not) lead to a solution • Many types of maze problem can be solved with backtracking 10 ADA Unit -3 I.S Borse Backtracking (animation) dead end ? dead end dead end ? start ? ? dead end dead end ? success! 11 ADA Unit -3 I.S Borse Terminology I A tree is composed of nodes There are three kinds of nodes: Backtracking can be thought of as The (one) root node searching a tree for a particular “goal” leaf node Internal nodes Leaf nodes 12 ADA Unit -3 I.S Borse Terminology II • Live node: A node which has been generated and all of whose children have not yet generated • E-node: The live node whose children are currently being generated • Dead node : which is not expanded further or all of whose children have been generated • DFS: Depth first node generation with bounding function is called backtracking. 13 ADA Unit -3 I.S Borse The backtracking algorithm • Backtracking is really quite simple--we “explore” each node, as follows: • To “explore” node N: 1. If N is a goal node, return “success” 2. If N is a leaf node, return “failure” 3. For each child C of N, 3.1. Explore C 3.1.1. If C was successful, return “success” 4. Return “failure” 14 ADA Unit -3 I.S Borse n-Queens Problem • Given: n-queens and an nxn chess board • Find: A way to place all n queens on the board s.t. no queens are attacking another queen. • How could you formulate this problem? • How would you represent a solution? ADA Unit -3 I.S Borse 15 How Queens Work ADA Unit -3 I.S Borse 16 First Solution Idea • Consider every placement of each queen 1 at a time. – How many placements are there? ADA Unit -3 I.S Borse 17 First Solution Idea • Consider every possible placement – How many placements are there? n2 64 4,426,165,368 n 8 ADA Unit -3 I.S Borse 18 Second Solution Idea • Don’t place 2 queens in the same row. – Now how many positions must be checked? ADA Unit -3 I.S Borse 19 The Eight Queens Problem 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 ADA Unit -3 I.S Borse 20 The Eight Queens Problem 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 ADA Unit -3 I.S Borse 21 The Eight Queens Problem 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 ADA Unit -3 I.S Borse 22 The Eight Queens Problem 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 ADA Unit -3 I.S Borse 23 Second Solution Idea • Don’t place 2 queens in the same row. – Now how many positions must be checked? Represent a positioning as a vector [x1, …, x8] Where each element is an integer 1, …, 8. nn 88 16,777,216 ADA Unit -3 I.S Borse 24 Third Solution Idea • Don’t place 2 queens in the same row or in the same column. • Generate all permutations of (1,2…8) – Now how many positions must be checked? ADA Unit -3 I.S Borse 25 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 (1,2,3,4,5,6,7,8) ADA Unit -3 I.S Borse 26 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 (1,2,3,4,5,6,8,7) ADA Unit -3 I.S Borse 27 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 (1,2,3,4,5,8,6,7) ADA Unit -3 I.S Borse 28 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 (1,2,3,4,5,8,7,6) ADA Unit -3 I.S Borse 29 Third Solution Idea • Don’t place 2 queens in the same row or in the same column. • Generate all permutations of (1,2…8) – Now how many positions must be checked? n! 8! 40,320 • We went from C(n2, n) to nn to n! – And we’re happy about it! • We applied explicit constraints to shrink our search space. ADA Unit -3 I.S Borse 30 Four Queens Problem 1 2 3 4 ADA Unit -3 I.S Borse 31 Four Queens Problem x1 = 1 x2 = 2 x3 = 3 x4 = 4 1 2 3 4 Each branch of the tree represents a decision to place a queen. The criterion function can only be applied to leaf nodes. ADA Unit -3 I.S Borse 32 Four Queens Problem x1 = 1 x2 = 2 x3 = 3 x4 = 4 1 2 3 4 Is this leaf node a solution? ADA Unit -3 I.S Borse 33 Four Queens Problem x1 = 1 x2 = 2 x3 = 3 x3 = 4 x4 = 4 x4 = 3 1 2 3 4 Is this leaf node a solution? ADA Unit -3 I.S Borse 34 Four Queens Problem Using the criterion function, this is the search tree. ADA Unit -3 I.S Borse 35 Four Queens Problem x1 = 1 x2 = 2 x3 = 3 x3 = 4 x4 = 4 x4 = 3 1 2 3 4 A better question: Is any child of this node ever going to be a solution? ADA Unit -3 I.S Borse 36 Four Queens Problem x1 = 1 x2 = 2 x3 = 3 x3 = 4 x4 = 4 x4 = 3 1 2 3 4 A better question: Is any child of this node ever going to be a solution? No. 1 is attacking 2. Adding queens won’t fix the problem. ADA Unit -3 I.S Borse 37 Four Queens Problem x1 = 1 x2 = 2 1 2 3 4 The partial criterion or feasibility function checks that a node may eventually have a solution. Will this node have a solution? no. ADA Unit -3 I.S Borse 38 Four Queens Problem x1 = 1 x2 = 2 x2 = 3 1 2 3 4 Will this node have a solution? Maybe. ADA Unit -3 I.S Borse 39 Four Queens Problem x1 = 1 x2 = 2 x2 = 3 x3 = 2 1 2 3 4 Will this node have a solution? No.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages120 Page
-
File Size-