Recursive Methods Noter ch.2. QUIZ What is the result of the method call foo(4) ? 1.Prints 4 2.Prints 1 3.Prints 1 2 3 4 4.Prints 4 3 2 1 5.Compiler error:

Slides:



Advertisements
Similar presentations
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 13 – Recursion.
Advertisements

Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
Recursion - see Recursion. RHS – SOC 2 Recursion We know that: –We can define classes –We can define methods on classes –Mehtods can call other methods.
Chapter 18 Recursion "To iterate is human, to recurse divine.", L. Peter Deutsch.
CHAPTER 13 Recursion. Recursive Solution A recursive solution  solves a problem by solving a smaller instance of the problem. Example  How do we go.
Recursion.
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 English –Return (Oxford/Webster) –procedure repeating itself indefinitely or until condition met, such as grammar rule (Webster) adequate: satisfactory.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Recursion Gordon College CPS212
CHAPTER 17 RECURSION CHAPTER GOALS –To learn about the method of recursion –To understand the relationship between recursion and iteration –To analysis.
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.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Unit 7 1 Unit 7: Recursion H Recursion is a fundamental programming technique that can provide an elegant solution to a certain kinds of problems H In.
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. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 13 - Recursion.
Recursion Recursion is a math and programming tool –Technically, not necessary Advantages of recursion –Some things are very easy to do with it, but difficult.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
M180: Data Structures & Algorithms in Java
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
AP Computer Science Instructor Alabama School of Fine Arts
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
1Recursion. 2 Outline thinking recursively recursive algorithms iteration vs. recursion recursive functions integer exponentiation (pow) infinite recursion.
JAVA Advanced Control Structures. Objectives Be able to use a switch statement for selective execution. Be able to implement repetition using alternative.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Thirteen: Recursion.
CHAPTER 17 RECURSION. CHAPTER GOALS To learn about the method of recursion To understand the relationship between recursion and iteration To analysis.
Chapter 18 Recursion. Chapter Goals To learn about the method of recursion To understand the relationship between recursion and iteration To analyze problems.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
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 =
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
Copyright © 2013 by John Wiley & Sons. All rights reserved. RECURSION CHAPTER Slides by Rick Giles 13.
ICOM 4015: Advanced Programming Lecture 13 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Thirteen:
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Mutual Recursion A set of methods calls each other cooperatively and repeatedly Expression is a term, or sum or difference of terms Term is a factor, or.
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
1 Recursion Recursive definitions Recursive methods Run-time stack & activation records => Read section 2.3.
Recursion Chapter 11. How it works “A recursive computation solves a problem by using the solution of the same problem with simpler inputs” Big Java,
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Functions. functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion - see Recursion
Chapter 13 Recursion.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Recursion Chapter 11.
Lesson #6 Modular Programming and Functions.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion - see Recursion
Unit 3 Test: Friday.
Chapter 13 – Recursion Big Java by Cay Horstmann
Lesson #6 Modular Programming and Functions.
Chapter 18 Recursion.
Java Programming: Chapter 9: Recursion Second Edition
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion Method calling itself (circular definition)
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
ICS103 Programming in C Lecture 11: Recursive Functions
Types of Recursive Methods
Presentation transcript:

Recursive Methods Noter ch.2

QUIZ What is the result of the method call foo(4) ? 1.Prints 4 2.Prints 1 3.Prints Prints Compiler error: method foo is not allowed to call itself 6.Runtime error: method foo is not allowed to call itself 7.I don’t know public static void foo(int x) { System.out.println(x); if (x>1) foo(x-1); }

Recursive Methods Recursive problem solution –Problems that are naturally solved by recursion –Derivative of rational function Examples: –Recursive function: Fibonacci numbers –Recursive graphics: Fractals –Mutual recursion: Expression evaluation –Randomization and recursion: Random plants/trees General aspects –Termination –Recursion versus iteration: simplicity vs efficiency

Natural Recursion: Derivative of Rational Function Recursive method calls itself

QUIZ What is the proper recursive definition of the factorial function? 1.n! = n * (n-1)! 2.n! = 3.n! = 4.n! = 5.n! = 6.I don’t know Factorial function: n! = 1 * 2 * 3 *... * (n-1) * n (n-1)!For n > 1 1For n = 1 (n-1)!For n > 1 n-1For n = 1 n*(n-1)!For n > 1 1For n = 1 n*(n-1)For n > 1 (n-1)!For n = 1

QUIZ What is the proper method header? 1.public static void factorial() 2.public static int factorial() 3.public static void factorial(int n) 4.public static int factorial(int n) 5.I don’t know Factorial function: n! = 1 * 2 * 3 *... * (n-1) * n Java method for computing factorial function: { }

