78
Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : [email protected] TA: 鄭鄭鄭 鄭鄭鄭

Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : [email protected] TA: 鄭筱親 陳昱豪

Embed Size (px)

Citation preview

Page 1: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Chapter 10Recursion

Dr. Jiung-yao HuangDept. Comm. Eng.Nat. Chung Cheng Univ.E-mail : [email protected]: 鄭筱親 陳昱豪

Page 2: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-2

Outline• 10.1 THE NATURE OF RECURSION• 10.2 TRACING A RECURSIVE FUNCTION• 10.3 RECURSIVE MATHMETICAL FUNCTIONS• 10.4 RECURSIVE FUNCTIONS WITH ARRAY AND

STRING PARAMETERS– CASE STUDY

• FINDING CAPITAL LETTERS IN A STRING– CASE STUDY

• RECURSIVE SELECTION SORT• 10.5 PROBLEM SOLVING WITH RECURSION

– CASE STUDY• OPERATIONS ON SETS

• 10.6 A CLASSIC CASE STUDY IN RECURSION– TOWERS OF HANOI

• 10.7 COMMON PROGRAMMING ERRORS

Page 3: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-3

10.1 THE NATURE OF RECURSION

• Recursive function– Function that calls itself or that is part of a cycle in the

sequence of function calls• Simple case

– Problem case for which a straightforward solution is known

• Recursive solution characteristics– One or more simple cases of the problem have a

straightforward, nonrecursive solution– The other cases can be redefined in terms of problems

that are closer to the simple cases– By applying this redefinition process every time the

recursive function is called, eventually the problem is reduced entirely to simple cases, which are relatively easy to solve

Page 4: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-4

Recursive Algorithm

if this is a simple case

solve it

else

redefine the problem using recursion

Page 5: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-5

Figure 10.1 Splitting a Problem into Smaller Problems

Page 6: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-6

EXAMPLE 10.1

• Figure 10.2 implements multiplication as the recursive C function multiply that returns the product m x n of its two arguments.

• The body of function multiply implements the general form of a recursive algorithm.

• The simplest case is reached when the condition n==1 is true.

Page 7: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-7

Figure 10.2 Recursive Function multiply

Page 8: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-8

EXAMPLE 10.2

• Develop a function to count the number of times a particular character appears in a string.

• Figure 10.3 shows thought process that fits into our generic else clause.

Page 9: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-9

Figure 10.3 Thought Process of Recursive Algorithm Developer

Page 10: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-10

Figure 10.4 Recursive Function to Count a Character in a String

Page 11: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-11

10.2 TRACING A RECURSIVE FUNCTION

• Two types of tracing the execution of a recursive function– Tracing a recursive function that returns a value– Tracing a void function that is recursive

Page 12: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-12

Tracing a recursive function that returns a value

• Activation frame– Representation of one call to a function

• Figure 10.5 shows three calls to function multiply.

Page 13: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-13

Figure 10.5 Trace of Function multiply

Page 14: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-14

Tracing a void function that is recursive

• Function reverse_input_words in Fig.10.6 is a recursive module that takes n words of input and prints them in reverse order.

• Terminating condition– A condition that is true when a recursive

algorithm is processing a simple case

Page 15: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-15

Figure 10.6 Function reverse_input_words

Page 16: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-16

Figure 10.7 Trace of reverse_input_words(3) When the Words Entered are "bits" "and" "bytes"

Page 17: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-17

Figure 10.8 Sequence of Events for Trace of reverse_input_words(3)

Page 18: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-18

Parameter And Local Variable Stacks (1/5)

• C uses the stack data structure to keep track of the values of n and word at any given point.

After first call to reverse_input_words

n word

3 ?

Page 19: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-19

Parameter And Local Variable Stacks (2/5)

Before the second call to reverse_input_words

n word

3 bits

After second call to reverse_input_words

n word

3 bits2 ?

Page 20: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-20

Parameter And Local Variable Stacks (3/5)

Before the third call to reverse_input_words

n word

bits32 and

After third call to reverse_input_words

n word

3 bits2 and1 ?

Page 21: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-21

Parameter And Local Variable Stacks (4/5)

During this execution of the function, the word “bytes” is scanned and stored in word, and “bytes” is echo printed

immediately because n is 1 (a simple case)

n word

3 bits2 and1 bytes

The function return pops both stacksAfter first return

n word

3 bits2 and

Page 22: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-22

Parameter And Local Variable Stacks (5/5)

Because control is returned to a printf call, the value of word at the top of the stack is then displayed.

Another return occurs, poping the stacks again.After second return

n word

3 bits

Page 23: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-23

Implementation of Parameter Stacks in C

• System stack– Area of memory where parameters and local variables

are allocated when a function is called and deallocated when the function returns

• When and how to trace recursive functions– During algorithm development, it is best to trace a

specific case simply by trusting any recursive call to return a correct value based on the function purpose.

– Figure 10.9 shows a self-tracing version of function multiply as well as output generated by the call.

Page 24: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-24

