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"?

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

PROLOG 8 QUEENS PROBLEM.
CPS 100, Fall Backtracking by image search.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
The N-Queens Problem lab01.
CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
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 A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
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
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 15 More on Recursion Lecture 15 More on Recursion.
Two Dimensional Arrays
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.
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.
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.
Data Structures Using Java1 Chapter 5 Recursion. Data Structures Using Java2 Chapter Objectives Learn about recursive definitions Explore the base case.
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
CompSci 100 Prog Design and Analysis II Oct 26, 2010 Prof. Rodger CPS 100, Fall
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 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.
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
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
Algorithm Design and Analysis (ADA)
Building Java Programs
Recursion Copyright (c) Pearson All rights reserved.
Board: Objects, Arrays and Pedagogy
Back Tracking.
Analysis and design of algorithm
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.
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"?
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:

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

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 * ... 1 2 3 4 5 6 7 8 Q ...

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 * ... 1 2 3 4 5 6 7 8 Q ...

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/Constructor Description 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

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."); } ...

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

Graphical User Interfaces Involve large numbers of interacting objects and classes Highly framework-dependent Path of code execution unknown Users can interact with widgets in any order Event-driven In Java, AWT vs. Swing; GUI builders vs. writing by hand

Swing Framework Great case study in OO design

Composite Layout