+ All Categories
Home > Documents > 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin...

61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin...

Date post: 15-Jul-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
125
61A Lecture 7 Wednesday, February 4
Transcript
Page 1: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

61A Lecture 7

Wednesday, February 4

Page 2: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Announcements

2

Page 3: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Announcements

• Project 1 is due Thursday 2/5 @ 11:59pm; Early bonus point for submitting on Wednesday!

2

Page 4: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Announcements

• Project 1 is due Thursday 2/5 @ 11:59pm; Early bonus point for submitting on Wednesday!

§Extra tutor office hours on Wednesday 2/4 (See Piazza for details)

2

Page 5: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Announcements

• Project 1 is due Thursday 2/5 @ 11:59pm; Early bonus point for submitting on Wednesday!

§Extra tutor office hours on Wednesday 2/4 (See Piazza for details)

• Midterm 1 is on Monday 2/9 from 7pm to 9pm!

2

Page 6: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Announcements

• Project 1 is due Thursday 2/5 @ 11:59pm; Early bonus point for submitting on Wednesday!

§Extra tutor office hours on Wednesday 2/4 (See Piazza for details)

• Midterm 1 is on Monday 2/9 from 7pm to 9pm!

§Review session on Saturday 2/7

2

Page 7: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Announcements

• Project 1 is due Thursday 2/5 @ 11:59pm; Early bonus point for submitting on Wednesday!

§Extra tutor office hours on Wednesday 2/4 (See Piazza for details)

• Midterm 1 is on Monday 2/9 from 7pm to 9pm!

§Review session on Saturday 2/7

§HKN review session on Sunday 2/8

2

Page 8: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Announcements

• Project 1 is due Thursday 2/5 @ 11:59pm; Early bonus point for submitting on Wednesday!

§Extra tutor office hours on Wednesday 2/4 (See Piazza for details)

• Midterm 1 is on Monday 2/9 from 7pm to 9pm!

§Review session on Saturday 2/7

§HKN review session on Sunday 2/8

§Includes topics up to and including this lecture

2

Page 9: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Announcements

• Project 1 is due Thursday 2/5 @ 11:59pm; Early bonus point for submitting on Wednesday!

§Extra tutor office hours on Wednesday 2/4 (See Piazza for details)

• Midterm 1 is on Monday 2/9 from 7pm to 9pm!

§Review session on Saturday 2/7

§HKN review session on Sunday 2/8

§Includes topics up to and including this lecture

§Closed book/note exam, except for one page (2 sides) of hand-written notes & study guide

2

Page 10: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Announcements

• Project 1 is due Thursday 2/5 @ 11:59pm; Early bonus point for submitting on Wednesday!

§Extra tutor office hours on Wednesday 2/4 (See Piazza for details)

• Midterm 1 is on Monday 2/9 from 7pm to 9pm!

§Review session on Saturday 2/7

§HKN review session on Sunday 2/8

§Includes topics up to and including this lecture

§Closed book/note exam, except for one page (2 sides) of hand-written notes & study guide

§Cannot attend? Fill out the conflict form by Wednesday 2/4! http://goo.gl/2P5fKq

2

Page 11: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Announcements

• Project 1 is due Thursday 2/5 @ 11:59pm; Early bonus point for submitting on Wednesday!

§Extra tutor office hours on Wednesday 2/4 (See Piazza for details)

• Midterm 1 is on Monday 2/9 from 7pm to 9pm!

§Review session on Saturday 2/7

§HKN review session on Sunday 2/8

§Includes topics up to and including this lecture

§Closed book/note exam, except for one page (2 sides) of hand-written notes & study guide

§Cannot attend? Fill out the conflict form by Wednesday 2/4! http://goo.gl/2P5fKq

• Optional Hog strategy contest ends Wednesday 2/18 @ 11:59pm

2

Page 12: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Hog Contest Rules

3

Page 13: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Hog Contest Rules

•Up to two people submit one entry; Max of one entry per person

3

Page 14: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Hog Contest Rules

•Up to two people submit one entry; Max of one entry per person

•Your score is the number of entries against which you win more than 50% of the time

3

Page 15: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Hog Contest Rules

