recursive)exploraon)ii - stanford university...recursive)exploraon)ii cs 106b lecture 9 jan 27, 2016...
TRANSCRIPT
![Page 1: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/1.jpg)
Recursive Explora0on II
CS 106B Lecture 9
Jan 27, 2016
Chris Piech
A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century B.C.E.
![Page 2: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/2.jpg)
One Line Change to Starter
![Page 3: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/3.jpg)
Today’s Route
Decision trees
9 Letters
Tree framework
Backtracking
Many Examples
![Page 4: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/4.jpg)
Decision Trees
![Page 5: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/5.jpg)
Decision Tree Undergrad
Oceanography CS
Native Studies
AI HCI Graphics CompBio
Research Startup
![Page 6: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/6.jpg)
Decision Tree
![Page 7: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/7.jpg)
Decision Tree
![Page 8: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/8.jpg)
Decision Tree
![Page 9: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/9.jpg)
Decision Tree Undergrad
Oceanography CS
Native Studies
AI HCI Graphics CompBio
Research Startup
![Page 10: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/10.jpg)
Tree Template
![Page 11: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/11.jpg)
Tree Template
// a general template for working with treesvoid recursiveExploration( state ) { if (simpleCase || foundSolution) { // base case return without recursing. } else { // recursive case for(each possible nextState from state) { recursiveExploration(nextState); } }}
Current state
Next state 0
Next state 1
Next state n
Option 0 Option n
Generally you will do other work
State is the top of a tree And so is nextState
![Page 12: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/12.jpg)
Start Simple
![Page 13: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/13.jpg)
Output all Files on Computer
![Page 14: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/14.jpg)
My Documents
File System
![Page 15: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/15.jpg)
My Documents
Teaching
Research Writing
File System
![Page 16: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/16.jpg)
My Documents
Teaching
Research Writing
cs106a cs106b cs221
File System
![Page 17: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/17.jpg)
File System
![Page 18: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/18.jpg)
File System
![Page 19: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/19.jpg)
File System
![Page 20: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/20.jpg)
My Documents
Teaching
Research Writing
cs106a cs106b cs221
File System
![Page 21: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/21.jpg)
My Documents
Teaching
Research Writing
cs106a cs106b cs221
File System
![Page 22: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/22.jpg)
My Documents
Teaching
Research Writing
cs106a cs106b cs221
File System
![Page 23: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/23.jpg)
Tree Template
![Page 24: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/24.jpg)
// a general template for working with treesvoid recursiveExploration( state ) { if (simpleCase || foundSolution) { // base case return without recursing. } else { // recursive case for(each possible nextState from state) { recursiveExploration(nextState); } }}
Tree Template
![Page 25: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/25.jpg)
// a general template for working with treesvoid recursiveExploration(string currDir) { if (simpleCase || foundSolution) { // base case return without recursing. } else { // recursive case for(each possible nextState from state) { recursiveExploration(nextState); } }}
Tree Template
![Page 26: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/26.jpg)
// a general template for working with treesvoid recursiveExploration(string currDir) { if (simpleCase || foundSolution) { // base case return without recursing. } else { // recursive case for(each possible nextState from state) { recursiveExploration(nextState); } }}
Tree Template
![Page 27: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/27.jpg)
// a general template for working with treesvoid recursiveExploration(string currDir) { if (!isDirectory(currDirr)) { // base case cout << currDirr << endl; } else { // recursive case for(each possible nextState from state) { recursiveExploration(nextState); } }}
Tree Template
![Page 28: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/28.jpg)
// a general template for working with treesvoid recursiveExploration(string currDir) { if (!isDirectory(currDirr)) { // base case cout << currDirr << endl; } else { // recursive case for(each possible nextState from state) { recursiveExploration(nextState); } }}
Tree Template
![Page 29: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/29.jpg)
// a general template for working with treesvoid 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
![Page 30: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/30.jpg)
// a general template for working with treesvoid 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
![Page 31: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/31.jpg)
// a general template for working with treesvoid 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); } }}
Tree Template
![Page 32: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/32.jpg)
// a general template for working with treesvoid 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
![Page 33: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/33.jpg)
Recursive Explora0on on the Internet
![Page 34: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/34.jpg)
Looking For Treasure
![Page 35: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/35.jpg)
Found Treasure Map but Lost it
![Page 36: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/36.jpg)
Today’s Route
Decision trees
9 Letters
Tree framework
Backtracking
Many Examples
![Page 37: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/37.jpg)
Today’s Route
Decision trees
9 Letters
Tree framework
Backtracking
Many Examples
![Page 38: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/38.jpg)
Trees are Everywhere
![Page 39: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/39.jpg)
recursive exploration
recursion
collections
simpleC++
Prerequisites
functionCalls
simpleC++
![Page 40: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/40.jpg)
Prerequisites
![Page 41: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/41.jpg)
Prerequisites
![Page 42: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/42.jpg)
Prerequisites
![Page 43: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/43.jpg)
Trees are Everywhere
![Page 44: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/44.jpg)
Syntax Tree
![Page 45: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/45.jpg)
Syntax Tree
![Page 46: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/46.jpg)
Syntax Tree
![Page 47: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/47.jpg)
Today’s Route
Decision trees
9 Letters
Tree framework
Backtracking
Many Examples
![Page 48: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/48.jpg)
Today’s Route
Decision trees
9 Letters
Tree framework
Backtracking
Many Examples
![Page 49: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/49.jpg)
Labyrinth
![Page 50: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/50.jpg)
Θ
Θ
• 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.
Θ
Right Hand Rule
![Page 51: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/51.jpg)
Maze Decision Tree
Θ
Θ Θ
Θ
![Page 52: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/52.jpg)
• 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.
Enumerated Types in C++
![Page 53: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/53.jpg)
/* * 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 Collec0on
![Page 54: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/54.jpg)
Maze Collec0on
bool isOutside(Point pt);
bool wallExists(Point pt, Direction dir);
void unmarkSquare(Point pt);
bool isMarked(Point pt);
void markSquare(Point pt);
![Page 55: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/55.jpg)
Maze
![Page 56: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/56.jpg)
/* * 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; }
The SolveMaze Func0on
![Page 57: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/57.jpg)
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 NORTH EAST SOUTH
xxx
x
Θ x x xx x
xxxxxx
xxx
xxxxxx
xx
xxxx x
x x xΘ
Don’t follow the recursion more than one level. Depend on the recursive leap of faith.
dir
start (3, 3) NORTH EAST SOUTH
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)
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)
Tracing the SolveMaze Func0on
![Page 58: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/58.jpg)
• 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?
Reflec0ons on Maze
![Page 59: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/59.jpg)
• How does the algorithm keep track of the “big picture” of what paths it still needs to explore?
• Suppose that the program has reached the following position:
x
x x x
xΘ
Reflec0ons on Maze
![Page 60: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/60.jpg)
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
xxΘ
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
xx 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
xx 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
(5, 3) WEST
xx 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
(4, 3)
xx
Θ
xxx
Each Frame Remembers One Choice
![Page 61: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/61.jpg)
Today’s Route
Decision trees
9 Letters
Tree framework
Backtracking
Many Examples
![Page 62: Recursive)Exploraon)II - Stanford University...Recursive)Exploraon)II CS 106B Lecture 9 Jan 27, 2016 A journey of a thousand miles begins with a single step. —Lao Tzu, 6th century](https://reader034.vdocuments.net/reader034/viewer/2022042300/5eca788c39e31a12a35eb8ae/html5/thumbnails/62.jpg)
The End