CSE 143 read: 12.5 Lecture 18: recursive backtracking.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
The N-Queens Problem lab01.
CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
Building Java Programs
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking What is backtracking?
Backtracking.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
Building Java Programs
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
Recursion: Backtracking
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
NxN Queens Problem Q- -- -Q Q- -- -Q Q- -- QQ -- Q- Q- Q- -Q QQ -- -Q -- -Q Q- -Q -Q Q- Q- -Q Q- -- Q- -- QQ Q- -Q -Q -Q -- QQ -- -Q START.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Two Dimensional Arrays
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
CHAPTER 4 RECURSION. BASICALLY, A METHOD IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
CSE 143 Lecture 18 Recursive Backtracking reading: 12.5 or "Appendix R" on course web site slides adapted from Marty Stepp and Hélène Martin
1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 Problem-solving approaches  divide & conquer  greedy  backtracking examples: N-queens, Boggle,
Building Java Programs Appendix R Recursive backtracking.
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze  Certain mazes can be solved using the “right-hand”
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
Recursion Copyright (c) Pearson All rights reserved.
Recursion. 2 recursion: The definition of an operation in terms of itself. –Solving a problem using recursion depends on solving smaller occurrences of.
CPS 100, Spring Search, Backtracking,Heuristics l How do you find a needle in a haystack?  How does a computer play chess?  Why would you write.
CSE 143 Lecture 17 Recursive Backtracking slides created by Marty Stepp ideas and examples taken from Stanford University.
1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
CSG3F3/ Desain dan Analisis Algoritma
Backtracking, Search, Heuristics
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
read: 12.5 Lecture 17: recursive backtracking
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CSE 143 Lecture 19 More Recursive Backtracking
Algorithm Design and Analysis (ADA)
Building Java Programs
Recursion Copyright (c) Pearson All rights reserved.
Back Tracking.
Analysis and design of algorithm
Exercise: fourAB Write a method fourAB that prints out all strings of length 4 composed only of a’s and b’s Example Output aaaa baaa aaab baab aaba baba.
Exercise: Dice roll sum
CSE 143 Lecture 17 Recursive Backtracking
Ch. 6 Recursion as a Problem Solving Technique
Exercise: Dice roll sum Write a method diceSum similar to diceRoll, but it also accepts a desired sum and prints only arrangements that add up to.
Exercise: Dice roll sum
Algorithms: Design and Analysis
CSE 143 Lecture 18 More Recursive Backtracking
CSC 172 DATA STRUCTURES.
Recursive backtracking
Exercise: Dice rolls Write a method diceRoll that accepts an integer parameter representing a number of 6-sided dice to roll, and output all possible arrangements.
CSE 143 Lecture 18 More Recursive Backtracking
Exercise: Dice roll sum
Recursive backtracking
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Backtracking, Search, Heuristics
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Backtracking, Search, Heuristics
Presentation transcript:

CSE 143 read: 12.5 Lecture 18: recursive backtracking

2 Backtracking strategies When solving a backtracking problem, ask these questions: What are the "choices" in this problem? What is the "base case"? (How do I know when I'm out of choices?) How do I "make" a choice? Do I need to create additional variables to remember my choices? Do I need to modify the values of existing variables? How do I explore the rest of the choices? Do I need to remove the made choice from the list of choices? Once I'm done exploring, what should I do? How do I "un-make" a choice?

3 The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"? How do we "make" or "un-make" a choice? How do we know when to stop? Q Q Q Q Q Q Q Q

4 Naive algorithm for (each square on board): Place a queen there. Try to place the rest of the queens. Un-place the queen. How large is the solution space for this algorithm? 64 * 63 * 62 * Q

5 Better algorithm idea Observation: In a working solution, exactly 1 queen must appear in each row and in each column. Redefine a "choice" to be valid placement of a queen in a particular column. How large is the solution space now? 8 * 8 * 8 * Q Q 4 5Q 6 7 8

6 Exercise Suppose we have a Board class with these methods: Write a method solveQueens that accepts a Board as a parameter and tries to place 8 queens on it safely. Your method should stop exploring if it finds a solution. Method/ConstructorDescription public Board(int size) construct empty board public boolean isSafe(int row, int column) true if queen can be safely placed here public void place(int row, int column) place queen here public void remove(int row, int column) remove queen from here public String toString() text display of board

7 Recall: Backtracking A general pseudo-code algorithm for backtracking problems: Explore(choices): if there are no more choices to make: stop. else, for each available choice C: Choose C. Explore the remaining choices. Un-choose C, if necessary. (backtrack!)

8 Exercise solution // Searches for a solution to the 8 queens problem // with this board, reporting the first result found. public static void solveQueens(Board board) { if (solveQueens(board, 1)) { System.out.println("One solution is as follows:"); System.out.println(board); } else { System.out.println("No solution found."); }...

9 Exercise solution, cont'd. // Recursively searches for a solution to 8 queens on this // board, starting with the given column, returning true if a // solution is found and storing that solution in the board. // PRE: queens have been safely placed in columns 1 to (col-1) public static boolean solveQueens(Board board, int col) { if (col > board.size()) { return true; // base case: all columns are placed } else { // recursive case: place a queen in this column for (int row = 1; row <= board.size(); row++) { if (board.isSafe(row, col)) { board.place(row, col); // choose if (explore(board, col + 1)) { // explore return true; // solution found } b.remove(row, col); // un-choose } return false; // no solution found }