QUIZ What is the proper base case? 1.return 1; 2.return factorial(1); 3.if (n==1) return 1; 4.if (n==1) return factorial(1); 5.return n-1; 6.return factorial(n-1); 7.if (n==1) return n-1; 8.if (n==1) return factorial(n-1); 9.I don’t know Factorial function: n! = 1 * 2 * 3 *... * (n-1) * n Java method for computing factorial function: { }

QUIZ What is the proper recursive case? 1.factorial(n-1); 2.factorial(n*n-1); 3.n*factorial(n-1); 4.return factorial(n-1); 5.return factorial(n*n-1); 6.return n*factorial(n-1); 7.I don’t know Factorial function: n! = 1 * 2 * 3 *... * (n-1) * n Java method for computing factorial function: { }

Recursive Function: Fibonacci Numbers The sequence of Fibonacci numbers is First 10 terms are –1, 1, 2, 3, 5, 8, 13, 21, 34, 55

Recursive Function: Fibonacci Numbers Recursive method corresponding to recursive definition of Fibonacci numbers public long fib(int n) { if (n <= 1) return 1; else return fib(n - 1) + fib(n - 2); }

Tree Structure of Method Call fib(4)

QUIZ Which lines should replace to obtain a correct and recursive computation of triangle area ? (for n=4, the area is 10) 1.None of a,b,c,d 2.a 3.b 4.c 5.d 6.More than one of a,b,c,d could be used 7.I don’t know n>=1 */ public int area(int n) { } a)return n*(n+1)/2; b)if (n==1) return 1; else return area(n-1); c)return n+area(n-1); d)if (n==1) return 1; else return n+area(n-1); n

Recursive graphics The snowflake is built from 3 Koch lines of order 4. The Koch line is defined recursively

Fractals: Koch lines of order 1-4 A line of order 0 is a straight line A line of order n consists of 4 lines of order n-1 each of 1/3 length

Crayon class State of Crayon object –Colour –Width (pixel) –Position (coordinates) –Direction (0-360 deg) Public methods –Relative changes –move(d): draw line of lenght d (from position in direction to new position) –jump(d): change position without drawing –turn(a): add a degrees to direction –Absolute changes –moveto(x,y): –jumpto(x,y): –turnto(a):

KochLineTest public class KochLineTest { public static void main(String[] args) { c = new Crayon(Color.red,1); c.jumpto(50,150); kochLine(4,300); c.turn(120); } private static Crayon c; private static void kochLine(int order, double len) {...} }

