Building Java Programs

Slides:



Advertisements
Similar presentations
BackTracking Algorithms
Advertisements

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.
Backtracking What is backtracking?
Backtracking.
The Power of Calling a Method from Itself Svetlin Nakov Telerik Corporation
Building Java Programs
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
Recursion Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
CHAPTER 4 RECURSION. BASICALLY, A FUNCTION IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
CHAPTER 4 RECURSION. BASICALLY, A METHOD IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
Chapter 5 Recursion. Basically, a method is recursive if it includes a call to itself.
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
Building Java Programs Appendix R 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"?
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Kruse/Ryba ch051 Object Oriented Data Structures Recursion Introduction to Recursion Principles of Recursion Backtracking: Postponing the Work Tree-Structured.
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
TOWERS OF HANOI. : If n = 1, move disk 1 from pole 'A' to pole 'B'. else: 1.First, move n-1 disks from pole 'A' to pole 'C', using pole 'B' as.
Recursion. 2 recursion: The definition of an operation in terms of itself. –Solving a problem using recursion depends on solving smaller occurrences of.
CSE 143 Lecture 17 Recursive Backtracking slides created by Marty Stepp ideas and examples taken from Stanford University.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
CSG3F3/ Desain dan Analisis Algoritma
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Depth-First Search N-Queens Problem Hamiltonian Circuits
Recursive Exploration II
Programming Abstractions
Data Structures and Algorithms
read: 12.5 Lecture 17: recursive backtracking
Recursion (Continued)
Constraint Satisfaction Problems vs. Finite State Problems
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
Java Software Structures: John Lewis & Joseph Chase
Algorithm Design and Analysis (ADA)
Programming Abstractions
Recursion Copyright (c) Pearson All rights reserved.
Chapter 12 Recursion (methods calling themselves)
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.
The Power of Calling a Method from Itself
Exercise: Dice roll sum
CSE 143 Lecture 17 Recursive Backtracking
Recursion.
Road Map - Quarter CS Concepts Data Structures Java Language
CSE 143 Lecture 17 Recursive Backtracking
CSE 143 Lecture 15 Recursive Backtracking
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
Backtracking.
Recursion James Brucker.
Algorithms: Design and Analysis
Road Map - Quarter CS Concepts Data Structures Java Language
CSE 143 Lecture 18 More Recursive Backtracking
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
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"?
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
Presentation transcript:

Building Java Programs read: 12.5 Recursive backtracking

Exhaustive Search Iterate through all elements of a search space Useful to solve problems that require making decisions Each decision leads to new choices Insufficient information to make a thoughtful choice Depth first search: we go deep down one path rather than broad Natural to implement recursively: call stack keeps track of decision points in right order (opposite from visited)

Exercise: Permutations Write a method permute that accepts a string as a parameter and outputs all possible rearrangements of the letters in that string. The arrangements may be output in any order. Example: permute("TEAM") outputs the following sequence of lines: TEAM TEMA TAEM TAME TMEA TMAE ETAM ETMA EATM EAMT EMTA EMAT ATEM ATME AETM AEMT AMTE AMET MTEA MTAE META MEAT MATE MAET interesting variation: Output only the *unique* rearrangements. For example, permute("GOOGLE") has two Os, but swapping them in order produces the same string, which shouldn't appear twice in the output.

Examining the problem We want to generate all possible sequences of letters. for (each possible first letter): for (each possible second letter): for (each possible third letter): ... print! Each permutation is a set of choices or decisions: Which character do I want to place first? Which character do I want to place second? solution space: set of all possible sets of decisions to explore

Decision tree chosen available T E A M T E A M E T A M ... T E A M T A E M T M E A T E A M T E M A T A E M T A M E T M E A T M A E T E A M T E M A T A E M T A M E T M E A T M A E

Exercise solution // Outputs all permutations of the given string. public static void permute(String s) { permute(s, ""); } private static void permute(String s, String chosen) { if (s.length() == 0) { // base case: no choices left to be made System.out.println(chosen); } else { // recursive case: choose each possible next letter for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); // choose s = s.substring(0, i) + s.substring(i + 1); chosen += c; permute(s, chosen); // explore s = s.substring(0, i) + c + s.substring(i); chosen = chosen.substring(0, chosen.length() - 1); } // un-choose

Exercise solution 2 // Outputs all permutations of the given string. public static void permute(String s) { permute(s, ""); } private static void permute(String s, String chosen) { if (s.length() == 0) { // base case: no choices left to be made System.out.println(chosen); } else { // recursive case: choose each possible next letter for (int i = 0; i < s.length(); i++) { String ch = s.substring(i, i + 1); // choose String rest = s.substring(0, i) + // remove s.substring(i + 1); permute(rest, chosen + ch); // explore } // (don't need to "un-choose" because } // we used temp variables)

Backtracking Useful to solve problems that require making decisions Each decision leads to new choices Some (but not all!) sequence(s) of choices will be a solution Insufficient information to make a thoughtful choice Systematically prune out infeasible solutions

Backtracking strategies When solving a backtracking problem, ask these questions: What are the "choices" in this problem? What is the "base case"? (How do I know when I'm out of choices?) How do I "make" a choice? Do I need to create additional variables to remember my choices? Do I need to modify the values of existing variables? How do I explore the rest of the choices? Do I need to remove the made choice from the list of choices? Once I'm done exploring, what should I do? How do I "un-make" a choice?

Maze class Suppose we have a Maze class with these methods: Method/Constructor Description public Maze(String text) construct a given maze public int getHeight(), getWidth() get maze dimensions public boolean isExplored(int r, int c) public void setExplored(int r, int c) get/set whether you have visited a location public void isWall(int r, int c) whether given location is blocked by a wall public void mark(int r, int c) public void isMarked(int r, int c) whether given location is marked in a path public String toString() text display of maze

Exercise: solve maze Write a method solveMaze that accepts a Maze and a starting row/column as parameters and tries to find a path out of the maze starting from that position. If you find a solution: Your code should stop exploring. You should mark the path out of the maze on your way back out of the recursion, using backtracking. (As you explore the maze, squares you set as 'explored' will be printed with a dot, and squares you 'mark' will display an X.)

Decision tree position (row 1, col 7) choices  (these never change)     (1, 6) (0, 7) wall (2, 7) wall (1, 8) (1, 5) (0, 6) wall (2, 6) wall (1, 7) visited (1, 7) visited (0, 8) wall (2, 8) (1, 9) wall ... (1, 4) (0, 5) wall (2, 5) (1, 6) visited ... ...

Recall: Backtracking A general pseudo-code algorithm for backtracking problems: Explore(choices): if there are no more choices to make: stop. else, for each available choice C: Choose C. Explore the remaining choices. Un-choose C, if necessary. (backtrack!) What are the choices in this problem?