+ All Categories
Home > Documents > 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh...

1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh...

Date post: 14-Dec-2015
Category:
Upload: reginald-isaac-daniels
View: 215 times
Download: 0 times
Share this document with a friend
24
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignme nt 1 due tomorrow . Should have started working on it.
Transcript

1CompSci 105 SS 2005

Principles of Computer Science

Lecture 6: Recursion

Lecturer: Santokh Singh

Assignment 1 due

tomorrow. Should

have started

working on it.

2

Recursion:Compute using self-reference

Iteration: Compute using loops (for, while, etc.)

3

Definition: “A phone directory is a list of names and telephone numbers in alphabetical order by surname”

Searching a Phone Directory

“It’s easy if there’s only one name in the book!”

“But it’s hard if the book is any larger.”

4

Factorial

Factorial(n) = 1 x 2 x 3 x … n

Factorial(0) = 1

Iterative Definition

Factorial(n) = n * Factorial(n-1)

Factorial(0) = 1

Recursive Definition

Textbook, pp. 51-52

5

In Java

public static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

return n * fact( n-1 );

}

}Textbook, p. 53

6

Tracing Recursive Programs

Recursion for void methods

The Towers of Hanoi

7public static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

return n * fact( n-1 );

}

}

Textbook, p. 57-58

n = ?A: fact(n-1) = ?return = ?

A

For each method call, make a box with space for:

Values of method’s parameters and local variables

Return values of all called methods

Return value for this method

Box Tracing

8public static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

return n * fact( n-1 );

}

}

Textbook, p. 57-58

n = 3A: fact(n-1) = ?return = ?

A

9public static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

return n * fact( n-1 );

}

}

Textbook, p. 57-58

n = 3A: fact(n-1) = ?return = ?

n = 2A: fact(n-1) = ?return = ?

AA

10public static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

return n * fact( n-1 );

}

}

Textbook, p. 57-58

n = 1A: fact(n-1) = ?return = ?

n = 3A: fact(n-1) = ?return = ?

n = 2A: fact(n-1) = ?return = ?

A

A

A

11public static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

return n * fact( n-1 );

}

}

Textbook, p. 57-58

n = 0 A: fact(n-1) = ?return = ?

n = 1A: fact(n-1) = ?return = ?

n = 3A: fact(n-1) = ?return = ?

n = 2A: fact(n-1) = ?return = ?

A

A

A

A

12public static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

return n * fact( n-1 );

}

}

Textbook, p. 57-58

n = 0 A: fact(n-1) = ?return = 1

n = 1A: fact(n-1) = 1return = ?

n = 3A: fact(n-1) = ?return = ?

n = 2A: fact(n-1) = ?return = ?

A

A

A

A

13public static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

return n * fact( n-1 );

}

}

Textbook, p. 57-58

n = 0 A: fact(n-1) = ?return = 1

n = 1A: fact(n-1) = 1return = 1

n = 3A: fact(n-1) = ?return = ?

n = 2A: fact(n-1) = 1return = ?

A

A

A

A

14public static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

return n * fact( n-1 );

}

}

Textbook, p. 57-58

n = 0 A: fact(n-1) = ?return = 1

n = 1A: fact(n-1) = 1return = 1

n = 3A: fact(n-1) = 2return = ?

n = 2A: fact(n-1) = 1return = 2

A

A

A

A

15

Proving recursive algorithms

1. Prove each base cases works

2. Prove each recursive case works*

3. Prove all recursive calls meet a base casepublic static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

return n * fact( n-1 );

}

} Proof by inductionTextbook, p. 751-5

16

Tracing Recursive Programs

Recursion for void methods

The Towers of Hanoi

17

Reversing a String

Textbook, p. 59ff

god“ ”

18

Reversing a String

Textbook, p. 59ff

god“ ”• If string is empty

do nothing

19

Reversing a String

Textbook, p. 59ff

god“ ”• If string is empty

do nothing• Otherwise,

Output last character

Reverse substring of first (n-1) characters

20

Reversing a String

Formal Box TraceTextbook, p. 62

godreverse “ ”• If string is empty

do nothing• Otherwise,

Output last character

Reverse substring of first (n-1) characters

21

More than one way ….

Textbook, p. 63ff

• If string is empty

do nothing• Otherwise,

Reverse substring of last (n-1) characters

Output first character

22

Invariants

public static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

// Invariant:

return n * fact( n-1 );

}

}Textbook, p. 56

23

Loop invariants

Is there something that we want to be true every time the while test is executed?

// Computes the sum of

// the first n items.

int sum = 0;

int j = 0;

while (j < n) {

sum += item[j];

j++;

}

24

Loop invariants

sum contains the sum of the first j items

j is in the range 0..n-1

Textbook, p. 12

// Computes the sum of

// the first n items.

int sum = 0;

int j = 0;

while (j < n) {

sum += item[j];

j++;

}


Recommended