chapter 16 recursive functions - university of...

34
© Christian Jacob Chapter Overview Chapter 16 Recursive Functions 16.1 Recursive Functions 16.1.1 Iterative versus Recursive 16.1.2 Comparing Iterative and Recursive Processes 16.2 Further Examples with Recursion 16.2.1 String Reversion 16.2.2 Recursion over Arrays 16.3 The Towers of Hanoi 16.3.1 Problem Definition 16.3.2 Problem Definition 16.3.3 Ideas for a Recursive Solution 16.3.4 A Recursive Tower-of-Hanoi Algorithm

Upload: phungcong

Post on 29-Mar-2018

243 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

©

Christian Jacob

s

Chapter Overview

Chapter 16

Recursive Functions

16.1 Recursive Functions

16.1.1 Iterative versus Recursive

16.1.2 Comparing Iterative and Recursive Processe

16.2 Further Examples with Recursion

16.2.1 String Reversion

16.2.2 Recursion over Arrays

16.3 The Towers of Hanoi

16.3.1 Problem Definition

16.3.2 Problem Definition

16.3.3 Ideas for a Recursive Solution

16.3.4 A Recursive Tower-of-Hanoi Algorithm

Page 2: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

©

Christian Jacob

Chapter Overview

16.4 References

Page 3: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 3

Chapter 16: Recursion

©

Christian Jacob

Prev Next Last

2 * 1

First Back TOC

16.1 Recursive Functions

16.1.1 Iterative versus Recursive

Definitions of the Factorial Function

factorial(n) := n!

:= n * (n-1) * … *

:= i

i 1=

n

Page 4: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 4 Chapter 16: Recursion © Christian Jacob

Prev Next Last

orial function:

First Back TOC

Iterative Implementation

factorial(n) :=

Here is a typical iterative C++ implementation of the fact

int factorial_iter (int n){

int i, f=1;

for(i=1; i <= n; i++) f *= i;

return f;}

i

i 1=

n

Page 5: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 5 Chapter 16: Recursion © Christian Jacob

Prev Next Last

… * 2 * 1

n-2) * … * 2 *1

* 2 * 1

First Back TOC

Towards a Recursive Definition

factorial(n) := = n * (n-1) *

factorial(n-1) := =

(n-1) * (

factorial(n-2) := =

(n-2) * …

factorial(2) := =

2 * 1

i

i 1=

n

i

i 1=

n 1–

i

i 1=

n 2–

i

i 1=

2

Page 6: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 6 Chapter 16: Recursion © Christian Jacob

Prev Next Last

version 1)

First Back TOC

A Recursive Definition

Recursive Implementation of the Factorial Function (

int factorial_rec (int n){

if(n == 1) return 1;

return n * factorial_rec(n-1);}

factorial n( ) := 1 if n = 1

n factorial n 1–( )⋅ if n 1>

Page 7: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 7 Chapter 16: Recursion © Christian Jacob

Prev Next Last

cesses

First Back TOC

16.1.2 Comparing Iterative and Recursive Pro

Iterative Version

int factorial_iter (int n){

int i, f=1;for(i=1; i <= n; i++) f *= i;return f;

}

Executing this program generates a linear iterative process:

Iteration i f n______________________________________

1 1 1 42 2 2 43 3 6 44 4 24 45 5

Page 8: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 8 Chapter 16: Recursion © Christian Jacob

Prev Next Last

c(n-1));

ample shows:

First Back TOC

Recursive Version

int factorial_rec(int n){

(n == 1) ? 1 : (n * factorial_re}

This generates a linear recursive process, as the following ex

fac_rec(5)(5 * fac_rec(4))(5 * (4 * fac_rec(3)))(5 * (4 * (3 * fac_rec(2))))(5 * (4 * (3 * (2 * fac_rec(1)))))(5 * (4 * (3 * (2 * 1))))(5 * (4 * (3 * 2)))(5 * (4 * 6))(5 * 24)(120)

Page 9: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 9 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on

a string backwards.

”);

First Back TOC Further Examples with Recursion

16.2 Further Examples with Recursi

16.2.1 String Reversion

The following example uses a recursive function to print

void reverse(char *s){

if(*s)reverse(s+1)

elsereturn;

}

void main(){

cout << reverse(“Left to right}

Page 10: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 10 Chapter 16: Recursion © Christian Jacob

Prev Next Last

can be defined as

this element.

element and the sum

array[])

First Back TOC Further Examples with Recursion

16.2.2 Recursion over Arrays

A function for adding elements m through n of an array,follows:

• If there is only one element, the sum is the value of

• Otherwise, the sum is calculated by adding the first of the rest.

Here is the C++ implementation:

