Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

CSC 205 Programming II Lecture 10 Towers of Hanoi.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
COMPSCI 105 S Principles of Computer Science Recursion 1.
CHAPTER 13 Recursion. Recursive Solution A recursive solution  solves a problem by solving a smaller instance of the problem. Example  How do we go.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
Recursion Chapter 8. 2 Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Ch. 3: Recursion. Recursive Solutions Recursion –An extremely powerful problem-solving technique –Breaks a problem into smaller identical problems –An.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
COMPSCI 105 S Principles of Computer Science Recursion 3.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Data Structures Using C++ 2E Chapter 6 Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Recursion 1. CHAPTER TWO Recursion: A recursive function is a function that calls itself. Two functions A and B are mutually recursive, if A calls B and.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
1 Storage Classes, Scope, and Recursion Lecture 6.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
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 l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
COSC 2006 Data Structures I Recursion II
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 13 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Recursive Functions for Tasks(13.1) Recursive Functions.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 14 Recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion CENG 707.
Recursion CENG 707.
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion: The Mirrors
Recursion: The Mirrors
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Recursion: The Mirrors
Recursion: The Mirrors
Recursion: The Mirrors
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

Chapter 2 Recursion: The Mirrors

© 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem- solving technique –Breaks a problem in smaller identical problems –An alternative to iteration, which involves loops A sequential search is iterative –Starts at the beginning of the collection –Looks at every item in the collection in order A binary search is recursive –Repeatedly halves the collection and determines which half could contain the item –Uses a divide and conquer strategy

© 2005 Pearson Addison-Wesley. All rights reserved2-3 Recursive Solutions Facts about a recursive solution –A recursive method calls itself –Each recursive call solves an identical, but smaller, problem –A test for the base case enables the recursive calls to stop Base case: a known case in a recursive definition –Eventually, one of the smaller problems must be the base case

© 2005 Pearson Addison-Wesley. All rights reserved2-4 Recursive Solutions Four questions for construction of recursive solutions –How can you define the problem in terms of a smaller problem of the same type? –How does each recursive call diminish the size of the problem? –What instance of the problem can serve as the base case? –As the problem size diminishes, will you reach this base case?

© 2005 Pearson Addison-Wesley. All rights reserved2-5 A Recursive void Method: Writing a String Backward Problem –Given a string of characters, write it in reverse order Recursive solution –Each recursive step of the solution diminishes by 1 the length of the string to be written backward –Base case: write the empty string backward

© 2005 Pearson Addison-Wesley. All rights reserved2-6 A Recursive void Method: Writing a String Backward Figure 2.6 A recursive solution

© 2005 Pearson Addison-Wesley. All rights reserved2-7 Multiplying Rabbits (The Fibonacci Sequence) “Facts” about rabbits –Rabbits never die –A rabbit reaches sexual maturity exactly two months after birth, that is, at the beginning of its third month of life –At the beginning of every month, each sexually mature male-female pair gives birth to exactly one male-female pair

© 2005 Pearson Addison-Wesley. All rights reserved2-8 Multiplying Rabbits (The Fibonacci Sequence) Problem –Given a pair of rabbits in month 1 –How many pairs of rabbits are alive in month n? Recurrence relation rabbit(n) = rabbit(n-1) + rabbit(n-2)

© 2005 Pearson Addison-Wesley. All rights reserved2-9 Multiplying Rabbits (The Fibonacci Sequence) Figure 2.10 Recursive solution to the rabbit problem

© 2005 Pearson Addison-Wesley. All rights reserved2-10 Multiplying Rabbits (The Fibonacci Sequence) Base cases –rabbit(2), rabbit(1) Recursive definition rabbit(n) = 1if n is 1 or 2 rabbit(n-1) + rabbit(n-2)if n > 2 Fibonacci sequence –The series of numbers rabbit(1), rabbit(2), rabbit(3), and so on

© 2005 Pearson Addison-Wesley. All rights reserved2-11 Implementation: Multiplying Rabbits (Recursive) int rabbit(int n){ // Precondition: n is a positive integer. // Postcondition: Returns the nth Fibonacci // number. if (n <= 2) return 1; else // n > 2, so n-1 > 0 and n-2 > 0 return rabbit(n-1) + rabbit(n-2); }

© 2005 Pearson Addison-Wesley. All rights reserved2-12 Implementation: Multiplying Rabbits (Iterative) int iterativeRabbit(int n){ // Iterative solution to the rabbit problem. // initialize base cases: int previous = 1; // initially rabbit(1) int current = 1; // initially rabbit(2) int next = 1; // result when n is 1 or 2 // compute next rabbit values when n >= 3 for (int i = 3; i <= n; ++i){ // current is rabbit(i-1), previous is rabbit(i-2) next = current + previous; // rabbit(i) previous = current; // get ready for current = next; // next iteration } return next; }

© 2005 Pearson Addison-Wesley. All rights reserved2-13 Binary Search A high-level binary search if (anArray is of size 1) { Determine if anArray’s item is equal to value } else { Find the midpoint of anArray Determine which half of anArray contains value if (value is in the first half of anArray) { binarySearch (first half of anArray, value) } else { binarySearch(second half of anArray, value) }

© 2005 Pearson Addison-Wesley. All rights reserved2-14 Binary Search Implementation issues: –How will you pass “half of anArray ” to the recursive calls to binarySearch ? –How do you determine which half of the array contains value? –What should the base case(s) be? –How will binarySearch indicate the result of the search?

© 2005 Pearson Addison-Wesley. All rights reserved2-15 Organizing Data: The Towers of Hanoi Figure a) The initial state; b) move n - 1 disks from A to C; c) move one disk from A to B; d) move n - 1 disks from C to B

