Announcement The fourth test will be 75 minutes, will consist of two parts and will take place next week. The programming part will be about Chapter 2-6,

Slides:



Advertisements
Similar presentations
Recursive Definitions and Structural Induction
Advertisements

Recursion.
Recursive Definitions Rosen, 3.4. Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite 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.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
Department of Computer Engineering Recursive Problem Solving Computer Programming for International Engineers.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
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.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
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,
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
R ECURRSION Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University of Hail Chapter.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
Writing recursive methods. We’ve seen a couple of examples Change maker Factorial Categories of problems suited to recursion – natural language processing.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Program Development and Design Using C++, Third Edition
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
1 Recursively Enumerable and Recursive Languages.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Recursion.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Topic 6 Recursion.
MSc/ICY Software Workshop , Semester 2
Recursion (Part 2).
RECURSION.
Recursion notes Chapter 8.
CS 211 Object Oriented Programming
Review: recursion Tree traversals
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Chapter 10.
Java Software Structures: John Lewis & Joseph Chase
Model-view-controller
Chapter 5: Control Structures II
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
FOR LOOPS.
Chapter 12 Recursion (methods calling themselves)
Proving algorithms (programs) correct with induction
Programming application CC213
Unit 3 Test: Friday.
Java Programming: Chapter 9: Recursion Second Edition
Recursion notes Chapter 8.
Algorithms An algorithm is a set of instructions used to solve a specific problem In order to be useful, an algorithm must have the following properties:
Summary.
ITEC324 Principle of CS III
Recursive Thinking.
Presentation transcript:

Announcement The fourth test will be 75 minutes, will consist of two parts and will take place next week. The programming part will be about Chapter 2-6, excluding Section 2.6, 4.5, and 6.8.8. The "written" part will be about Chapter 2-6, excluding Section 2.6, 4.5, and 6.8.8. During the test, you will have access to the textbook. You may bring a blank piece of paper to the test

Recursion notes Chapter 8

Fibonacci Numbers the sequence of additional pairs 0, 1, 1, 2, 3, 5, 8, 13, ... are called Fibonacci numbers base cases F(0) = 0 F(1) = 1 recursive definition F(n) = F(n – 1) + F(n – 2)

Recursive Methods & Return Values a recursive method can return a value example: compute the nth Fibonacci number public static int fibonacci(int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; else { int f = fibonacci(n - 1) + fibonacci(n - 2); return f;

Fibonacci Call Tree F(5) F(4) F(3) F(3) F(2) F(2) F(1) 1 F(2) F(1) 1 F(1) 1 F(0) F(1) 1 F(0)

A Better Recursive Fibonacci public class Fibonacci { private static Map<Integer, Long> values = new HashMap<Integer, Long>(); static { Fibonacci.values.put(0, (long) 0); Fibonacci.values.put(1, (long) 1); } public static long getValue(int n) { Long value = Fibonacci.values.get(n); if (value != null) { return value; value = Fibonacci.getValue(n - 1) + Fibonacci.getValue(n - 2); Fibonacci.values.put(n, value);

Better Fibonacci Call Tree 2 F(3) F(2) 1 F(2) F(1) 1 F(1) 1 F(0)

Compute Powers of 10 write a recursive method that computes 10n for any integer value n recall: 10n = 1 / 10-n if n < 0 100 = 1 10n = 10 * 10n-1

public static double powerOf10(int n) { if (n < 0) { return 1 public static double powerOf10(int n) { if (n < 0) { return 1.0 / powerOf10(-n); } else if (n == 0) { return 1.0; return n * powerOf10(n - 1);

A Better Powers of 10 recall: 10n = 1 / 10-n if n < 0 100 = 1 10n = 10 * 10n-1 if n is odd 10n = 10n/2 * 10n/2 if n is odd

public static double powerOf10(int n) { if (n < 0) { return 1 public static double powerOf10(int n) { if (n < 0) { return 1.0 / powerOf10(-n); } else if (n == 0) { return 1.0; else if (n % 2 == 1) { return 10 * powerOf10(n - 1); double value = powerOf10(n / 2); return value * value;

Proving Correctness and Termination to show that a recursive method accomplishes its goal you must prove: that the base case(s) and the recursive calls are correct that the method terminates

Proving Correctness to prove correctness: prove that each base case is correct assume that the recursive invocation is correct and then prove that each recursive case is correct

printItToo public static void printItToo(String s, int n) { if (n == 0) { return; } else { System.out.print(s); printItToo(s, n - 1);

Correctness of printItToo (prove the base case) If n == 0 nothing is printed; thus the base case is correct. Assume that printItToo(s, n-1) prints the string s exactly(n – 1) times. Then the recursive case prints the string s exactly(n – 1)+1 = n times; thus the recursive case is correct.

Proving Termination to prove that a recursive method terminates: define the size of a method invocation; the size must be a non-negative integer number prove that each recursive invocation has a smaller size than the original invocation

Termination of printItToo printItToo(s, n) prints n copies of the string s; define the size of printItToo(s, n) to be n The size of the recursive invocation printItToo(s, n-1) is n-1 (by definition) which is smaller than the original size n.

countZeros public static int countZeros(long n) { if(n == 0L) { // base case 1 return 1; } else if(n < 10L) { // base case 2 return 0; boolean lastDigitIsZero = (n % 10L == 0); final long m = n / 10L; if(lastDigitIsZero) { return 1 + countZeros(m); else { return countZeros(m);

Correctness of countZeros (base cases) If the number has only one digit then the method returns 1 if the digit is zero and 0 if the digit is not zero; therefore, the base case is correct. (recursive cases) Assume that countZeros(n/10L) is correct (it returns the number of zeros in the first (d – 1) digits of n). If the last digit in the number is zero, then the recursive case returns 1 + the number of zeros in the first (d – 1) digits of n, otherwise it returns the number of zeros in the first (d – 1) digits of n; therefore, the recursive cases are correct.

Termination of countZeros Let the size of countZeros(n) be d the number of digits in the number n. The size of the recursive invocation countZeros(n/10L) is d-1, which is smaller than the size of the original invocation.

Proving Correctness and Termination prove that fibonacci is correct and terminates prove that powersOf10 is correct and terminates

Proving Termination prove that the algorithm on the next slide terminates

public class Print { public static void done(int n) { if (n == 1) { System.out.println("done"); } else if (n % 2 == 0) { System.out.println("not done"); Print.done(n / 2); else { Print.done(3 * n + 1);