• No results found

C++ Programming: From Problem Analysis to Program Design, Fifth Edition

N/A
N/A
Protected

Academic year: 2020

Share "C++ Programming: From Problem Analysis to Program Design, Fifth Edition"

Copied!
28
0
0

Loading.... (view fulltext now)

Full text

(1)

Chapter 17: Recursion

C++ Programming: From Problem

(2)

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

(3)

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

(4)

Recursive 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

(5)

Recursive 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

(6)

Recursive Definitions (cont'd.)

• Think of a recursive function as having infinitely many copies of itself

– After completing a particular recursive call:

(7)

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

(8)

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

(9)

Infinite 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

(10)
(11)
(12)
(13)
(14)
(15)
(16)
(17)

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):

• 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:

(18)

Recursion or Iteration?

• There are usually two ways to solve a particular problem:

– Iteration (looping) – Recursion

• Which method is better—iteration or recursion?

(19)

Recursion 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

(20)

Recursion or Iteration? (cont'd.)

• Overhead associated with executing a (recursive) function in terms of:

– Memory space – Computer time

(21)

Recursion or Iteration? (cont'd.)

• 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

(22)

Recursion 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

(23)

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

(24)

Programming 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

(25)

Programming Example (cont'd.)

• Binary representation of 35 is the binary

(26)

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

(27)

Summary (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

(28)

Summary (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

References

Related documents

We present a general algorithm, pre-determinization, that makes an arbitrary weighted transducer over the tropical semiring or an arbitrary unambiguous weighted transducer over

The treatment of choice of chest wall TB is still debated, nevertheless the majority of described cases have been treated with surgical debridement (or excision based on

It could therefore be sug- gested that consuming dietary sources of endogenous antioxidant precursors, such as thiol donors, may enhance exercise performance and recovery without

Most significantly, recursive algorithms start with a large version of the problem and works towards smaller, simpler versions.. Dynamic programming algorithms starts with the

However, organizing clinical concepts along basic science concepts, as the resident groups mainly did, might be a necessary developmental phase in the building of a sound

• The syntax of an input statement using cin and the extraction operator >> is:4. • The extraction operator >>

Así, la introducción de un enfoque participativo, horizontal y con perspectiva de género fue fundamental para promover la implicación de las mariscadoras en este proceso