cs61a lecture 9

118
CS61A Lecture 9 Amir Kamil UC Berkeley February 11, 2013

Upload: others

Post on 27-Oct-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS61A Lecture 9

CS61A Lecture 9

Amir KamilUC Berkeley

February 11, 2013

Page 2: CS61A Lecture 9

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

Page 3: CS61A Lecture 9

Factorial

Page 4: CS61A Lecture 9

The factorial of a non‐negative integer n is

Factorial

Page 5: CS61A Lecture 9

The factorial of a non‐negative integer n is

Factorial

Page 6: CS61A Lecture 9

The factorial of a non‐negative integer n is

Factorial

Page 7: CS61A Lecture 9

The factorial of a non‐negative integer n is

Factorial

Page 8: CS61A Lecture 9

The factorial of a non‐negative integer n is

Factorial

Page 9: CS61A Lecture 9

The factorial of a non‐negative integer n is

This is called a recurrence relation;

Factorial

Page 10: CS61A Lecture 9

The factorial of a non‐negative integer n is

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

Factorial

Page 11: CS61A Lecture 9

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

Page 12: CS61A Lecture 9

Computing Factorial

Page 13: CS61A Lecture 9

We can compute factorial using the direct definition

Computing Factorial

Page 14: CS61A Lecture 9

We can compute factorial using the direct definition

Computing Factorial

Page 15: CS61A Lecture 9

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

Page 16: CS61A Lecture 9

Computing Factorial

Page 17: CS61A Lecture 9

Can we compute it using the recurrence relation?

Computing Factorial

Page 18: CS61A Lecture 9

Can we compute it using the recurrence relation?

Computing Factorial

Page 19: CS61A Lecture 9

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)

Page 20: CS61A Lecture 9

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)

Page 21: CS61A Lecture 9

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)

Page 22: CS61A Lecture 9

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Page 23: CS61A Lecture 9

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Page 24: CS61A Lecture 9

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Page 25: CS61A Lecture 9

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Page 26: CS61A Lecture 9

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Page 27: CS61A Lecture 9

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Page 28: CS61A Lecture 9

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Compute 2!

Page 29: CS61A Lecture 9

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Compute 2!

Compute 1!

Page 30: CS61A Lecture 9

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Compute 2!

Compute 1!

Page 31: CS61A Lecture 9

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Compute 2!

Compute 1!

Page 32: CS61A Lecture 9

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Compute 2!

Compute 1!

Page 33: CS61A Lecture 9

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

Compute 3!

Compute 2!

Compute 1!

Page 34: CS61A Lecture 9

Recursive Functions

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

return 1return n * factorial(n - 1)

Page 35: CS61A Lecture 9

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)

Page 36: CS61A Lecture 9

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)

Page 37: CS61A Lecture 9

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)

Page 38: CS61A Lecture 9

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

Page 39: CS61A Lecture 9

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

Page 40: CS61A Lecture 9

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

Page 41: CS61A Lecture 9

Recursion Example: Heavy Box

a

Heavy Book

Heavy Book

Heavy Book

Heavy Book

Page 42: CS61A Lecture 9

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)

Page 43: CS61A Lecture 9

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)

Page 44: CS61A Lecture 9

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)

Page 45: CS61A Lecture 9

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)

Page 46: CS61A Lecture 9

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)

Page 47: CS61A Lecture 9

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)

Page 48: CS61A Lecture 9

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)

Page 49: CS61A Lecture 9

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)

Page 50: CS61A Lecture 9

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)

Page 51: CS61A Lecture 9

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)

Page 52: CS61A Lecture 9

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)

Page 53: CS61A Lecture 9

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)

Page 54: CS61A Lecture 9

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)

Page 55: CS61A Lecture 9

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)

Page 56: CS61A Lecture 9

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)

Page 57: CS61A Lecture 9

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)

Page 58: CS61A Lecture 9

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)

Page 59: CS61A Lecture 9

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)

Page 60: CS61A Lecture 9

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)

Page 61: CS61A Lecture 9

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)

Page 62: CS61A Lecture 9

Recursion Example: Duplication

Page 63: CS61A Lecture 9

Recursion Example: Duplication

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

duplicate(0.6 * size))

Page 64: CS61A Lecture 9

Recursion Example: Duplication

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

duplicate(0.6 * size))

Page 65: CS61A Lecture 9

Recursion Example: Duplication

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

duplicate(0.6 * size))

Page 66: CS61A Lecture 9

Recursion Example: Duplication

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

duplicate(0.6 * size))

Page 67: CS61A Lecture 9

