COMP 103 RECURSION.

Slides:



Advertisements
Similar presentations
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.
Advertisements

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Computer Science II Recursion Professor: Evan Korth New York University.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion CITS1001.
COMP 103 Introduction to Trees.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Recursion COMP 102 #32.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
SATMathVideos.Net A set S consists of all multiples of 4. Which of the following sets are contained within set S? A) S2 only B) S4 only C) S2 and S4 D)
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Programming with Recursion 1 © 2010 Goodrich, Tamassia.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Recursion COMP 102 #30.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Lesson #5 Repetition and Loops.
to understand recursion you must understand recursion
ESC101: Introduction to Computing
Introduction to Recursion
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Recursion Chapter 11.
Lesson #5 Repetition and Loops.
Java 4/4/2017 Recursion.
RECURSION COMP 103 Thomas Kuehne 2016-T2 Lecture 16
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Announcements Final Exam on August 17th Wednesday at 16:00.
Lesson #6 Modular Programming and Functions.
CS 177 Week 15 Recitation Slides
Algorithm design and Analysis
Lesson #5 Repetition and Loops.
More About Recursion Java.
Announcements Final Exam on August 19th Saturday at 16:00.
RECURSION Annie Calpe
Stacks.
Announcements Final Exam on December 25th Monday at 16:00.
Basics of Recursion Programming with Recursion
Chapter 17 Recursion.
Lesson #6 Modular Programming and Functions.
Lesson #5 Repetition and Loops.
Recursion Pepper.
Recursion Chapter 12.
EECE.2160 ECE Application Programming
Presentation transcript:

COMP 103 RECURSION

RECAP-TODAY RECAP TODAY Recursion Selection Sort, Insertion Sort 2 RECAP Selection Sort, Insertion Sort Cost Analysis – those were the “slow” sorts: O(n2) TODAY Recursion

Recursion Methods can call other methods.... 3 Methods can call other methods.... ...so could a method call itself ? Something is recursive if it is defined in terms of itself We will look at: tail recursion (the simplest) diagrams of what's going on recursion with a wrapper method nested recursion multiple recursion returning values

Example 4 /** Draw stream of bubbles from (x,y) to the top of the screen */ public void drawBubbles(int x, int y){ if ( y>0 ) { drawOneBubble(x, y); drawBubbles(x, y-15); } y=0 y=70 x=30 drawBubbles (30, 40); drawOneBubble(30,40); drawBubbles (30, 55); drawOneBubble(30,55); drawBubbles (30, 70); drawOneBubble(30,70);

Tracing the calls drawBubbles(30, 70) drawBubbles(30, 55) public void drawBubbles(int x, int y){ if ( y>0 ) { drawOneBubble(x, y); drawBubbles(x, y-15); } return drawBubbles(30, 70) drawOneBubble(30, 70) drawBubbles(30, 55) return drawOneBubble(30, 55) drawBubbles(30, 40) return drawOneBubble(30, 40) drawBubbles(30, 25) return drawOneBubble(30, 25) drawBubbles(30, 10) return drawOneBubble(30, 10) drawBubbles(30, -5) return

Recursion with wrapper method 6 /** Draw stream of bubbles from (x,y) to the top of the screen the bubbles should get larger as they go up. */ public void drawBubbles(int x, int y){ UI.setColor(Color.blue); drawBubblesRec(x, y, 10); } /** Recursive method */ public void drawBubblesRec(int x, int y, int size){ if ( y>0 ) { UI.fillOval(x, y, size, size); drawBubblesRec(x, y-size-6, size+2); Initial size Condition Do First Do the rest Arguments for the rest

Tail recursion vs nested recursion. 7 Tail recursion: recursive call is the last step in the method. easier to understand because don’t have to “go back” after call. Nested recursion: recursive call is in the middle. have to go back to previous call to finish it off. Example: Print an “onion” : ( ( ( ( ( ( ( ) ) ) ) ) ) ) public void onion (int layers){ UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } open do the inside close

Nested Recursion at work 8 onion(4) ⇒ public void onion (int layers){ UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); ( ( ( ( ) ) ) ) public void onion (int layers){ UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 4 . public void onion (int layers){ UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 3 . ✔ public void onion (int layers){ UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 2 . ✔ ✔ public void onion (int layers){ UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 1 . ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔

Alternative Visualisation 9 onion(4) ✔ print( “(” ) print( “)” ) onion(3) ✔ ✔ ✔ print( “(” ) print( “)” ) onion(2) ✔ ✔ ✔ print( “(” ) print( “)” ) onion(1) ✔ ✔ ✔ print( “(” ) print( “)” ) ✔ ✔ To understand recursive methods: draw a diagram!

Kinds of Recursion Tail recursion vs. Nested recursion 10 Tail recursion vs. Nested recursion Tail: recursive call is the last step in a method ⇒ don’t have to “go back” and do more. Nested: recursive call is in the middle ⇒ action … recursive call … more actions Single recursion vs. Multiple recursion Single: method only makes one recursive call Multiple: method makes more than one recursive call Single Recursion

Multiple Recursion Draw a recursive arch-wall: 11 Draw a recursive arch-wall: Consists of an arch with two half size arch-walls on top of it.

Multiple Recursion to draw an ArchWall of given base size (wd,ht): 12 to draw an ArchWall of given base size (wd,ht): draw a base arch of size (wd,ht) if wd is not too small draw a half size archwall on the left draw a half size archwall on the right public void archWall (int x, int y, int wd, int ht){ drawArch(x, y, wd, ht); if ( wd > 20 ) { int w = wd/2; // width of next smaller arch int h = ht/2; // height of next smaller arch archWall(x, y-h, w, h); // left half archWall(x+w, y-h, w, h); // right half }

Tracing the execution: 13 archWall(10, 300, 80, 40) aw(50,280,40,20) drawArch aw(10, 280, 40, 20) drawArch aw(10,270,20,10) aw(30,270,20,10) drawArch aw(50,270,20,10) aw(70,270,20,10) drawArch drawArch drawArch drawArch

Recursion that returns a value 14 What if the method returns a value? ⇒ get value from recursive call, then do something with it typically, perform computation on value, then return answer. Compound interest value at end of n th year = value at end of previous year * (1 + interest). value(deposit, year) = deposit [if year is 0] = value(deposit, year-1) * (1+rate)

Recursion returning a value 15 /** Compute compound interest of a deposit */ public double value(double deposit, double rate, int year){ if (year == 0) return deposit; else { int prev = value(deposit, rate, year-1); return prev * (1 + rate); }

Recursion with return: execution 16 public double value(double deposit, double rate, int year){ if (year == 0) return deposit; else { int prev = value(deposit, rate, year-1); ← step 1 return prev * (1 + rate); ← step 2 } value(1000, 0.1, 4) value(1000, 0.1, 3) * 1.1 value(1000, 0.1, 2) * 1.1 value(1000, 0.1, 1) * 1.1 value(1000, 0.1, 0) * 1.1 1464.1 1331 1210 1100 1000