int sum(int first, int last, int {

if(first == last)return array[first];

/* else */return

(array[first] + sum(first+1,last,array));

}

Page 11: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 11 Chapter 16: Recursion © Christian Jacob

Prev Next Last

ed by this function:

First Back TOC Further Examples with Recursion

Here is another example of the recursive process generat

Sum(1 8 3 2) =

(1 + Sum(8 3 2)) =

(1 + (8 + Sum(3 2))) =

(1 + (8 + (3 + Sum( 2 )))) =

(1 + (8 + (3 + 2))) =

(1 + (8 + 6)) =

(1 + 14) =

( 15 )

Page 12: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 12

Chapter 16: Recursion

©

Christian Jacob

Prev Next Last

C

First Back TOC The Towers of Hanoi

16.3 The Towers of Hanoi

16.3.1 Problem Definition

A

B

Page 13: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 13

Chapter 16: Recursion

©

Christian Jacob

Prev Next Last

C

First Back TOC The Towers of Hanoi

16.3The Towers of Hanoi

16.3.1 Problem Definition

A

B

Page 14: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 14

Chapter 16: Recursion

©

Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 0 — Initial

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 15: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 15 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 1 — Recursion 1

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 16: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 16 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 2 — Recursion 1

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 17: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 17 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 3 — Recursion 1

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 18: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 18 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 4 — Recursion 1

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 19: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 19 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 5 — Recursion 1

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 20: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 20 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 6 — Recursion 1

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 21: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 21 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 7 — Recursion 1

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 22: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 22 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 8 — Move

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 23: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 23 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 9 — Recursion 2

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 24: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 24 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 10 — Recursion 2

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 25: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 25 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 11 — Recursion 2

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 26: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 26 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 12 — Recursion 2

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 27: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 27 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 13 — Recursion 2

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 28: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 28 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 14 — Recursion 2

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 29: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 29 Chapter 16: Recursion © Christian Jacob

Prev Next Last

on the board).

other tower.

First Back TOC The Towers of Hanoi

Step 15 — Recursion 2

Initial Setup:

Tower A contains n disks of different sizes.Disks can only go on top of smaller disks (or directly

Objec tive:

Move all

n

disks from tower A to tower B.

Moves:

Take the top disk of a tower and move the disk to anNo disk may be put on top of a smaller disk.

A B C

Page 30: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 30 Chapter 16: Recursion © Christian Jacob

Prev Next Last

B C

B C

B C

First Back TOC The Towers of Hanoi

16.3.2 Ideas for a Recursive Solution

… is equivalent to …

A B C A

move tower

with n disks

A B C A

A B C A

move tower

with

n-1

disks

move tower

with

n-1

disks

move

disk

Page 31: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 31 Chapter 16: Recursion © Christian Jacob

Prev Next Last

m

First Back TOC The Towers of Hanoi

16.3.3 A Recursive Tower-of-Hanoi Algorith

The Main Algorithm:

int main(){

int n;

cout << “How many disks? “;cin >> n;

if( n > 0)moveTower(n, ‘A‘, ‘B‘, ‘C‘);

return 0;}

Exponential problem: n

disks --->

2

n

-1

moves

Page 32: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 32 Chapter 16: Recursion © Christian Jacob

Prev Next Last

First Back TOC The Towers of Hanoi

Algorithm output:

How many disks? 4

Move top disk from tower A to tower C.Move top disk from tower A to tower B.Move top disk from tower C to tower B.Move top disk from tower A to tower C.Move top disk from tower B to tower A.Move top disk from tower B to tower C.Move top disk from tower A to tower C.Move top disk from tower A to tower B.Move top disk from tower C to tower B.Move top disk from tower C to tower A.Move top disk from tower B to tower A.Move top disk from tower C to tower B.Move top disk from tower A to tower C.Move top disk from tower A to tower B.Move top disk from tower C to tower B.

Page 33: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 33

Chapter 16: Recursion

©

Christian Jacob

Prev Next Last

char to,

o);

m);

wer “ << from“.” << endl;

First Back TOC The Towers of Hanoi

void moveTower(int n, char from, char temp){

if( n==1 )moveDisk(from, to);

else {moveTower(n-1, from, temp, tmoveDisk(from, to);moveTower(n-1, temp, to, fro

}}

void moveDisk(char from, char to){

cout << “Move top disk from tocout << “ to tower “ << to <<

}

Page 34: Chapter 16 Recursive Functions - University of Calgarypages.cpsc.ucalgary.ca/.../Fall00/CPSC231/Slides/16-Recursion.pdf · Christian Jacob Chapter Overview Chapter 16 Recursive Functions

Page 34

Chapter 16: Recursion

©

Christian Jacob

Prev Next Last

, Boston, MA: WCB/

ill, Berkeley, CA,

First Back TOC References

16.4 References

• G. Blank and R. Barnes, The Universal MachineMcGraw-Hill, 1998. Chapter 9.8.

• H. Schildt, C++ from the Ground Up, McGraw-H1998. Chapter 7.