CPS 100, Fall 2011 10.1 GridGame APT How would you solve this problem using recursion?

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
CPS 100, Fall Backtracking by image search.
CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
Review for Exam 1 As you arrive…please get a handout.
CPS 100, Fall Algorithms and Idioms l How do you avoid calculating the same thing over and over and over and over and …  Does it matter?  Recalculate.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Language Issues for Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.3 © Mitchell Wand, This work is licensed under a Creative.
1 CSC 421: Algorithm Design & Analysis Spring 2013 See online syllabus: (also on BlueLine2) Course.
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?
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 See online syllabus (also available through BlueLine): Course goals:
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
CSC 142 Computer Science II Zhen Jiang West Chester University
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 See online syllabus (also available through BlueLine): Course goals:
Inheritance Only not the good kind of inheritance where you discover your Great Aunt left you a yacht. The bad kind that involves obscure Java syntax.
CPS 100, Fall PFTN2Wks l Making progress on Boggle assignment  Many, many classes, each designed to do one thing  Some related by inheritance,
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.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze  Certain mazes can be solved using the “right-hand”
Computer Science Projects Internal Assessment. Mastery Item Claimed Justification Where Listed Random Access File – Searching Lines P. 53 Random.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 See online syllabus (also available through Blackboard): Course goals:
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class.
CS1101 Group1 Discussion 10 Lek Hsiang Hui comp.nus.edu.sg
Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013.
CompSci 100 Prog Design and Analysis II Oct 26, 2010 Prof. Rodger CPS 100, Fall
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Backtracking & Brute Force Optimization Intro2CS – weeks
CompSci Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the “right-hand”
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 -
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
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.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Multiple Inheritance, Interfaces, Mixins 1.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
CSC 421: Algorithm Design & Analysis
Game playing Types of games Deterministic vs. chance
CSC 427: Data Structures and Algorithm Analysis
Backtracking, Search, Heuristics
Data Structures and Algorithms
CSC 421: Algorithm Design & Analysis
Chapter 14 Abstract Classes and Interfaces
Backtracking and Branch-and-Bound
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:

CPS 100, Fall GridGame APT How would you solve this problem using recursion?

CPS 100, Fall What we will do today l GridWorld: 1 final example of backtracking  Make clear the basic algorithmic approach NQueens, pathSum, and Boggle’s findWordOnBoard have in common l Subclasses: like interfaces with code in them l A few more hints on Boggle

CPS 100, Fall GridGame APT Key insight: if my opponent has 0 winning moves after I make a move, then that move guarantees I win.

CPS 100, Fall Basic ideas in backtracking search l Enumerate all possible choices/moves  We try these choices in order, committing to a choice  If the choice doesn't pan out we must undo the choice Backtracking step, choices must be undoable l Inherently recursive, when to stop searching?  When all columns tried in N queens  When a player has won or lost the gridgame  When the sum has been found, or when we know the sum cannot be found because we’ve gone over  In general: when we know if our choices have led to success or failure l Summary: enumerate choices, try a choice, undo a choice, this is brute force search: try everything

CPS 100, Fall Where we are thus far l You have seen the general idea of backtracking:  Step 1: Make a choice  Step 2: Check to see if the choice leads to success  Step 3: If not, undo the choice and make the next one l Coming Up: Subclasses: like interfaces but with code l A few more hints on Boggle

CPS 100, Fall Subclasses: When a Class “extends” Rather than “implements”

CPS 100, Fall //in Superclass.java abstract class Superclass { //just like an interface abstract void printStuff(); //but has some code too void printFoo() { System.out.println(“FOO”); } } //in Subclass.java class Subclass { void printStuff() { System.out.println(“STUFF”); } } //elsewhere //note how similar this is to interfaces SuperClass var = new Subclass(); var.printStuff(); //prints STUFF var.printFoo(); //prints FOO

CPS 100, Fall Inheritance concepts l Parent/super class can be extended by subclassing  Possible to use methods from parent class, subs have them!  Possible to override parent methods, change behavior  Possible to do both! Call super.doThis(); l Often you don't have access to parent.java file  Still can subclass, use methods, extend/override them  Do NOT have access to private data fields  DO have access to protected data fields l Hard to do OO design, leave for later courses  But get an idea now as to power and utility

CPS 100, Fall Where we are thus far l You have seen the general idea of backtracking:  Step 1: Make a choice  Step 2: Check to see if the choice leads to success  Step 3: If not, undo the choice and make the next one l You’ve seen how subclasses work: SuperClass var = new Subclass(); var.printStuff(); //calls a subclass method var.printFoo(); //calls a superclass method l Coming Up: A few more hints on Boggle

CPS 100, Fall BinarySearchLexicon l You do not have to implement binary search (it’s in Collections.binarySearch) BUT you do have to sort the list before you use binary search (the provided code does this sort for you…just be aware in general) l Three results from Lexicon (see SimpleLexicon for an example of how to return these)  NOT A WORD (e.g. “zz”)  A WORD (e.g. “do”)  Not a word, but the PREFIX of a word (e.g. “dy”)

CPS 100, Fall Boggle Search for Word l Starting at board location (row,col): find a string S  We want to keep track of where we are in the string  Also track what board locations used for S search l How do we know when we're done?  Base case of recursive, backtracking call  Where we are in the string? l How do we keep track of used locations?  Store in array list: tentatively use current one, recurse  If we don’t succeed, take off the last one stored!

CPS 100, Fall Base Cases for GoodWordOnBoardFinder l In to find a particular word on the board, I can think of 4 different kinds of base cases. 1. Word is not at this square 2. You’ve already used this cell 3. You’ve found ever letter in the word 4. You are off the board l Does the order we check these base cases matter?

CPS 100, Fall Using Howto Hints l Helper method in GoodWordOnBoardFinder  Needed: board, row, col, word, word-index, list  Search for "skunk" at (0,0) at (0,1) at …  Call help(board,row,col,"skunk",0,list)  If we find the 's', where do we look next and for what?  If we don't find the 's' what does that mean?  If (row,col) isn't legal position then …  If we've found the last 'k' what does that mean?

CPS 100, Fall Where we’ve been l You have seen the general idea of backtracking:  Step 1: Make a choice  Step 2: Check to see if the choice leads to success  Step 3: If not, undo the choice and make the next one l You’ve seen how subclasses work: SuperClass var = new Subclass(); var.printStuff(); //calls a subclass method var.printFoo(); //calls a superclass method l A few more hints on Boggle  You don’t have to write your own search for BinarySearchLexicon  Be careful with your base cases in GoodWordOnBoardFinder