1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
The N-Queens Problem lab01.
Eight queens puzzle. The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard such that none of them are able to capture.
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?
Chapter5 RECURSION. Outline 1. Introduction to Recursion 2. Principles of Recursion 3. Backtracking: Postponing the Work 4. Tree-Structured Programs:
Lecture 4 1) RECURSION 2)BACKTRACKING 3)LOOK AHEAD.
Chapter 5 Recursion as a Problem-Solving Technique.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
Data Structures Using C++ 2E Chapter 6 Recursion.
Dr. Jouhaina Chaouachi Siala
1 Chapter 1 RECURSION. 2 Chapter 1  Subprogram implementation  Recursion  Designing Recursive Algorithms  Towers of Hanoi  Backtracking  Eight Queens.
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion Chapter 5.
Announcements.
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
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Contents of Chapter 7 Chapter 7 Backtracking 7.1 The General method
Two Dimensional Arrays
1 Data Structures CSCI 132, Spring 2014 Lecture 4 Implementing Life.
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.
Sudoku Jordi Cortadella Department of Computer Science.
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
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 19 Lists.
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
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 18 More Recursive Backtracking slides created by Marty Stepp
1 Data Structures CSCI 132, Spring 2014 Lecture 18 Recursion and Look-Ahead.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Analysis & Design of Algorithms (CSCE 321)
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
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.
Recursion Powerful Tool
Backtracking, Search, Heuristics
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
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
1) RECURSION 2) BACKTRACKING 3) LOOK AHEAD
Recursion.
Recursion Copyright (c) Pearson All rights reserved.
Analysis and design of algorithm
Jordi Cortadella Department of Computer Science
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
CSE 143 Lecture 18 More Recursive Backtracking
Recursion as a Problem-Solving Technique
CSE 143 Lecture 18 More Recursive Backtracking
Exercise: Dice roll sum
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"?
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Backtracking, Search, Heuristics
Data Structures & Programming
Presentation transcript:

1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking

2 A Limerick int factorial(int sum) {if (sum == 1) return 1; if (sum != 1) return product(sum, factorial(sum - 1)); }

3 Backtracking It is often useful to solve a problem through trial and error, and backtracking: Try one step and carry it out as far as possible. If it doesn't lead to a solution, back up to an intermediate step and try another step. This is known as backtracking. If the steps in a solution to the problem are similar to the whole problem (i.e. they are smaller versions of the larger problem), then we can use recursion to implement this approach.

4 The Eight Queens Problem Can we place 8 queens on a chessboard so that none can take another? (I.e. none share a row, column or diagonal)? Q

5 Strategy for solving the problem with n queens If we have m queens positioned on the board (n x n board), we need to find positions for the remaining n-m queens so that none attacks another. Given a configuration: If n queens are on the board, print it out--it's solved. Otherwise, for each unguarded position: place a queen on that position solve from this new configuration. remove the queen from that position.

6 Implementing solution in C++ void solve_from (Queens &configuration) { } //Row number is kept track of in the Queens class and it // is the same as the number of queens on the board.

7 Implementing solution in C++ void solve_from (Queens &configuration) { if (configuration.is_solved()) { configuration.print(); } else { for (int col=0; col < configuration.board_size, col++){ if (configuration.unguarded(col) { configuration.insert(col); solve_from(configuration); configuration.remove(col); } //Row number is kept track of in the Queens class and it // is the same as the number of queens on the board.

8 The Queens class const int max_board = 30; class Queens { public: Queens(int size); bool is_solved( ) const; void print( ) const; bool unguarded(int col) const; void insert(int col); void remove(int col); int board_size; private: int count; bool queen_square[max_board][max_board]; };

9 Implementing Queens( ) Queens :: Queens (int size) { }

10 Implementing Queens( ) Queens :: Queens (int size) { board_size = size; count = 0; for (int row = 0; row < board_size; row++) { for (int col = 0; col < board_size; col++) { queen_square[row][col] = false; }

11 Implementing insert( ) void Queens :: insert(int col) { }

12 Implementing insert( ) void Queens :: insert(int col) { queen_square[count][col] = true; count++; }

13 Implementing unguarded( ) bool Queens :: unguarded(int col) const { int i; bool ok = true; for (i = 0; ok && (i < count); i++) { ok = !queen_square[i][col]; } //squares in same column above current row for (i = 0; ok && (count - i >= 0) && (col - i >= 0); i++) { ok = !queen_square[count - i][col - i]; } //squares in upper left diagonal for (i = 0; ok && (count - i >= 0) && (col + i < board_size); i++) { ok = !queen_square[count - i][col + i]; } //squares in upper right diagonal return ok; }

14 Running time of Queens solution Board size: Number of solutions: Running time (seconds) Both the number of solutions and the time to find them all increase rapidly as the board size increases. It is possible to increase the efficiency somewhat (see text for details).

15 Efficiency of Queens solution The Queens solution is reasonably efficient: If try all possible placements of queens randomly, get: 64 things taken 8 at a time = 4.4 x 10 9 possibilities. If you know you only have 1 queen per row and use this fact, the number of possible placements decreases to about : 8 8 = 16.8 x 10 6 possibilities. Rejecting guarded columns when placing the queens gives: 8! = 4.0 x 10 5 possibilities.