Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.

Similar presentations


Presentation on theme: "C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion."— Presentation transcript:

1 C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion

2 C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will: Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover what is a recursive algorithm Learn about recursive functions Explore how to use recursive functions to implement recursive algorithms

3 C++ Programming: Program Design Including Data Structures, Fourth Edition3 Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself 0! = 1(1) n! = n x (n-1)! if n > 0(2) The definition of factorial in equations (1) and (2) is called a recursive definition −Equation (1) is called the base case −Equation (2) is called the general case

4 C++ Programming: Program Design Including Data Structures, Fourth Edition4 Recursive Definitions (continued) Recursive definition: defining a problem in terms of a smaller version of itself −Every recursive definition must have one (or more) base cases −The general case must eventually reduce to a base case −The base case stops the recursion

5 C++ Programming: Program Design Including Data Structures, Fourth Edition5 Recursive Definitions (continued) Recursive algorithm: finds a solution by reducing problem to smaller versions of itself −Must have one (or more) base cases −General solution must eventually reduce to a base case Recursive function: a function that calls itself Recursive algorithms are implemented using recursive functions

6 C++ Programming: Program Design Including Data Structures, Fourth Edition6 Recursive Definitions (continued) Think of a recursive function as having infinitely many copies of itself −After completing a particular recursive call: Control goes back to the calling environment, which is the previous call Execution begins from the point immediately following the recursive call

7 Every call to a recursive function has its own code and its own set of parameters and local variables cout << fact(4) << endl;

8 C++ Programming: Program Design Including Data Structures, Fourth Edition8 Direct and Indirect Recursion Directly recursive: a function that calls itself Indirectly recursive: a function that calls another function and eventually results in the original function call Tail recursive function: function in which the last statement executed is the recursive call −Example: the function fact

9 C++ Programming: Program Design Including Data Structures, Fourth Edition9 Infinite Recursion Infinite recursion: every recursive call results in another recursive call −In theory, infinite recursion executes forever Because computer memory is finite: −Function executes until the system runs out of memory −Results in an abnormal program termination

10 C++ Programming: Program Design Including Data Structures, Fourth Edition10 Infinite Recursion (continued) To design a recursive function: −Understand problem requirements −Determine limiting conditions −Identify base cases and provide a direct solution to each base case −Identify general cases and provide a solution to each general case in terms of smaller versions of itself

11 C++ Programming: Program Design Including Data Structures, Fourth Edition11 Problem Solving Using Recursion

12 C++ Programming: Program Design Including Data Structures, Fourth Edition12 Problem Solving Using Recursion (continued)

13 cout << largest(list, 0, 3) << endl;

14 C++ Programming: Program Design Including Data Structures, Fourth Edition14 Problem Solving Using Recursion (continued)

15 cout << rFibNum(2, 3, 5) << endl;

16 C++ Programming: Program Design Including Data Structures, Fourth Edition16 Problem Solving Using Recursion (continued)

17

18 C++ Programming: Program Design Including Data Structures, Fourth Edition18 Problem Solving Using Recursion (continued)

19 C++ Programming: Program Design Including Data Structures, Fourth Edition19 Tower of Hanoi: Analysis How long would it take to move 64 disks from needle 1 to needle 3? −About 500 years (rate of 1 billion moves/sec): 2 64  1 moves to move all 64 disks to needle 3 If computer can generate 1 billion (10 9 ) moves/sec, the number of moves it can generate in one year is: (3.2 × 10 7 ) × 10 9 = 3.2 × 10 16 Computer time required for 2 64 moves: 2 64 ≈ 1.6 × 10 19 = 1.6 × 10 16 × 10 3 = (3.2 × 10 16 ) × 500

20 C++ Programming: Program Design Including Data Structures, Fourth Edition20 Recursion or Iteration? There are usually two ways to solve a particular problem: −Iteration (looping) −Recursion Which method is better—iteration or recursion? In addition to the nature of the problem, the other key factor in determining the best solution method is efficiency

21 C++ Programming: Program Design Including Data Structures, Fourth Edition21 Recursion or Iteration? (continued) Whenever a function is called −Memory space for its formal parameters and (automatic) local variables is allocated When the function terminates −That memory space is then deallocated Every (recursive) call has its own set of parameters and (automatic) local variables

22 C++ Programming: Program Design Including Data Structures, Fourth Edition22 Recursion or Iteration? (continued) Overhead associated with executing a (recursive) function in terms of: −Memory space −Computer time A recursive function executes more slowly than its iterative counterpart

23 C++ Programming: Program Design Including Data Structures, Fourth Edition23 Recursion or Iteration? (continued) On slower computers, especially those with limited memory space −The slow execution of a recursive function would be visible Today’s computers are fast and have inexpensive memory −Execution of a recursion function is not noticeable

24 C++ Programming: Program Design Including Data Structures, Fourth Edition24 Recursion or Iteration? (continued) Choice between the two alternatives depends on the nature of the problem For problems such as mission control systems, efficiency is critical and dictates the solution method An iterative solution is more obvious and easier to understand than a recursive solution If the definition of a problem is inherently recursive, consider a recursive solution

25 C++ Programming: Program Design Including Data Structures, Fourth Edition25 Programming Example: Converting a Number from Binary to Decimal Use recursion to convert a nonnegative integer in decimal format (base 10) into the equivalent binary number (base 2) Define some terms: −Let x be an integer −Rightmost bit of x: remainder of x after division by 2 Rightmost bit of 33 is 1 Rightmost bit of 28 is 0

26 C++ Programming: Program Design Including Data Structures, Fourth Edition26 Programming Example (continued) To find the binary representation of 35: −Divide 35 by 2 The quotient is 17 and the remainder is 1 −Divide 17 by 2 The quotient is 8 and the remainder is 1 −Divide 8 by 2 The quotient is 4 and the remainder is 0 −Continue process until quotient becomes 0 Rightmost bit of 35 cannot be printed until we have printed the rightmost bit of 17

27 C++ Programming: Program Design Including Data Structures, Fourth Edition27 Programming Example (continued) Binary representation of 35 is the binary representation of 17 (35/2) followed by the rightmost bit of 35

28 decToBin(13, 2);

29 C++ Programming: Program Design Including Data Structures, Fourth Edition29 Summary Recursion: process of solving a problem by reducing it to smaller versions of itself Recursive definition: defines a problem in terms of smaller versions of itself −Has one or more base cases Recursive algorithm: solves a problem by reducing it to smaller versions of itself −Has one or more base cases

30 C++ Programming: Program Design Including Data Structures, Fourth Edition30 Summary (continued) The solution to the problem in a base case is obtained directly Recursive function: function that calls itself −Must have one or more base cases Recursive algorithms are implemented using recursive functions The general solution breaks the problem into smaller versions of itself

31 C++ Programming: Program Design Including Data Structures, Fourth Edition31 Summary (continued) The general case must eventually be reduced to a base case −The base case stops the recursion Directly recursive: a function calls itself Indirectly recursive: a function calls another function and eventually calls the original Tail recursive: the last statement executed is the recursive call


Download ppt "C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion."

Similar presentations


Ads by Google