recursion - top steptopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 designing...

55
1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion Design a recursive algorithm Determine when an recursion is an appropriate solution Write simple recursive functions Recursion Data Structures: A Pseudocode Approach with C , Second Edition

Upload: others

Post on 19-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

1

Chapter 2

Objectives

Upon completion you will be able to:

• Explain the difference between iteration and recursion

• Design a recursive algorithm

• Determine when an recursion is an appropriate solution

• Write simple recursive functions

Recursion

Data Structures: A Pseudocode Approach with C , Second Edition

Page 2: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

2

How to writing repetitive algorithms

Iteration

Recursion

Is a repetitive process in which an algorithm calls itself.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 3: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

3

2-1 Factorial - A Case Study

We begin the discussion of recursion with a case

study and use it to define the concept.

This section also presents an iterative and a

recursive solution to the factorial algorithm.

• Recursive Defined

• Recursive Solution

Data Structures: A Pseudocode Approach with C , Second Edition

Page 4: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

4

Iterative

The definition involves only the algorithm parameter(s) and not the algorithm itself.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 5: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

5

Recursive

A repetitive algorithm use recursion whenever the algorithm appears within the definition itself.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 6: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

6Data Structures: A Pseudocode Approach with C , Second Edition

Page 7: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

7

Note that

Recursion is a repetitive process in which an algorithm called itself.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 8: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

8Data Structures: A Pseudocode Approach with C , Second Edition

Page 9: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

9Data Structures: A Pseudocode Approach with C , Second Edition

Page 10: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

10

Which code is simpler?

Which one has not a loop?

Data Structures: A Pseudocode Approach with C , Second Edition

Page 11: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

11

Calling a recursive algorithm

Data Structures: A Pseudocode Approach with C , Second Edition

Page 12: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

12

2-2 Designing Recursive Algorithms

In this section we present an analytical

approach to designing recursive algorithms.

We also discuss algorithm designs that are not

well suited to recursion.

• The Design Methodology

• Limitation of Recusion

• Design Implemenation

Data Structures: A Pseudocode Approach with C , Second Edition

Page 13: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

13

The Design Methodology

Every recursive call either solves a part of the problem or it reduce the size of the problem.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 14: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

14

The Design Methodology

Base case

The statement that “solves” the problem.

Every recursive algorithm must have a base case.

General case

The rest of the algorithm

Contains the logic needed to reduce the size of the problem.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 15: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

15

Rules for designing a recursive algorithm

1. Determine the base case.

2. Determine the general case.

3. Combine the base case and the general cases into an algorithm.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 16: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

16

Combine the base case and the general cases into an algorithm

Each call must reduce the size of the problem and move it toward the base case.

The base case, when reached, must terminate without a call to the recursive algorithms; that is, it must execute a return.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 17: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

17

Limitations of recursion

You should not use recursion if the answer to any of the following questions is no:

1. Is the algorithm or data structure naturally suited to recursion?

2. Is the recursive solution shorter and more understandable?

3. Does the recursive solution run within acceptable time and space limits?

Data Structures: A Pseudocode Approach with C , Second Edition

Page 18: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

18

Design implementation – reverse keyboard input

Data Structures: A Pseudocode Approach with C , Second Edition

Page 19: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

19

data=6

data=20

data=14

data=5

※ 請注意 print data

在什麼時候執行

Data Structures: A Pseudocode Approach with C , Second Edition

Page 20: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

20

2-3 Recursive Examples

Slide 05

Four recursive programs are developed and

analyzed. Only one, the Towers of Hanoi, turns

out to be a good application for recursion.

• Greatest Common Divisor

• Fiboncci Numbers

• Prefix to Postfix Conversion

• The Towers of Honoi

Data Structures: A Pseudocode Approach with C , Second Edition

Page 21: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

21

GCD design

otherwise

aif

bif

bab

b

a

ba 0

