29
Recursion Lecture 2 Sections 20.1 - 20.4 Robb T. Koether Hampden-Sydney College Wed, Jan 17, 2018 Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 1 / 18

Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

RecursionLecture 2

Sections 20.1 - 20.4

Robb T. Koether

Hampden-Sydney College

Wed, Jan 17, 2018

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 1 / 18

Page 2: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

1 Recursion

2 Advantages and Disadvantages

3 Examples

4 Assignment

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 2 / 18

Page 3: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Outline

1 Recursion

2 Advantages and Disadvantages

3 Examples

4 Assignment

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 3 / 18

Page 4: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Recursion

Definition (Recursive Function)A recursive function is a function that calls on itself.

A recursive function must contain a decision structure that dividesthe code into two cases.

The recursive (general) case.The nonrecursive (base) case.

The code must guarantee that eventually the nonrecursive case iscalled.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 4 / 18

Page 5: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

1st-Order Recursion

Example (The Factorial Function)int factorial(int n){

if (n == 0 || n == 1)return 1;

elsereturn n*factorial(n - 1);

}

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 5 / 18

Page 6: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

An Analogy

Level 1 Level 2 Level 3 Level 4

Each “little man” has exactly the same instructions.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 6 / 18

Page 7: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

An Analogy

Level 1 Level 2 Level 3 Level 4

f(4)

The first man is given the number 4.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 6 / 18

Page 8: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

An Analogy

Level 1 Level 2 Level 3 Level 4

f(3)4

4 6= 1, so he stores the 4 and pass 4− 1 = 3 to the next room.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 6 / 18

Page 9: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

An Analogy

Level 1 Level 2 Level 3 Level 4

4 3 f(2)

3 6= 1, so he stores the 3 and pass 3− 1 = 2 to the next room.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 6 / 18

Page 10: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

An Analogy

Level 1 Level 2 Level 3 Level 4

4 3 2 f(1)

2 6= 1, so he stores the 2 and pass 2− 1 = 1 to the next room.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 6 / 18

Page 11: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

An Analogy

Level 1 Level 2 Level 3 Level 4

4 3 2 1

1 = 1, so he returns 1 to the previous room.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 6 / 18

Page 12: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

An Analogy

Level 1 Level 2 Level 3 Level 4

4 3 2 * 1 = 2

He computes 2 ∗ 1 = 2 and...

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 6 / 18

Page 13: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

An Analogy

Level 1 Level 2 Level 3 Level 4

4 3 2

...returns 2 to the previous room.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 6 / 18

Page 14: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

An Analogy

Level 1 Level 2 Level 3 Level 4

4 3 * 2 = 6

He computes 3 ∗ 2 = 6 and...

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 6 / 18

Page 15: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

An Analogy

Level 1 Level 2 Level 3 Level 4

4 6

...returns 6 to the previous room.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 6 / 18

Page 16: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

An Analogy

Level 1 Level 2 Level 3 Level 4

4 * 6 = 24

He computes 4 ∗ 6 = 24 and...

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 6 / 18

Page 17: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

An Analogy

Level 1 Level 2 Level 3 Level 4

24

...returns 6 to the previous room.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 6 / 18

Page 18: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

2nd-Order Recursion

Example (The Fibonacci Sequence)int fibon(int n){

if (n == 0 || n == 1)return n;

elsereturn fibon(n - 1) + fibon(n - 2);

}

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 7 / 18

Page 19: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Greatest Common Divisor

Example (Greatest Common Divisor)int gcd(int a, int b){

if (b == 0)return a;

elsereturn gcd(b, a % b);

}

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 8 / 18

Page 20: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Binomial Coefficients

Example (Binomial Coefficients)int binom(int n, int r){

if (r == 0 || r == n)return 1;

elsereturn binom(n - 1, r) + binom(n - 1, r - 1);

}

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 9 / 18

Page 21: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Outline

1 Recursion

2 Advantages and Disadvantages

3 Examples

4 Assignment

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 10 / 18

Page 22: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Advantages of Recursion

AdvantagesThe code may be much easier to write.Some situations are naturally recursive.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 11 / 18

Page 23: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Advantages of Recursion

Naturally recursive data structures:Linked lists.Binary trees.

Naturally recursive problems:Traversing linked lists.Traversing binary trees.Evaluating expressions.Differentiating functions.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 12 / 18

Page 24: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Disadvantages of Recursion

DisadvantagesRecursive functions are generally slower than nonrecursivefunctions.Excessive recursion may overflow the run-time stack.One must be very careful when writing recursive functions; they canbe tricky.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 13 / 18

Page 25: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Disadvantages of Recursion

There is shallow recursion and there is deep recursion.Shallow recursion will not overflow the stack, but it may take anexcessively long time to execute.Deep recursion is generally much faster, but it may overflow thestack.Sometimes each function call generates two or more recursivecalls at that level.This has the potential to consume an enormous amount of time.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 14 / 18

Page 26: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Outline

1 Recursion

2 Advantages and Disadvantages

3 Examples

4 Assignment

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 15 / 18

Page 27: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Examples of Recursive Functions

GCD.cpp.BinomialCoefficients.cpp.TowersOfHanoi.cpp.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 16 / 18

Page 28: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Outline

1 Recursion

2 Advantages and Disadvantages

3 Examples

4 Assignment

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 17 / 18

Page 29: Recursion - Lecture 2 Sections 20.1 - 20people.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2018/L… · Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018

Assignment

AssignmentRead Sections 20.1 - 20.4.

Robb T. Koether (Hampden-Sydney College) Recursion Wed, Jan 17, 2018 18 / 18