1 / 15

Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking

Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking. A Limerick. int factorial(int sum) {if (sum == 1) return 1; if (sum != 1) return product(sum, factorial(sum - 1)); }. Backtracking. It is often useful to solve a problem through trial and error , and backtracking:

willow-roy
Download Presentation

Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data StructuresCSCI 132, Spring 2014Lecture 17Backtracking

  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: 8 9 ... 13 Number of solutions: 92 352 73712 Running time (seconds) .05 .21 243.05 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 109 possibilities. If you know you only have 1 queen per row and use this fact, the number of possible placements decreases to about : 88 = 16.8 x 106 possibilities. Rejecting guarded columns when placing the queens gives: 8! = 4.0 x 105 possibilities.

More Related