Chapter 6 - Recursion

# Chapter 6 - Recursion - Recursion 1/28 Introduction to...

This preview shows pages 1–8. Sign up to view the full content.

1/28 Recursion

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2/28 Introduction to Recursion Recursive procedures are functions that invoke themselves either directly (call themselves from within themselves) or indirectly (call another function that calls original function.) Recursion: . An alternative to iteration . Recursion can be very elegant at times, . Not inexpensive to implement. .. Classic examples of recursion . Recursive calculation of a string length, factorials, divide and conquer, towers of Hanoi, binary searches, and more Recursive functions a used in many applied areas. . In artificial intelligence. . In searching data structures that are themselves "recursive" in nature, such as trees. Concepts and implementation of recursive functions is an important topic.
3/28 int public sum (int n) { if (n <= 1) return n; else return (n + sum(n-1)); } Looks pretty simple… Notice the recursion in the Else branch Note the ‘return’ calls itself! (calls the method it resides in!) Note the ‘Base Case’ Note the ‘Recursive Case’ Note the return is not ‘satisfied’ until it can add n + the sum (n-1)! Let’s look how this is executed! Consider: Want to sum the numbers from 1 to 5 Base case Recursive Case

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4/28 int public sum (int n) { if (n <= 1) return n; else return (n + sum(n-1)); } START WITH N = 5: We get a VALUE plus a (recursive) ‘ CALL :’ Thus the return is not “ satisfied ” because the call is not complete. Results of the ‘return’ are suspended until we have a value for sum(n-1). SUM(5): 5 + SUM(4) - A FUNCTION CALL (SUM(4)) WHOSE VALUE WE AWAIT SUM(4): 4 +SUM(3) -A FUNCTION CALL WHOSE VALUE WE AWAIT SUM(3): 3 + SUM(2) -A FUNCTION CALL WHOSE VALUE WE AWAIT SUM(2): 2 + SUM(1) - A FUNCTION CALL WHOSE VALUE WE AWAIT SUM(1): 1 OK. Now what? We finally have a call whose value is satisfied . So??
5/28 IT IS ONLY WHEN WE GET THE VALUE FOR SUM(1) = 1 (reached the base case ) THAT WE CAN GO "BACK UP " (one call at a time) THROUGH THE SUSPENDED FUNCTION CALLS AND CALCULATE THEIR VALUES AND THUS "COMPLETE THAT ORIGINAL CALL". .. We need to ‘satisfy’ a particular function call before we can proceed up the calling sequence. Note this notion of ‘completing ’ the call; that is, satisfying the call….

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6/28 WHEN WE GET SUM(1)=1, THEN WE REFER TO 2 + SUM(1) equals 2 + 1 = 3. (complete) WE NOW HAVE A VALUE FOR SUM(2). The call: SUM(2) is satisfied !! NEXT, GOING UP THE SUSPENDED CALLS. ... SUM (3) = 3+SUM(2), which is 3 + 3 OR 6; (now complete) We now have a value for SUM(3) The call: SUM(3) is ‘ satisfied .’ Proceed upward . NEXT, SUM (4) = 4+SUM(3), WHICH IS 4 + 6 OR 10; ( now complete ) FINALLY, SUM (5) = 5+SUM(4), WHICH IS 5 + 10 OR 15 ( now complete ) ALL THESE PREVIOUS FUNCTION CALLS ARE "PENDING" OR "INCOMPLETE" PENDING A RETURNED VALUE FROM THE CALLED FUNCTION.
7/28 . RECURSION => has a significant overhead… . "DEEPER" WE GO => Need ADDITIONAL COPIES OF THE DATA .. NOT ALWAYS SMALL NUMBER OF DATA ITEMS. . These ‘copies’ are stored in

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/04/2011 for the course COP 3540 taught by Professor Bobroggio during the Fall '11 term at University of South Florida - Tampa.

### Page1 / 29

Chapter 6 - Recursion - Recursion 1/28 Introduction to...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online