Recursion: The Mirrors

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

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.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
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.
Chapter 19 Recursion.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
COMPSCI 105 S Principles of Computer Science Recursion 3.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
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.
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.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
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.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
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.
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 Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
CSC 205 Programming II Lecture 8 Recursion.
Algorithm Efficiency and Sorting
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion CENG 707.
Recursion CENG 707.
Chapter 15 Recursion.
RECURSION.
Recursion: The Mirrors
Recursion: The Mirrors
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Recursion Chapter 10.
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Recursion: The Mirrors
Recursion Chapter 11.
Programming application CC213
CS201: Data Structures and Discrete Mathematics I
Recursion: The Mirrors
Java Programming: Chapter 9: Recursion Second Edition
Algorithm Efficiency and Sorting
Chapter 18 Recursion.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Algorithm Efficiency and Sorting
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Algorithm Efficiency and Sorting
Recursion: The Mirrors
Presentation transcript:

Recursion: The Mirrors Chapter 2 Recursion: The Mirrors

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 reserved

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 reserved

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 reserved

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 reserved

Writing a String Backward void writeBackward( char *str ){ if (str[0] == '\0') return; writeBackward( str + 1 ); cout << str[0]; } © 2005 Pearson Addison-Wesley. All rights reserved

The Fibonacci Sequence Recurrence relation F(n) = F(n-1) + F(n-2) Base cases F(1) = 1, F(2) = 1 © 2005 Pearson Addison-Wesley. All rights reserved

Implementation: Recursive int fib(int n){ // Precondition: n is a positive integer // Postcondition: Returns the nth Fib. number if ( n <= 2 ) return 1; else return fib(n-1) + fib(n-2); } © 2005 Pearson Addison-Wesley. All rights reserved

Implementation: Iterative int iterativeFib(int n){ // Initialize base cases: int previous = 1; // initially fib(1) int current = 1; // initially fib(2) int next = 1; // result when n is 1 or 2 // compute next Fibonacci values when n >= 3 for (int i = 3; i <= n; ++i){ // current is fib(i-1) // previous is fib(i-2) next = current + previous; // fib(i) previous = current; // get ready for the current = next; // next iteration } return next; © 2005 Pearson Addison-Wesley. All rights reserved

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) binarySearch(second half of anArray, value) © 2005 Pearson Addison-Wesley. All rights reserved

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 reserved

Binary Search int binarySearch(int *arr, int low, int high, int key){ if (low > high) return -1; int mid = (low + high) / 2; if (arr[mid] == key) return mid; if (arr[mid] > key) return binarySearch(arr,low,mid - 1,key); return binarySearch(arr,mid + 1,high,key); } © 2005 Pearson Addison-Wesley. All rights reserved

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 reserved

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 reserved

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 reserved

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 reserved

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 reserved

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++ ) if ( A[i][j] && !labels[i][j] ) fourConnectivity( A, labels, row, column, i, j, currLabel++); return labels; © 2005 Pearson Addison-Wesley. All rights reserved

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); } © 2005 Pearson Addison-Wesley. All rights reserved