CS61A Lecture 6 Recursion Tom Magrino and Jon Kotker UC Berkeley EECS June 26, 2012.

Slides:



Advertisements
Similar presentations
CS61A Lecture 25 Delayed Sequences Jom Magrotker UC Berkeley EECS July 31, 2012.
Advertisements

CS61A Lecture 8 Data Abstraction Tom Magrino and Jon Kotker UC Berkeley EECS June 28, 2012.
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
R ECURSION. O VERVIEW Definition of Recursion Where recursion is used Recursion in Factorial (!n)
Kavita Math231 Recursion and Iteration. Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
CS61A Lecture 14 Object-Oriented Programming Jom Magrotker UC Berkeley EECS July 11, 2012.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
CS61A Lecture 3 Higher Order Functions Jon Kotker and Tom Magrino UC Berkeley EECS June 20, 2012.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Survey of Computer Science CSCI 110, Spring 2011 Lecture 14 Recursion.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that.
CS61A Lecture 21 Scheme Jom Magrotker UC Berkeley EECS July 24, 2012.
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
CS61A Lecture 22 Interpretation Jom Magrotker UC Berkeley EECS July 25, 2012.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
CS61A Lecture 13 Object-Oriented Programming Jom Magrotker UC Berkeley EECS July 10, 2012.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
CS61A Lecture 12 Immutable Trees Jom Magrotker UC Berkeley EECS July 9, 2012.
CS61A Lecture 23 Py Jom Magrotker UC Berkeley EECS July 26, 2012.
CS61A Lecture 27 Logic Programming Jom Magrotker UC Berkeley EECS August 2, 2012.
CS61A Lecture 20 Object-Oriented Programming: Implementation Jom Magrotker UC Berkeley EECS July 23, 2012.
CS61A Lecture 9 Immutable Data Structures Jom Magrotker UC Berkeley EECS July 2, 2012.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
R ECURRSION Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University of Hail Chapter.
CS61A Lecture 10 Immutable Data Structures Jom Magrotker UC Berkeley EECS July 3, 2012.
CS61A Lecture 11 Immutable Trees Jom Magrotker UC Berkeley EECS July 5, 2012.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Beginning Programming for Engineers Matlab Conditional Computation.
Algebraic Expressions Writing expressions. Using letter symbols for unknowns In algebra, we use letter symbols to stand for numbers. These letters are.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Applied Algorithms (Lecture 17) Recursion Fall-23
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Recursion UW CSE 160 Winter 2017
Recursion Spring 2015 UW CSE 160
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Functional Programming
Perform Function Operations and Composition
Conditional and iterative statements
Recursion UW CSE 160 Winter 2016
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion Taken from notes by Dr. Neil Moore
This Lecture Substitution model
Last Class We Covered Recursion Stacks Parts of a recursive function:
Recursion Recursio Recursi Recurs Recur Recu Rec Re R
Iteration – While Loops
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Recursion Examples.
Lecture 6 - Recursion.
Presentation transcript:

CS61A Lecture 6 Recursion Tom Magrino and Jon Kotker UC Berkeley EECS June 26, 2012

2 C OMPUTER S CIENCE IN THE N EWS

3 T ODAY Quick review of Iterative Improvement. Defining functions that call themselves. – Sometimes more than once.

4 R ECAP : N EWTON ’ S M ETHOD def iter_improve(update, isclose, guess=1): while not isclose(guess): guess = update(guess) return guess def find_root(f, initial_guess=10): return iter_improve(newton_update(f), lambda x: approx_zero(f(x)), initial_guess)

5 R ECAP : N EWTON ’ S M ETHOD

6 R ECAP : N EWTON ’ S M ETHOD I TERATIVE I MPROVEMENT ( WITH ONE FIX ) We can add a limit on the number of iterations. def iter_improve(update, isclose, guess=1, max_iter=5000): counter = 1 while not isclose(guess) and counter <= max_iter: guess = update(guess) counter += 1 return guess

7 P RACTICE : N EWTON ’ S M ETHOD Using find_root, write a function intersection(f, g) which takes two functions, f and g, and finds a point at which the two are equal. def intersection(f, g): return find_root(lambda x: f(x) – g(x))

8 P RACTICE : N EWTON ’ S M ETHOD Using find_root, write a function intersection(f, g) which takes two functions, f and g, and finds a point at which the two are equal. def intersection(f, g): return find_root(lambda x: f(x) – g(x))

9 C OMPUTING F ACTORIAL

10 C OMPUTING F ACTORIAL

11 C OMPUTING F ACTORIAL We generalized our definition. Can we do that with our code?

12 C OMPUTING F ACTORIAL Can we generalize here like we did with our mathematical expression?

