Recursion (Continued)

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
MATH 224 – Discrete Mathematics
More on Recursion More techniques 1. Binary search algorithm Binary searching for a key in an array is similar to looking for a word in dictionary Take.
CS50 SECTION: WEEK 3 Kenny Yu. Announcements  Watch Problem Set 3’s walkthrough online if you are having trouble.  Problem Set 1’s feedback have been.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Copyright © 2003 Pearson Education, Inc. Slide 1.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
RECURSION Lecture 7 CS2110 – Spring Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
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.
RECURSION Lecture 6 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
RECURSION Lecture 7 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
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.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
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.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
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.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
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.
RECURSION (CONTINUED) Lecture 9 CS2110 – Spring 2016.
CS 100Lecture 201 Announcements P5 due Thursday Final exam: Tuesday August 10th, 8AM, Olin 155, 2 hours Tomorrow, Wednesday: C, pointers Thursday, Friday:
Recursion Powerful Tool
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Sort & Search Algorithms
Recursion Version 1.0.
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Computer Science 4 Mr. Gerb
COMP 53 – Week Seven Big O Sorting.
Recursion (Continued)
Graph Search Lecture 17 CS 2110 Fall 2017.
University of Washington Computer Programming I
Recursion (Continued)
Announcements Final Exam on August 17th Wednesday at 16:00.
Quicksort 1.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Last Class We Covered Data representation Binary numbers ASCII values
Algorithm design and Analysis
Graph Search Lecture 17 CS 2110 Spring 2018.
CS October 2008 The while loop and assertions
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Recursion (Continued)
Searching CLRS, Sections 9.1 – 9.3.
Sit next to someone. Today, we do some work in pairs.
Searching, Sorting, and Asymptotic Complexity
Lecture Notes – Week 4 Chapter 5 (Loops).
Basics of Recursion Programming with Recursion
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Recursion (Continued)
Asymptotic complexity
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
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.
CS210- Lecture 3 Jun 6, 2005 Announcements
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Searching and Sorting Hint at Asymptotic Complexity
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 – Fall2016

Overview references to sections in text What is recursion? 7.1-7.39 slide 1-7 Base case 7.1-7.10 slide 13 How Java stack frames work 7.8-7.10 slide 28-32 Solutions to recitation problem sets. See Piazza Supplemental Study Material TA midsemester evaluation coming! PLEASE help us and complete the evaluations! A chance to help TAs and YOU this semester Prelim a week from Thursday. 32 conflicts so far. Look here for info: http://www.cs.cornell.edu/courses/CS2110/2016fa/exams.html

Summary of method call execution: 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. p ____ k ____ 7 8 public int m(int p) { int k= p+1; return p; } m(5+2) call stack

Understanding recursive methods Have a precise specification 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

deduplicate /** Return shortest substring x of s such that s = x + x + ⋯ + x public static String deduplicate(String s) {} Many solutions far too complicated! We show how to do this simply, using recursion –next lecture, using iteration.

deduplicate This is a linear search in 1..s.length() for smallest something. It’s guaranteed to succeed because x = s is a solution, but not necessarily the smallest. /** Return shortest substring x of s such that s = x + x + ⋯ + x public static String deduplicate(String s) { } for (int k= 1; k < s.length(); k= k+1) { String x= s.substring(0, k); }

deduplicate STAY AWAY FROM NESTED LOOPS. Put in comment saying what to do. /** Return shortest substring x of s such that s = x + x + ⋯ + x public static String deduplicate(String s) { } for (int k= 1; k < s.length(); k= k+1) { String x= s.substring(0, k); } // Return true if s is x + x + … + x return s;

deduplicate STAY AWAY FROM NESTED LOOPS. Write a method to return true if s is x + x + … + x /** = "s is formed by catenating x 1 or more times." * Precondition: x.length() > 0. */ public static boolean sIsCatX(String s, String x) { }

deduplicate STAY AWAY FROM NESTED LOOPS. Write a method to return true if s is x + x + … + x /** = "s is formed by catenating x 1 or more times." * Precondition: x.length() > 0. */ public static boolean sIsCatX(String s, String x) { int n= x.length(); if (s.length() < x.length()) return false; if (!x.equals(s.substring(0, n))) return false; return s.length() == n || sIsCatX(s.substring(n), x); }

deduplicate STAY AWAY FROM NESTED LOOPS. Write a method to return true if s is x + x + … + x /** Return shortest substring x of s such that s = x + x + ⋯ + x public static String deduplicate(String s) { } for (int k= 1; k < s.length(); k= k+1) { String x= s.substring(0, k); if (sIsCatX(s, x)) return x; } // Return true is s is x + x + … + x return s;

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

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

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

Difference between linear and log solutions? /** = b**n. Precondition: n >= 0 */ static int power(double b, int n) { if (n == 0) return 1; return b * power(b, n-1); } Number of recursive calls is n Number of recursive calls is ~ log n. /** = b**n. Precondition: n >= 0 */ static int power(double b, int n) { if (n == 0) return 1; if (n%2 == 0) return power(b*b, n/2); return b * power(b, n-1); } To show difference, we run linear version with bigger n until out of stack space. Then run log one on that n. See demo.

Table of log to the base 2 k n = 2^k log n (= k) 0 1 0 1 2 1 2 4 2 0 1 0 1 2 1 2 4 2 3 8 3 4 16 4 5 32 5 6 64 6 7 128 7 8 256 8 9 512 9 1024 10 2148 11 15 32768 15

Tiling Elaine’s kitchen 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 /** tile a 23 by 23 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 /** tile a 2n by 2n kitchen with 1 square filled. */ public static void tile(int n) { } We generalize to a 2n by 2n kitchen if (n == 0) return; Base case?

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

Tiling Elaine’s kitchen /** tile a 2n by 2n kitchen with 1 square filled. */ public static void tile(int n) { } if (n == 0) return; We can tile the upper-right 2n-1 by 2n-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!

Tiling Elaine’s kitchen /** tile a 2n by 2n 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;

Sierpinski triangles 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 2: 3 S triangles of depth 1 drawn at the 3 vertices of the triangle

Sierpinski triangles S triangle of depth 0: 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 p3 Sierpinski triangles of depth d-1 p2 p1

Conclusion 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