CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring 20111.

Slides:



Advertisements
Similar presentations
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
Advertisements

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.
SIGCSE Nifty assignments: Brute force and backtracking Steve Weiss Department of Computer Science University of North Carolina at Chapel Hill
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Announcements.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
CPS 100, Spring From Recursion to Self Reference public int calc(int n){ return n*calc(n-1); } l What is the Internet?  A network of networks.
CPS 100e, Spring Day, Week, Month Overview l Backtracking p/review  Boggle  APT l Assignment review  Boggle  APT l PQ Overview.
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.
Design and Analysis of Algorithms - Chapter 111 How to tackle those difficult problems... There are two principal approaches to tackling NP-hard problems.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
CPS 100, Fall PFTN2Wks l Making progress on Boggle assignment  Many, many classes, each designed to do one thing  Some related by inheritance,
Games. Adversaries Consider the process of reasoning when an adversary is trying to defeat our efforts In game playing situations one searches down the.
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.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
CPS 100, Fall GridGame APT How would you solve this problem using recursion?
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze  Certain mazes can be solved using the “right-hand”
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,
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
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
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
CSCE350 Algorithms and Data Structure Lecture 21 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
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 -
CPS 100, Spring Search, Trees, Games, Backtracking l Trees help with search  Set, map: binary search tree, balanced and otherwise  Quadtree.
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.
TWTG What work is still to come APT, Huff, APT
Game playing Types of games Deterministic vs. chance
TWTG What work is still to come APT, Huff, APT
Problem Solving: Brute Force Approaches
Backtracking, Search, Heuristics
BackTracking CS255.
CSSE 230 Day 25 Skip Lists.
Plan for the Week Review for Midterm Source code provided
Intro to Computer Science II
CSCI 104 Backtracking Search
Fundamentals of Programming II Backtracking with Stacks
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
Using a Stack Chapter 6 introduces the stack data type.
Problem Solving: Brute Force Approaches
Back Tracking.
Analysis and design of algorithm
Using a Stack Chapter 6 introduces the stack data type.
Exercise: Dice roll sum
adapted from Recursive Backtracking by Mike Scott, UT Austin
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
Exercise: Dice roll sum
CSE 143 Lecture 18 More Recursive Backtracking
Backtracking and Branch-and-Bound
CSE 143 Lecture 18 More 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"?
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
Owen Astrachan November 9, 2018
Presentation transcript:

CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring 20111

Announcements Written assignment due tonight – put.pdf file in Eclipse and submit it APTS due Tuesday after break – use recursion No Lab Friday March 4 or Monday March 14 Today – two examples of recursion – Blob counting – 8 Queens - backtracking CompSci 100e, Spring 20112

Blob Counting, Flood Fill Flood a region with color – Erase region, make transparent,.. – How do find the region? Finding regions, blobs, edges,.. – See blob counting code – What is a blob? Recursion helps, but necessary? – Performance, clarity, … – Ease of development 3CompSci 100e, Spring 2011

First Example - BlobCount How do we find images? Components? Paths? – Create information from data 4CompSci 100e, Spring 2011

Details and Idioms in blob code Method blobFill has four parameters – (row,column) of where search starts – Character being searched for (initially * or blob) – Character to fill with on success (e.g., count ‘2’ or ‘4’) Mark for visualization Mark to ensure we don’t search again! If (row,column) is part of blob, count it and ask neighbors for their counts – They’re part of blob (if never visited before) Return total of yourself and neighbors – Key to recursion: do one thing and ask for help 5CompSci 100e, Spring 2011

Blob questions What changes if diagonal cells are adjacent? – Conceptually and in code How do we find blob sizes in a range? – Not bigger than X, but between X and Y How would we number blobs by size rather than by when they’re found? – Do we have the tools to do this in existing code? Can we avoid recursion and do this iteratively? 6CompSci 100e, Spring 2011

Second Example – 8 Queens CompSci 100e, Spring 20117

Backtracking by image search 8CompSci 100e, Spring 2011

Searching with no guarantees Search for best move in automated game play – Can we explore every move? – Are there candidate moves ranked by “goodness”? – Can we explore entire tree of possible moves? Search with partial information – Predictive texting with T9 or iTap or … – Finding words on a Boggle board – What numbers fit in Sudoku square Try something, if at first you don’t succeed …. 9CompSci 100e, Spring 2011

Search, Backtracking,Heuristics How do you find a needle in a haystack? – How does a computer play chess? – Why would you write that program? – How does Bing/Googlemap find routes from one place to another? – Shortest path algorithms – Longest path algorithms Optimal algorithms and heuristic algorithms – When is close good enough? How do measure “closeness”? – When is optimality important, how much does it cost? 10CompSci 100e, Spring 2011

Classic problem: N queens Can queens be placed on a chess board so that no queens attack each other? – Easily place two queens – What about 8 queens? Make the board NxN, this is the N queens problem – Place one queen/column – Horiz/Vert/Diag attacks Backtracking – Tentative placement – Recurse, if ok done! – If fail, undo tentative, retry wikipedia-n-queens

Backtracking idea with N queens For each column C, tentatively place a queen – Try first row in column C, if ok, move onto next column Typically “move on” is recursive – If solved, done, otherwise try next row in column C Must unplace queen when failing/unwind recursion Each column C “knows” what row R it’s on – If first time, that’s row zero, but might be an attack – Unwind recursion/backtrack, try “next” location Backtracking: record an attempt go forward – Move must be “undoable” on backtracking/unwinding 12CompSci 100e, Spring 2011

N queens backtracking: Queens.java public boolean solve(int col){ if (col == mySize) return true; // try each row until all are tried for(int r=0; r < mySize; r++){ if (myBoard.safeToPlace(r,col)){ myBoard.setQueen(r,col,true); if (solve(col+1)){ return true; } myBoard.setQueen(r,col,false); } return false; } 13CompSci 100e, Spring 2011

Basic ideas in backtracking search 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 Inherently recursive, when to stop searching? – When all columns tried in N queens – When we have found the exit in a maze – When every possible moved tried in Tic-tac-toe or chess? Is there a difference between these games? Summary: enumerate choices, try a choice, undo a choice, this is brute force search: try everything 14CompSci 100e, Spring 2011

Pruning vs. Exhaustive Search If we consider every possible placement of 4 queens on a 4x4 board, how many are there? (N queens) – 4x4x4x4 if we don’t pay attention to any attacks – 4x3x2x1 if we avoid attacks in same row What about if we avoid diagonal attacks? – Pruning search space makes more search possible, still could be lots of searching to do! Estimate how long to calculate # solutions to the N- queens problem with our Java code…. 15CompSci 100e, Spring 2011

Queens Details How do we know when it’s safe to place a queen? – No queen in same row, or diagonal – For each column, store the row that a queen is in – See QBoard.java for details For GUI version, we use a decorator – The QBoardGUI is an IQueenState class and it has an IQueenState object in it – Appears as an IQueenState to client, but uses an existing one to help do its work – One of many object oriented design patterns, seen in Huff in the BitInputStream class 16CompSci 100e, Spring 2011