Education

Savitch Ch 14

Description
2. Chapter 14 Recursion Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3. Overview <ul><li>14.1 Recursive Functions for Tasks…
Categories
Published
of 70
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
  • 2. Chapter 14 Recursion Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
  • 3. Overview <ul><li>14.1 Recursive Functions for Tasks </li></ul><ul><li>14.2 Recursive Functions for Values </li></ul><ul><li>14.3 Thinking Recursively </li></ul>Slide 14-
  • 4. 14.1 Recursive Functions for Tasks Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
  • 5. Recursive Functions for Tasks <ul><li>A recursive function contains a call to itself </li></ul><ul><li>When breaking a task into subtasks, it may be that the subtask is a smaller example of the same task </li></ul><ul><ul><li>Searching an array could be divided into searching the first and second halves of the array </li></ul></ul><ul><ul><li>Searching each half is a smaller version of searching the whole array </li></ul></ul><ul><ul><li>Tasks like this can be solved with recursive functions </li></ul></ul>Slide 14-
  • 6. Case Study: Vertical Numbers <ul><li>Problem Definition: </li></ul><ul><ul><li>void write_vertical( int n ); //Precondition: n >= 0 //Postcondition: n is written to the screen vertically // with each digit on a separate line </li></ul></ul>Slide 14-
  • 7. Case Study: Vertical Numbers <ul><li>Algorithm design: </li></ul><ul><ul><li>Simplest case: If n is one digit long, write the number </li></ul></ul><ul><ul><li>Typical case: 1) Output all but the last digit vertically 2) Write the last digit </li></ul></ul><ul><ul><ul><li>Step 1 is a smaller version of the original task </li></ul></ul></ul><ul><ul><ul><li>Step 2 is the simplest case </li></ul></ul></ul>Slide 14-
  • 8. Case Study: Vertical Numbers (cont.) <ul><li>The write_vertical algorithm: </li></ul><ul><ul><li>if (n < 10) { cout << n << endl; } else // n is two or more digits long { write_vertical(n with the last digit removed); cout << the last digit of n << endl; } </li></ul></ul>Slide 14-
  • 9. <ul><li>Translating the pseudocode into C++ </li></ul><ul><ul><li>n / 10 returns n with the last digit removed </li></ul></ul><ul><ul><ul><li>124 / 10 = 12 </li></ul></ul></ul><ul><ul><li>n % 10 returns the last digit of n </li></ul></ul><ul><ul><ul><li>124 % 10 = 4 </li></ul></ul></ul><ul><li>Removing the first digit would be just as valid for defining a recursive solution </li></ul><ul><ul><li>It would be more difficult to translate into C++ </li></ul></ul>Case Study: Vertical Numbers (cont.) Slide 14- Display 14.1 ( 1 ) Display 14.1 ( 2 )
  • 10. Tracing a Recursive Call <ul><li>write_vertical(123) if (123 < 10) { cout << 123 << endl; } else // n is more than two digits { write_vertical(123/10); cout << (123 % 10) << endl; } </li></ul>Slide 14- Calls write_vertical(12) resume Output 3 Function call ends
  • 11. Tracing write_vertical(12) <ul><li>write_vertical(12) if (12 < 10) { cout << 12 << endl; } else // n is more than two digits { write_vertical(12/10); cout << (12 % 10) << endl; } </li></ul>Slide 14- Calls write_vertical(1) resume Output 2 Function call ends
  • 12. Tracing write_vertical(1) <ul><li>write_vertical(1) if (1 < 10) { cout << 1 << endl; } else // n is more than two digits { write_vertical(1/10); cout << (1 % 10) << endl; } </li></ul>Slide 14- Simplest case is now true Function call ends Output 1
  • 13. A Closer Look at Recursion <ul><li>write_vertical uses recursion </li></ul><ul><ul><li>Used no new keywords or anything &quot;new&quot; </li></ul></ul><ul><ul><li>It simply called itself with a different argument </li></ul></ul><ul><li>Recursive calls are tracked by </li></ul><ul><ul><li>Temporarily stopping execution at the recursive call </li></ul></ul><ul><ul><ul><li>The result of the call is needed before proceeding </li></ul></ul></ul><ul><ul><li>Saving information to continue execution later </li></ul></ul><ul><ul><li>Evaluating the recursive call </li></ul></ul><ul><ul><li>Resuming the stopped execution </li></ul></ul>Slide 14-
  • 14. How Recursion Ends <ul><li>Eventually one of the recursive calls must not depend on another recursive call </li></ul><ul><li>Recursive functions are defined as </li></ul><ul><ul><li>One or more cases where the task is accomplished by using recursive calls to do a smaller version of the task </li></ul></ul><ul><ul><li>One or more cases where the task is accomplished without the use of any recursive calls </li></ul></ul><ul><ul><ul><li>These are called base cases or stopping cases </li></ul></ul></ul>Slide 14-
  • 15. &quot;Infinite&quot; Recursion <ul><li>A function that never reaches a base case, in theory, will run forever </li></ul><ul><ul><li>In practice, the computer will often run out of resources and the program will terminate abnormally </li></ul></ul>Slide 14-
  • 16. Example: Infinite Recursion <ul><li>Function write_vertical, without the base case void new_write_vertical(int n) { new_write_vertical (n /10); cout << n % 10 << endl; } will eventually call write_vertical(0), which will call write_vertical(0),which will call write_vertical(0), which will call write_vertical(0), which will call write_vertical(0), which will call write_vertical(0), which will call write_vertical (0), … </li></ul>Slide 14-
  • 17. Stacks for Recursion <ul><li>Computers use a structure called a stack to keep track of recursion </li></ul><ul><ul><li>A stack is a memory structure analogous to a stack of paper </li></ul></ul><ul><ul><ul><li>To place information on the stack, write it on a piece of paper and place it on top of the stack </li></ul></ul></ul><ul><ul><ul><li>To place more information on the stack, use a clean sheet of paper, write the information, and place it on the top of the stack </li></ul></ul></ul><ul><ul><ul><li>To retrieve information, only the top sheet of paper can be read, and thrown away when it is no longer needed </li></ul></ul></ul>Slide 14-
  • 18. Last-in / First-out <ul><li>A stack is a last-in/first-out memory structure </li></ul><ul><ul><li>The last item placed is the first that can be removed </li></ul></ul><ul><li>Whenever a function is called, the computer uses a &quot;clean sheet of paper&quot; </li></ul><ul><ul><li>The function definition is copied to the paper </li></ul></ul><ul><ul><li>The arguments are plugged in for the parameters </li></ul></ul><ul><ul><li>The computer starts to execute the function body </li></ul></ul>Slide 14-
  • 19. Stacks and The Recursive Call <ul><li>When execution of a function definition reaches a recursive call </li></ul><ul><ul><li>Execution stops </li></ul></ul><ul><ul><li>Information is saved on a &quot;clean sheet of paper&quot; to enable resumption of execution later </li></ul></ul><ul><ul><li>This sheet of paper is placed on top of the stack </li></ul></ul><ul><ul><li>A new sheet is used for the recursive call </li></ul></ul><ul><ul><ul><li>A new function definition is written, and arguments are plugged into parameters </li></ul></ul></ul><ul><ul><ul><li>Execution of the recursive call begins </li></ul></ul></ul>Slide 14-
  • 20. The Stack and Ending Recursive Calls <ul><li>When a recursive function call is able to complete its computation with no recursive calls </li></ul><ul><ul><li>The computer retrieves the top &quot;sheet of paper&quot; from the stack and resumes computation based on the information on the sheet </li></ul></ul><ul><ul><li>When that computation ends, that sheet of paper is discarded and the next sheet of paper on the stack is retrieved so that processing can resume </li></ul></ul><ul><ul><li>The process continues until no sheets remain in the stack </li></ul></ul>Slide 14-
  • 21. Activation Frames <ul><li>The computer does not use paper </li></ul><ul><li>Portions of memory are used </li></ul><ul><ul><li>The contents of these portions of memory is called an activation frame </li></ul></ul><ul><ul><ul><li>The activation frame does not actually contain a copy of the function definition, but references a single copy of the function </li></ul></ul></ul>Slide 14-
  • 22. Stack Overflow <ul><li>Because each recursive call causes an activation frame to be placed on the stack </li></ul><ul><ul><li>infinite recursion can force the stack to grow beyond its limits to accommodate all the activation frames required </li></ul></ul><ul><ul><li>The result is a stack overflow </li></ul></ul><ul><ul><li>A stack overflow causes abnormal termination of the program </li></ul></ul>Slide 14-
  • 23. <ul><li>Any task that can be accomplished using recursion can also be done without recursion </li></ul><ul><ul><li>A nonrecursive version of a function typically contains a loop or loops </li></ul></ul><ul><ul><li>A non-recursive version of a function is usually called an iterative-version </li></ul></ul><ul><ul><li>A recursive version of a function </li></ul></ul><ul><ul><ul><li>Usually runs slower </li></ul></ul></ul><ul><ul><ul><li>Uses more storage </li></ul></ul></ul><ul><ul><ul><li>May use code that is easier to write and understand </li></ul></ul></ul>Recursion versus Iteration Slide 14- Display 14.2
  • 24. Section 14.1 Conclusion <ul><li>Can you </li></ul><ul><ul><li>Identify a possible source of a stack overflow error? </li></ul></ul><ul><ul><li>Write a recursive void-function with one parameter, a positive integer, that writes out that number of '*'s to the screen? </li></ul></ul><ul><ul><li>Write write_vertical so the digits are output in reverse order? </li></ul></ul>Slide 14-
  • 25. 14.2 Recursive Functions for Values Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
  • 26. Recursive Functions for Values <ul><li>Recursive functions can also return values </li></ul><ul><li>The technique to design a recursive function that returns a value is basically the same as what you have already seen </li></ul><ul><ul><li>One or more cases in which the value returned is computed in terms of calls to the same function with (usually) smaller arguments </li></ul></ul><ul><ul><li>One or more cases in which the value returned is computed without any recursive calls (base case) </li></ul></ul>Slide 14-
  • 27. <ul><li>To define a new power function that returns an int, such that int y = power(2,3); places 23 in y </li></ul><ul><ul><li>Use this definition: x n = x n-1 * x </li></ul></ul><ul><ul><li>Translating the right side to C++ gives: power(x, n-1) * x </li></ul></ul><ul><ul><li>The base case: n = = 0 and power should return 1 </li></ul></ul>Program Example: A Powers Function Slide 14- Display 14.3
  • 28. <ul><li>int power(2, 1) { … if (n > 0) return ( power(2, 1-1) * 2); else return (1); } </li></ul>Tracing power(2,1) Slide 14- Call to power(2,0) resume 1 return 2 Function Ends
  • 29. Tracing power(2,0) <ul><li>int power(2, 0) { … if (n > 0) return ( power(2, 0-1) * 2); else return (1); } </li></ul>Slide 14- Function call ends 1 is returned
  • 30. <ul><li>Power(2, 3) results in more recursive calls: </li></ul><ul><ul><li>power( 2, 3 ) is power( 2, 2 ) * 2 </li></ul></ul><ul><ul><li>Power( 2, 2 ) is power( 2, 1 ) * 2 </li></ul></ul><ul><ul><li>Power( 2, 1 ) is power( 2, 0 ) * 2 </li></ul></ul><ul><ul><li>Power ( 2, 0 ) is 1 (stopping case) </li></ul></ul><ul><li>See details in </li></ul>Tracing power(2, 3) Slide 14- Display 14.4
  • 31. Section 14.2 Conclusion <ul><li>Can you </li></ul><ul><ul><li>Determine the output of this function if called with rose(4)? int rose(int n) { if ( n <= 0) return 1; else return ( rose (n-1) * n); } </li></ul></ul>Slide 14-
  • 32. 14.3 Thinking Recursively Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
  • 33. Thinking Recursively <ul><li>When designing a recursive function, you do not need to trace out the entire sequence of calls </li></ul><ul><ul><li>If the function returns a value </li></ul></ul><ul><ul><ul><li>Check that there is no infinite recursion: Eventually a stopping case is reached </li></ul></ul></ul><ul><ul><ul><li>Check that each stopping case returns the correct value </li></ul></ul></ul><ul><ul><ul><li>For cases involving recursion: if all recursive calls return the correct value, then the final value returned is the correct value </li></ul></ul></ul>Slide 14-
  • 34. Reviewing the power function <ul><li>There is no infinite recursion </li></ul><ul><ul><li>Notice that the second argument is decreased at each call. Eventually, the second argument must reach 0, the stopping case int power(int x, int n) { … if (n > 0) return ( power(x, n-1) * x); else return (1); } </li></ul></ul>Slide 14-
  • 35. Review of power (cont.) <ul><li>Each stopping case returns the correct value </li></ul><ul><ul><li>power(x, 0) should return x0 = 1 which it does int power(int x, int n) { … if (n > 0) return ( power(x, n-1) * x); else return (1); } </li></ul></ul>Slide 14-
  • 36. Review of power (cont.) <ul><li>All recursive calls return the correct value so the final value returned is correct </li></ul><ul><ul><li>If n > 1, recursion is used. So power(x,n-1) must return x n-1 so power(x, n) can return x n-1 * n = x n which it does int power(int x, int n) { … if (n > 0) return ( power(x, n-1) * x); else return (1); } </li></ul></ul>Slide 14-
  • 37. Recursive void-functions <ul><li>The same basic criteria apply to checking the correctness of a recursive void-function </li></ul><ul><ul><li>Check that there is no infinite recursion </li></ul></ul><ul><ul><li>Check that each stopping case performs the correct action for that case </li></ul></ul><ul><ul><li>Check that for each recursive case: if all recursive calls perform their actions correctly, then the entire case performs correctly </li></ul></ul>Slide 14-
  • 38. Case Study: Binary Search <ul><li>A binary search can be used to search a sorted array to determine if it contains a specified value </li></ul><ul><ul><li>The array indexes will be 0 through final_index </li></ul></ul><ul><ul><li>Because the array is sorted, we know a[0] <= a[1] <= a[2] <= … <= a[final_index] </li></ul></ul><ul><ul><li>If the item is in the list, we want to know where it is in the list </li></ul></ul>Slide 14-
  • 39. Binary Search: Problem Definition <ul><li>The function will use two call-by-reference parameters to return the outcome of the search </li></ul><ul><ul><li>One, called found, will be type bool. If the value is found, found will be set to true. If the value is found, the parameter, location, will be set to the index of the value </li></ul></ul><ul><li>A call-by-value parameter is used to pass the value to find </li></ul><ul><ul><li>This parameter is named key </li></ul></ul>Slide 14-
  • 40. Binary Search Problem Definition (cont.) <ul><li>Pre and Postconditions for the function: //precondition: a[0] through a[final_index] are // sorted in increasing order //postcondition: if key is not in a[0] - a[final_index] // found = = false; otherwise // found = = true </li></ul>Slide 14-
  • 41. Binary Search Algorithm Design <ul><li>Our algorithm is basically: </li></ul><ul><ul><li>Look at the item in the middle </li></ul></ul><ul><ul><ul><li>If it is the number we are looking for, we are done </li></ul></ul></ul><ul><ul><ul><li>If it is greater than the number we are looking for, look in the first half of the list </li></ul></ul></ul><ul><ul><ul><li>If it is less than the number
  • We Need Your Support
    Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

    Thanks to everyone for your continued support.

    No, Thanks