13 C OMPUTING F ACTORIAL

14 It’s 3 * fact(2)… 1 Fiona, what is fact(3)? It’s 4 * fact(3)… Fred C OMPUTING F ACTORIAL def fact(n): if n == 1 or n == 0: return 1 return n * fact(n – 1) Computer, what is fact(4)? Fred, what is fact(4)? Fiona Ferris, what is fact(2)? It’s 2 * fact(1)… Fatima, what is fact(1)? FatimaFerris

15 R ECURSIVE F UNCTIONS A function is a recursive function if the body calls the function itself, either directly or indirectly. Recursive functions typically have 2 main pieces: 1.Recursive case(s), where the function calls itself. 2.Base case(s), where the function does NOT recursively call itself and instead returns a direct answer. This is what ensures that the recursion will eventually stop. def fact(n): if n == 1 or n == 0: return 1 return n * fact(n – 1) Base Case Recursive Case

16 R ECURSION IN E VERY D AY L IFE : E ATING C HOCOLATE You have a bar of chocolate with n small pieces. How do you eat it? 1.You eat 1 piece of chocolate. 2.You eat a bar of n – 1 pieces of chocolate. What’s your base case? – You have no more chocolate.

17 P RACTICE : R ECURSION How would I rewrite the summation function from last week to use recursion? def summation(n, term): if n == 0: return 0 return term(n) + summation(n – 1, term)

18 P RACTICE : R ECURSION How would I rewrite the summation function from last week to use recursion? def summation(n, term): if n == 0: return 0 return term(n) + summation(n – 1, term)

19 P RACTICE : R ECURSION What does the following function calculate? def fun(a, b): if b == 0: return 0 elif b % 2 == 0: return fun(a + a, b / 2) return fun(a, b – 1) + a It’s multiplying a times b!

20 P RACTICE : R ECURSION What does the following function calculate? def fun(a, b): if b == 0: return 0 elif b % 2 == 0: return fun(a + a, b / 2) return fun(a, b – 1) + a a * b It’s multiplying a times b!

21 P RACTICE : R ECURSION

22 P RACTICE : R ECURSION

23 A NNOUNCEMENTS Bug-Submit is now available! Project 1 is due Friday Homework 3 is due Friday

24 T REE R ECURSION

25 T REE R ECURSION So why is it called tree recursion? def fib(n): if n == 0: return 0 elif n == 1: return 1 return fib(n – 1) + fib(n – 2) fib(4) fib(3) fib(2) fib(1) fib(0) fib(1) fib(2) fib(1) fib(0)

26 P RACTICE : T REE R ECURSION Suppose I want to count all the different routes I could take from (0, 0) to (x, y) on a grid moving only up and right. Write the function paths(x, y) to calculate the number of routes to (x, y). def paths(x, y): if x == 0 or y == 0: return 1 return paths(x – 1, y) \ + paths(x, y – 1)

27 P RACTICE : T REE R ECURSION Suppose I want to count all the different routes I could take from (0, 0) to (x, y) on a grid moving only up and right. Write the function paths(x, y) to calculate the number of routes to (x, y). def paths(x, y): if x == 0 or y == 0: return 1 return paths(x – 1, y) \ + paths(x, y – 1)

28 P RACTICE : T REE R ECURSION Suppose I want to print all the different routes I could take from (0, 0) to (x, y) on a grid moving only up and right. Write the function directions(x, y) which prints the each different set of directions using a combination of “UP” and “RIGHT” that one could take. Hint: use a helper function that does the recursion and keeps track of the “directions so far.” def directions(x, y): def dir_helper(x, y, so_far): if x == 0 and y == 0: print(so_far) elif y > 0: dir_helper(x, y – 1, so_far + “ UP”) elif x > 0: dir_helper(x-1, y, so_far + “ RIGHT”) dir_helper(x, y, “”)

29 P RACTICE : T REE R ECURSION Suppose I want to print all the different routes I could take from (0, 0) to (x, y) on a grid moving only up and right. Write the function directions(x, y) which prints the each different set of directions using a combination of “UP” and “RIGHT” that one could take. Hint: use a helper function that does the recursion and keeps track of the “directions so far.” def directions(x, y): def dir_helper(x, y, so_far): if x == 0 and y == 0: print(so_far) elif y > 0: dir_helper(x, y – 1, so_far + “ UP”) elif x > 0: dir_helper(x-1, y, so_far + “ RIGHT”) dir_helper(x, y, “”)

30 C ONCLUSION Recursion is a way for functions to be defined using themselves. Recursive functions have two parts: – Recursive case(s), where the function calls itself. – Base case(s), where the function does not call itself (stopping the recursion). Tree recursion is used by functions that make more than one recursive call (at the same time) in the definition.