Let’s try it one more time!. Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what.

Slides:



Advertisements
Similar presentations
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Advertisements

Lesson 1-5 Solving Inequalities September Objective:
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Proof Points Key ideas when proving mathematical ideas.
Computer Science 1620 Loops.
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
2420 Review Questions Chapter 6.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Midterm Logistics Where? 2050 VLSB When? Monday, 4:10 to 5:40 What to do? –“Review problems” from Thursday/Friday in UCWise. –Two practice exams and solutions.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
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.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 8: Recursion Data Structures.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
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.
The switch Statement, DecimalFormat, and Introduction to Looping
This will let you see the game you are going to play on the inside. It will help you see what all goes into creating instance to that goes into setting.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Some Notes on Recursion Data.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
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 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.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Section 3.1: Proof Strategy Now that we have a fair amount of experience with proofs, we will start to prove more difficult theorems. Our experience so.
Recursion Self-Referencing Functions. Problem # 1 Write a function that, given n, computes n! n! == (n-1) n n! == 1  2 ...  (n-1)  nExample:
LOOPING What are the 3 structures of writing code? sequential decision repetition Def. Looping is the repetition of statements in a program. There various.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Data Structures Recursion Phil Tayco Slide version 1.0 Mar. 8, 2015.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 Put your name if you want your attendance credit. Please put your as well. CSC 320 is done at 11:30 so I did NOT fill in the box for 11:30 for.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Week 6.  Lab 1 and 2 results  Common mistakes in Style  Lab 1 common mistakes in Design  Lab 2 common mistakes in Design  Tips on PE preparation.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Decision Making and Branching (cont.)
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Integers are any numbers from a set. Integers are also negative and positive numbers. Positive integers are greater than zero. They can be written with.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Week91 APCS-A: Java Problem Solving November 2, 2005.
CPS Today’s topics Programming Recursion Invariants Reading Great Ideas, p Brookshear, Section Upcoming Copyrights, patents, and.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion Topic 5.
Recursion -- Introduction
Recursion Practice requiring pointers
Java Software Structures: John Lewis & Joseph Chase
Announcements Final Exam on August 17th Wednesday at 16:00.
Chapter 12 Recursion (methods calling themselves)
Searching, Sorting, and Asymptotic Complexity
Self-Referencing Functions
Presentation transcript:

Let’s try it one more time!

Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what the procedure will do. What are its inputs and what are its outputs. In a sentence, what does it accomplish? 3.Restate the problem recursively. Look for instances of the problem you defined in step 2. If the procedure returns a value, recursive calls to the function must use the returned values and every path through the function must return a value. 4.Write the procedure using instances of itself. Only worry about a single call of the routine. This is similar to proof by induction. You only solve for one case, but all other cases take care of themselves. Do not be tempted to follow the recursion. Use the routines having the faith that they will work. If you try to follow the recursion while you are writing the procedure, you will become hopelessly lost. How can you follow the recursion when the recursive routine is not finished yet? 5.Make sure you take care of the base case stopping the recursion. I like to take care of this last as it is easier to pinpoint the ending conditions after the general case has been written. Some personalities may not be able to delay this decision, but you will find coding is faster if you are able to delay it. It is like delaying the condition on a loop. In all but the simplest of loops, it is easier to write the termination condition after the loop body is written. 6.Once the routine is written, go ahead and follow the recursion, if you wish.

