© Calvin College, 2009 1 recursion n. See recursion. See also tail recursion. - The Jargon Dictionary, v. 4.2.2.

Slides:



Advertisements
Similar presentations
The Towers of Hanoi or Apocalypse When?.
Advertisements

Factorial Recursion stack Binary Search Towers of Hanoi
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Recursion Definition: A method that calls itself. Recursion can be direct or indirect. Indirect recursion involves a method call that eventually recalls.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Outline Relational Operators Boolean Operators Truth Tables Precedence Table Selection and Algorithms The if – else Variations of if The switch.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
Recursion Gordon College CPS212
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
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.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Recursion.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Recursion Self-Referencing Functions. Problem # 1 Write a function that, given n, computes n! n! == (n-1) n n! == 1  2 ...  (n-1)  nExample:
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
JAVA Advanced Control Structures. Objectives Be able to use a switch statement for selective execution. Be able to implement repetition using alternative.
Chapter 8 Recursion Modified.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
1 Controlling Behavior Chap.5 Study Sections 5.1 – 5.3 The if and for Statements.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion.
Calvin College Controlling Behavior The if, switch and for Statements.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Chapter Topics Chapter 16 discusses the following main topics:
COMP 51 Week Fourteen Recursion.
Chapter 5: Control Structures II
Recursion Chapter 10.
Java Software Structures: John Lewis & Joseph Chase
The Basics of Recursion
Recursion Definition: A method that calls itself. Examples
Recursion.
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Building Java Programs
Self-Referencing Functions
Recursive Thinking.
Presentation transcript:

© Calvin College, recursion n. See recursion. See also tail recursion. - The Jargon Dictionary, v

© Calvin College, Additional Control Structures ● Advanced control statements: – Selection: Switch statement;Switch statement – Repetition: Non-counting loops.Non-counting loops ● Recursion Recursion

© Calvin College, The switch Statement ● Multi-alternative selection can be implemented using: – The multi-branch if statement – The switch statement ● The multi-branch if statement can always be used. ● The switch statement is a specialized form that operates more efficiently under certain circumstances.

© Calvin College, switch : Syntax and Behavior expression StatementList 1 value 1 StatementList 2 value 2 StatementList n switch (expression) { case value 1 : statementList 1 case value 2 : statementList 2 … default: statementList n } Requirements: ● expression must be integer compatible; ● values must be constants or literals; ● Cases check for equality. otherwise

