Programming with Recursion

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Using Recursion1 Programming with Recursion.
Advertisements

Data Structures Chuan-Ming Liu
© 2004 Goodrich, Tamassia Using Recursion1. © 2004 Goodrich, Tamassia Using Recursion2 Recall the Recursion Pattern (§ 2.5) Recursion: when a method calls.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Revision Using Recursion1 Recursion. Revision Using Recursion2 Recall the Recursion Pattern Recursion: when a method calls itself Classic example--the.
© 2004 Goodrich, Tamassia Using Recursion1. © 2004 Goodrich, Tamassia Using Recursion2 Recall the Recursion Pattern (§ 2.5) Recursion: when a method calls.
Programming with Recursion
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Fall 2006CSC311: Data Structures1 Chapter 3 Arrays, Linked Lists, and Recursion Objectives –Using Arrays –Singly Linked Lists –Doubly Linked Lists –Circularly.
Recursion.
Recursion Chapter 5 Outline Induction Linear recursion –Example 1: Factorials –Example 2: Powers –Example 3: Reversing an array Binary recursion –Example.
Introduction to Recursion by Dr. Bun Yue Professor of Computer Science 2013
Using Recursion1 Recursion © 2010 Goodrich, Tamassia.
1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.
Recursion.
Comp 249 Programming Methodology Chapter 10 – Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Introduction to Recursion CSCI 3333 Data Structures.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
1 Searching and Sorting Linear Search Binary Search.
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
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.
Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself.
CSC 212 More Recursion, Stacks, & Queues. Linear Recursion Test for the bases cases  At least one base case needs to be defined If not at a base case,
Programming with Recursion 1 © 2010 Goodrich, Tamassia.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Recursion1 © 2013 Goodrich, Tamassia, Goldwasser.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Recursion.
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
COMP9024: Data Structures and Algorithms
Recursion Topic 5.
Recursion 5/4/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Applied Discrete Mathematics Week 2: Functions and Sequences
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Analysis of Algorithms
Chapter 15 Recursion.
Recursion and Logarithms
Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Searching and Sorting Linear Search Binary Search ; Reading p
Algorithm design and Analysis
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
Data Structures Review Session
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Recall Some Algorithms And Algorithm Analysis Lots of Data Structures
Recall Brute Force as a Problem Solving Technique
Programming with Recursion
At the end of this session, learner will be able to:
Comp 249 Programming Methodology
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
CS210- Lecture 3 Jun 6, 2005 Announcements
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Sequences 6/1/2019 7:49 PM Using Recursion Using Recursion.
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
Advanced Analysis of Algorithms
Presentation transcript:

Programming with Recursion COPYRIGHT © 2005, MICHAEL T. GOODRICH AND ROBERTO TAMASSIA.

3.5 Recursion We have seen that repetition can be achieved by writing loops, such as for loops and while loops. Another way to achieve repetition is through recursion, which occurs when a function calls itself. Recursion: when a method calls itself Classic example--the factorial function: n! = 1· 2· 3· ··· · (n-1)· n Recursive definition: As a Java method:

Content of a Recursive Method Base case(s) Values of the input variables for which we perform no recursive calls are called base cases (there should be at least one base case). Every possible chain of recursive calls must eventually reach a base case. Recursive calls Calls to the current method. Each recursive call should be defined so that it makes progress towards a base case.

Visualizing Recursion Example recursion trace: Recursion trace A box for each recursive call An arrow from each caller to callee An arrow from each callee to caller showing return value recursiveFactorial ( 4 ) 3 2 1 return call * = 6 24 final answer

Binary Search Binary search uses a recursive method to search an array to find a specified value The array must be a sorted array: a[0]≤a[1]≤a[2]≤. . . ≤ a[finalIndex] If the value is found, its index is returned If the value is not found, -1 is returned Note: Each execution of the recursive method reduces the search space by about a half © 2006 Pearson Addison-Wesley. All rights reserved

Binary Search An algorithm to solve this task looks at the middle of the array or array segment first If the value looked for is smaller than the value in the middle of the array Then the second half of the array or array segment can be ignored This strategy is then applied to the first half of the array or array segment © 2006 Pearson Addison-Wesley. All rights reserved

