Fundaments of Game Design

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
Copyright © 2003 Pearson Education, Inc. Slide 1.
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.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
M180: Data Structures & Algorithms in Java
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 8 Recursion Modified.
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 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Lecture 13 Recursion part 2 Richard Gesick. Advanced Recursion Sometimes recursion involves processing a collection. While it could be done using 'foreach'
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.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Function Recursion to understand recursion you must understand recursion.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Recursion Powerful Tool
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
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.
to understand recursion you must understand recursion
Recursion DRILL: Please take out your notes on Recursion
RECURSION.
Recursion CSC 202.
To understand recursion, you have to understand recursion!
Programming Fundamentals Lecture #7 Functions
Recursion, Tail Recursion
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
to understand recursion you must understand recursion
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
MSIS 655 Advanced Business Applications Programming
Applied Algorithms (Lecture 17) Recursion Fall-23
Lecture 17 Recursion part 1 Richard Gesick.
Fundamentals of Programming
Lecture 18 Recursion part 2 Richard Gesick.
Basics of Recursion Programming with Recursion
Lecture 13 Recursion part 2 CSE /26/2018.
Lecture 12 Recursion part 1 CSE /26/2018.
Recursion.
Presentation transcript:

Fundaments of Game Design Recursion Richard Gesick

Objectives Simple single recursion Multiple recursion Non-linear recursion Object based recursion

Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only the base case(s). Each method call divides the problem into two conceptual pieces: a piece that the method knows how to do and a recursive call, or recursion step that solves a smaller problem. A sequence of returns ensues until the original method call returns the result to the caller.

Activation Stack We use the concept of the activation stack to keep track of what memory and instructions "live" inside of each method.  When a method is invoked, a new frame for it is placed atop the activation stack.  When a method is finished, the frame is removed and control is returned to the next frame down.

Recursive Technique Design First: Determine the base case, ie, what is the stopping point for the recursion. It should normally be the simplest case. Second: What is the case that is just one step above it? Can it be generalized enough to fit?

Common Programming Error Either omitting the base case or writing the recursion step incorrectly so that it does not converge on the base case will cause infinite recursion, eventually exhausting memory. This error is analogous to the problem of an infinite loop in an iterative (non-recursive) solution.

Stack Overflow Avoid stack overflow.  Stack overflow occurs when you recurse too many times and run out of memory.  Often, it is more efficient to perform calculations via iteration (looping), but it can also be easier to express an algorithm via recursion. Recursion is especially useful for non-linear situations

Advanced Recursion Sometimes recursion involves processing a collection.  While it could be done using 'foreach' or some other iterative approach, recall that recursion works best for non-linear structures.  Recursion is also useful for making use of the activation stack as a "reminder" of where we've been and what remains to be done.

Non-linear Recursion Folders and files within a computer system are an excellent example of a non-linear structure that is easily processed via recursion.  This is also possible using iteration, but we would have more coding work to do in handling where we've been in the file system.  We can leverage the activation stack to remember for us.

Non-linear Recursion Consider the steps to count the total number of files: If we're in a folder with no files (or subfolders), then we return 0 Otherwise, we return the count of the files in the folder + all the files in the subfolders It's the second part that's tricky... but notice that's where the recursive call comes in.  We just need to recursively call for each subfolder (so this is recursion wrapped inside of iteration):

solution private int CountFiles(DirectoryInfo di) { int file_count = 0; file_count += di.GetFiles().Length; foreach (DirectoryInfo sub_folder in di.GetDirectories())     file_count += CountFiles(sub_folder); return file_count; }

Is this recursion? class Box { Box[] arrayOfBoxes; int boxID; void Print () { Console.Writeline (“Box ”+ID+“ is here!”); foreach (Box b in arrayOfBoxes) { b.Print(); }