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.

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Recursion CS 367 – Introduction to Data Structures.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array.
Recursion Introduction to Computing Science and Programming I.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
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:
Programming with Recursion
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.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
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.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
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. 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.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
Recursion n General Form of Recursive methods n Examples of Simple Recursion n Hand Simulating Recursive methods n Proving Recursive methods Correct n.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
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.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
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.
CompSci Review of Recursion with Big-Oh  Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
CS 206 Introduction to Computer Science II 10 / 10 / 2008 Instructor: Michael Eckmann.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Let’s try it one more time!. Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what.
Recursion Powerful Tool
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Recursion 4-Jun-18.
Introduction to Computing Science and Programming I
Class 11: Two-argument recursion
Weighted Interval Scheduling
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursion 12-Nov-18.
Writing LISP functions
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Data Structures Review Session
Announcements Final Exam on August 19th Saturday at 16:00.
Recursion 29-Dec-18.
Stacks.
Recursion 10-Apr-19.
Recursion 23-Apr-19.
Lisp.
ITEC324 Principle of CS III
Recursive Thinking.
Presentation transcript:

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 consists of: –An open parenthesis, "(" –Zero or more atoms or lists, and –A close parenthesis, ")"

Definitions II Indirect recursion is when a thing is defined in terms of other things, but those other things are defined in terms of the first thing Example: A list is: –An open parenthesis, –Zero or more S-expressions, and –A close parenthesis An S-expression is an atom or a list

Understanding recursion The usual way to teach recursion is to "trace through" a recursion, seeing what it does at each level This may be a good way to understand how recursion works......but it's a terrible way to try to use recursion There is a better way

Base cases and recursive cases Every valid recursive definition consists of two parts: –One or more base cases, where you compute the answer directly, without recursion –One or more recursive cases, where you do part of the work, and recur with a simpler problem

Information hiding function spread (int A[], int size) { int max, min; sort(A, size); min = A[0]; max = A[size - 1]; return max - min; } Can you understand this function without looking at sort ?

Stepping through called functions Functions should do something simple and understandable When you try to understand a function, you should not have to step through the code of the functions that it calls When you try to understand a recursive function, you should not have to step through the code of the functions it calls

We have small heads It's hard enough to understand one level of one function at a time It's almost impossible to keep track of many levels of the same function all at once But you can understand one level of one function at a time......and that's all you need to understand in order to use recursion well

The four rules Do the base cases first Recur only with a simpler case Don't use global or reference variables Don't look down

Do the base cases first Every recursive function must have some things it can do without recursion These are the simple, or base, cases Test for these cases, and do them first This is just writing ordinary, nonrecursive code

Recur only with a simpler case If the problem isn't simple enough to be a base case, break it into two parts: –A simpler problem of the same kind (for example, a smaller number, or a shorter list) –Extra work not solved by the simpler problem Combine the results of the recursion and the extra work into a complete solution "Simpler" means "more like a base case"

Example 1: member Is value X a member of list L ? boolean member(X, L) { if (L is the empty list) return false; // this is a base case if (X equals the first element in L) return true; // another base case return member(L - first element, X); // simpler because more like empty list }

Example 2: double Double every element of a list of numbers boolean double(L) { if (L is the empty list) return the empty list; // base case else { L2 = double (L - first element); // recur D = 2 * first element in L; // extra work return (list made by adding D to L2); // combine }

It's OK to use locals variables and parameters passed by value A function has its own copy of –local variables –parameters passed by value Each level of a recursive function has its own copy of these variables and parameters Changing them at one level does not change them at other levels One level can't interfere with another level

It's bad to use global variables or parameters passed by reference There is only one copy of a global variable If a parameter is passed by reference, there is only one copy of it If such a variable is changed by a recursive function, it's changed at all levels The various levels interfere with one another This can get very confusing Don't let this happen to you!

Don't look down When you write or debug a recursive function, think about this level only Wherever there is a recursive call, assume that it works correctly If you can get this level correct, you will automatically get all levels correct You really can't understand more than one level at a time, so don't even try

Reprise Do the base cases first Recur only with a simpler case Don't use global or reference variables Don't look down

The End