Recursion Example: Duplication

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

duplicate(0.6 * size))

Page 68: CS61A Lecture 9

Recursion Example: Duplication

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

duplicate(0.6 * size))

Page 69: CS61A Lecture 9

Recursion Example: Duplication

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

duplicate(0.6 * size))

Page 70: CS61A Lecture 9

Recursion Example: Duplication

No base case!

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

duplicate(0.6 * size))

Page 71: CS61A Lecture 9

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))

Page 72: CS61A Lecture 9

Recursion Example: Dreaming

Page 73: CS61A Lecture 9

Recursion Example: Dreaming

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

else:return dream(level + 1)

Page 74: CS61A Lecture 9

Recursion Example: Dreaming

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

else:return dream(level + 1)

Page 75: CS61A Lecture 9

Recursion Example: Dreaming

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

else:return dream(level + 1)

Global frame

Page 76: CS61A Lecture 9

Recursion Example: Dreaming

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

else:return dream(level + 1)

Global frame dream func dream(level)

Page 77: CS61A Lecture 9

Recursion Example: Dreaming

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

else:return dream(level + 1)

Global frame dream func dream(level)

Page 78: CS61A Lecture 9

Recursion Example: Dreaming

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

else:return dream(level + 1)

Global framedream

dream func dream(level)

Page 79: CS61A Lecture 9

Recursion Example: Dreaming

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

else:return dream(level + 1)

Global framedream level  1

dream func dream(level)

Page 80: CS61A Lecture 9

Recursion Example: Dreaming

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

else:return dream(level + 1)

Global framedream level  1

dream func dream(level)

Page 81: CS61A Lecture 9

Recursion Example: Dreaming

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

else:return dream(level + 1)

Global framedream level  1

dream func dream(level)

Page 82: CS61A Lecture 9

Recursion Example: Dreaming

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

else:return dream(level + 1)

Global framedream

dream

level  1dream func dream(level)

Page 83: CS61A Lecture 9

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)

Page 84: CS61A Lecture 9

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)

Page 85: CS61A Lecture 9

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)

Page 86: CS61A Lecture 9

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)

Page 87: CS61A Lecture 9

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)

Page 88: CS61A Lecture 9

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)

Page 89: CS61A Lecture 9

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)

Page 90: CS61A Lecture 9

Reversing the Order of Recursive Calls

Page 91: CS61A Lecture 9

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

Reversing the Order of Recursive Calls

Page 92: CS61A Lecture 9

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

Page 93: CS61A Lecture 9

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

Page 94: CS61A Lecture 9

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)

Page 95: CS61A Lecture 9

Reverse Environment Diagram

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

Page 96: CS61A Lecture 9

Here is how the reversedcomputation evolves

Reverse Environment Diagram

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

Page 97: CS61A Lecture 9

Here is how the reversedcomputation evolves

Reverse Environment Diagram

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

Page 98: CS61A Lecture 9

Here is how the reversedcomputation evolves

Reverse Environment Diagram

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

Page 99: CS61A Lecture 9

Here is how the reversedcomputation evolves

Reverse Environment Diagram

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

Compute 3!

Page 100: CS61A Lecture 9

Here is how the reversedcomputation evolves

Reverse Environment Diagram

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

Compute 3!

Compute 3*2*1

Page 101: CS61A Lecture 9

Here is how the reversedcomputation evolves

Reverse Environment Diagram

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

Compute 3!

Compute 3*2*1

Compute 3*2

Page 102: CS61A Lecture 9

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

Page 103: CS61A Lecture 9

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

Page 104: CS61A Lecture 9

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

Page 105: CS61A Lecture 9

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

Page 106: CS61A Lecture 9

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

Page 107: CS61A Lecture 9

Fibonacci Sequence

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

Page 108: CS61A Lecture 9

The Fibonacci sequence is defined as

Fibonacci Sequence

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

Page 109: CS61A Lecture 9

The Fibonacci sequence is defined as

Fibonacci Sequence

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

Page 110: CS61A Lecture 9

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

Page 111: CS61A Lecture 9

The Fibonacci sequence is defined as

Fibonacci Sequence

Example: http://goo.gl/DZbRG

Page 112: CS61A Lecture 9

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

Page 113: CS61A Lecture 9

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

Page 114: CS61A Lecture 9

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

Page 115: CS61A Lecture 9

Tree recursion

Page 116: CS61A Lecture 9

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

Tree recursion

Page 117: CS61A Lecture 9

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

Tree recursion

Page 118: CS61A Lecture 9

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