Binary Search If the value looked for is larger than the value in the middle of the array or array segment Then the first half of the array or array segment can be ignored This strategy is then applied to the second half of the array or array segment If the value looked for is at the middle of the array or array segment, then it has been found If the entire array (or array segment) has been searched in this way without finding the value, then it is not in the array © 2006 Pearson Addison-Wesley. All rights reserved

Pseudocode for Binary Search © 2006 Pearson Addison-Wesley. All rights reserved

Recursive Method for Binary Search © 2006 Pearson Addison-Wesley. All rights reserved

Execution of the Method search (Part 1 of 2) © 2006 Pearson Addison-Wesley. All rights reserved

Execution of the Method search (Part 1 of 2) © 2006 Pearson Addison-Wesley. All rights reserved

3.5.1 Linear Recursion The simplest form of recursion is linear recursion, where a method is defined so that it makes at most one recursive call each time it is invoked. This Test for base cases. Begin by testing for a set of base cases (there should be at least one). Every possible chain of recursive calls must eventually reach a base case, and the handling of each base case should not use recursion. Recur once. Perform a single recursive call. (This recursive step may involve a test that decides which of several possible recursive calls to make, but it should ultimately choose to make just one of these calls each time we perform this step.) Define each possible recursive call so that it makes progress towards a base case.

Summing the Elements of an Array Recursively Suppose, for example, we are given an array, A, of n integers that we wish to sum together. We can solve this summation problem using linear recursion by observing that the sum of all n integers in A is equal to A[0], if n = 1, or the sum of the first n − 1 integers in A plus the last element in A. 24-Apr-19 Computer Science Department

A Simple Example of Linear Recursion Example recursion trace: Algorithm LinearSum(A, n) { Input : A integer array A and an integer n such that A has at least n elements Output : The sum of the first n integers in A if ( n = 1) return A[0]; else return LinearSum(A, n - 1) + A[n]; } LinearSum ( A , 5 ) 1 2 3 4 call return [ ] = + = 7 6 13 15 20

Output: 20 24-Apr-19 Computer Science Department

Defining Arguments for Recursion In creating recursive methods, it is important to define the methods in ways that facilitate recursion. This sometimes requires we define additional parameters that are passed to the method. For example, we defined the array reversal method as ReverseArray(A, i, j), not ReverseArray(A).

Reversing an Array Algorithm ReverseArray(A, i, j) { Input : An array A and nonnegative integer indices i and j Output : The reversal of the elements in A starting at index i and ending at j if (i < j ) swap A[i] and A[j]; ReverseArray(A, i + 1, j - 1); }

Output: 54321 24-Apr-19 Computer Science Department

Tail Recursion Using recursion can often be a useful tool for designing algorithms that have elegant, short definitions. But this usefulness does come at a modest cost. When we use a recursive algorithm to solve a problem, we have to use some of the memory locations in our computer to keep track of the state of each active recursive call. When computer memory is at a premium, then, it is useful in some cases to be able to derive non-recursive algorithms from recursive ones. Tail recursion occurs when a linearly recursive method makes its recursive call as its last step. 24-Apr-19 Computer Science Department

