Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences

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

LEVEL II, TERM II CSE – 243 MD. MONJUR-UL-HASAN LECTURER DEPT OF CSE, CUET Recursive & Dynamic Programming.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
© 2004 Goodrich, Tamassia Using Recursion1 Programming with Recursion.
Lecture 8 of Computer Science II Recursions Instructor: Mr.Ahmed Al Astal.
Data Structures Chuan-Ming Liu
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
© 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.
Revision Using Recursion1 Recursion. Revision Using Recursion2 Recall the Recursion Pattern Recursion: when a method calls itself Classic example--the.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 20: Sorting.
© 2004 Goodrich, Tamassia Using Recursion1. © 2004 Goodrich, Tamassia Using Recursion2 Recall the Recursion Pattern (§ 2.5) Recursion: when a method calls.
Programming with Recursion
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.
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
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.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Recursion.
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.
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 Chapter 3. Recursion Lecture 6. In functions and data structures.
Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself.
Dr. Alagoz Data Structures & Algorithm Analysis in JAVA (CMPE250) Fatih Alagöz Office: ETA42 Ext.6652
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.
Recursion1 © 2013 Goodrich, Tamassia, Goldwasser.
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! =
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
COMP9024: Data Structures and Algorithms
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.
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.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Analysis of Algorithms
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Recursions.
Recursion and Logarithms
Algorithm design and Analysis
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
CS201: Data Structures and Discrete Mathematics I
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recall Some Algorithms And Algorithm Analysis Lots of Data Structures
Recall Brute Force as a Problem Solving Technique
Programming with Recursion
Programming with Recursion
Programming with Recursion
At the end of this session, learner will be able to:
Recursive Algorithms 1 Building a Ruler: drawRuler()
CS210- Lecture 3 Jun 6, 2005 Announcements
Sequences 6/1/2019 7:49 PM Using Recursion Using Recursion.
Searching.
Lecture 20 – Practice Exercises 4
Advanced Analysis of Algorithms
Presentation transcript:

Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences 7/26/2019 7:36 PM Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion

The Recursion Pattern Recursion: when a method calls itself Classic example--the factorial function: n! = 1· 2· 3· ··· · (n-1)· n Recursive definition: As a Java method: © 2013 Goodrich, Tamassia, Goldwasser Recursion

Recursion in Art © 2013 Goodrich, Tamassia, Goldwasser Recursion

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. Recursion © 2013 Goodrich, Tamassia, Goldwasser

Visualizing Recursion Example 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 Recursion © 2013 Goodrich, Tamassia, Goldwasser

Example: English Ruler Print the ticks and numbers like an English ruler: Recursion © 2013 Goodrich, Tamassia, Goldwasser

Using Recursion drawTicks(length) Input: length of a ‘tick’ Slide by Matt Stallmann included with permission. Using Recursion drawTicks(length) Input: length of a ‘tick’ Output: ruler with tick of the given length in the middle and smaller rulers on either side drawTicks(length) if( length > 0 ) then drawTicks( length - 1 ) draw tick of the given length © 2013 Goodrich, Tamassia, Goldwasser Recursion

Recursive Drawing Method The drawing method is based on the following recursive definition An interval with a central tick length L >1 consists of: An interval with a central tick length L-1 An single tick of length L drawTicks ( 3 ) Output previous pattern repeats drawOneTick 1 2 Recursion © 2013 Goodrich, Tamassia, Goldwasser

A Recursive Method for Drawing Ticks on an English Ruler Note the two recursive calls © 2013 Goodrich, Tamassia, Goldwasser Recursion

Binary Search Search for an integer, target, in an ordered list. © 2013 Goodrich, Tamassia, Goldwasser Recursion

Visualizing Binary Search We consider three cases: If the target equals data[mid], then we have found the target. If target < data[mid], then we recur on the first half of the sequence. If target > data[mid], then we recur on the second half of the sequence. © 2013 Goodrich, Tamassia, Goldwasser Recursion

Analyzing Binary Search Runs in O(log n) time. The remaining portion of the list is of size high – low + 1. Each recursive call divides the search region in half; hence, there can be at most log n levels. © 2013 Goodrich, Tamassia, Goldwasser Recursion