0

)mod,gcd(

),gcd(

Greatest Common Divisor Recursive Definition

Data Structures: A Pseudocode Approach with C , Second Edition

Page 22: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

22

Pseudocode

Data Structures: A Pseudocode Approach with C , Second Edition

Page 23: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

23

GCD C implementation

Data Structures: A Pseudocode Approach with C , Second Edition

Page 24: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

24

gcd(10, 25)

gcd(25, 10)

gcd(10, 5)

gcd(5, 0)

Data Structures: A Pseudocode Approach with C , Second Edition

Page 25: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

25

Another G.C.D. Recursive Definition

Data Structures: A Pseudocode Approach with C , Second Edition

Page 26: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

26

Fibonacci numbers

Mentioned a problem FIBONACCI:A pair of rabbits, a month later able to produce a pair of rabbits, and newborn rabbit have in a month after fertility, but also gave birth to rabbits.Start from a pair of rabbits, a year later there will be a number of rabbits?

Data Structures: A Pseudocode Approach with C , Second Edition

Page 27: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

27

Fibonacci numbers

Data Structures: A Pseudocode Approach with C , Second Edition

Page 28: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

28

Fibonacci numbers -

Each number is the sum of the previous two numbers.

The first few numbers in the Fibonacci series are

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

Data Structures: A Pseudocode Approach with C , Second Edition

Page 29: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

29

Fibonacci numbers

Data Structures: A Pseudocode Approach with C , Second Edition

Page 30: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

30

Fibonacci numbers

Data Structures: A Pseudocode Approach with C , Second Edition

Page 31: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

31

Fibonacci numbers – an example

Data Structures: A Pseudocode Approach with C , Second Edition

Page 32: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

32

0 represents .T.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 33: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

33

(Continued)

Data Structures: A Pseudocode Approach with C , Second Edition

Page 34: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

34

Analysis

Data Structures: A Pseudocode Approach with C , Second Edition

Page 35: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

35

We omit

Prefix to Postfix Conversion

The Towers of Honoi

Data Structures: A Pseudocode Approach with C , Second Edition

Page 36: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

36

HW2

Write a recursive algorithm to calculate the combination of n objects taken k at a time.

Due date : (甲:941012、乙:941012)

Data Structures: A Pseudocode Approach with C , Second Edition

Page 37: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

37

2-3 Recursive Examples

Slide 05

Four recursive programs are developed and

analyzed. Only one, the Towers of Hanoi, turns

out to be a good application for recursion.

•The Towers of Honoi

Data Structures: A Pseudocode Approach with C , Second Edition

Page 38: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

38

Towers of Hanoi Problem:

Invented by French mathematician Lucas in 1880s.

Original problem set in India in a holy place called Benares.

There are 3 diamond needles fixed on a brass plate. One needle contains 64 pure gold disks. Largest resting on the brass plate, other disks of decreasing diameters.

Called tower of Brahma.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 39: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

39

Towers of Hanoi Problem:

Priests are supposed to transfer the disks from one needle to the other such that at

no time a disk of larger diameter should sit on a disk of smaller diameter.

Only one disk can be moved at a time.

Later setting shifted to Honoi, but the puzzle and legend remain the same.

How much time would it take? Estimate…..

Data Structures: A Pseudocode Approach with C , Second Edition

Page 40: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

40Data Structures: A Pseudocode Approach with C , Second Edition

Page 41: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

41

Towers of Hanoi Problem:

Today we know that we need to have

264-1

Data Structures: A Pseudocode Approach with C , Second Edition

Page 42: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

42

Recursive Towers of Hanoi Design

Find a pattern of moves.

Case 1:

move one disk from source to destination needle.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 43: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

43

Move two disks

Case 2:

Move one disk to auxiliary needle.

Move one disk to destination needle.

Move one disk to from auxiliary to destination needle.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 44: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

44Data Structures: A Pseudocode Approach with C , Second Edition

Page 45: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

45

Move three disks

Case 3:

Move two disks from source to auxiliaryneedle.

Move one disk from source to destinationneedle.

Move two disks from auxiliary to destination needle.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 46: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

46

(Continued)

Move two disks from source to auxiliary needle.

Move two disks from auxiliary to destination needle.

Data Structures: A Pseudocode Approach with C , Second Edition

Page 47: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

47

Algorithm Tower of Hanoi

Towers (numDisks, source, dest, auxiliary)

numDisks is number of disks to be moved

source is the source tower

dest is the destination tower

auxiliary is the auxiliary tower

Data Structures: A Pseudocode Approach with C , Second Edition

Page 48: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

48Data Structures: A Pseudocode Approach with C , Second Edition

Page 49: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

Data Structures: A Pseudocode Approach with C 49

Generalize Tower of Hanoi

General case

Move n-1 disks from source to auxiliary.

Base case

Move one disk from source to destination.

General case

Move n-1 disks from auxiliary to destination.

Page 50: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

50Data Structures: A Pseudocode Approach with C , Second Edition

Page 51: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

51Data Structures: A Pseudocode Approach with C , Second Edition

Page 52: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

52Data Structures: A Pseudocode Approach with C , Second Edition

Page 53: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

53Data Structures: A Pseudocode Approach with C , Second Edition

Page 54: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

54Data Structures: A Pseudocode Approach with C , Second Edition

Page 55: Recursion - TOP STEPtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing

55Data Structures: A Pseudocode Approach with C , Second Edition