Recursive Exploration II

Slides:



Advertisements
Similar presentations
Chapter Objectives To learn about recursive data structures and recursive methods for a LinkedList class To understand how to use recursion to solve the.
Advertisements

BackTracking Algorithms
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Lance Danzy Melinda Katanbafnezhad. The “A Mazing Problem” is a classical experiment from psychology where scientists carefully observe a rat going through.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Backtracking.
State-Space Searches.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 11: Stacks and Mazes Announcements 1.Midterm.
Recursive Backtracking Eric Roberts CS 106B January 25, 2013.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
1 Storage Classes, Scope, and Recursion Lecture 6.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
1 AVL Trees II Implementation. 2 Download: 2011_03_28_AVL_Tree/ File AVL_Tree_Demo.zip
Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm.
Recursion Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zWhat is a recursive function?
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
State-Space Searches. 2 State spaces A state space consists of A (possibly infinite) set of states The start state represents the initial problem Each.
Implementation of the Hangman Game in C++
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Sudoku Jordi Cortadella Department of Computer Science.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
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.
Backtracking and Games Eric Roberts CS 106B January 28, 2013.
Building Java Programs Appendix R Recursive backtracking.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Chapter 6 Questions Quick Quiz
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
Kruse/Ryba ch051 Object Oriented Data Structures Recursion Introduction to Recursion Principles of Recursion Backtracking: Postponing the Work Tree-Structured.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
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.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Game playing Types of games Deterministic vs. chance
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Backtracking, Search, Heuristics
Recursion Topic 5.
Recursion Chapter 12.
Programming Abstractions
CSCI 104 Backtracking Search
Variables A piece of memory set aside to store data
read: 12.5 Lecture 17: recursive backtracking
Fundamentals of Programming II Backtracking with Stacks
Java Software Structures: John Lewis & Joseph Chase
7.4 Problem Solving with Recursion
Programming Abstractions
Building Java Programs
Chapter 12 Recursion (methods calling themselves)
Lab 07 – 3D Maze.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
adapted from Recursive Backtracking by Mike Scott, UT Austin
Haskell Tips You can turn any function that takes two inputs into an infix operator: mod 7 3 is the same as 7 `mod` 3 takeWhile returns all initial.
Java for Beginners University Greenwich Computing At School DASCO
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
State-Space Searches.
State-Space Searches.
Backtracking, Search, Heuristics
State-Space Searches.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
CSC 205 – Java Programming II
Backtracking, Search, Heuristics
Lecture 20 – Practice Exercises 4
Unit II Game Playing.
Presentation transcript:

Recursive Exploration II A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century B.C.E. Chris Piech CS 106B Lecture 9 Jan 27, 2016

One Line Change to Starter

Wrapper Functions int gcd(int a, int b) { return 0; }

