Recursion 10-23-2002. Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? Don’t let problems hang.

Slides:



Advertisements
Similar presentations
Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
Advertisements

Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
Recursion in Python. Recursion Problems in every area of life can be defined recursively, that is, they can be described in terms of themselves. An English.
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
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:
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
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:
29-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 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.
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
Dynamic Programming Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have any questions.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Intro to C++ And Some Tools Opening Discussion zHave any questions come up since last class? Have you had a chance to look over the project.
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 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)
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
More Red-Black Trees Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zDo you have.
Recursion Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zWhat is a recursive function?
String Processing Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have questions.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Do-while and for Loops Opening Discussion zWhat did we talk about last class? zAt the end of last class I asked you to write a loop that would.
MIPS Machine Assembly Language Opening Discussion zWhat did we talk about last class? zHave you seen anything interesting in the news? zDo you.
IB Computer Science Unit 5 – Advanced Topics Recursion.
AVL Trees Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignments? zYour minute essay last.
1 CS 177 Week 12 Recitation Slides Running Time and Performance.
Threads Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment?
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Arrays: Part Opening Discussion zWhat did we talk about last class? zWhat do you think the picture of memory looks like when we declare.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Java Graphics Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zOffset between class.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
Red-Black Trees Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have any questions.
Mergesort and Quicksort Opening Discussion zWhat did we talk about last class? zDo you have any questions about assignment #4? Have you thought.
Inheritance and Virtual Functions Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class?
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Priority Queues Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignments? The designs for assignment.
Graphics in Java Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have any questions.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
Function Recursion to understand recursion you must understand recursion.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
Stack, Queues, and Priority Queues: Linked List Based
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Java 4/4/2017 Recursion.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Recursion, Tail Recursion
Recursion Output Input
slides adapted from Marty Stepp and Hélène Martin
Fundamentals of Programming
Recursion.
Augmented Data Structures and the Test
Recursion Taken from notes by Dr. Neil Moore
Fundaments of Game Design
slides adapted from Marty Stepp and Hélène Martin
Last Class We Covered Recursion Stacks Parts of a recursive function:
slides created by Marty Stepp
Presentation transcript:

Recursion

Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? Don’t let problems hang you up too long. zWhat happens in C when a function calls another function? What about when a function calls itself? Can we utilize this to do interesting things?

Min Element of 2D Array zReturn does not mean print. int minElem(int a[][10],int len) { int min=a[0][0],j,k; for(j=0; j<len; j++) { for(k=0; k<10; k++) { if(a[j][k]<min) { min=a[j][k]; } return min; }

Encrypt and Decrypt zLet’s look really quick at possible implementations of encrypt and decrypt from assignment #4. zNote the use of a static variable to keep track of how many characters had been read in the two functions. Outside of that, the logic is fairly simple, but it has to be done in the right order.

Recursive Functions zWe have looked at functions calling other functions, but what about when they call themselves? A function that calls itself is called a recursive function. They are heavily used in the mathematical theory of CS, but they can also be incredibly powerful tools in our code. zThere are some problems that are easy to solve with recursion, but extremely hard to solve without it.

Recursion and the Stack zThe real key to recursion is that one the stack, every call to the function gets its own stack frame and hence its own copy of all the local variables and arguments. zThis gives recursion some “memory” that it can go back to when functions return. zLanguages that don’t use a stack for function calls can’t be used for writing recursion.

Looping with Recursion zThe simplest use of recursion is simply to implement a loop. When doing this, typically a function takes a value as an argument and calls itself with a larger or smaller value of that argument. zThe recursive call has to be conditional. Otherwise it is like an inifinite loop, but this type causes a stack overflow and a seg fault.

Recursion in Multiple Directions zRecursion is more powerful (and more useful) when the function contains two or more possible calls of itself. In this case, a loop can’t be easily used as a substitute without major logic modifications. zDrawing out the call path of these types of functions produces what computer scientists call a tree. zNormally I do Fibonacci numbers for this.

Flood Fill zIf you have used a painting package, you are familiar with the idea of “filling” the area around a specific location with a certain color. zOne way of doing this easily in code is with a recursive function that calls itself up to 4 times if adjacent pixels are blank. zLet’s write a bit of code for simple loops and this.

Minute Essay zWrite a recursive function to calculate Fibonacci numbers. zEnjoy fall break and I will see you next week. I’ll actually be out of town from Thursday afternoon until Sunday and it is very unlikely that I will be able to get to so you really need to get your questions to me by early tomorrow.