•Up to two people submit one entry; Max of one entry per person

•Your score is the number of entries against which you win more than 50% of the time

•All strategies must be deterministic, pure functions of the current player scores

3

Page 16: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Hog Contest Rules

•Up to two people submit one entry; Max of one entry per person

•Your score is the number of entries against which you win more than 50% of the time

•All strategies must be deterministic, pure functions of the current player scores

•All winning entries will receive 2 points of extra credit

3

Page 17: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Hog Contest Rules

•Up to two people submit one entry; Max of one entry per person

•Your score is the number of entries against which you win more than 50% of the time

•All strategies must be deterministic, pure functions of the current player scores

•All winning entries will receive 2 points of extra credit

•The real prize: honor and glory

3

Page 18: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Hog Contest Rules

•Up to two people submit one entry; Max of one entry per person

•Your score is the number of entries against which you win more than 50% of the time

•All strategies must be deterministic, pure functions of the current player scores

•All winning entries will receive 2 points of extra credit

•The real prize: honor and glory

Kaylee MannYan Duan & Ziming LiBrian Prike & Zhenghao QianParker Schuh & Robert Chatham

Fall 2011 Winners

3

Page 19: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Hog Contest Rules

•Up to two people submit one entry; Max of one entry per person

•Your score is the number of entries against which you win more than 50% of the time

•All strategies must be deterministic, pure functions of the current player scores

•All winning entries will receive 2 points of extra credit

•The real prize: honor and glory

Kaylee MannYan Duan & Ziming LiBrian Prike & Zhenghao QianParker Schuh & Robert Chatham

Fall 2011 Winners

3

Chenyang Yuan Joseph Hui

Fall 2012 Winners

Page 20: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Hog Contest Rules

•Up to two people submit one entry; Max of one entry per person

•Your score is the number of entries against which you win more than 50% of the time

•All strategies must be deterministic, pure functions of the current player scores

•All winning entries will receive 2 points of extra credit

•The real prize: honor and glory

Kaylee MannYan Duan & Ziming LiBrian Prike & Zhenghao QianParker Schuh & Robert Chatham

Fall 2011 Winners

3

Chenyang Yuan Joseph Hui

Fall 2012 Winners

Paul Bramsen Sam Kumar & Kangsik Lee Kevin Chen

Fall 2013 Winners

Page 21: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Hog Contest Rules

•Up to two people submit one entry; Max of one entry per person

•Your score is the number of entries against which you win more than 50% of the time

•All strategies must be deterministic, pure functions of the current player scores

•All winning entries will receive 2 points of extra credit

•The real prize: honor and glory

Kaylee MannYan Duan & Ziming LiBrian Prike & Zhenghao QianParker Schuh & Robert Chatham

Fall 2011 Winners

3

Chenyang Yuan Joseph Hui

Fall 2012 Winners

Paul Bramsen Sam Kumar & Kangsik Lee Kevin Chen

Fall 2013 Winners

Alan Tong & Elaine Zhao Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen Zizheng Tai & Yihe Li

Fall 2014 Winners

Page 22: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Hog Contest Rules

•Up to two people submit one entry; Max of one entry per person

•Your score is the number of entries against which you win more than 50% of the time

•All strategies must be deterministic, pure functions of the current player scores

•All winning entries will receive 2 points of extra credit

•The real prize: honor and glory

Kaylee MannYan Duan & Ziming LiBrian Prike & Zhenghao QianParker Schuh & Robert Chatham

Fall 2011 Winners

3

Chenyang Yuan Joseph Hui

Fall 2012 Winners

Paul Bramsen Sam Kumar & Kangsik Lee Kevin Chen

Fall 2013 Winners

Alan Tong & Elaine Zhao Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen Zizheng Tai & Yihe Li

Fall 2014 Winners

YOUR NAME COULD BE HERE... FOREVER!

Spring 2015 Winners

Page 23: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Order of Recursive Calls

Page 24: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

The Cascade Function

(Demo)

5Interactive Diagram

Page 25: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

The Cascade Function

(Demo)

5Interactive Diagram

Page 26: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

The Cascade Function

(Demo)

