Exercise: Dice roll sum

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

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
Building Java Programs
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Lecture 4 1) RECURSION 2)BACKTRACKING 3)LOOK AHEAD.
Backtracking.
Building Java Programs
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
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
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.
Probability 2 Compound Probability.  Now lets consider the following:  2 dice are rolled and the numbers are added together.  What are the numbers.
CSE 143 Lecture 18 Recursive Backtracking reading: 12.5 or "Appendix R" on course web site slides adapted from Marty Stepp and Hélène Martin
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.
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"?
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.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Eight Queens Problem The problem is to place 8 queens on a chess board so that none of them can attack the other. A chess board can be considered a plain.
Search Problems Russell and Norvig: Chapter 3, Sections 3.1 – 3.3 CS121 – Winter 2003.
CSG3F3/ Desain dan Analisis Algoritma
Intro to Computer Science II
Using Base Ten Blocks to Make Numbers to 100
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
1) RECURSION 2) BACKTRACKING 3) LOOK AHEAD
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: fourAB Write a method fourAB that prints out all strings of length 4 composed only of a’s and b’s Example Output aaaa baaa aaab baab aaba baba.
Exercise: Dice roll sum
Lecture 7: Linked List Basics reading: 16.2
CSE 143 Lecture 17 Recursive Backtracking
Road Map - Quarter CS Concepts Data Structures Java Language
CSE (c) S. Tanimoto, 2001 Search-Introduction
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
Road Map - Quarter CS Concepts Data Structures Java Language
CSE 143 Lecture 18 More Recursive Backtracking
Backtracking and Branch-and-Bound
Russell and Norvig: Chapter 3, Sections 3.1 – 3.3
Solving problems by searching
CSE (c) S. Tanimoto, 2002 State-Space Search
Recursive backtracking
Exercise: Dice rolls Write a method diceRoll that accepts an integer parameter representing a number of 6-sided dice to roll, and output all possible arrangements.
CSE 143 Lecture 18 More Recursive Backtracking
Recursive backtracking
Building Java Programs
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 (c) S. Tanimoto, 2004 State-Space Search
CSE 143 Lecture 12 Recursive Backtracking
slides created by Marty Stepp
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

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 exactly that sum. diceSum(2, 7); diceSum(3, 7); [1, 6] [2, 5] [3, 4] [4, 3] [5, 2] [6, 1] [1, 1, 5] [1, 2, 4] [1, 3, 3] [1, 4, 2] [1, 5, 1] [2, 1, 4] [2, 2, 3] [2, 3, 2] [2, 4, 1] [3, 1, 3] [3, 2, 2] [3, 3, 1] [4, 1, 2] [4, 2, 1] [5, 1, 1]

Consider all paths? chosen available desired sum - 3 dice 5 1 2 dice 2 4 2 dice 5 2 dice 6 2 dice 1, 1 1 die 1, 2 1 die 1, 3 1 die 1, 4 1 die 1, 5 1 die 1, 6 1 die 1, 1, 1 1, 1, 2 1, 1, 3 1, 1, 4 1, 1, 5 1, 1, 6 1, 6, 1 1, 6, 2 ...

New decision tree chosen available desired sum - 3 dice 5 1 2 dice 2 4 2 dice 5 2 dice 6 2 dice 1, 1 1 die 1, 2 1 die 1, 3 1 die 1, 4 1 die 1, 5 1 die 1, 6 1 die 1, 1, 1 1, 1, 2 1, 1, 3 1, 1, 4 1, 1, 5 1, 1, 6 1, 6, 1 1, 6, 2 ...

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