© 2005 Pearson Addison-Wesley. All rights reserved2-16 The Towers of Hanoi Pseudocode solution solveTowers(count, source, destination, spare) if (count is 1) { Move a disk directly from source to destination } else { solveTowers(count-1, source, spare, destination) solveTowers(1, source, destination, spare) solveTowers(count-1, spare, destination, source) }

© 2005 Pearson Addison-Wesley. All rights reserved2-17 Recursion and Efficiency Some recursive solutions are so inefficient that they should not be used Factors that contribute to the inefficiency of some recursive solutions –Overhead associated with method calls –Inherent inefficiency of some recursive algorithms Do not use a recursive solution if it is inefficient and there is a clear, efficient iterative solution

© 2005 Pearson Addison-Wesley. All rights reserved2-18 Fall 2006 – Midterm Question Consider a salesman at a grocery store. The salesman uses a scale with two buckets to measure the weight of the purchased items. The salesman also has a finite number of metal weights (e.g., 1kg, 2kg, 5kg, 8kg, etc.) to be used in the weighing process. Write a recursive function that checks whether these metal weights are sufficient to weigh a purchased item. –Your recursive function should take the available metal weights and the weight of the purchased item as input. The function returns true if the metal weights can be used to weigh the item; returns false otherwise. –For example, if the available metal weights are 1kg, 2kg, 5kg and 8kg, and the purchased item is 13kg, the salesman can put the purchased item into one bucket and the 5kg and 8kg metal weights into the other bucket to measure the weight of the item. If the purchased item is 12kg, the salesman can put the purchased item and the 1kg metal weight into one bucket, and the 5kg and 8kg weights into the other bucket to measure the weight of the item. However, an 18kg item cannot be measured using these metal weights.

© 2005 Pearson Addison-Wesley. All rights reserved2-19 Fall 2006 – Midterm Question bool scale( const int *weights, const int size, const int item, const int i, const int sum ) { if ( sum == item ) return true; if ( i == size ) return false; bool result = false; result = scale( weights, size, item, i + 1, sum + weights[i]); if ( result == false ) result = scale( weights, size, item, i + 1, sum - weights[i]); if ( result == false ) result = scale( weights, size, item, i + 1, sum ); return result; }

© 2005 Pearson Addison-Wesley. All rights reserved2-20 Finding Connected Components Suppose that we want to segment cell nuclei in a tissue image. Suppose that it is a gray level image (its pixels contain intensity values between 0 and 255). For this purpose, we first apply some image processing to obtain its black-and- white form. In this form, each pixel value is either 0 and 1. Then, we are going to find the connected components on the black pixels. So, we write a recursive function for finding the connected components.

© 2005 Pearson Addison-Wesley. All rights reserved2-21 Finding Connected Components Suppose that we want to segment cell nuclei in a tissue image. Suppose that it is a gray level image (its pixels contain intensity values between 0 and 255). For this purpose, we first apply some image processing to obtain its black-and- white form. In this form, each pixel value is either 0 and 1. Then, we are going to find the connected components on the black pixels. So, we write a recursive function for finding the connected components.

Finding Connected Components Similarly, in the image below, we want to identify individual buildings. Connected components analysis can be used after obtaining a black-and-white image of buildings. © 2005 Pearson Addison-Wesley. All rights reserved2-22

Finding Connected Components Similarly, in the image below, we want to identify individual buildings. Connected components analysis can be used after obtaining a black-and-white image of buildings. © 2005 Pearson Addison-Wesley. All rights reserved2-23

© 2005 Pearson Addison-Wesley. All rights reserved2-24 Finding Connected Components int **findConnectedComponents(int **A, int row, int column){ int **labels, i, j, currLabel; labels = new int *[row]; for ( i = 0; i < row; i++ ){ labels[i] = new int[column]; for ( j = 0; j < column; j++ ) labels[i][j] = 0; } currLabel = 1; for ( i = 0; i < row; i++ ) for ( j = 0; j < column; j++ ) if ( A[i][j] && !labels[i][j] ) fourConnectivity( A, labels, row, column, i, j, currLabel++); return labels; }

© 2005 Pearson Addison-Wesley. All rights reserved2-25 Finding Connected Components void fourConnectivity( int **A, int **labels, int row, int column, int i, int j, int currLabel){ if (A[i][j] == 0) return; if (labels[i][j] > 0) return; labels[i][j] = currLabel; if (i-1 >= 0) fourConnectivity(A,labels,row,column,i-1,j,currLabel); if (i+1 < row) fourConnectivity(A,labels,row,column,i+1,j,currLabel); if (j-1 >= 0) fourConnectivity(A,labels,row,column,i,j-1,currLabel); if (j+1 < column) fourConnectivity(A,labels,row,column,i,j+1,currLabel); }