Announcements.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Magic Task Task 1Task 2Task 3Task 4 Task 5Task 6Task 7Task 8 Task 9Task 10 NC Level 3 to 8.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
Greedy Algorithms. Announcements Exam #1 ◦ See me for 2 extra points if you got #2(a) wrong. Lab Attendance ◦ 12 Labs, so if anyone needs to miss a lab.
1 CSE1301 Computer Programming: Lecture 23 Algorithm Design (Part 1)
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?
CSE1301 Computer Programming: Lecture 27 Game Programming: Bingo.
1 CSE1301 Computer Programming: Lecture 25 Software Engineering 2.
Module 6 Lesson 16.
Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday.
More Dynamic Programming Floyd-Warshall Algorithm.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
COSC2007 Data Structures II
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Dijkstra’s Algorithm. This algorithm finds the shortest path from a source vertex to all other vertices in a weighted directed graph without negative.
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
Two Dimensional Arrays
Announcements Network Flow today, depending on how far we get, we will do String Matching on Wednesday Then Review for Final next Monday This week’s lab.
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.
Dynamic Programming. Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming.
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
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
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:
Announcements Finish up Network Flow today Then Review for Final on Monday ◦ HW#5 is due on Monday, let me or the TA’s know if you have trouble starting.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Backtracking & Brute Force Optimization Intro2CS – weeks
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.
Satisfiability and SAT Solvers CS 270 Math Foundations of CS Jeremy Johnson.
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
CompSci Problem Solving: Sudoku  Rules of the Game Sudoku is played with a 9 by 9 "board" consisting of nine 3 by 3 sub-boards. The symbols 1 -
Dynamic Programming … Continued
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.
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
Sit-In Lab 1 Ob-CHESS-ion
CSE 143 Lecture 19 More Recursive Backtracking
Using a Stack Chapter 6 introduces the stack data type.
Algorithm Design and Analysis (ADA)
Analysis and design of algorithm
Using a Stack Chapter 6 introduces the stack data type.
Jordi Cortadella Department of Computer Science
Exercise: Dice roll sum
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
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
© T Madas.
CSE 143 Lecture 18 More Recursive Backtracking
Exercise: Dice roll sum
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

Announcements

Magic Number Square Tentaizu Approach Backtracking Magic Number Square Tentaizu Approach

Magic Number Squares A 3x3 magic number square has 9 numbers {1,2,… 9} that must be arranged in such a way that every row, column, and diagonal has the same sum. For a 3x3 magic number square the sum is 3*(3*3+1)/2 = 15. 15 15

Magic Number Square It is possible to have different sized magic number squares of size nxn, where numbers {1,2,… n*n} must be arranged in such a way that every row and column has the same sum. For a nxn magic number square the sum is n*(n*n+1)/2

Magic Number Square Say we want to create a class MagicSquare that takes in an int n and returns all valid Magic Number Squares of size nxn. Example, n = 3 returns 8 magic squares: 2 7 6 9 5 1 4 3 8 4 9 2 3 5 7 8 1 6 8 1 6 3 5 7 4 9 2 2 9 4 7 5 3 6 1 8 6 1 8 7 5 3 2 9 4 8 3 4 1 5 9 6 7 2 4 3 8 9 5 1 2 7 6 6 7 2 1 5 9 8 3 4

Magic Number Square How would we start? The plan like we did with Eight Queens is just to try a number in the first position. Then we can recursively solve the board at the next position given that the number has been placed. Then we systematically “throw out” boards that do not meet our constraints. What do we need to do that? First we will want an empty board that we will fill in one square at a time. Then we will want to mark which numbers in the set (1, … , n*n) have already been used. We need to calculate the sum that each row, column, and diagonal must add up to.

