320 likes | 535 Views
Object Oriented Data Structures. Recursion Introduction to Recursion Principles of Recursion Backtracking: Postponing the Work Tree-Structured Programs: Look Ahead in Games. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. C. C. C. A. A. A. A.
E N D
Object Oriented Data Structures RecursionIntroduction to RecursionPrinciples of RecursionBacktracking: Postponing the WorkTree-Structured Programs: Look Ahead in Games Kruse/Ryba ch05
Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion Kruse/Ryba ch05
C C C A A A A M M M M M M M M M M M M M M M A A A D D D B D D D D D D D Stack Frames Time Kruse/Ryba ch05
Finish M A D B C D D Tree of Subprogram Calls Start Kruse/Ryba ch05
1 if n = 0 n*(n-1)! if n > 0 n! = 1 if n = 0 and x not 0 x*(xn-1) if n > 0 and x not 0 xn = Recursive Definitions Kruse/Ryba ch05
Designing Recursive Algorithms • Find the key step • Find a stopping rule (base case) • Outline your algorithm • Check termination • Draw a recursion tree Kruse/Ryba ch05
Tail Recursion • The very last action of a function is a recursive call to itself • Explicit use of a stack not necessary • Reassign the calling parameters to the values specified in the recursive call and then repeat the function Kruse/Ryba ch05
Backtracking An algorithm which attempts to complete a search for a solution to a problem by constructing partial solutions, always ensuring that the partial solutions remain consistent with the requirements. The algorithm then attempts to extend a partial solution toward completion, but when an inconsistency with the requirements of the problem occurs, the algorithm backs up (backtracks) by removing the most recently constructed part of the solution and trying another possibility. Kruse/Ryba ch05
Knight's Tour Legal Knight Moves Kruse/Ryba ch05
Knight's Tour Legal Knight Moves Kruse/Ryba ch05
Knight's Tour Legal Knight Moves Kruse/Ryba ch05
Knight's Tour 10 64 1 31 33 53 26 62 63 34 51 12 7 28 25 30 32 2 11 27 52 54 61 9 41 8 24 35 50 29 6 13 18 40 5 49 36 55 3 60 23 14 46 57 39 21 16 42 37 56 4 48 19 59 44 17 22 58 20 15 38 45 47 43 Legal Knight Moves Kruse/Ryba ch05
Application: Depth- And Breadth-First Search F S Kruse/Ryba ch05
Hexadecimal Numbers • Hexadecimal – Base 16 numbering system 0-F • Decimal - Base 10 numbering system 0-9 • Octal - Base 8 numbering system 0-7 • Hexadecimal Digits 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F • Decimal Digits 0,1,2,3,4,5,6,7,8,9 • Ocal Digits 0,1,2,3,4,5,6,7 • What’s . ? Kruse/Ryba ch05
Hexadecimal Digit Values • 3210 Oct Dec Hex • 0000 0 0 0 • 0001 1 1 1 • 0010 2 2 2 • 0011 3 3 3 • 0100 4 4 4 • 0101 5 5 5 • 0110 6 6 6 • 0111 7 7 7 • 1000 10 8 8 • 1001 11 9 9 • 1010 12 10 A • 1011 13 11 B • 1100 14 12 C • 1101 15 13 D • 1110 16 14 E • 1111 17 15 F 3 2 1 0 23,22,21,20 8 4 2 1 Kruse/Ryba ch05
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Cell Description 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Kruse/Ryba ch05
14 12 5 4 6 9 4 3 10 10 9 5 2 13 2 14 14 10 12 2 9 1 1 3 11 Application: Depth- And Breadth-First Search Kruse/Ryba ch05
Class Cell Maintain: • A number. This is an integer value used to identify the cell. • Cells are numbered consecutively from left to right and top to bottom. (Order is important!) • A list of neighboring cells. Each cell will have an entry in this list for all other neighbor cell that can be reached. • A Boolean value, named visited, that will be used to mark a cell once it has been visited. • Traversing a maze often results in dead ends, and the need to back up and start again. • Marking cells avoids repeating effort and potentially walking around in circles. Kruse/Ryba ch05
Class Description classcell {public: cell(intn) : number(n), visited(false) {} void addNeighbor(cell * n){neighbors.push_back(n);}voidvisit (deque<cell *> &); protected:intnumber;boolvisited; list <cell *> neighbors; };//end class cell Kruse/Ryba ch05
Class Maze classmaze {public: maze(istream&);voidsolveMaze(); protected: cell * start;boolfinished;deque<cell *> path; // used to hold the path // or paths currently // being traversed };//end class maze Kruse/Ryba ch05
maze::maze(istream & infile)// initialize maze by reading from file{intnumRows, numColumns;int counter = 1; cell * current = 0; infile >> numRows >> numColumns; vector <cell *> previousRow (numRows, 0); Kruse/Ryba ch05
for(inti = 0; i < numRows; i++)for(int j=0; j<numColumns; j++) { current = new cell(counter++);int walls;infile >> walls; if((i>0) && ((walls & 0x04)==0)) { current->addNeighbor(previousRow[j])previousRow[j]->addNeighbor(current);} if((j>0> && ((walls & 0x08) == 0)) { current->addNeighbor(previousRow[j-1]);previousRow[j-1]->addNeighbor(current);}previousRow[j] = current;} start = current; finished = false; }//end maze() Kruse/Ryba ch05
1 2 2 3 3 3 14 12 5 4 6 4 4 4 9 4 3 10 10 5 5 1 5 4 2 3 9 5 2 13 2 previousRow[0] previousRow[1] 14 14 10 12 2 9 1 1 3 11 previousRow[2] previousRow[4] previousRow[3] previousRow[j] Kruse/Ryba ch05
2 2 3 3 3 14 12 5 4 6 4 4 4 9 4 3 10 10 5 5 2 3 5 4 6 9 5 2 13 2 6 6 1 previousRow[1] 14 14 10 12 2 9 1 1 3 11 previousRow[2] previousRow[3] previousRow[4] 1 previousRow[j] previousRow[0] Kruse/Ryba ch05
2 2 2 3 3 3 14 12 5 4 6 4 4 4 9 4 3 10 10 5 5 5 7 3 4 6 9 5 2 13 2 7 7 6 6 1 14 14 10 12 2 9 1 1 3 11 previousRow[2] previousRow[3] previousRow[4] 1 previousRow[j] previousRow[0] previousRow[1] Kruse/Ryba ch05
void maze::solveMaze()// solve the maze puzzle { start->visit(path);while ((!finished) && (! path.empty())) { cell * current = path.front();path.pop_front(); finished = current->visit(path);}if ( ! finished)cout << “no solution found\n”;}//end solveMaze() Kruse/Ryba ch05
bool cell::visit(deque<cell *> & path) { //depth firstif(visited) // already been herereturn false; visited = true; // mark as visitedcout << “visiting cell “ << number << endl;if (number == 1) {cout << “puzzle solved\n”;return true;} list <cell *>:: iterator start, stop; start = neighbors.begin(); stop = neighbors.end();for ( ; start != stop; ++start)if (! (*start)->visited)path.push_front(*start);return false; } Kruse/Ryba ch05
bool cell::visit(deque<cell *> & path){// breadth firstif(visited) // already been herereturn false; visited = true; // mark as visitedcout << “visiting cell “ << number << endl;if (number == 1) {cout << “puzzle solved\n”; return true;} list <cell *>:: iterator start, stop; start = neighbors.begin(); stop = neighbors.end();for ( ; start != stop; ++start)if (! (*start)->visited)path.push_back(*start);return false; } Kruse/Ryba ch05
Depth First vs. Breadth First • Because all paths of length one are investigated before examining paths of length two, and all paths of length two before examining paths of length three, a breadth-first search is guaranteed to always discover a path from start to goal containing the fewest steps, whenever such a path exists. Kruse/Ryba ch05
Depth First vs. Breadth First • Because one path is investigated before any alternatives are examined, a depth-first search may, if it is lucky, discover a solution more quickly than the equivalent breadth-first algorithm. Kruse/Ryba ch05
Depth First vs. Breadth First • In particular, suppose for a particular problem that some but not all paths are infinite, and at least one path exists from start to goal that is finite. • A breadth-first search is guaranteed to find a shortest solution. • A depth-first search may have the unfortunate luck to pursue a never-ending path, and can hence fail to find a solution. Kruse/Ryba ch05
Chapter 5 Ripples Away Kruse/Ryba ch05