data structures: a pseudocode approach with c1 chapter 2 objectives upon completion you will be able...

55
Data Structures: A Pseudocode Approach with C 1 Chapter Chapter 2 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 Recursion

Upload: arabella-harris

Post on 30-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 1

ChapterChapter 22

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

RecursionRecursion

Page 2: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 2

How to writing repetitive algorithms

Iteration Recursion

Is a repetitive process in which an algorithm calls itself.

請同學區分這三個字 repetitive、 iterative、 recursive,特別是看中文書的同學。

Page 3: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 3

2-1 Factorial - A Case Study

We begin the discussion of recursion with a case We begin the discussion of recursion with a case study and use it to define the concept. study and use it to define the concept. This section also presents an iterative and a This section also presents an iterative and a recursive solution to the factorial algorithm.recursive solution to the factorial algorithm.

• Recursive Defined• Recursive Solution

Page 4: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 4

Iterative

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

Page 5: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 5

Recursive

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

Page 6: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 6

Page 7: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 7

Note that

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

Page 8: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 8

Page 9: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 9

Page 10: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 10

Which code is simpler?

Which one has not a loop?

Page 11: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 11

Calling a recursive algorithm

Page 12: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 12

2-2 Designing Recursive Algorithms

In this section we present an analytical In this section we present an analytical approach to designing recursive algorithms. approach to designing recursive algorithms. We also discuss algorithm designs that are not We also discuss algorithm designs that are not well suited to recursion.well suited to recursion.

• The Design Methodology• Limitation of Recusion• Design Implemenation

Page 13: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 13

The Design Methodology

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

Page 14: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 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.

Page 15: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 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.

Page 16: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 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.

Page 17: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 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?

Page 18: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 18

Design implementation – reverse keyboard input

Page 19: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 19

data=6

data=20

data=14

data=5

※ 請注意 print data 在什麼時候執行

Page 20: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 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

Page 21: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 21

GCD design

otherwise

aif

bif

bab

b

a

ba 0

0

)mod,gcd(

),gcd(

Greatest Common Divisor Recursive Definition

Page 22: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 22

Pseudocode

歐基里德輾轉相除法

Page 23: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 23

GCD C implementation

Page 24: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 24

gcd(10, 25) gcd(25, 10) gcd(10, 5) gcd(5, 0)

Page 25: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 25

Another G.C.D. Recursive Definition

Page 26: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 26

Fibonacci numbers

在費波那西的〈算經〉中提到一個問題: 假如一對兔子,一個月後能生產一對兔子,

而新生兔子在一個月後便具備生育能力,也能生下一對兔子。

那麼若從一對兔子開始,一年之後將會有多少對兔子?

Page 27: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 27

Fibonacci numbers

Page 28: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 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, …

Page 29: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 29

Fibonacci numbers -- 費式數

Page 30: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 30

Fibonacci numbers

Page 31: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 31

Fibonacci numbers – an example

Page 32: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 32

0 represents .T.

Page 33: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 33

(Continued)

Page 34: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 34

Analysis

Page 35: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 35

We omit

Prefix to Postfix Conversion The Towers of Honoi

Page 36: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 36

HW2

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

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

Page 37: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 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

Page 38: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 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.

Page 39: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 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…..

Page 40: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 40

Page 41: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 41

Towers of Hanoi Problem:

Today we know that we need to have

264-1

Page 42: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 42

Recursive Towers of Hanoi Design Find a pattern of moves. Case 1:

move one disk from source to destination needle.

Page 43: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 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.

Page 44: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 44

Page 45: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 45

Move three disks

Case 3: Move two disks from source to auxiliary

needle. Move one disk from source to

destination needle. Move two disks from auxiliary to

destination needle.

Page 46: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 46

(Continued)

Move two disks from source to auxiliary needle.

Move two disks from auxiliary to destination needle.

Page 47: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 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

Page 48: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 48

Page 49: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

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: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 50

Page 51: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 51

Page 52: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 52

Page 53: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 53

Page 54: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 54

Page 55: Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion

Data Structures: A Pseudocode Approach with C 55