© Calvin College, switch & if : Examples switch (dayCode) { case 1: System.out.println("Sunday"); break; case 2: System.out.println("Monday"); break; case 3: System.out.println("Tuesday"); break; case 4: System.out.println("Wednesday"); break; case 5: System.out.println("Thursday"); break; case 6: System.out.println("Friday"); break; case 7: System.out.println("Saturday"); break; default: System.out.println("invalid code"); } if (dayCode == 1) { System.out.println("Sunday"); } else if (dayCode == 2) { System.out.println("Monday"); } else if (dayCode == 3) { System.out.println("Tuesday"); } else if (dayCode == 4) { System.out.println("Wednesday"); } else if (dayCode == 5) { System.out.println("Thursday"); } else if (dayCode == 6) { System.out.println("Friday"); } else if (dayCode == 7) { System.out.println("Saturday"); } else { System.out.println("invalid code); }

© Calvin College, switch : Fall-Through Behavior ● The switch statement implemented fall- through behavior. ● More than one case can be associated with one statement list. ● If only one list is to be executed, then that list must explicitly exit the switch using: – break – return – throw – exit

© Calvin College, switch : Examples switch (dayCode) { case 1: System.out.println("Sunday"); break; case 2: System.out.println("Monday"); break; case 3: System.out.println("Tuesday"); break; case 4: System.out.println("Wednesday"); break; case 5: System.out.println("Thursday"); break; case 6: System.out.println("Friday"); break; case 7: System.out.println("Saturday"); break; default: System.out.println("invalid code"); } switch (dayCode) { case 1: case 7: System.out.println("weekend"); break; case 2: case 3: case 4: case 5: case 6: System.out.println("weekday"); break; default: System.out.println("invalid code"); }

© Calvin College, package c09quality.application_temperature; public class Temperature2 { // absolute value constants copied here... public static enum ScaleName { FAHRENHEIT, CELSIUS, KELVIN } private double myDegrees; private ScaleName myScale; public Temperature2() { myDegrees = 0.0; myScale = ScaleName.CELSIUS; } public Temperature2(double degrees, ScaleName scale) throws Exception{ if (isValid(degrees, scale)) { myDegrees = degrees; myScale = scale; } else { throw new Exception("Invalid temperature: " + degrees + " " + scale); } private static boolean isValid(double degrees, ScaleName scale) { switch (scale) { case CELSIUS: return degrees >= ABSOLUTE_ZERO_CELSIUS; case FAHRENHEIT: return degrees >= ABSOLUTE_ZERO_FAHRENHEIT; case KELVIN: return degrees >= ABSOLUTE_ZERO_KELVIN; default: return false; } Continued

© Calvin College, public void setScale(ScaleName scale) { switch (scale) { case CELSIUS: convertToCelsius(); break; case FAHRENHEIT: convertToFahrenheit(); break; case KELVIN: convertToKelvin(); break; } private void convertToCelsius() { switch (myScale) { case FAHRENHEIT: myDegrees = 5.0 / 9.0 * (myDegrees ); break; case KELVIN: myDegrees = myDegrees ; break; } myScale = ScaleName.CELSIUS; } // two conversion methods copied here... }

© Calvin College, package c09quality.application_temperature; import static c09quality.application_temperature.Temperature2.ScaleName.CELSIUS; import static c09quality.application_temperature.Temperature2.ScaleName.FAHRENHEIT; import static c09quality.application_temperature.Temperature2.ScaleName.KELVIN; // other imports included here... public class Temperature2ConverterController extends JFrame implements ActionListener { // other methods included public void actionPerformed(ActionEvent ae) { String actionCommand = ae.getActionCommand(); double degrees; messageField.setText(""); try { if (actionCommand.equalsIgnoreCase("celsius")) { degrees = Double.parseDouble(celsiusField.getText()); temperature = new Temperature2(degrees, CELSIUS); } else if (actionCommand.equalsIgnoreCase("fahrenheit")) { degrees = Double.parseDouble(fahrenheitField.getText()); temperature = new Temperature2(degrees, FAHRENHEIT); } else if (actionCommand.equalsIgnoreCase("kelvin")) { degrees = Double.parseDouble(kelvinField.getText()); temperature = new Temperature2(degrees, KELVIN); } } catch (Exception e) { messageField.setText("Illegal input: " + e.getMessage()); } if (temperature != null) { temperature.setScale(CELSIUS); thermometer.setTemperature(temperature.getDegrees()); celsiusField.setText(new Double(temperature.getDegrees()).toString()); temperature.setScale(FAHRENHEIT); fahrenheitField.setText(new Double(temperature.getDegrees()).toString()); temperature.setScale(KELVIN); kelvinField.setText(new Double(temperature.getDegrees()).toString()); } // other methods included here... }

© Calvin College, Non-Counting Loops ● So far, we’ve used counting for loops. ● There are other repetition statements: – while statement – do - while statement – Forever loop

© Calvin College, The while Loop while (condition) statement condition False True A while loop executes a statement based on a boolean condition. Statement

© Calvin College, while : Example System.out.print("Guess a number from 1-10: "); int number = -1; while (number != 7) { number = keyboard.nextInt(); } System.out.println("You guessed it!");

© Calvin College, The do - while Loop do statement while (condition); A do - while loop is a post-test version of the while statement. statement condition True False

© Calvin College, do-while : Example System.out.print("Guess a number from 1-10: "); do { number = keyboard.nextInt(); } while (number != 7); System.out.println("You guessed it!");

© Calvin College, The Forever Loop while (true) { StatementList 1 if (ExitCondition) break; StatementList 2 } StatementList 1 ExitCondition StatementList 2 True False A for loop without the sub-expressions is a non-counting, non-conditional loop.

© Calvin College, Forever Example System.out.print("Guess a number from 1-10 (0 to give up): "); while (true) { number = keyboard.nextInt(); if (number == 7) { System.out.println("You guessed it!"); break; } else if (number == 0) { System.out.println("Quitter!"); break; } System.out.print("try again: "); }

© Calvin College, while : Example public static int computeGCD(int a, int b) throws Exception { if ((a <= 0) || (b <= 0)) { throw new Exception("illegal values"); } int remainder = 1; while (remainder != 0) { remainder = a % b; if (remainder == 0) { return b; } else { a = b; b = remainder; } return 0; }

© Calvin College, package c09quality.application_temperature; public class Temperature2 { // absolute value constants copied here... public static enum ScaleName { FAHRENHEIT, CELSIUS, KELVIN } private double myDegrees; private ScaleName myScale; public Temperature2() { myDegrees = 0.0; myScale = ScaleName.CELSIUS; } public Temperature2(double degrees, ScaleName scale) throws Exception{ if (isValid(degrees, scale)) { myDegrees = degrees; myScale = scale; } else { throw new Exception("Invalid temperature: " + degrees + " " + scale); } private static boolean isValid(double degrees, ScaleName scale) { switch (scale) { case CELSIUS: return degrees >= ABSOLUTE_ZERO_CELSIUS; case FAHRENHEIT: return degrees >= ABSOLUTE_ZERO_FAHRENHEIT; case KELVIN: return degrees >= ABSOLUTE_ZERO_KELVIN; default: return false; }

© Calvin College, Recursion ● Introduction Introduction ● Examples: – Factorial; Factorial – Towers of Hanoi; Towers of Hanoi – Graphical examples. Graphical examples ● Lisp Lisp

© Calvin College, Introduction ● Recursion is a way of defining functions self-referentially. ● Examples: – Droste effect; – (Parenthetical comments (especially comments within comments), etc.); – A chain of phone callers on hold; – Inductive Proofs. Image from google.com

© Calvin College, from The Cat in the Hat Comes Back, Dr. Seuss

© Calvin College, Example: Factorial ● Write a function that, given n, computes n! n! == 1 * 2 *... * (n-1) * n ● Example: 5! == 1 * 2 * 3 * 4 * 5 == 120 ● Assumptions: – Receive n, a non-negative integer. – Return n!, a double (to avoid integer overflow).

© Calvin College, Preliminary Analysis ● This can be viewed as a counting problem, so we could solve it iteratively: public static int factorial1(int n){ int result = 1; for (int i = 2; i <= n; i++) result *= i; return result; }

© Calvin College, An Alternate Analysis ● Consider the following alternate analysis: n! == 1 * 2 *... * (n-1) * n (n-1)! == 1 * 2 *... * (n-1) n! == (n-1)! * n ● Historically, this is how the factorial function was defined.

© Calvin College, 2009 The Mechanics of Recursion Design recursive functions using a three- step process: 1. Identify a base case - an instance of the problem whose solution is trivial. E.g., The factorial function has two base cases: if n == 0: n! == 1 if n == 1: n! == 1 from The Cat in the Hat Comes Back, Dr. Seuss

© Calvin College, Identify an induction step - a means of solving the non-trivial instances of the problem using one or more “smaller” instances of the problem. E.g., n! == (n-1)!  n Mechanics (cont.) from The Cat in the Hat Comes Back, Dr. Seuss

© Calvin College, Form an algorithm that includes the base case and induction step, and ensures that each inductive step moves toward the base case. factorial: Receive n. If n > 1 then Return factorial(n-1) * n. Else Return 1. Mechanics (cont.) from The Cat in the Hat Comes Back, Dr. Seuss

© Calvin College, 2009 public static int factorial(int n) { if (n > 1) return factorial(n-1) * n; else return 1; } Implementation

© Calvin College, 2009 ● Move disks from a source pin to a target pin, using the third pin as an auxiliary. ● Rules: – move only one disk at a time; – never put a larger disk onto a smaller one. Example: Towers of Hanoi

© Calvin College, 2009 ● Today’s problem is to write a program that generates the instructions for the priests to follow in moving the disks. ● While quite difficult to solve iteratively, this problem has a simple and elegant recursive solution. Design

© Calvin College, 2009 public static void main (String [] args) { Scanner keyboard = new Scanner(System.in); System.out.println("The Hanoi Towers\n"); System.out.print("number of disks: "); int n = keyboard.nextInt(); move(n, 'A', 'B', 'C'); } Driver Program

© Calvin College, 2009 ● Base case: ● Inductive case: Design (cont.)

© Calvin College, 2009 We can combine these steps into the following algorithm: 0.Receive n, src, dest, aux. 1.If n > 1: a. move(n-1, src, aux, dest); b. move(1, src, dest, aux); c. move(n-1, aux, dest, src); Else Display “Move the top disk from ” + src + “ to ” + dest. Algorithm

© Calvin College, 2009 public static void move(int n, char src, char dest, char aux) { if (n > 1) { move(n-1, src, aux, dest); move(1, src, dest, aux); move(n-1, aux, dest, src); } else System.out.println("Move the top disk from " + src + " to " + dest); } Implementation

© Calvin College, 2009 How many “moves” does it take to solve this problem as a function of n, the number of disks to be moved. n# of moves required______ i2 i Algorithm Analysis

© Calvin College, 2009 Given a “super-printer” that can generate and print 1,048,576 (2 20 ) instructions/second, how long would it take to print instructions ? 2 64 /2 20 = 2 44 seconds  2 44 / 2 6 = 2 38 minutes  2 38 / 2 6 = 2 32 hours  2 32 / 2 5 = 2 27 days  2 27 / 2 9 = 2 18 years  2 18 / 2 7 = 2 11 centuries  2 11 / 2 4 = 2 7 = 128 millennia Analysis (cont.)

© Calvin College, Graphical Examples ● Sierpinski triangles ● Tree fractals ● Koch’s snowflake

© Calvin College, ● McCarthy received the Turing award in 1971 for his seminal contributions to AI (including the name of the field). ● He developed Lisp, one of the oldest programming languages. John McCarthy Artificial Intelligence What’s the Big Idea from (defun reverse (l acc) “reverses the order of the element of L” (if (null l) acc (reverse (cdr l) (cons (car l) acc))))