Linear Recursion Test for base cases Recur once 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 step may have a test that decides which of several possible recursive calls to make, but it should ultimately make just one of these calls Define each possible recursive call so that it makes progress towards a base case. © 2013 Goodrich, Tamassia, Goldwasser Recursion

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

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 then Swap A[i] and A[ j] ReverseArray(A, i + 1, j - 1) return © 2013 Goodrich, Tamassia, Goldwasser Recursion

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 paramaters that are passed to the method. For example, we defined the array reversal method as ReverseArray(A, i, j), not ReverseArray(A). Python version: © 2013 Goodrich, Tamassia, Goldwasser Recursion

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. © 2013 Goodrich, Tamassia, Goldwasser Recursion

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. © 2013 Goodrich, Tamassia, Goldwasser Recursion

Recursive Squaring Method Algorithm Power(x, n): Input: A number x and integer n = 0 Output: The value xn if n = 0 then return 1 if n is odd then y = Power(x, (n - 1)/ 2) return x · y ·y else y = Power(x, n/ 2) return y · y © 2013 Goodrich, Tamassia, Goldwasser Recursion

Analysis Algorithm Power(x, n): Input: A number x and integer n = 0 Output: The value xn if n = 0 then return 1 if n is odd then 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 use a variable twice here rather than calling the method twice. © 2013 Goodrich, Tamassia, Goldwasser Recursion

Tail Recursion Tail recursion occurs when a linearly recursive method makes its recursive call as its last step. 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 do Swap A[i ] and A[ j ] i = i + 1 j = j - 1 return © 2013 Goodrich, Tamassia, Goldwasser Recursion

Binary Recursion Binary recursion occurs whenever there are two recursive calls for each non-base case. Example from before: the DrawTicks method for drawing ticks on an English ruler. © 2013 Goodrich, Tamassia, Goldwasser Recursion

Another Binary Recusive 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 then 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 © 2013 Goodrich, Tamassia, Goldwasser Recursion

Computing Fibonacci Numbers Fibonacci numbers are defined recursively: F0 = 0 F1 = 1 Fi = Fi-1 + Fi-2 for i > 1. Recursive algorithm (first attempt): Algorithm BinaryFib(k): Input: Nonnegative integer k Output: The kth Fibonacci number Fk if k = 1 then return k else return BinaryFib(k - 1) + BinaryFib(k - 2) © 2013 Goodrich, Tamassia, Goldwasser Recursion

Analysis Let nk be the number of recursive calls by BinaryFib(k) 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 nk at least doubles every other time That is, nk > 2k/2. It is exponential! © 2013 Goodrich, Tamassia, Goldwasser Recursion

A Better Fibonacci Algorithm Use linear recursion instead Algorithm LinearFibonacci(k): Input: A nonnegative integer k Output: Pair of Fibonacci numbers (Fk , Fk-1) if k = 1 then return (k, 0) else (i, j) = LinearFibonacci(k - 1) return (i +j, i) LinearFibonacci makes k-1 recursive calls © 2013 Goodrich, Tamassia, Goldwasser Recursion

Multiple Recursion Motivating example: Multiple recursion: summation puzzles pot + pan = bib dog + cat = pig boy + girl = baby Multiple recursion: makes potentially many recursive calls not just one or two © 2013 Goodrich, Tamassia, Goldwasser Recursion

Algorithm for Multiple Recursion Algorithm PuzzleSolve(k,S,U): Input: Integer k, sequence S, and set U (universe of elements to test) Output: 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 then Test whether S is a configuration that solves the puzzle if S solves the puzzle then 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 © 2013 Goodrich, Tamassia, Goldwasser Recursion

Slide by Matt Stallmann included with permission. Example cbb + ba = abc a,b,c stand for 7,8,9; not necessarily in that order 799 + 98 = 997 [] {a,b,c} [a] {b,c} a=7 [b] {a,c} b=7 [c] {a,b} c=7 [ab] {c} a=7,b=8 c=9 [ac] {b} a=7,c=8 b=9 [ba] {c} b=7,a=8 [bc] {a} b=7,c=8 a=9 [ca] {b} c=7,a=8 [cb] {a} c=7,b=8 © 2013 Goodrich, Tamassia, Goldwasser Recursion

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

Reading [G] Chapter 5