cs61a lecture 9

Post on 27-Oct-2021

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS61A Lecture 9

Amir KamilUC Berkeley

February 11, 2013

HW3 due Tuesday at 7pm

Hog due today! Hog contest due later; see announcement tonight

Midterm Wednesday at 7pm See course website for assigned locations, more info

Midterm review in lab this week

Announcements

Factorial

The factorial of a non‐negative integer n is

Factorial

The factorial of a non‐negative integer n is

Factorial

The factorial of a non‐negative integer n is

Factorial

The factorial of a non‐negative integer n is

Factorial

The factorial of a non‐negative integer n is

Factorial

The factorial of a non‐negative integer n is

This is called a recurrence relation;

Factorial

The factorial of a non‐negative integer n is

This is called a recurrence relation;Factorial is defined in terms of itself

Factorial

The factorial of a non‐negative integer n is

This is called a recurrence relation;Factorial is defined in terms of itselfCan we write code to compute factorial using the same pattern?

Factorial

Computing Factorial

We can compute factorial using the direct definition

Computing Factorial

We can compute factorial using the direct definition

Computing Factorial

We can compute factorial using the direct definition

Computing Factorial

def factorial_iter(n):if n == 0 or n == 1:

return 1total = 1while n >= 1:

total, n = total * n, n - 1return total

Computing Factorial

Can we compute it using the recurrence relation?

Computing Factorial

Can we compute it using the recurrence relation?

Computing Factorial

Can we compute it using the recurrence relation?

Computing Factorial

def factorial(n):if n == 0 or n == 1:

return 1return n * factorial(n - 1)

Can we compute it using the recurrence relation?

Computing Factorial

def factorial(n):if n == 0 or n == 1:

return 1return n * factorial(n - 1)

Can we compute it using the recurrence relation?

This is much shorter! But can a function call itself?

Computing Factorial

def factorial(n):if n == 0 or n == 1:

return 1return n * factorial(n - 1)

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Compute 2!

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Compute 2!

Compute 1!

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Compute 2!

Compute 1!

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Compute 2!

Compute 1!

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Compute 2!

Compute 1!

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Compute 2!

Compute 1!

Recursive Functions

def factorial(n):if n == 0 or n == 1:

return 1return n * factorial(n - 1)

A function is recursive if the body calls the function itself, either directly or indirectly

Recursive Functions

def factorial(n):if n == 0 or n == 1:

return 1return n * factorial(n - 1)

A function is recursive if the body calls the function itself, either directly or indirectly

Recursive functions have two important components:ion itself, either directly or indirectly

Recursive Functions

def factorial(n):if n == 0 or n == 1:

return 1return n * factorial(n - 1)

A function is recursive if the body calls the function itself, either directly or indirectly

Recursive functions have two important components:1. Base case(s), where the function directly computes an 

answer without calling itself

Recursive Functions

def factorial(n):if n == 0 or n == 1:

return 1return n * factorial(n - 1)

A function is recursive if the body calls the function itself, either directly or indirectly

Recursive functions have two important components:1. Base case(s), where the function directly computes an 

answer without calling itself

Recursive Functions

def factorial(n):if n == 0 or n == 1:

return 1return n * factorial(n - 1)

Base case

A function is recursive if the body calls the function itself, either directly or indirectly

Recursive functions have two important components:1. Base case(s), where the function directly computes an 

answer without calling itself2. Recursive case(s), where the function calls itself as part of 

the computation

Recursive Functions

def factorial(n):if n == 0 or n == 1:

return 1return n * factorial(n - 1)

Base case

A function is recursive if the body calls the function itself, either directly or indirectly

Recursive functions have two important components:1. Base case(s), where the function directly computes an 

answer without calling itself2. Recursive case(s), where the function calls itself as part of 

the computation

Recursive Functions

def factorial(n):if n == 0 or n == 1:

return 1return n * factorial(n - 1)

Recursive case

Base case

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

Heavy Book

Heavy Book

Heavy Book

Heavy Book

adef lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

Heavy Book

Heavy Book

Heavy Book

a

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

Heavy Book

Heavy Book

a

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

Heavy Book

a

Heavy Book

Heavy Book

Heavy Book

def lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Bookdef lift_box(box):if too_heavy(box):

book = remove_book(box)lift_box(box)add_book(box, book)

else:move_box(box)

Recursion Example: Duplication

Recursion Example: Duplication

def duplicate(size):return (duplicate(0.6 * size) +

duplicate(0.6 * size))

Recursion Example: Duplication

def duplicate(size):return (duplicate(0.6 * size) +

duplicate(0.6 * size))

Recursion Example: Duplication

def duplicate(size):return (duplicate(0.6 * size) +

duplicate(0.6 * size))

Recursion Example: Duplication

def duplicate(size):return (duplicate(0.6 * size) +

duplicate(0.6 * size))

Recursion Example: Duplication

def duplicate(size):return (duplicate(0.6 * size) +

duplicate(0.6 * size))

Recursion Example: Duplication

def duplicate(size):return (duplicate(0.6 * size) +

duplicate(0.6 * size))

Recursion Example: Duplication

def duplicate(size):return (duplicate(0.6 * size) +

duplicate(0.6 * size))

Recursion Example: Duplication

No base case!

def duplicate(size):return (duplicate(0.6 * size) +

duplicate(0.6 * size))

