7.4 Problem Solving with Recursion

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.
Recursion Chapter 7.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Recursion Chapter 5.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion Chapter 7.
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.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
Recursion Chapter 5.
1 Chapter 1 RECURSION. 2 Chapter 1  Subprogram implementation  Recursion  Designing Recursive Algorithms  Towers of Hanoi  Backtracking  Eight Queens.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Recursion Chapter 5.
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.
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.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day.
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.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Kruse/Ryba ch051 Object Oriented Data Structures Recursion Introduction to Recursion Principles of Recursion Backtracking: Postponing the Work Tree-Structured.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
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 Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion Powerful Tool
Backtracking, Search, Heuristics
Recursion Topic 5.
Section 4.4 Counting Blobs
Recursion (Part 2).
Recursion: The Mirrors
Recursive Exploration II
Recursion Chapter 12.
Bill Tucker Austin Community College COSC 1315
CSCI 104 Backtracking Search
Fundamentals of Programming II Backtracking with Stacks
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
More Recursion.
Java Software Structures: John Lewis & Joseph Chase
8.1 Tree Terminology and Applications
Types of Algorithms.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 12 Recursion (methods calling themselves)
Additional Control Structures
Types of Algorithms.
Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2002 Bin Sort, Radix.
Chapter 8: Recursion Java Software Solutions
Lab 07 – 3D Maze.
Graphs.
adapted from Recursive Backtracking by Mike Scott, UT Austin
The Hanoi Tower Problem
Chapter 11 Recursion.
Recursion When performing a repetitive task either: a loop recursion
Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2001 Bin Sort, Radix.
Chapter 8: Recursion Java Software Solutions
Algorithms: Design and Analysis
Types of Algorithms.
8.1 Tree Terminology and Applications
8.2 Tree Traversals Chapter 8 - Trees.
Presentation transcript:

7.4 Problem Solving with Recursion 7.5 Backtracking Chapter 7 – Recursion

Attendance Quiz #24 Recursion

Tip #26: "Path of Least Resistance" Recursion "The great learner expects resistance and overcomes it…. Some learning has been easy for you. But more often your enemy has been discouragement. You may try to avoid that by choosing to learn only what is easy for you, looking for the path of least resistance. But the great learner expects difficulty as part of learning and is determined to work through it." Henry B. Eyring, BYU Speeches, A Child of God;

7.4, pgs. 426-434 7.4 Problem Solving with Recursion Case Study: Towers of Hanoi Case Study: Counting Cells in a Blob 7.4, pgs. 426-434

Counting Cells in a Blob Recursion Consider how we might process an image that is presented as a two- dimensional grid of color values. Information in the image may come from an X-ray an MRI satellite imagery etc. Problem: The goal is to determine the size of any area in the image that is considered abnormal because of its color values. Given a two-dimensional grid of cells, each cell contains either a normal background color or a second color, which indicates the presence of an abnormality. A blob is a collection of contiguous abnormal cells. A user will enter the x, y coordinates of a cell in the blob, and the program will determine the count of all cells in that blob.

Counting Cells in a Blob Recursion enum color { BACKGROUND, ABNORMAL, TEMPORARY }; const int ROW_SIZE = 5; const int COL_SIZE = 5; /** Count number of cells that contains grid[r][c] pre: Abnormal cells are in ABNORMAL color. Other cells are in BACKGROUND color. post: All cells in the blob are in TEMPORARY color. @parm r The row of the blob cell @parm c The column of a blob cell @return The number of cells in the blob that contains grid[r][c] */ int count_cells(color grid[ROW_SIZE][COL_SIZE], int r, int c) { if (r < 0 || r >= ROW_SIZE || c < 0 || c >= COL_SIZE) return 0; if (grid[r][c] != ABNORMAL) return 0; grid[r][c] = TEMPORARY; return 1 + count_cells(grid, r - 1, c - 1) + count_cells(grid, r - 1, c + 0) + count_cells(grid, r - 1, c + 1) + count_cells(grid, r + 0, c - 1) + count_cells(grid, r + 0, c + 1) + count_cells(grid, r + 1, c - 1) + count_cells(grid, r + 1, c + 0) + count_cells(grid, r + 1, c + 1); }

Test Counting Cells in a Blob Recursion #include <iostream> using namespace std; int count_cells(color[ROW_SIZE][COL_SIZE], int, int); int main() { color grid[ROW_SIZE][COL_SIZE] = { {BACKGROUND, ABNORMAL, BACKGROUND, ABNORMAL, ABNORMAL}, {BACKGROUND, ABNORMAL, BACKGROUND, BACKGROUND, ABNORMAL}, {BACKGROUND, BACKGROUND, BACKGROUND, ABNORMAL, BACKGROUND}, {BACKGROUND, ABNORMAL, BACKGROUND, BACKGROUND, BACKGROUND}, {BACKGROUND, ABNORMAL, BACKGROUND, ABNORMAL, BACKGROUND} }; // Enter row and column of a cell in the blob. int row; cout << "Enter row: "; std::cin >> row; int col; cout << "Enter column: "; std::cin >> col; // Display results. cout << count_cells(grid, row, col) << endl; }

Test Counting Cells in a Blob Recursion Verify that the code works for the following cases: A starting cell that is on the edge of the grid A starting cell that has no neighboring abnormal cells A starting cell whose only abnormal neighbor cells are diagonally connected to it A "bull's-eye": a starting cell whose neighbors are all normal but their neighbors are abnormal A starting cell that is normal A grid that contains all abnormal cells A grid that contains all normal cells     