5Interactive Diagram

Page 27: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

The Cascade Function

• Each cascade frame is from a different call to cascade.

(Demo)

5Interactive Diagram

Page 28: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

The Cascade Function

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

(Demo)

5Interactive Diagram

Page 29: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

The Cascade Function

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(Demo)

5Interactive Diagram

Page 30: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

The Cascade Function

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(Demo)

5Interactive Diagram

Page 31: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

The Cascade Function

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(Demo)

5Interactive Diagram

Page 32: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

The Cascade Function

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(Demo)

5Interactive Diagram

Page 33: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

The Cascade Function

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(Demo)

5Interactive Diagram

Page 34: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Two Definitions of Cascade

6

(Demo)

Page 35: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Two Definitions of Cascade

6

def cascade(n): if n < 10: print(n) else: print(n) cascade(n//10) print(n)

def cascade(n): print(n) if n >= 10: cascade(n//10) print(n)

(Demo)

Page 36: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Two Definitions of Cascade

6

def cascade(n): if n < 10: print(n) else: print(n) cascade(n//10) print(n)

def cascade(n): print(n) if n >= 10: cascade(n//10) print(n)

(Demo)

• If two implementations are equally clear, then shorter is usually better

Page 37: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Two Definitions of Cascade

6

def cascade(n): if n < 10: print(n) else: print(n) cascade(n//10) print(n)

def cascade(n): print(n) if n >= 10: cascade(n//10) print(n)

(Demo)

• If two implementations are equally clear, then shorter is usually better

• In this case, the longer implementation is more clear (at least to me)

Page 38: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Two Definitions of Cascade

6

def cascade(n): if n < 10: print(n) else: print(n) cascade(n//10) print(n)

def cascade(n): print(n) if n >= 10: cascade(n//10) print(n)

(Demo)

• If two implementations are equally clear, then shorter is usually better

• In this case, the longer implementation is more clear (at least to me)

• When learning to write recursive functions, put the base cases first

Page 39: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Two Definitions of Cascade

6

def cascade(n): if n < 10: print(n) else: print(n) cascade(n//10) print(n)

def cascade(n): print(n) if n >= 10: cascade(n//10) print(n)

(Demo)

• If two implementations are equally clear, then shorter is usually better

• In this case, the longer implementation is more clear (at least to me)

• When learning to write recursive functions, put the base cases first

• Both are recursive functions, even though only the first has typical structure

Page 40: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Example: Inverse Cascade

Page 41: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Inverse Cascade

Write a function that prints an inverse cascade:

8

Page 42: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

1 12 123123412312 1

Inverse Cascade

Write a function that prints an inverse cascade:

8

Page 43: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

1 12 123123412312 1

Inverse Cascade

Write a function that prints an inverse cascade:

8

1 12 123123412312 1

def inverse_cascade(n): grow(n) print(n) shrink(n)

Page 44: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

1 12 123123412312 1

Inverse Cascade

Write a function that prints an inverse cascade:

8

def f_then_g(f, g, n): if n: f(n) g(n)

1 12 123123412312 1

def inverse_cascade(n): grow(n) print(n) shrink(n)

Page 45: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

1 12 123123412312 1

Inverse Cascade

Write a function that prints an inverse cascade:

8

grow = lambda n: f_then_g(grow, print, n//10) shrink = lambda n: f_then_g(print, shrink, n//10)

def f_then_g(f, g, n): if n: f(n) g(n)

1 12 123123412312 1

def inverse_cascade(n): grow(n) print(n) shrink(n)

Page 46: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

1 12 123123412312 1

Inverse Cascade

Write a function that prints an inverse cascade:

8

grow = lambda n: f_then_g(grow, print, n//10) shrink = lambda n: f_then_g(print, shrink, n//10)

def f_then_g(f, g, n): if n: f(n) g(n)

1 12 123123412312 1

def inverse_cascade(n): grow(n) print(n) shrink(n)

Page 47: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

Page 48: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 49: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 50: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 51: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 52: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

... , 35

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 53: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 54: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

def fib(n):

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 55: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

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

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 56: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

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

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 57: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

def fib(n): if n == 0: return 0 elif n == 1:

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 58: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

def fib(n): if n == 0: return 0 elif n == 1: return 1

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 59: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

def fib(n): if n == 0: return 0 elif n == 1: return 1 else:

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 60: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

10

Page 61: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

Page 62: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

Page 63: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(3)

Page 64: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)fib(3)

Page 65: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 66: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 67: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 68: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 69: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 70: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 71: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 72: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 73: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 74: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 75: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 76: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 77: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 78: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 79: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 80: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 81: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 82: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 83: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

11

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

(Demo)

Page 84: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Repetition in Tree-Recursive Computation

12

Page 85: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Repetition in Tree-Recursive Computation

This process is highly repetitive; fib is called on the same argument multiple times

12

Page 86: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Repetition in Tree-Recursive Computation

fib(5)

fib(3)

fib(1)

1

fib(4)

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

This process is highly repetitive; fib is called on the same argument multiple times

12

Page 87: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Repetition in Tree-Recursive Computation

fib(5)

fib(3)

fib(1)

1

fib(4)

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

This process is highly repetitive; fib is called on the same argument multiple times

12

(We can speed up this computation dramatically in a few weeks by remembering results)

Page 88: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Example: Counting Partitions

Page 89: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

14

Page 90: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

14

count_partitions(6, 4)

Page 91: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

14

count_partitions(6, 4)

3 + 3 = 6

1 + 1 + 2 + 2 = 6

2 + 4 = 6

1 + 1 + 4 = 6

1 + 2 + 3 = 61 + 1 + 1 + 3 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 61 + 1 + 1 + 1 + 1 + 1 = 6

Page 92: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

14

count_partitions(6, 4)

3 + 3 = 6

1 + 1 + 2 + 2 = 6

2 + 4 = 6

1 + 1 + 4 = 6

1 + 2 + 3 = 61 + 1 + 1 + 3 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 61 + 1 + 1 + 1 + 1 + 1 = 6

Page 93: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

14

count_partitions(6, 4)

3 + 3 = 6

1 + 1 + 2 + 2 = 6

2 + 4 = 6

1 + 1 + 4 = 6

1 + 2 + 3 = 61 + 1 + 1 + 3 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 61 + 1 + 1 + 1 + 1 + 1 = 6

Page 94: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

14

count_partitions(6, 4)

3 + 3 = 6

1 + 1 + 2 + 2 = 6

2 + 4 = 6

1 + 1 + 4 = 6

1 + 2 + 3 = 61 + 1 + 1 + 3 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 61 + 1 + 1 + 1 + 1 + 1 = 6

Page 95: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

count_partitions(6, 4)

Page 96: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

count_partitions(6, 4)

Page 97: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

count_partitions(6, 4)

Page 98: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

count_partitions(6, 4)

Page 99: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

count_partitions(6, 4)

Page 100: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

count_partitions(6, 4)

Page 101: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

count_partitions(6, 4)

Page 102: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

count_partitions(6, 4)

Page 103: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

count_partitions(6, 4)

Page 104: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

count_partitions(6, 4)

Page 105: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

count_partitions(6, 4)

Page 106: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

count_partitions(6, 4)

Page 107: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

count_partitions(6, 4)

Page 108: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

count_partitions(6, 4)

Page 109: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

count_partitions(6, 4)

Page 110: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

15

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

count_partitions(6, 4)

Page 111: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

Page 112: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

Page 113: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

else:

Page 114: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

else: with_m = count_partitions(n-m, m)

Page 115: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1)

Page 116: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 117: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 118: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 119: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0:

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 120: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0: return 1

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 121: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0: return 1 elif n < 0:

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 122: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0: return 1 elif n < 0: return 0

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 123: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0: return 1 elif n < 0: return 0 elif m == 0:

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 124: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0: return 1 elif n < 0: return 0 elif m == 0: return 0

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 125: 61A Lecture 7 - University of California, Berkeleycs61a/sp15/assets/...Sam Kumar & Kangsik Lee Kevin Chen ... Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

16

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0: return 1 elif n < 0: return 0 elif m == 0: return 0

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

(Demo)

Interactive Diagram


Recommended