Recursion Example: Duplication

Futurama Season 6, Episode 17 “Benderama”

© Twentieth Century Fox Film Corporation

No base case!

def duplicate(size):return (duplicate(0.6 * size) +

duplicate(0.6 * size))

Recursion Example: Dreaming

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global frame

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global frame dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global frame dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global framedream

dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global framedream level  1

dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global framedream level  1

dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global framedream level  1

dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global framedream

dream

level  1dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global framedream

dream

level  1

level  2

dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global framedream

dream

level  1

level  2

dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global framedream

dream

level  1

level  2

dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global framedream

dream

dream

level  1

level  2

dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global framedream

dream

dream

level  1

level  2

level  3

dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global framedream

dream

dream

level  1

level  2

level  3

dream func dream(level)

Recursion Example: Dreaming

def dream(level):if level == 3:return inception()

else:return dream(level + 1)

Global framedream

dream

dream

level  1

level  2

level  3

Inception© Warner Brothers Entertainment

dream func dream(level)

Reversing the Order of Recursive Calls

Some recursive computations may be done more easily by reversing the order of recursive calls.

Reversing the Order of Recursive Calls

Some recursive computations may be done more easily by reversing the order of recursive calls.A helper function helps us to do this.

Reversing the Order of Recursive Calls

Some recursive computations may be done more easily by reversing the order of recursive calls.A helper function helps us to do this.

Reversing the Order of Recursive Calls

Some recursive computations may be done more easily by reversing the order of recursive calls.A helper function helps us to do this.

Reversing the Order of Recursive Calls

def factorial2(n):return factorial_helper(n, 1)

def factorial_helper(n, k):if k >= n:

return kreturn k * factorial_helper(n, k + 1)

Reverse Environment Diagram

Example: http://goo.gl/6zz0z

Here is how the reversedcomputation evolves

Reverse Environment Diagram

Example: http://goo.gl/6zz0z

Here is how the reversedcomputation evolves

Reverse Environment Diagram

Example: http://goo.gl/6zz0z

Here is how the reversedcomputation evolves

Reverse Environment Diagram

Example: http://goo.gl/6zz0z

Here is how the reversedcomputation evolves

Reverse Environment Diagram

Example: http://goo.gl/6zz0z

Compute 3!

Here is how the reversedcomputation evolves

Reverse Environment Diagram

Example: http://goo.gl/6zz0z

Compute 3!

Compute 3*2*1

Here is how the reversedcomputation evolves

Reverse Environment Diagram

Example: http://goo.gl/6zz0z

Compute 3!

Compute 3*2*1

Compute 3*2

Here is how the reversedcomputation evolves

Reverse Environment Diagram

Example: http://goo.gl/6zz0z

Compute 3!

Compute 3*2*1

Compute 3*2

Compute 3

Here is how the reversedcomputation evolves

Reverse Environment Diagram

Example: http://goo.gl/6zz0z

Compute 3!

Compute 3*2*1

Compute 3*2

Compute 3

Here is how the reversedcomputation evolves

Reverse Environment Diagram

Example: http://goo.gl/6zz0z

Compute 3!

Compute 3*2*1

Compute 3*2

Compute 3

Here is how the reversedcomputation evolves

Reverse Environment Diagram

Example: http://goo.gl/6zz0z

Compute 3!

Compute 3*2*1

Compute 3*2

Compute 3

Here is how the reversedcomputation evolves

Reverse Environment Diagram

Example: http://goo.gl/6zz0z

Compute 3!

Compute 3*2*1

Compute 3*2

Compute 3

Fibonacci Sequence

Example: http://goo.gl/9UJxG

The Fibonacci sequence is defined as

Fibonacci Sequence

Example: http://goo.gl/9UJxG

The Fibonacci sequence is defined as

Fibonacci Sequence

Example: http://goo.gl/9UJxG

The Fibonacci sequence is defined as

Fibonacci Sequence

def fib_iter(n):if n == 0:

return 0fib_n, fib_n_1 = 1, 0k = 1while k < n:

fib_n, fib_n_1 = fib_n_1 + fib_n, fib_nk += 1

return fib_nExample: http://goo.gl/9UJxG

The Fibonacci sequence is defined as

Fibonacci Sequence

Example: http://goo.gl/DZbRG

The Fibonacci sequence is defined as

Fibonacci Sequence

def fib(n):if n == 0:

return 0elif n == 1:

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

Example: http://goo.gl/DZbRG

The Fibonacci sequence is defined as

Fibonacci Sequence

def fib(n):if n == 0:

return 0elif n == 1:

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

Example: http://goo.gl/DZbRG

The Fibonacci sequence is defined as

Fibonacci Sequence

def fib(n):if n == 0:

return 0elif n == 1:

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

Two recursive calls!

Example: http://goo.gl/DZbRG

Tree recursion

Executing the body of a function may entail more than one recursive call to that function

Tree recursion

Executing the body of a function may entail more than one recursive call to that functionThis is  called tree recursion

Tree recursion

Executing the body of a function may entail more than one recursive call to that functionThis is  called tree recursion

Tree recursion

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0)fib(1)

01

fib(2)

fib(0)fib(1)

01

fib(3)

fib(1)

1

fib(2)

fib(0)fib(1)

01

top related