Recursion: Backtracking

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Types of Algorithms.
The Efficiency of Algorithms
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
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.
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?
November 10, 2009Introduction to Cognitive Science Lecture 17: Game-Playing Algorithms 1 Decision Trees Many classes of problems can be formalized as search.
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.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Backtracking.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
1 Chapter 1 RECURSION. 2 Chapter 1  Subprogram implementation  Recursion  Designing Recursive Algorithms  Towers of Hanoi  Backtracking  Eight Queens.
Recursion Chapter 5.
Announcements.
COSC2007 Data Structures II
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
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.
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.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
Backtracking and Games Eric Roberts CS 106B January 28, 2013.
Data Structures & Algorithms
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"?
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
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.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Kruse/Ryba ch051 Object Oriented Data Structures Recursion Introduction to Recursion Principles of Recursion Backtracking: Postponing the Work Tree-Structured.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
CSCE350 Algorithms and Data Structure Lecture 21 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
An Analysis of the n- Queens problem Saleem Karamali.
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.
CSG3F3/ Desain dan Analisis Algoritma
Backtracking, Search, Heuristics
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
Sit-In Lab 1 Ob-CHESS-ion
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
7.4 Problem Solving with Recursion
Recursion Copyright (c) Pearson All rights reserved.
Jordi Cortadella Department of Computer Science
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
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
Presentation transcript:

Recursion: Backtracking Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma

Objectives (Chapter 5) Understand backtracking algorithms and use them to solve problems Use recursive functions to implement backtracking algorithms See how the choice of data structures can affect the efficiency of a program

The Eight-Queens Puzzle How to place eight queens on a chess board so that no queen can take another Remember that in chess, a queen can take another piece that lies on the same row, the same column or the same diagonal (either direction) There seems to be no analytical solutions

Solutions do exists Requires luck coupled with trial and error Or much exhaustive computation

Solve the Puzzle How will you solve the problem? Put the queens on the board one by one in some systematic/logical order Make sure that the queen placed will not be taken by another already on the board If you are lucky to put eight queens on the board, one solution is found Otherwise, some queens need to be removed and placed elsewhere to continue the search for a solution

Why put the queens in a systematic order? Make sure that you will not consider the same situation again The whole process is suitable for recursive programming A large problem can be divided into small subproblems with a similar nature