Figure 10.9 Recursive Function multiply with Print Statements to Create Trace and Output from multiply(8, 3)

Page 25: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-25

Figure 10.9 Recursive Function multiply with Print Statements to Create Trace and Output from multiply(8, 3) (cont’d)

Page 26: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-26

10.3 RECURSIVE MATHMETICAL FUNCTIONS

• Many mathmatical functions can be defined recursively.

• For example– The factorial of a number n (n!)

Page 27: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-27

Figure 10.10 Recursive factorial Function

Page 28: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-28

EXAMPLE 10.4

• Figure 10.11 shows the trace of fact=factorial(3)

• Figure 10.12 uses iterative version to solve the factorial of the number n

Page 29: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-29

Figure 10.11 Trace of fact = factorial(3);

Page 30: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-30

Figure 10.12 Iterative Function factorial

Page 31: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-31

EXAMPLE 10.5

• The Fibonacci numbers are a sequence of numbers that have many varied uses.

• The Fibonacci sequence is defined as– Fibonacci1 is1

– Fibonacci2 is 1

– Fibonaccin is Fibonaccin-2 +Fibonaccin-1, for n>2

Page 32: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-32

Figure 10.13 Recursive Function fibonacci

Page 33: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-33

EXAMPLE 10.6

• Euclid’s algorithm for finding the gcd can be defined recursively– gcd(m,n) is n if n divides m evently– gcd(m,n) is gcd(n, remainder of m divided by n)

otherwise

Page 34: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-34

Figure 10.14 Program Using Recursive Function gcd

Page 35: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-35

Figure 10.14 Program Using Recursive Function gcd (cont’d)

Page 36: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-36

10.4 RECURSIVE FUNCTIONS WITH ARRAY AND STRING PARAMETERS

CASE STUDY:FINDING CAPITAL LETTERS IN A STRING(1/4)

Step 1: Problem– Form a string containing all the capital letters

found in another string.

Page 37: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-37

CASE STUDY:FINDING CAPITAL LETTERS IN A

STRING(2/4)Step 2: Analysis

– Problem Input• char *str

– Problem Output• char *caps

Page 38: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-38

CASE STUDY:FINDING CAPITAL LETTERS IN A

STRING(3/4)Step 3: Design

– Algorithm1. if str is the empty string

2. Store empty string in caps

(a string with no letters certainly has no caps)

else

3. if initial letter of str is a capital letter

4. Store in caps this letter and the capital letters

from the rest of str

else

5. Store in caps the capital letters from the rest of str

Page 39: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-39

CASE STUDY:FINDING CAPITAL LETTERS IN A

STRING(4/4)Step 4: Implementation (Figure10.15)

Step 5: Testing (Figure 10.16 、 Figure 10.17)

Page 40: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-40

Figure 10.15 Recursive Function to Extract Capital Letters from a String

Page 41: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-41

Figure 10.16 Trace of Call to Recursive Function find_caps

Page 42: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-42

Figure 10.17 Sequence of Events for Trace of Call to find_caps from printf Statements

Page 43: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-43

CASE STUDYRECURSIVE SELECTION SORT(1/4)

Step 1: Problem– Sort an array in ascending order using a

selection sort.

Page 44: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-44

CASE STUDYRECURSIVE SELECTION SORT(2/4)

Step 2: Analysis (Figure 10.18)

Page 45: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-45

Figure 10.18 Trace of Selection Sort

Page 46: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-46

CASE STUDYRECURSIVE SELECTION SORT(3/4)

Step 3: Design– Recursive algorithm for selection sort

1. if n is 1

2. The array is sorted.

else

3. Place the largest array value in last array

element

4. Sort the subarray which excludes the last

array element (array[0]..array[n-2])

Page 47: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-47

CASE STUDYRECURSIVE SELECTION SORT(4/4)

Step 4: Implementation (Figure10.19)

Step 5: Testing

Page 48: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-48

Figure 10.19 Recursive Selection Sort

Page 49: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-49

Figure 10.19 Recursive Selection Sort (cont’d)

Page 50: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-50

10.5 PROBLEM SOLVING WITH RECURSIONCASE STUDY

OPERATIONS ON SETS(1/4)

Step 1: Problem– Develop a group of functions to perform the E

(is an element of), (is a subset of) and ∪(union) operations on sets of characters.

– Also develop functions to check that a certain set is valid (that is, that it contains no duplicate characters), to check for the empty set, and to print a set in standard set notation.

Set: any collection of distinct things considered as a wholeFrom wikipedia

Page 51: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-51

CASE STUDYOPERATIONS ON SETS(2/4)

Step 2: Analysis – Character strings provide a fairly natural representation

of sets of characters.– Like sets, strings can be of varying sizes and can be

empty.– If a character array that is to hold a set is declared to

have one more than the number of characters in the universal set (to allow room for the null character), then set operations should never produce a string that will overflow the array.

Page 52: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-52

CASE STUDYOPERATIONS ON SETS(3/4)

Step 3: Design– Algorithm for is_empty(set)

