RECURSION (CONTINUED) Lecture 9 CS2110 – Spring 2016.

Slides:



Advertisements
Similar presentations
Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Advertisements

Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
RECURSION Lecture 6 CS2110 – Fall Recursion Overview 4  Recursion is a powerful technique for specifying functions, sets, and programs 
Computer Science II Recursion Professor: Evan Korth New York University.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
1 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
RECURSION Lecture 7 CS2110 – Spring Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
RECURSION Lecture 6 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
RECURSION Lecture 7 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
RECURSION Lecture 6 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
INDUCTION Lecture 23 CS2110 – Spring 2015 A scientist gave a lecture on astronomy. He described how the earth orbits the sun, which, in turn, orbits the.
Data Structures & Algorithms
RECURSION, CONTINUED Lecture 8 CS2110 – Fall 2015.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
RECURSION Lecture 6 CS2110 – Spring Recursion 2  Arises in three forms in computer science  Recursion as a mathematical tool for defining a function.
INDUCTION Lecture 21 CS2110 – Spring 2014 A well-known scientist (Bertrand Russell?) once gave a public lecture on astronomy. He described how the earth.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
1 Recursion and induction We teach these early, instead of new object- oriented ideas, so that those who are new to Java can have a chance to catch up.
FIBONACCI NUMBERS AND RECURRENCES Lecture 26 CS2110 – Spring 2016 Fibonacci (Leonardo Pisano) ? Statue in Pisa Italy.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion (Continued)
Recursion.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Introduction to Recursion
Computer Science 4 Mr. Gerb
Fibonacci numbers and recurrences
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Fibonacci numbers Golden Ratio, recurrences
Recursion (Continued)
Fibonacci numbers Golden Ratio, recurrences
Java Software Structures: John Lewis & Joseph Chase
Recursion (Continued)
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Copyright (c) Pearson All rights reserved.
Applied Algorithms (Lecture 17) Recursion Fall-23
Chapter 12 Recursion (methods calling themselves)
Algorithm design and Analysis
Recursion 2-Dec-18.
ASTs, Grammars, Parsing, Tree traversals
Recursion 29-Dec-18.
Recursion (Continued)
Unit 3 Test: Friday.
Sit next to someone. Today, we do some work in pairs.
Lecture 13 Recursion part 2 CSE /26/2018.
Recursion (Continued)
Asymptotic complexity
Fibonacci numbers Golden Ratio, recurrences
Chapter 18 Recursion.
CS100A Lecture 25 1 December 1998 Chance to replace grade on Prelim 3, Question 2. Everyone is expected to be in lecture on Thursday, 3 December.
Lawrence Snyder University of Washington
Searching and Sorting Hint at Asymptotic Complexity
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

RECURSION (CONTINUED) Lecture 9 CS2110 – Spring 2016

Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion? slide 1-7  Base case slide 13  How Java stack frames work slide Solutions to exception-handling problem set: lecture-notes page, course website, row for recitation 3. A3 regrades will be done by end of weekend TA midsemester evaluation coming! PLEASE help us and complete the evaluations! A chance to help Tas and YOU this semester

