Kruse/Ryba ch051 Object Oriented Data Structures Recursion Introduction to Recursion Principles of Recursion Backtracking: Postponing the Work Tree-Structured.

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

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
Solving Problems by Searching Currently at Chapter 3 in the book Will finish today/Monday, Chapter 4 next.
CPSC 322, Lecture 5Slide 1 Uninformed Search Computer Science cpsc322, Lecture 5 (Textbook Chpt 3.4) January, 14, 2009.
Uninformed Search Jim Little UBC CS 322 – Search 2 September 12, 2014
State Space Search 2 Chapter 3 Three Algorithms. Backtracking Suppose We are searching depth-first No further progress is possible (i.e., we can only.
Lance Danzy Melinda Katanbafnezhad. The “A Mazing Problem” is a classical experiment from psychology where scientists carefully observe a rat going through.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
MAE 552 – Heuristic Optimization Lecture 27 April 3, 2002
Constraint Satisfaction Problems
Lecture 4 1) RECURSION 2)BACKTRACKING 3)LOOK AHEAD.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
Using Search in Problem Solving
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Chapter 5 Outline Formal definition of CSP CSP Examples
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CS261 Data Structures DFS and BFS – Edge List Representation.
Data Structures Using C++ 2E Chapter 6 Recursion.
1 Chapter 1 RECURSION. 2 Chapter 1  Subprogram implementation  Recursion  Designing Recursive Algorithms  Towers of Hanoi  Backtracking  Eight Queens.
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
Recursion Chapter 5.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
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.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Introduction to search Chapter 3. Why study search? §Search is a basis for all AI l search proposed as the basis of intelligence l inference l all learning.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion: Backtracking
CS 415 – A.I. Slide Set 5. Chapter 3 Structures and Strategies for State Space Search – Predicate Calculus: provides a means of describing objects and.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day.
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.
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.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Basic Problem Solving Search strategy  Problem can be solved by searching for a solution. An attempt is to transform initial state of a problem into some.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
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 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Recursion. 2 Overview  Learn about recursive definitions  Explore the base case and the general case of a recursive definition  Discover recursive.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
CPSC 322, Lecture 5Slide 1 Uninformed Search Computer Science cpsc322, Lecture 5 (Textbook Chpt 3.5) Sept, 13, 2013.
Brian Williams, Fall 041 Analysis of Uninformed Search Methods Brian C. Williams Sep 21 st, 2004 Slides adapted from: Tomas Lozano Perez,
Recursion Powerful Tool
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Recursive Exploration II
CSCI 104 Backtracking Search
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
7.4 Problem Solving with Recursion
Data Structures and Algorithms for Information Processing
Chapter 12 Recursion (methods calling themselves)
Chapter 11 Graphs.
adapted from Recursive Backtracking by Mike Scott, UT Austin
Basics of Recursion Programming with Recursion
Presentation transcript:

Kruse/Ryba ch051 Object Oriented Data Structures Recursion Introduction to Recursion Principles of Recursion Backtracking: Postponing the Work Tree-Structured Programs: Look Ahead in Games

Kruse/Ryba ch052

3 Stack Frames M A MA B M A M C A M D C A M C A M A M M D MD M D D M D D D M D D M M Time

Kruse/Ryba ch054 Tree of Subprogram Calls M D D D A B C Start Finish

Kruse/Ryba ch055 Recursive Definitions n! = 1 if n = 0 n*(n-1)! if n > 0 x n = 1 if n = 0 and x not 0 x*(x n-1 ) if n > 0 and x not 0

Kruse/Ryba ch056 Designing Recursive Algorithms Find the key step Find a stopping rule (base case) Outline your algorithm Check termination Draw a recursion tree

Kruse/Ryba ch057 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 ch058 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 Legal Knight Moves

Kruse/Ryba ch0513 Application: Depth- And Breadth- First Search S F

Hexadecimal Numbers Kruse/Ryba ch0514 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. ?

Hexadecimal Digit Values Kruse/Ryba ch OctDecHex A B C D E F ,2 2,2 1,

Kruse/Ryba ch0516 Cell Description

Kruse/Ryba ch0517 Application: Depth- And Breadth- First Search

Kruse/Ryba ch0518 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 ch0519 Class Description class cell { public: cell(int n) : number(n), visited(false) {} void addNeighbor(cell * n) {neighbors.push_back(n);} void visit (deque &); protected: int number; bool visited; list neighbors; };//end class cell

Kruse/Ryba ch0520 Class Maze class maze { public: maze(istream &); void solveMaze(); protected: cell * start; bool finished; deque path; // used to hold the path // or paths currently // being traversed };//end class maze

Kruse/Ryba ch0521 maze::maze(istream & infile) // initialize maze by reading from file { int numRows, numColumns; int counter = 1; cell * current = 0; infile >> numRows >> numColumns; vector previousRow (numRows, 0);

Kruse/Ryba ch0522 for(int i = 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 ch previousRow[j] 1 previousRow[0] 1 3 previousRow[2] previousRow[3] 4 2 previousRow[1] 2 5 previousRow[4]

Kruse/Ryba ch previousRow[j] 1 previousRow[0] 6 3 previousRow[2] previousRow[3] 4 2 previousRow[1] 2 5 previousRow[4]

Kruse/Ryba ch previousRow[j] 1 previousRow[0] 6 3 previousRow[2] previousRow[3] 4 2 previousRow[1] 7 5 previousRow[4]

Kruse/Ryba ch0526 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 ch0527 bool cell::visit(deque & path) { //depth first if(visited) // already been here return false; visited = true; // mark as visited cout << “visiting cell “ << number << endl; if (number == 1) { cout :: iterator start, stop; start = neighbors.begin(); stop = neighbors.end(); for ( ; start != stop; ++start) if (! (*start)->visited) path.push_front(*start); return false; }

Kruse/Ryba ch0528 bool cell::visit(deque & path) {// breadth first if(visited) // already been here return false; visited = true; // mark as visited cout << “visiting cell “ << number << endl; if (number == 1) { cout :: iterator start, stop; start = neighbors.begin(); stop = neighbors.end(); for ( ; start != stop; ++start) if (! (*start)->visited) path.push_back(*start); return false; }

Kruse/Ryba ch0529 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 ch0530 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 ch0531 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 ch0532 Chapter 5 Ripples Away