1. Is initial character ‘\0’ ?– Algorithm for is_element(ele, set)

1. if is_empty(set)2. Answer is false

else if initial character of set matches ele3. Answer is true

else4. Answer depends on whether ele is

in the rest of set

Page 53: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-53

Step 3: Design (cont’s)

– Algorithm for is_set(set)1. if is_empty(set)

2. Answer is trueelse if is_element(initial set character, rest of set)

3. Answer is falseelse

4. Answer depends on whether rest of set is valid set

– Algorithm for is_subset(sub, set)1. if is_empty(sub)

2. Answer is trueelse if initial character of sub is not an element of set

3. Answer is falseelse

4. Answer depends on whether rest of sub is a subset of set

Page 54: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-54

Step 3: Design (cont’s)

– Algorithm for union of set1 and set21. if is_empty(set1)

2. Result is set2

else if initial character of sset1 is also an element of set2

3. Result is union of the rest of set1 with set2

else

4. Result includes initial character of set1 and the

union of the rest of set1 with set2

Page 55: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-55

Step 3: Design (cont’s)

– Algorithm for print_set(set)1. Output a { .

2. if set is not empty, print elements seperated by commas.

3. Output a } .

– Algorithm for print_with_commas(set)1. If set has exactly one element

2. Print it

else3. Print initial element and a comma

4. print_with_commas the rest of set

Page 56: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-56

CASE STUDYOPERATIONS ON SETS(4/4)

Step 4: Implementation (Figure10.20)

Step 5: Testing

Page 57: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-57

Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings

Page 58: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-58

Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (cont’d)

Page 59: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-59

Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (cont’d)

Page 60: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-60

Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (cont’d)

Page 61: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-61

Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (cont’d)

Page 62: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-62

Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (cont’d)

Page 63: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-63

10.6 A CLASSIC CASE STUDY IN RECURSIONTOWERS OF HANOI (1/)

Step 1: Problem– Move n disks from peg A to peg C using peg B

as needed.– The following conditions apply

• 1. Only one disk at a time may be moved, and this disk must be the top disk on a peg

• 2. A larger disk can never be placed on top of a smaller disk

Page 64: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-64

TOWERS OF HANOI (2/)

Step 2: Analysis – Problem inputs

• int n• char from_peg• char to_peg• char aux_peg

– Problem output• A list of individual disk moves

Page 65: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-65

Figure 10.21 Towers of Hanoi

Page 66: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-66

Figure 10.22 Towers of Hanoi After Steps 1 and 2

Page 67: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-67

Figure 10.23 Towers of Hanoi After Steps 1, 2, 3.1, and 3.2

Page 68: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-68

TOWERS OF HANOI (3/)

Step 3: Design– Algorithm

1. if n is 1 then2. Move disk 1 from the from peg to the to peg

else3. Move n-1 disks from the from peg to the

auxiliary peg using the to peg4. Move disk n from the from peg to the to peg5. Move n-1 disks from the auxiliary peg to the

to peg using the from peg

Page 69: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-69

TOWERS OF HANOI (4/4)

Step 4: Implementation (Figure10.24)

Step 5: Testing

Page 70: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-70

Figure 10.24 Recursive Function tower

Page 71: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-71

Figure 10.25 Trace of tower ('A', 'C', 'B', 3);

Page 72: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-72

Figure 10.26 Output Generated by tower('A', 'C', 'B', 3);

Page 73: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-73

Comparison of Iterative and Recursive Functions

• Recursive– Requires more time and space because of extra

function calls– Is much easier to read and understand– To researchers developing solutions to the

complex problems that are at the frontiers of their research areas, the benefits gained from increased clarity far outweigh the extra cost in time and memory of running a recursive program

Page 74: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-74

10.7 COMMON PROGRAMMING ERRORS

• A recursive function may not be terminate properly.– A run-time error message noting stack overflow or an

access violation is an indicator that a recursive function is not terminating

• Be aware that it is critical that every path through a nonvoid function leads to a return statement

• The recopying of large arrays or other data structures can quickly consume all available memory

• Introduce a nonrecursive function to handle preliminaries and call the recursive function when there is error checking

Page 75: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-75

Chapter Review

• A recursive function either calls itself or initiates a sequence of function calls in which it may be called again

• Designing a recursive solution involves identifying simple cases that have straightforward solutions and then redefining more complex cases in terms of problems that are closer to simple cases

• Recursive functions depend on the fact that for each cal to a function, space is allocated on the stack for the function’s parameters and local variables

Page 76: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-76

Question?

Page 77: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-77

Programming Projects 1

• Develop a program to count pixels (picture elements) belonging to an object in a photograph.

• The data are in a two-dimensional grid of cells, each of which may be empty (value 0) or filled (value 1).

• The filled cells that are connected form a blob (an object).

• Figure 10.27 shows a grid with three blobs.• Include in your program a function blob_check that

takes as parameters the grid and the x-y coordinates of a cell and returns as its value the number of cells in the blob to which the indicated cell belongs.

Page 78: Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 10-78

Figure 10.27 Grid with Three Blobs