About prelim :30-7PM. Kennedy 116: last name begins with A..Lib. 2. 7:30-9:PM. Kennedy 116: last name begins with Lie..Z. 3. Gates 405 if authorized to have quiet room or more time. Complete P1Conflict on the CMS. Issue with this, see point Conflict with scheduled time but can make other one: Complete P1Conflict. Go to other prelim. 5. All other conflicts (e.g. won't be in town). Megan Gatch State name, netid, conflict clearly and thoroughly. Don’t complete P1Conflict. We’ll get back to you. Complete P1Conflict (if you have to) by end of 9 March.

Hoare triple for if-statement 4 What do we need to know this is true? {Q} if (B) S {R} Many of you wrote this: If {Q && B} S {R} then {Q} if (B) S {R} But what if B is false? Doesn’t R still have to be true after execution of the if-statement? and Q && !B => R

Summary of method call execution: 5  1. Push frame for call onto call stack.  2. Assign arg values to pars.  3. Execute method body.  4. Pop frame from stack and (for a function) push return value on the stack.  For function call: When control given back to call, pop return value, use it as the value of the function call. public int m(int p) { int k= p+1; return p; } m(5+2) call stack p ____ k ____ 7 8

Summary of method call execution: 6  1. Push frame for call onto call stack.  2. Assign arg values to pars.  3. Execute method body.  4. Pop frame from stack and (for a function) push return value on the stack.  For function call: When control given back to call, pop return value, use it as the value of the function call. public int m(int p) { int k= p+1; return k; } m(5+2) call stack p ____ k ____ 7 8 8

Summary of method call execution: 7  1. Push frame for call onto call stack.  2. Assign arg values to pars.  3. Execute method body.  4. Pop frame from stack and (for a function) push return value on the stack.  For function call: When control given back to call, pop return value, use it as the value of the function call. public int m(int p) { int k= p+1; return k; } m(5+2) call stack 8

Understanding recursive methods 8 1.Have a precise specification 2.Check that the method works in the base case(s). 3. Look at the recursive case(s). In your mind, replace each recursive call by what it does according to the spec and verify correctness. 4. (No infinite recursion) Make sure that the args of recursive calls are in some sense smaller than the pars of the method

The Fibonacci Function 9 Mathematical definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n  1) + fib(n  2), n ≥ 2 Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, … /** = fibonacci(n). Pre: n >= 0 */ static int fib(int n) { if (n <= 1) return n; // { 1 < n } return fib(n-2) + fib(n-1); } two base cases! Fibonacci (Leonardo Pisano) ? Statue in Pisa, Italy Giovanni Paganucci 1863

Example: Count the e’s in a string 10  countEm(‘e’, “it is easy to see that this has many e’s”) = 4  countEm(‘e’, “Mississippi”) = 0 /** = number of times c occurs in s */ public static int countEm(char c, String s) { if (s.length() == 0) return 0; // { s has at least 1 character } if (s.charAt(0) != c) return countEm(c, s.substring(1)); // { first character of s is c} return 1 + countEm (c, s.substring(1)); } substring s[1..], i.e. s[1], …, s(s.length()-1)

Computing b n for n >= 0 11 Power computation:  b 0 = 1  If n != 0, b n = b * b n-1  If n != 0 and even, b n = (b*b) n/2 Judicious use of the third property gives far better algorithm Example: 3 8 = (3*3) * (3*3) * (3*3) * (3*3) = (3*3) 4

Computing b n for n >= 0 12 Power computation:  a 0 = 1  If n != 0, b n = b b n-1  If n != 0 and even, b n = (b*b) n/2 /** = b**n. Precondition: n >= 0 */ static int power(double a, double n) { if (n == 0) return 1; if (n%2 == 0) return power(b*b, n/2); return b * power(b, n-1); } Suppose n = 16 Next recursive call: 8 Next recursive call: 4 Next recursive call: 2 Next recursive call: 1 Then 0 16 = 2**4 Suppose n = 2**k Will make k + 2 calls

Computing b n for n >= 0 13 /** = b**n. Precondition: n >= 0 */ static int power(double a, double n) { if (n == 0) return 1; if (n%2 == 0) return power(b*a, b/2); return b * power(b, n-1); } Suppose n = 16 Next recursive call: 8 Next recursive call: 4 Next recursive call: 2 Next recursive call: 1 Then 0 16 = 2**4 Suppose n = 2**k Will make k + 2 calls If n = 2**k k is called the logarithm (to base 2) of n: k = log n or k = log(n)

Tiling Elaine’s kitchen 14 Kitchen in Gries’s house: 8 x 8. Fridge sits on one of 1x1 squares His wife, Elaine, wants kitchen tiled with el-shaped tiles –every square except where the refrigerator sits should be tiled. 8 8 /** tile a 2 3 by 2 3 kitchen with 1 square filled. */ public static void tile(int n) We abstract away keeping track of where the filled square is, etc.

Tiling Elaine’s kitchen 15 /** tile a 2 n by 2 n kitchen with 1 square filled. */ public static void tile(int n) { } We generalize to a 2 n by 2 n kitchen Base case? if (n == 0) return;

Tiling Elaine’s kitchen 16 /** tile a 2 n by 2 n kitchen with 1 square filled. */ public static void tile(int n) { } n > 0. What can we do to get kitchens of size 2 n-1 by 2 n-1 if (n == 0) return; 2n2n 2n2n

Tiling Elaine’s kitchen 17 /** tile a 2 n by 2 n kitchen with 1 square filled. */ public static void tile(int n) { } We can tile the upper-right 2 n-1 by 2 n-1 kitchen recursively. But we can’t tile the other three because they don’t have a filled square. What can we do? Remember, the idea is to tile the kitchen! if (n == 0) return;

Tiling Elaine’s kitchen 18 /** tile a 2 n by 2 n kitchen with 1 square filled. */ public static void tile(int n) { } if (n == 0) return; Place one tile so that each kitchen has one square filled; Tile upper left kitchen recursively; Tile upper right kitchen recursively; Tile lower left kitchen recursively; Tile lower right kitchen recursively;

S triangle of depth 2: 3 S triangles of depth 1 drawn at the 3 vertices of the triangle Sierpinski triangles 19 S triangle of depth 0 S triangle of depth 1: 3 S triangles of depth 0 drawn at the 3 vertices of the triangle

S triangle of depth d at points p1, p2, p3: 3 S triangles of depth d-1 drawn at at p1, p2, p3 Sierpinski triangles 20 S triangle of depth 0: the triangle p1 p2 p3 Sierpinski triangles of depth d-1

Conclusion 21 Recursion is a convenient and powerful way to define functions Problems that seem insurmountable can often be solved in a “divide-and-conquer” fashion:  Reduce a big problem to smaller problems of the same kind, solve the smaller problems  Recombine the solutions to smaller problems to form solution for big problem