Docsity

Lecture Slides on Recursion - Computer Science I/Lab | CMPU 101, Study notes of Computer Science

Toggle sidebar
bg1
Chapter 18
Recursion
CMPU-101: Problem-Solving and Abstraction
Marc Smith
Vassar College
Spring 2007
2
Mathematical Recursive Definitions
Factorial
0! = 1
n! = n · (n-1)!
one base case
one recursive step
Recursive functions are characterized by:
1. A base case: Value when recursion stops.
2. A recursive operation: The function calls
itself.
The recursive operation must bring the
value closer to the base case.
3
Fibonacci Numbers
fib(0) = 1
fib(1) = 1
fib(n) = fib(n-2) + fib(n-1)
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
two base cases
one recursive step
4
Recursive Methods
A recursive method is one that calls itself, either directly or
indirectly.
Like recursive functions, they have at least one base case
and at least one recursive expression.
5
Properties of Recursive Methods
1. The subproblems must have the same form as the
larger problem.
2. There must be one or more instances that can be
solved without recursion. These are known as the
base cases.
3. All recursive calls must make progress towards a
base case, so that the computation will halt.
6
Closed Forms
1)1(2)(
1)0(
+!"=
=
nSnS
S
...
15)3(
7)2(
3)1(
1)0(
=
=
=
=
S
S
S
S
12)( 1!=+n
nS
Recursively defined functions contain
“self-referential” expressions.
Closed forms are formulae that
require no recursive calls
pf3
pf4

Partial preview of the text

Download Lecture Slides on Recursion - Computer Science I/Lab | CMPU 101 and more Study notes Computer Science in PDF only on Docsity!

Chapter 18

Recursion

CMPU- 101 : Problem-Solving and Abstraction

Marc Smith

Vassar College

Spring 2007

2

Mathematical Recursive Definitions

Factorial

n! = n · (n-1)!

one base case

one recursive step

Recursive functions are characterized by:

  1. A base case: Value when recursion stops.
  2. A recursive operation: The function calls itself. The recursive operation must bring the value closer to the base case. 3

Fibonacci Numbers

fib(0) = 1

fib(1) = 1

fib(n) = fib(n-2) + fib(n-1)

two base cases

one recursive step

4

Recursive Methods

A recursive method is one that calls itself, either directly or

indirectly.

Like recursive functions, they have at least one base case

and at least one recursive expression.

Properties of Recursive Methods

1. The subproblems must have the same form as the

larger problem.

2. There must be one or more instances that can be

solved without recursion. These are known as the

base cases.

3. All recursive calls must make progress towards a

base case, so that the computation will halt.

Closed Forms

() 2 ( 1 ) 1 ( 0 ) 1 = "! + = Sn S n S ... ( 3 ) 15 ( 2 ) 7 ( 1 ) 3 ( 0 ) 1 = = = = S S S

S S ( n )= 2 n +^1! 1

Recursively defined functions contain “self-referential” expressions. Closed forms are formulae that require no recursive calls

7 Infinite Recursion — Prog 1801

private static void f() { f(); }

java.lang.StackOverFlowError

at Prog1801.f(Prog1801.java:17)

at Prog1801.f(Prog1801.java:17)

at Prog1801.f(Prog1801.java:17)

at Prog1801.f(Prog1801.java:17)

at Prog1801.f(Prog1801.java:17)

Stack overflow occurs when a program runs out of space to keep track of method calls. 8 Counting Stack Depth — Prog 1802

class Prog

private static int depth = 0;

private static void f()

depth++;

System.out.println(depth);

f();

9 Stack Overflow

Your Mileage May Vary. Run Prog 1802 on your computer system & note how far you get before observing a StackOverFlow error. 10 For each invocation of f

Java has to remember:

the parameters of this invocation, if any;

the return address — where to resume execution after

completing this particular invocation.

If f takes more parameters, the stack will overflow sooner.

Hand-Tracing

public static int a ( int x, int y )

if ( x == 0 ) return y; // base case

return 1 + a(x-1,y); // recursive call

a( 3 , 5 ) 1 + a( 2 , 5 ) 1 + 1 + a( 1 , 5 ) 1 + 1 + 1 + a( 0 , 5 ) 1 + 1 + 1 + 5 1 + 1 + 6 1 + 7 The recursion 8 “bottoms out” at the call a(0,5) What does method a compute? sumInts

public static int sumInts ( int x )

if ( x == 0 ) return 0; // base case

int total = sumInts(x-1); // recursive call

return total + x;

} sumInts( 4 )

sumInts( 3 ) + 4 (sumInts( 2 ) + 3 ) + 4 ((sumInts( 1 ) + 2 ) + 3 ) + 4 (((sumInts( 0 ) + 1 ) + 2 ) + 3 ) + 4 ((( 0 + 1 ) + 2 ) + 3 ) + 4

Recursion in the Past

Theory of computation

What problems can & can't be solved

Recursion in the Present

Compiling

Natural Language Processing (NLP)

Artificial Intelligence (AI)

Data structures — trees and linked lists

Computer graphics — fractals

Distributed & parallel processing