Case Study: Finding a Path Through a Maze 7.5 Backtracking Case Study: Finding a Path Through a Maze 7.5, pgs. 435-440

Backtracking Recursion Backtracking is an approach to implementing a systematic, non- repetitive, trial and error search for a solution to a maze. As one encounters a path junction, a path is chosen. Either you will reach your destination and exit the maze, or you won’t be able to go any farther. If you can’t go any farther, you will need to consider alternative paths—you will need to backtrack until you reach a fork and follow a branch you did not travel hoping to reach your destination. If you never try the same path more than once, you will eventually find a solution path if one exists. Problems that are solved by backtracking can be described as a set of choices made by some function. Recursion allows you to implement backtracking in a relatively straightforward manner. Each activation frame is used to remember the choice that was made at that particular decision point.algorithm.

Finding a Path through a Maze Recursion Problem Use backtracking to find and display the path through a maze From each point in a maze you can move to the next cell in a horizontal or vertical direction if the cell is not blocked. Analysis The maze will consist of an array of cells. The starting point is at the top left corner maze[0][0]. The exit point is at the bottom right corner, that is maze[ROW_SIZE - 1][COL_SIZE - 1]. All cells on the path will be BACKGROUND color. All cells that represent barriers will be ABNORMAL color. Cells that we have visited will be TEMPORARY color. If we find a path, all cells on the path will be set to PATH color.

Design find_maze_path(x, y) Recursion  if the current cell is outside the maze Return false (you are out of bounds).  if the current cell is part of the barrier or has been visited already Return false (you are off the path or in a cycle).  if the current cell is the maze exit Recolor it to the path color and return true (you have successfully completed the maze).  Try to find a path from the current path to the exit by marking the current cell as on the path by recoloring it to the path color.  for each neighbor of the current cell if a path exists from the neighbor to the maze exit Return true.  // No neighbor of the current cell is on the path Recolor the current cell to the temporary color (visited) Return false. There is no attempt to find the shortest path through the maze. We just show the first path that is found.

Implementation Recursion bool find_maze_path(color maze[HEIGHT][WIDTH], int r, int c) { if ((r < 0) || (r >= HEIGHT) || (c < 0) || (c >= WIDTH)) return false; if (maze[r][c] != BACKGROUND) return false; if ((r == HEIGHT - 1) && (c == WIDTH - 1)) maze[r][c] = PATH; return true; // Success! } maze[r][c] = PATH; // Recursive case if (find_maze_path(maze, r - 1, c) || find_maze_path(maze, r + 1, c) || find_maze_path(maze, r, c - 1) || find_maze_path(maze, r, c + 1)) return true; maze[r][c] = TEMPORARY; return false; /** Wrapper for maze path function */ bool find_maze_path(color maze[HEIGHT][WIDTH]) { return find_maze_path(maze, 0, 0); // (0,0) is the start point }

Testing #include <iostream> using namespace std; Recursion #include <iostream> using namespace std; enum color { BACKGROUND, ABNORMAL, TEMPORARY, PATH }; const int HEIGHT = 5; const int WIDTH = 5; int main() { color maze[HEIGHT][WIDTH] = { {BACKGROUND, ABNORMAL, BACKGROUND, ABNORMAL, ABNORMAL}, {BACKGROUND, ABNORMAL, BACKGROUND, ABNORMAL, ABNORMAL}, {BACKGROUND, BACKGROUND, BACKGROUND, ABNORMAL, BACKGROUND}, {BACKGROUND, ABNORMAL, BACKGROUND, BACKGROUND, BACKGROUND}, {BACKGROUND, ABNORMAL, BACKGROUND, ABNORMAL, BACKGROUND} }; cout << boolalpha << find_maze_path(maze) << endl; return 0; }

Marking a Cell as Visited Recursion Would the program still work if instead of recoloring a “dead end” to the temporary color, we recolored it to the background color? The answer is “Yes”. This would not affect the ability of the algorithm to find a path or to determine that none exists; however, it would affect the algorithm’s efficiency: After backtracking, the function could try to place on the path a cell that had been found to be a dead end. The cell would be classified once again as a dead end. Marking it as a dead end (color TEMPORARY) the first time prevents this from happening. Test for a variety of test cases: Mazes that can be solved Mazes that can't be solved A maze with no barrier cells A maze with a single barrier cell at the exit point

18 – Sequential Containers

Towers of Hanoi Recursion Move the three disks to a different peg, maintaining their order (largest disk on bottom, smallest on top, etc.) Only the top disk on a peg can be moved to another peg A larger disk cannot be placed on top of a smaller disk Move disk: if n is 1 Move disk 1 (the smallest disk) from the starting peg to the destination peg else Move the top n – 1 disks from the starting peg to the temporary peg (neither starting nor destination peg) Move disk n (the disk at the bottom) from the starting peg to the destination peg Move the top n – 1 disks from the temporary peg to the destination peg

Towers of Hanoi show_moves(4, 'L', 'R', 'M') Recursion void show_moves(int n, char start, char dest, char temp) { if (n == 1) cout << "Move disk 1 from " << start << " to " << dest << endl; } else show_moves(n - 1, start, temp, dest); cout << "Move disk " << n << " from " << start << " to " << dest << endl; show_moves(n - 1, temp, dest, start); show_moves(4, 'L', 'R', 'M') Move disk 1 from L to M Move disk 2 from L to R Move disk 1 from M to R Move disk 3 from L to M Move disk 1 from R to L Move disk 2 from R to M Move disk 4 from L to R Move disk 2 from M to L Move disk 3 from M to R