Let’s try it This question involves a game with teddy bears. The game starts when I give you some bears. You can then give back some bears, but you must follow these rules (where ct is the number of bears that you have): If ct is even, then you may give back exactly ct/2 bears. If ct is divisible by 3 or 4, then you may multiply the last two digits of ct and give back this many bears. (By the way, the last digit of ct is ct%10, and the next-to-last digit is ((ct%100)/10). If ct is divisible by 5, then you may give back exactly 42 bears. The goal of the game is to end up with EXACTLY 42 bears. 1. Decide that the problem needs a recursive solution. This one cries for a recursive solution!

SO…. For example, suppose that you start with 250 bears. Then you could make these moves: --Start with 250 bears. --Since 250 is divisible by 5, you may return 42 of the bears, leaving you with 208 bears. --Since 208 is even, you may return half of the bears, leaving you with 104 bears. --Since 104 is even, you may return half of the bears, leaving you with 52 bears. --Since 52 is divisible by 4, you may multiply the last two digits (resulting in 10) and return these 10 bears. This leaves you with 42 bears. --You have reached the goal!

Try it! 2. Decide specifically what the procedure will do. What are its inputs and what are its outputs. In a sentence, what does it accomplish?

Boolean winBearGame(int ct) Returns true if you can win the Bear Game starting with ct bears.

Try it 3. Restate the problem recursively. Look for instances of the problem you defined in step 2. Be sure to use “winBearGame” in defining “winBearGame”. If you don’t, it isn’t recursive.

Return true if you have 42 bears or if you have double 42 bears.

Bad, no recursive call of winBearGames Second attempt: return true if you have 42 bears return true if ct is divisible by 5 AND winBearGame(ct-42) There are other cases, but this seems to be the right approach. Try to define the rest of it.

Try it 3. Restate the problem recursively. Look for instances of the problem you defined in step 2. Return true if one of these things happen: Ct = 42 Ct%2==0 and winBearGame(ct – ct/2) Ct%3 ==0|| ct%4 ==0 and winBearGame(ct-multOfDigits) Ct%5==0 and winBearGame(ct-42)

Try it! 4. Write the procedure using instances of itself. Only worry about a single call of the routine. This is similar to proof by induction. You only solve for one case, but all other cases take care of themselves.

Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0) winBearGame(ct – ct/2) if (ct%3==0|| ct%4==0){ dig1 = ct%10; dig2 = ((ct%100/10) winBearGame(ct-dig1*dig2) if (ct%5 ==0) winBearGame(ct-42) }

We didn’t use the return values! If you don’t use them, it is likely wrong. Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0) winBearGame(ct – ct/2) if (ct%3==0|| ct%4==0){ dig1 = ct%10; dig2 = ((ct%100/10) winBearGame(ct-dig1*dig2) if (ct%5 ==0) winBearGame(ct-42) }

But now we don’t always return a value Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0) return winBearGame(ct – ct/2) if (ct%3==0|| ct%4==0){ dig1 = ct%10; dig2 = ((ct%100/10) return winBearGame(ct-dig1*dig2) if (ct%5 ==0) return winBearGame(ct-42) }

How close are we? Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0) return winBearGame(ct – ct/2) if (ct%3==0|| ct%4==0){ dig1 = ct%10; dig2 = ((ct%100/10) return winBearGame(ct-dig1*dig2) if (ct%5 ==0) return winBearGame(ct-42) return false; }

Just because we CAN give back have the bears doesn’t me we WANT to. Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0) return winBearGame(ct – ct/2) if (ct%3==0|| ct%4==0){ dig1 = ct%10; dig2 = ((ct%100/10) return winBearGame(ct-dig1*dig2) if (ct%5 ==0) return winBearGame(ct-42) }

How about this… Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0 and winBearGame(ct – ct/2)) return true; dig1 = ct%10; dig2 = ((ct%100/10) if (ct%3==0|| ct%4==0) and winBearGame(ct-dig1*dig2) return true; if (ct%5 ==0) and winBearGame(ct-42) return true; return false; }

Try it! 5. Make sure you take care of the base case stopping the recursion. I like to take care of this last as it is easier to pinpoint the ending conditions after the general case has been written. What is the base case for our code? When do you stop executing?

What is the base case? We seem to have several. Do we have enough? Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0 and winBearGame(ct – ct/2)) return true; dig1 = ct%10; dig2 = ((ct%100/10) if (ct%3==0|| ct%4==0) and winBearGame(ct-dig1*dig2) return true; if (ct%5 ==0) and winBearGame(ct-42) return true; return false; }

Suppose ct became zero, what would happen? Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0 and winBearGame(ct – ct/2)) return true; dig1 = ct%10; dig2 = ((ct%100/10) if (ct%3==0|| ct%4==0) and winBearGame(ct-dig1*dig2) return true; if (ct%5 ==0) and winBearGame(ct-42) return true; return false; }

How about this? Boolean winBearGame( int ct) { if (ct==42) return true; if (ct < 42) return false; if (ct%2 ==0 and winBearGame(ct – ct/2)) return true; dig1 = ct%10; dig2 = ((ct%100/10) if (ct%3==0|| ct%4==0) and winBearGame(ct-dig1*dig2) return true; if (ct%5 ==0) and winBearGame(ct-42) return true; return false; }

Now go ahead and trace it, if you want. 6. Once the routine is written, go ahead and follow the recursion, if you wish. While I believe that you should know HOW to trace it, generally, the tracing will get you even more confused and you will think even correct code is wrong.