An Outline of the Recursive Function Recursive function: solve_from Given a configuration of queens on the chessboard, search for all solutions Class Queens Represent a particular configuration of queens on the chessboard Code sketch solve_from (Queens configuration) { if configuration already contains eight queens Print configuration else for every square p that is unguarded by configuration { add a queen on square p to configuration; solve_from(configuration); remove the queen from square p of configuration; } Stop Condition Emphasize the all back tracking algorithms use this exact format. If a student uses something else, it won’t be correct. Emphasize the importance of the for loop. The essence of back-tracking is to first figure out the steps needed to build the partial solution towards the final solution. Secondly, at each step, figure out all the options to try one at a time. It is a brute force approach. Sub-problems. Need to try all possibilities

How to find the next square to try? There must be a queen, exactly one queen in each row There can never be more than one queen in each row Therefore, we can place the queens one row at a time in order What we have decided is actually a systematic way to solve the problem

Example: Four Queens X: Guarded squares ?: Other squares that have not been tried

Backtracking Backtracking algorithms Search for a solution by constructing partial solutions that remain consistent with the requirements of the problem, and then extending a partial solution toward completion When inconsistency occurs, the algorithms backs up (backtracks) by removing the most recently constructed part of the solution, and then trying another possibility

Suitable for implementations using Recursion, or Stacks Only the most recent part is used Useful for situations where many possibilities may first appear, but few survive further tests Scheduling problems Arranging sports tournaments Designing a compiler Parsing

Main program int main( ) /* Pre: The user enters a valid board size. Post: All solutions to the n-queens puzzle for the selected board size are printed. Uses: The class Queens and the recursive function solve_from. */ { int board_size; print_information( ); cout << "What is the size of the board? " << flush; cin >> board_size; if (board size < 0 || board size > max_board) cout << "The number must be between 0 and “ << max_board << endl; else { Queens configuration(board_size); // Initialize empty configuration. solve_from(configuration); // Find all solutions extending configuration. }

The configuration: Queens class Constructor set the user-selected board size and initialize the empty Queens object print print the solutions unguarded bool Queens :: unguarded(int col) const; Post: Returns true or false according as the square in the first unoccupied row (row count) and column col is not guarded by any queen. Why no row number?

insert remove is_solved void Queens :: insert(int col); Pre: The square in the first unoccupied row (row count) and column col is not guarded by any queen. Post: A queen has been inserted into the square at row count and column col; row count has been incremented by 1. remove void Queens :: remove(int col); Pre: There is a queen in the square in row count - 1 and column col. Post: The above queen has been removed; count has been decremented by 1. is_solved bool Queens :: is_solved( ) const; Post: The function returns true if the number of queens already placed equals board size; otherwise, it returns false.

The backtracking function: solve_from void solve_from(Queens &configuration) /* Pre: The Queens configuration represents a partially completed arrangement of non-attacking queens on a chessboard. Post: All n-queens solutions that extend the given configuration are printed. The configuration is restored to its initial state. Uses: The class Queens and the function solve_from , recursively. */ { 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); // Recursively continue to add queens. configuration.remove(col); } See the advantage of using OOP rather than procedure-based.

Implementation of the Queens Class Store the chessboard as a 2-dimensional array with entries indicating the locations of the queens 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 col); void reminsert(int ove(int col); int board_size; // dimension of board = maximum number of queens private: int count; // current number of queens = first unoccupied row bool queen_square[max_board][max_board]; };

is_solved, remove and print are also trivial Constructor Queens :: Queens(int size) /* Post: The Queens object is set up as an empty configuration on a chessboard with size squares in each row and column. */ { 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; } insert void Queens :: insert(int col) /* Pre: The square in the first unoccupied row (row count ) and column col is not guarded by any queen. Post: A queen has been inserted into the square at row count and column col ; count has been incremented by 1. */ { queen_square[count++][col] = true; } is_solved, remove and print are also trivial A part of the big 2-d Boolean array is used.

unguarded bool Queens :: unguarded(int col) const /* Post: Returns true or false according as the square in the first unoccupied row (row count ) and column col is not guarded by any queen. */ { int i; bool ok = true; // turns false if there is a queen in column or diagonal for (i = 0; ok && i < count; i++) // Check upper part of column ok = !queen_square[i][col]; for (i = 1; ok && count - i >= 0 && col - i >= 0; i++) // Check upper-left diagonal ok = !queen_square[count - i][col - i]; for (i = 1; ok && count - i >= 0 && col + i < board size; i++) // Check upper-right diagonal ok = !queen_square[count - i][col + i]; return ok; }

Exercise Describe a rectangular maze by indicating its paths and walls within an array. Write a backtracking program to find a way through the maze You are a tournament director and need to arrange a round robin tournament among N = 2k players. In this tournament, everyone plays exactly one game each day; after N - 1 days, a match occurred between every pair of players. Write a backtracking program to do this.

Review and Refinement The time increases rapidly with the board size First refinement Use the 2-dimensional array to keep track of all the squares that are guarded by queens For each square, keep a count of the number of queens guarding the square Faster, but we still need the loops to update the guard counts for each square

Second refinement Objective Key idea Eliminate all loops Key idea Each row, column and diagonal can contain at most one queen Keep track of unguarded squares by using three bool arrays: col_free, upward_free and downward_free Diagonals from the lower left to the upper right are called upward diagonals Diagonals from the upper left to the lower right are called downward diagonals An integer array is used to record the column number for the queens in each row

Eliminate loops It is trivial to identify each column How to identify each diagonal For upward diagonals, the row and column indices have a constant sum The sum ranges from 0 to 2  board_size – 2 The sum can be used to identify each upward diagonals The square in row i and column j is in upward diagonal number i + j For downward diagonals, the difference of the row and column indices is constant The difference ranges from -board_size+1 to board_size–1 The downward diagonal can be numbered using the difference The square in row i and column j is in downward diagonal number i - j + board_size - 1

Refined Implementation Revised Queens class 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 col_free[max_board]; bool upward_free[2 * max_board - 1]; bool downward_free[2 * max_board - 1]; // column number of queen in each row int queen_in_row[max_board]; };

Constructor Queens :: Queens(int size) /* Post: The Queens object is set up as an empty configuration on a chessboard with size squares in each row and column. */ { board_size = size; count = 0; for (int i = 0; i < board_size; i++) col_free[i] = true; for (int j = 0; j < (2 * board size - 1); j++) upward_free[j] = true; for (int k = 0; k < (2 * board size - 1); k++) downward_free[k] = true; }

Insertion void Queens :: insert(int col) /* Pre: The square in the first unoccupied row (row count ) and column col is not guarded by any queen. Post: A queen has been inserted into the square at row count and column col; count has been incremented by 1. */ { queen_in_row[count] = col; col_free[col] = false; upward_free[count + col] = false; downward_free[count - col + board_size - 1] = false; count++; }

Unguarded bool Queens :: unguarded(int col) const /* Post: Returns true or false according as the square in the first unoccupied row (row count ) and column col is not guarded by any queen. */ { return col_free[col] && upward_free[count + col] && downward_free[count - col + board_size - 1]; }

Evaluation New Implementation 2-D array Implementation

Analysis of Backtracking Effectiveness of Backtracking Naïve approach Consider all configurations: put all 8 queens on the board and reject illegal configurations There can be only one queen in each row 88 = 16,777,216 There can be only one queen in each column 8! = 40,320 Our program is even better, since it rejects squares in guarded diagonals

Part of the recursion tree for the eight-queens problem

Lower Bounds Number of solutions For n-queens problem, the amount of work done by backtracking problem still grows very fast To place a queen in each of the first n/4 rows, backtracking algorithm investigates a minimum of n(n - 3)(n - 6) … (n – 3 * n/4) positions n (n - 3) (n - 6) … (n – 3 * n / 4) > ( n / 4) (n/4) Exponential growth Number of solutions Not bounded by any polynomial in n Even not bounded by any exponential form kn, where k is a constant It is proved to be an unsolved problem