recursive method kochLine void kochLine(int order, double len) { if (order == 0) c.move(len); else if (order > 0) { kochLine(order-1,len/3); c.turn(-60); kochLine(order-1,len/3); c.turn(120); kochLine(order-1,len/3); c.turn(-60); kochLine(order-1,len/3); }

QUIZ Which figures may be drawn using the method? 1.neither 2.a 3.b 4.a+b 5.I don’t know private void line(int order, double len, int sign) { if (order == 0) c.move(len); else if (order > 0) { c.turn(sign*60); line(order-1,len/2,-sign); c.turn(-sign*60); line(order-1,len/2,sign); c.turn(-sign*60); line(order-1,len/2,-sign); c.turn(sign*60); } a b

Mutual Recursion up and down are mutually recursive: public void down(int n) { while(n%2==0) n = n/2; up(n); } public void up(int n) { if (n>1) { n = 3*n+1; down(n); } } It is unknown whether the call down(m) terminates for all m !

QUIZ Which assertions are correct? 1.Both odd and even always stops 2.odd always stops, but even may loop indefinitely 3.even always stops, but odd may loop indefinitely 4.Both odd and even may loop indefinitely 5.I don’t know Precondition for both methods: n >= 0 public boolean even(int n) { if (n==0) return true; return odd(n-1); } public boolean odd(int n) { if (n==0) return false; return even(n-1); }

Using Mutual Recursion Problem: – compute the value of arithmetic expressions such as * 5 (3 + 4) * 5 1 – (2 – (3 – (4 – 5))) Precedence rules: –* and / take precedence over + and – –may overrule using parentheses (... )

Syntax diagram for expression number

Syntax tree for two expressions

Mutually recursive methods Implement 3 methods that call each other recursively getExpressionValue getTermValue getFactorValue An ExpressionTokenizer is used to group input in tokens. A token being a string of digits or one of "+", "-", "*", "/", "(", ")". Methods: peekToken nextToken

public class Evaluator { public Evaluator(String anExpression) { tokenizer = new ExpressionTokenizer(anExpression); } public int getExpressionValue() {... } public int getTermValue() {... } public int getFactorValue() {... } private ExpressionTokenizer tokenizer; }

public int getExpressionValue() { int value = getTermValue(); while ("+".equals(tokenizer.peekToken()) || "-".equals(tokenizer.peekToken())) { String operator = tokenizer.nextToken(); int value2 = getTermValue(); if ("+".equals(operator)) value = value + value2; else value = value - value2; } return value; }

public int getTermValue() { int value = getFactorValue(); while ("*".equals(tokenizer.peekToken()) || "/".equals(tokenizer.peekToken())) { String operator = tokenizer.nextToken(); int value2 = getFactorValue(); if ("*".equals(operator)) value = value * value2; else value = value / value2; } return value; }

public int getFactorValue() { int value; if ("(".equals(tokenizer.peekToken())) { tokenizer.nextToken(); value = getExpressionValue(); tokenizer.nextToken(); // read ")" } else value = Integer.parseInt(tokenizer.nextToken()); return value; }

QUIZ Which code could replace ? 1.Neither a nor b 2.a 3.b 4.a or b 5.I don’t know public boolean getTermValue() { } if ("!".equals(tokenizer.peekToken())) { tokenizer.nextToken(); return !getFactorValue(); } return getFactorValue(); boolean value = getFactorValue(); if ("!".equals(tokenizer.peekToken())) { tokenizer.nextToken(); value = !value; } return value; a b (Part of) syntax diagram for Boolean expression

Random trees and flowers

Recursive trees leaf, branch, trunk differ by colors and thickness

Variation by randomization Each tree consists of a trunk and up to BRANCH_MAX=6 smaller trees Each of the subtrees are drawn with probability BRANCH_PROB=0.4 Subtrees are tilted with angle to neighbor being BRANCH_ANGLE=13 deg.

class CCrayon CCrayon is a variant of Crayon with 2 extra methods: – setColor – setWidth

class TreeTest public class TreeTest { public static void main(String[] args) { c = new CCrayon(); c.jumpto(200,400); c.turn(-90); tree(6,40); } private static CCrayon c; private static void tree(int order, int len) {...} private static void leaf(int len) {... } private final static int BRANCH_MAX = 6; private final static int BRANCH_ANGLE = 13; private final static double BRANCH_PROB = 0.4; private final static double LEAF_PROB = 0.3; }

Recursive method tree public void tree(int order, int len) { if (order==0) leaf(len/2); else { int bias = (int) (2*Math.random()); if (order+bias >=6) c.setColor(Color.black); else if (order+bias >=4) c.setColor(Color.gray); else c.setColor(Color.green); c.setWidth(2*order); c.move(len); c.turn((BRANCH_ANGLE*(BRANCH_MAX-1))/2.0); for (int i = 1 ; i<=BRANCH_MAX ; i = i+1 ) { if (Math.random()<BRANCH_PROB) tree(order-1, len-2); c.turn(-BRANCH_ANGLE); } c.turn((BRANCH_ANGLE*(BRANCH_MAX+1))/2.0); c.turn(180); c.jump(len); c.turn(-180); //return pen to base of tree }

Method leaf void leaf(int len) { if (Math.random()<LEAF_PROB) { if (Math.random()<0.5) c.setColor(Color.red); else c.setColor(Color.yellow); c.setWidth(2); c.turn(-BRANCH_ANGLE/2.0); c.move(len); c.turn(BRANCH_ANGLE); c.move(len); c.turn(180-BRANCH_ANGLE); c.move(len); c.turn(BRANCH_ANGLE); c.move(len); c.turn(180-BRANCH_ANGLE/2.0); }

Recursion in general: Termination Recursive call need not terminate public void loopingRecursiveMethod() { loopingRecursiveMethod(); } Any reasonable recursive method should have recursion condition public long fib(int n) { if (n <= 1) return 1; else return fib(n - 1) + fib(n - 2); } private void kochLine(int order, double len) { if (order == 0) no recursive call else if (order > 0) recursive call } no recursive call recursive call

Thinking recursively vs efficiency Recursive solution may be natural and simple to program public long fib(int n) { if (n <= 1) return 1; else return fib(n - 1) + fib(n - 2); } But iterative solution may be more efficient public long iterativeFib(int n) { if (n <= 1) return 1; long fold = 1; long fold2 = 1; long fnew = 1; for (int i = 2; i <= n; i++) { fnew = fold + fold2; fold2 = fold; fold = fnew; } return fnew; }

QUIZ How many recursive calls result in total from the call bin(n,k) (approximate number)? 1.n 2. 3.n+k 4.nk 5. 6.I don’t know 0<=k<=n */ public int bin(int n, int k) { if (k==0 || k==n) return 1; return bin(n-1,k-1) + bin(n-1,k); } k=0k=1k=2k=3k=4k=5k=6 n=111 n=2121 n=31331 n= n= n=