Exercise: Dice roll sum

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

BackTracking Algorithms
Problem Solving by Searching Copyright, 1996 © Dale Carnegie & Associates, Inc. Chapter 3 Spring 2007.
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
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
Building Java Programs
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Problem Solving by Searching Copyright, 1996 © Dale Carnegie & Associates, Inc. Chapter 3 Spring 2004.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Backtracking.
Building Java Programs
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Busby, Dodge, Fleming, and Negrusa. Backtracking Algorithm Is used to solve problems for which a sequence of objects is to be selected from a set such.
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.
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.
Chapter 5 Recursion. Basically, a method is recursive if it includes a call to itself.
Backtracking and Games Eric Roberts CS 106B January 28, 2013.
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.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
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"?
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
CSG3F3/ Desain dan Analisis Algoritma
Intro to Computer Science II
CSCI 104 Backtracking Search
Lecture 27 Creating Custom GUIs
Data Structures and Algorithms
read: 12.5 Lecture 17: recursive 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"?
Problem Solving by Searching
CSE 143 Lecture 19 More Recursive Backtracking
Using a Stack Chapter 6 introduces the stack data type.
Building Java Programs
Recursion Copyright (c) Pearson All rights reserved.
Building Java Programs
Board: Objects, Arrays and Pedagogy
Back Tracking.
Using a Stack Chapter 6 introduces the stack data type.
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.
CSE 143 Lecture 17 Recursive Backtracking
Road Map - Quarter CS Concepts Data Structures Java Language
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
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
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
Building Java Programs
Exercise: Dice roll sum
Recursive 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"?
Solving problems by searching
CSE 143 Lecture 12 Recursive Backtracking
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
CSC 205 – Java Programming II
Solving Problems by Searching
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 ...

Optimizations We need not visit every branch of the decision tree. Some branches are clearly not going to lead to success. We can preemptively stop, or prune, these branches. Inefficiencies in our dice sum algorithm: Sometimes the current sum is already too high. (Even rolling 1 for all remaining dice would exceed the sum.) Sometimes the current sum is already too low. (Even rolling 6 for all remaining dice would not reach the sum.) When finished, the code must compute the sum every time. (1+1+1 = ..., 1+1+2 = ..., 1+1+3 = ..., 1+1+4 = ..., ...)

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

Extra: 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