Tail Recursion The array reversal method is an example. Such methods can be easily converted to non-recursive methods (which saves on some resources). Example: Algorithm IterativeReverseArray(A, i, j ) { Input : An array A and nonnegative integer indices i and j Output : The reversal of the elements in A starting at index i and ending at j while ( i < j ) { swap A[i] and A[j]; i = i + 1; j = j – 1; }

Computing Powers The power function, p(x,n)=xn, can be defined recursively: This leads to an power function that runs in O(n) time (for we make n recursive calls). We can do better than this, however.

Recursive Squaring We can derive a more efficient linearly recursive algorithm by using repeated squaring: For example, 24 = 2(4/2)2 = (24/2)2 = (22)2 = 42 = 16 25 = 21+(4/2)2 = 2(24/2)2 = 2(22)2 = 2(42) = 32 26 = 2(6/ 2)2 = (26/2)2 = (23)2 = 82 = 64 27 = 21+(6/2)2 = 2(26/2)2 = 2(23)2 = 2(82) = 128.

A Recursive Squaring Method Algorithm Power(x, n) { Input : A number x and integer n = 0 Output : The value xn if (n = 0) return 1; if (n is odd ) { y = Power(x, (n - 1)/ 2); return x*y*y; } else { y = Power(x, n/ 2); return y*y; } }

Analyzing the Recursive Squaring Method Algorithm Power(x, n) { Input : A number x and integer n = 0 Output : The value xn if (n = 0) return 1; if (n is odd ) { y = Power(x, (n - 1)/ 2); return x*y*y; } else { y = Power(x, n/ 2); return y*y; } } Each time we make a recursive call we halve the value of n; hence, we make log n recursive calls. That is, this method runs in O(log n) time. It is important that we used a variable twice here rather than calling the method twice.

Output: power of 3^4 =81 power of 5^5 =3125

3.5.2 Binary Recursion Binary recursion occurs whenever there are two recursive calls for each non-base case.

Binary Recursive Method Problem: add all the numbers in an integer array A: Algorithm BinarySum(A, i, n) { Input : An array A and integers i and n Output : The sum of the n integers in A starting at index i if (n = 1) return A[i]; return BinarySum(A, i, n/ 2) + BinarySum(A, i + n/ 2, n/ 2); } Example trace: 3 , 1 2 4 8 7 6 5

Output: 14 24-Apr-19 Computer Science Department

Computing Fibanacci Numbers Fibonacci numbers are defined recursively: F0 = 0 F1 = 1 Fi = Fi¡1 + Fi¡2 for i > 1. As a recursive algorithm (first attempt): Algorithm BinaryFib(k) { Input : Nonnegative integer k Output : The kth Fibonacci number Fk if ( k = 1) return k; else return BinaryFib(k - 1) + BinaryFib(k - 2); }

Output: fibonacci of 7 =13 fibonacci of 2 =1 24-Apr-19 Computer Science Department

Analyzing the Binary Recursion Fibonacci Algorithm Let nk denote number of recursive calls made by BinaryFib(k). Then n0 = 1 n1 = 1 n2 = n1 + n0 + 1 = 1 + 1 + 1 = 3 n3 = n2 + n1 + 1 = 3 + 1 + 1 = 5 n4 = n3 + n2 + 1 = 5 + 3 + 1 = 9 n5 = n4 + n3 + 1 = 9 + 5 + 1 = 15 n6 = n5 + n4 + 1 = 15 + 9 + 1 = 25 n7 = n6 + n5 + 1 = 25 + 15 + 1 = 41 n8 = n7 + n6 + 1 = 41 + 25 + 1 = 67. Note that the value at least doubles for every other value of nk. That is, nk > 2k/2. It is exponential!

3.5.3 Multiple Recursion Generalizing from binary recursion, we use multiple recursion when a method may make multiple recursive calls, with that number potentially being more than two. 24-Apr-19 Computer Science Department

3.5.3 Multiple Recursion One of the most common applications of this type of recursion is used when we wish to enumerate various configurations in order to solve a combinatorial puzzle. Solving a combinatorial puzzle by enumerating and testing all possible configurations. One way to look at the is that it enumerates every possible size-k ordered subset of U, and tests each subset for being a possible solution to our puzzle. 24-Apr-19 Computer Science Department

Algorithm for Multiple Recursion Algorithm PuzzleSolve(k,S,U) { Input: An integer k, sequence S, and set U (the universe of elements to test) Output: An enumeration of all k-length extensions to S using elements in U without repetitions for all e in U do Remove e from U; // e is now being used Add e to the end of S; if (k = 1) Test whether S is a configuration that solves the puzzle; if ( S solves the puzzle ) return “Solution found: ” S; else PuzzleSolve(k - 1, S,U); Add e back to U; // e is now unused Remove e from the end of S; }

Visualizing PuzzleSolve ( 3 , () ,{ a b c } ) Initial call 2 1 ab ac cb ca bc ba abc acb bac bca cab cba

Exercises Describe a way to use recursion to add all the elements in a n × n (two dimensional) array of integers. Describe a recursive algorithm for finding the maximum element in an array A of n elements? Draw the recursion trace for the execution of method PuzzleSolve(3,S, U) (Code Fragment 3.37), where S is empty and U = {a,b,c,d}. Give a recursive algorithm to compute the product of two positive integers, m and n, using only addition and subtraction. 24-Apr-19 Computer Science Department

References Chapter 3.5, Data Structures and Algorithms by M. Goodrich, R. Tamassia, M. Goldwasser .