Top Banner
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.
24

1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

Dec 14, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

1CompSci 105 SS 2005

Principles of Computer Science

Lecture 6: Recursion

Lecturer: Santokh Singh

Assignment 1 due

tomorrow. Should

have started

working on it.

Page 2: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

2

Recursion:Compute using self-reference

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

Page 3: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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.”

Page 4: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 5: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

5

In Java

public static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

return n * fact( n-1 );

}

}Textbook, p. 53

Page 6: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

6

Tracing Recursive Programs

Recursion for void methods

The Towers of Hanoi

Page 7: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 8: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 9: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 10: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 11: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 12: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 13: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 14: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 15: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 16: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

16

Tracing Recursive Programs

Recursion for void methods

The Towers of Hanoi

Page 17: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

17

Reversing a String

Textbook, p. 59ff

god“ ”

Page 18: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

18

Reversing a String

Textbook, p. 59ff

god“ ”• If string is empty

do nothing

Page 19: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 20: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 21: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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

Page 22: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

22

Invariants

public static int fact( int n ) {

if ( n == 0 ) {

return 1;

}

else {

// Invariant:

return n * fact( n-1 );

}

}Textbook, p. 56

Page 23: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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++;

}

Page 24: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

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++;

}