Wrapper Functions int gcdHelper(int a, int b, Stack<string> myStack) { /// now the params are changed. return 0; } int gcd(int a, int b) { Stack<string> myStack; return gcdHelper(a, b, myStack);

Today’s Route Backtracking Tree Examples Tree framework 9 Letters Decision trees Exploration River

Decision Trees

Decision Tree Undergrad Oceanography Native Studies CS AI HCI Graphics CompBio Research Startup

Decision Tree

Decision Tree

Decision Tree

Decision Tree Undergrad Oceanography Native Studies CS AI HCI Graphics CompBio Research Startup

Tree Template

State is the top of a tree Tree Template // a general template for working with trees void recursiveExploration( state ) { if (simpleCase || foundSolution) { // base case return without recursing. } else { // recursive case for(each possible nextState from state) { recursiveExploration(nextState); } State is the top of a tree And so is nextState Current state Next state 0 state 1 state n Option 0 Option 1 Option n Generally you will do other work

Start Simple

Output all Files on Computer

File System My Documents

File System My Documents Research Writing Teaching

File System My Documents Research Writing Teaching cs106a cs106b cs221

File System

File System

File System

File System My Documents Research Writing Teaching cs106a cs106b cs221

File System My Documents Research Writing Teaching cs106a cs106b cs221

File System My Documents Research Writing Teaching cs106a cs106b cs221

Tree Template

Tree Template // a general template for working with trees void recursiveExploration( state ) { if (simpleCase || foundSolution) { // base case return without recursing. } else { // recursive case for(each possible nextState from state) { recursiveExploration(nextState); }

Tree Template // a general template for working with trees void recursiveExploration(string currDir) { if (simpleCase || foundSolution) { // base case return without recursing. } else { // recursive case for(each possible nextState from state) { recursiveExploration(nextState); }

Tree Template // a general template for working with trees void recursiveExploration(string currDir) { if (simpleCase || foundSolution) { // base case return without recursing. } else { // recursive case for(each possible nextState from state) { recursiveExploration(nextState); }

Tree Template // a general template for working with trees void recursiveExploration(string currDir) { if (!isDirectory(currDirr)) { // base case cout << currDirr << endl; } else { // recursive case for(each possible nextState from state) { recursiveExploration(nextState); }

Tree Template // a general template for working with trees void recursiveExploration(string currDir) { if (!isDirectory(currDirr)) { // base case cout << currDirr << endl; } else { // recursive case for(each possible nextState from state) { recursiveExploration(nextState); }

Tree Template // a general template for working with trees void recursiveExploration(string currDir) { if (!isDirectory(currDirr)) { // base case cout << currDirr << endl; } else { // get all next states Vector<string> dirFiles; listDirectory(currDir, dirFiles); // recursive case for(each possible nextState from state) { recursiveExploration(nextState); }

Tree Template // a general template for working with trees void recursiveExploration(string currDir) { if (!isDirectory(currDirr)) { // base case cout << currDirr << endl; } else { // get all next states Vector<string> dirFiles; listDirectory(currDir, dirFiles); // recursive case for(each possible nextState from state) { recursiveExploration(nextState); }

Tree Template // a general template for working with trees void recursiveExploration(string currDir) { if (!isDirectory(currDirr)) { // base case cout << currDirr << endl; } else { // get all next states Vector<string> dirFiles; listDirectory(currDir, dirFiles); // recursive case for(string file : dirFiles) { string next = currDir + “/” + file; recursiveExploration(next); }

Output All Files // a general template for working with trees void recursiveExploration(string currDir) { if (!isDirectory(currDirr)) { // base case cout << currDirr << endl; } else { // get all next states Vector<string> dirFiles; listDirectory(currDir, dirFiles); // recursive case for(string file : dirFiles) { string next = currDir + “/” + file; recursiveExploration(next); }

Recursive Exploration on the Internet

Looking For Treasure

Found Treasure Map but Lost it

Today’s Route Backtracking Many Examples Tree framework 9 Letters Decision trees Exploration River

Today’s Route Backtracking Many Examples Tree framework 9 Letters Decision trees Exploration River

Trees are Everywhere

Prerequisites recursive exploration recursion functionCalls collections functionCalls simpleC++ simpleC++

Prerequisites

Prerequisites

Prerequisites

Trees are Everywhere

Syntax Tree

Syntax Tree

Syntax Tree

Today’s Route Backtracking Tree Examples Tree framework 9 Letters Decision trees Exploration River

Today’s Route Backtracking Tree Examples Tree framework 9 Letters Decision trees Exploration River

Labyrinth

Right Hand Rule The most widely known strategy for solving a maze is called the right-hand rule, in which you put your right hand on the wall and keep it there until you find an exit. If Theseus applies the right-hand rule in this maze, the solution path looks like this. Unfortunately, the right-hand rule doesn’t work if there are loops in the maze that surround either the starting position or the goal. In this maze, the right-hand rule sends Theseus into an infinite loop.   

Maze Decision Tree    

Enumerated Types in C++ It is often convenient to define new types in which the possible values are chosen from a small set of possibilities. Such types are called enumerated types. enum name { list of element names }; In C++, you define an enumerated type like this: The code for the maze program uses enum to define a new type consisting of the four compass points, as follows: enum Direction { NORTH, EAST, SOUTH, WEST }; You can then declare a variable of type Direction and use it along with the constants NORTH, EAST, SOUTH, and WEST.

Maze Collection /* * Class: Maze * ----------- * This class represents a two-dimensional maze contained in a rectangular * grid of squares. The maze is read in from a data file in which the * characters '+', '-', and '|' represent corners, horizontal walls, and * vertical walls, respectively; spaces represent open passageway squares. * The starting position is indicated by the character 'S'. For example, * the following data file defines a simple maze: * * +-+-+-+-+-+ * | | * + +-+ + +-+ * |S | | */ class Maze { public:

Maze Collection bool isOutside(Point pt); bool wallExists(Point pt, Direction dir); void markSquare(Point pt); void unmarkSquare(Point pt); bool isMarked(Point pt);

Maze

The SolveMaze Function /* * Function: solveMaze * Usage: solveMaze(maze, start); * ------------------------------ * Attempts to generate a solution to the current maze from the specified * start point. The solveMaze function returns true if the maze has a * solution and false otherwise. The implementation uses recursion * to solve the submazes that result from marking the current square * and moving one step along each open passage. */ bool solveMaze(Maze & maze, Point start) { if (maze.isOutside(start)) return true; if (maze.isMarked(start)) return false; maze.markSquare(start); for (Direction dir = NORTH; dir <= WEST; dir++) { if (!maze.wallExists(start, dir)) { if (solveMaze(maze, adjacentPoint(start, dir))) { return true; } maze.unmarkSquare(start); return false;

Tracing the SolveMaze Function bool solveMaze(Maze & maze, Point start) { if (maze.isOutside(start)) return true; if (maze.isMarked(start)) return false; maze.markSquare(start); for (Direction dir = NORTH; dir <= WEST; dir++) { if (!maze.wallExists(start, dir)) { if (solveMaze(maze, adjPt(start, dir))) { return true; } maze.unmarkSquare(start); return false; x x x x x x x bool solveMaze(Maze & maze, Point start) { if (maze.isOutside(start)) return true; if (maze.isMarked(start)) return false; maze.markSquare(start); for (Direction dir = NORTH; dir <= WEST; dir++) { if (!maze.wallExists(start, dir)) { if (solveMaze(maze, adjPt(start, dir))) { return true; } maze.unmarkSquare(start); return false; dir start (3, 4) bool solveMaze(Maze & maze, Point start) { if (maze.isOutside(start)) return true; if (maze.isMarked(start)) return false; maze.markSquare(start); for (Direction dir = NORTH; dir <= WEST; dir++) { if (!maze.wallExists(start, dir)) { if (solveMaze(maze, adjPt(start, dir))) { return true; } maze.unmarkSquare(start); return false; dir start (3, 2) x x x x x x x x x x  x x x x  x x x  x x x x x x x x x x x  start dir dir (3, 3) SOUTH EAST NORTH EAST NORTH SOUTH Don’t follow the recursion more than one level. Depend on the recursive leap of faith.

Reflections on Maze The solveMaze program is a useful example of how to search all paths that stem from a branching series of choices. At each square, the solveMaze program calls itself recursively to find a solution from one step further along the path. To give yourself a better sense of why recursion is important in this problem, think for a minute or two about what it buys you and why it would be difficult to solve this problem iteratively. In particular, how would you answer the following questions: What information does the algorithm need to remember as it proceeds with the solution, particularly about the options it has already tried? In the recursive solution, where is this information kept? How might you keep track of this information otherwise?

Reflections on Maze Suppose that the program has reached the following position: x  How does the algorithm keep track of the “big picture” of what paths it still needs to explore?

Each Frame Remembers One Choice bool solveMaze(Maze & maze, Point start) { if (maze.isOutside(start)) return true; if (maze.isMarked(start)) return false; maze.markSquare(start); for (Direction dir = NORTH; dir <= WEST; dir++) { if (!maze.wallExists(start, dir)) { if (solveMaze(maze, adjPt(start, dir))) { return true; } maze.unmarkSquare(start); return false; dir start (3, 3) SOUTH x  bool solveMaze(Maze & maze, Point start) { if (maze.isOutside(start)) return true; if (maze.isMarked(start)) return false; maze.markSquare(start); for (Direction dir = NORTH; dir <= WEST; dir++) { if (!maze.wallExists(start, dir)) { if (solveMaze(maze, adjPt(start, dir))) { return true; } maze.unmarkSquare(start); return false; dir start (3, 4) EAST x  bool solveMaze(Maze & maze, Point start) { if (maze.isOutside(start)) return true; if (maze.isMarked(start)) return false; maze.markSquare(start); for (Direction dir = NORTH; dir <= WEST; dir++) { if (!maze.wallExists(start, dir)) { if (solveMaze(maze, adjPt(start, dir))) { return true; } maze.unmarkSquare(start); return false; dir start (4, 4) EAST x  bool solveMaze(Maze & maze, Point start) { if (maze.isOutside(start)) return true; if (maze.isMarked(start)) return false; maze.markSquare(start); for (Direction dir = NORTH; dir <= WEST; dir++) { if (!maze.wallExists(start, dir)) { if (solveMaze(maze, adjPt(start, dir))) { return true; } maze.unmarkSquare(start); return false; dir start (3) NORTH x  bool solveMaze(Maze & maze, Point start) { if (maze.isOutside(start)) return true; if (maze.isMarked(start)) return false; maze.markSquare(start); for (Direction dir = NORTH; dir <= WEST; dir++) { if (!maze.wallExists(start, dir)) { if (solveMaze(maze, adjPt(start, dir))) { return true; } maze.unmarkSquare(start); return false; dir start (5, 3) WEST x  bool solveMaze(Maze & maze, Point start) { if (maze.isOutside(start)) return true; if (maze.isMarked(start)) return false; maze.markSquare(start); for (Direction dir = NORTH; dir <= WEST; dir++) { if (!maze.wallExists(start, dir)) { if (solveMaze(maze, adjPt(start, dir))) { return true; } maze.unmarkSquare(start); return false; dir start (4, 3) x 

Standard Exploration Makes a new “state” parameter // a general template for working with trees void recursiveExploration(string currDir) { if (!isDirectory(currDirr)) { // base case cout << currDirr << endl; } else { // get all next states Vector<string> dirFiles; listDirectory(currDir, dirFiles); // recursive case for(string file : dirFiles) { string next = currDir + “/” + file; recursiveExploration(next); } Makes a new “state” parameter

Backtracking often on Reference // a general template for working with trees void recursiveExploration(string & currDir) { if (!isDirectory(currDirr)) { // base case cout << currDirr << endl; } else { // get all next states Vector<string> dirFiles; listDirectory(currDir, dirFiles); // recursive case for(string file : dirFiles) { currDir += “/” + file; recursiveExploration(next); currDir = getHead(currDirr); } Changes curr and undoes change.

Backtracking Tree Template // a general template for working with trees void recursiveExploration( & state ) { if (simpleCase || foundSolution) { // base case return without recursing. } else { // recursive case for(each possible change from state) { state = makeChange(state); recursiveExploration(state); state = unmakeChange(state); }

Backtracking Tree Template II // a general template for working with trees void recursiveExploration( & state ) { if (simpleCase || foundSolution || marked) { // base case return without recursing. } else { // recursive case markState(state); for(each possible change from state) { recursiveExploration(state); } unmarkState(state);

Today’s Route Backtracking Tree Examples Tree framework 9 Letters Decision trees Exploration River

The End