Title: Chapter 16: Recursion
1C ProgrammingProgram Design IncludingData
Structures, Fifth Edition
2Objectives
- 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
3Recursive Definitions
- Recursion solving a problem by reducing it to
smaller versions of itself - 0! 1 (1)
- n! n x (n-1)! if n gt 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
4Recursive Definitions (cont'd.)
- 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
5Recursive Definitions (cont'd.)
- 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
6Recursive Definitions (cont'd.)
- 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
7Direct 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
8Infinite 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
9Infinite Recursion (cont'd.)
- 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
10Problem Solving Using Recursion
11Problem Solving Using Recursion (cont'd.)
12Problem Solving Using Recursion (cont'd.)
13Problem Solving Using Recursion (cont'd.)
14Problem Solving Using Recursion (cont'd.)
15Problem Solving Using Recursion (cont'd.)
16Problem Solving Using Recursion (cont'd.)
17Tower 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)
- 264 ? 1 moves to move all 64 disks to needle 3
- If computer can generate 1 billion (109)
moves/sec, the number of moves it can generate in
one year is - (3.2 107) 109 3.2 1016
- Computer time required for 264 moves
- 264 1.6 1019 1.6 1016 103 (3.2
1016) 500
18Recursion or Iteration?
- There are usually two ways to solve a particular
problem - Iteration (looping)
- Recursion
- Which method is betteriteration or recursion?
- Factors
- Nature of the problem
- Efficiency
19Recursion or Iteration? (cont'd.)
- 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
20Recursion or Iteration? (cont'd.)
- Overhead associated with executing a (recursive)
function in terms of - Memory space
- Computer time
- A recursive function executes more slowly than
its iterative counterpart
21Recursion or Iteration? (cont'd.)
- On slower computers, especially those with
limited memory space - The slow execution of a recursive function would
be visible - Todays computers are fast and have inexpensive
memory - Execution of a recursion function is not
noticeable
22Recursion or Iteration? (cont'd.)
- Choice between the two alternatives depends on
the nature of the problem - Mission control systems or similar
- Efficiency is critical and dictates the solution
method - Sometimes iterative solution is more obvious and
easier to understand - If the definition of a problem is inherently
recursive, consider a recursive solution
23Programming 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
24Programming Example (cont'd.)
- 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
25Programming Example (cont'd.)
- Binary representation of 35 is the binary
representation of 17 (35/2) followed by the
rightmost bit of 35
26Summary
- 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
27Summary (cont'd.)
- 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
28Summary (cont'd.)
- 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