initialize all values to 0, it’s empty to start! public class MagicSquare { private int[][] square; private boolean[] possible; private int totalSqs; private int sum; private int numsquares; // Creates an empty MagicSquare object. public MagicSquare(int n) { // Fill in an empty square; 0 represents unfilled. square = new int[n][n]; for (int i=0; i<n; i++) for (int j=0; j<n; j++) square[i][j] = 0; // Start with all numbers being possible. totalSqs = n*n; possible = new boolean[totalSqs]; for (int i=0; i<totalSqs; i++) possible[i] = true; sum = n*(n*n+1)/2; numsquares = 0; } square[][] will hold the numbers we place. possible[] will hold the numbers that have still not bee used yet. sum will store what value the rows/columns/diagonals must sum to. square[][] is size nxn initialize all values to 0, it’s empty to start! There are n*n total numbers, so possible[] is size n*n. At first all of the numbers can be used so the entire array is set to true.

Try all possible values for this spot // Recursive method which fills in the square at (row,col). public void fill(int row, int col) { // Screen away incorrect squares. if (!checkRows() || !checkCols() || !checkDiags()) return; // Filled everything, so print !! if (row == square.length) { System.out.println(this); numsquares++; } // Try each possible number for this square. for (int i=0; i<totalSqs; i++) { if (possible[i]) { square[row][col] = i+1; possible[i] = false; // Go to the next square. int newcol = col+1; int newrow = row; if (newcol == square.length) { newrow++; newcol = 0; } // Recursively fill. fill(newrow, newcol); // Undo this square. square[row][col] = 0; possible[i] = true; Try all possible values for this spot Find the new row and col, so we can recursively fill the next spot. Base Cases: Either we broke the constraints, so we throw that board out. OR We’re DONE!! Recursively call fill() on our new spot. Undo this square for future recursive calls, so that we can try ALL valid boards

// Recursive method which fills in the square at (row,col) // Recursive method which fills in the square at (row,col). public void fill(int row, int col) { // Screen away incorrect squares. if (!checkRows() || !checkCols() || !checkDiags()) return; // Filled everything, so print !! if (row == square.length) { System.out.println(this); numsquares++; } // Try each possible number for this square. for (int i=0; i<totalSqs; i++) { if (possible[i]) { square[row][col] = i+1; possible[i] = false; // Go to the next square. int newcol = col+1; int newrow = row; if (newcol == square.length) { newrow++; newcol = 0; } // Recursively fill. fill(newrow, newcol); // Undo this square. square[row][col] = 0; possible[i] = true;

Determine whether each row is filled and the row’s sum. // Returns true iff all the rows are okay. public boolean checkRows() { // Try each row. for (int i=0; i<square.length; i++) { int test = 0; boolean unFilled = false; // Add up the values in this row. for (int j=0; j<square[i].length; j++) { test += square[i][j]; if (square[i][j] == 0) unFilled = true; } // If the row is filled and adds up to the wrong number, // this is an invalid square. if (!unFilled && test != sum) return false; // Never found proof of an invalid row. return true; Determine whether each row is filled and the row’s sum. If the row is filled and adds up to the wrong number then our row constraint is violated, and we return false. At this point we have checked all rows, and none of them violated our constraint.

Magic Number Square Decision Tree Shown on the board…

Tentaizu Approach The game is played on a 7x7 board. Exactly 10 of the 49 squares are each hiding a star. Your task is to determine which squares are hiding the stars. Other squares in the board provide clues: A number in a square indicates how many stars lie next to the square No square with a number in it contains a star, but a star may appear in a square with no adjacent numbers.

Tentaizu Approach Break down the problem into smaller steps Read in the board Determine the squares with numbers Determine the squares that are blank Place the stars in a backtracking method based on the possible locations Check that the current or finished board meets the requirements of the game Ten stars are present Each numbered square has the correct number of adjacent stars

Optional Homework Problem Get together with someone in the class and brainstorm how you will solve the Tentaizu assignment. Turn in a paper describing your strategy. What matrices and/or arrays will you need? What constraints will you check against? How will you know when these constraints are violated? How will you know when the board is complete? Show a decision tree diagramming how your Tentaizu algorithm will work. (Like how we did with the Magic Number Square)

References Slides adapted from Arup Guha’s Computer Science II Lecture notes: http://www.cs.ucf.edu/~dmarino/ucf/cop3503/le ctures/ Additional material from the textbook: Data Structures and Algorithm Analysis in Java (Second Edition) by Mark Allen Weiss Additional images: www.wikipedia.com xkcd.com