RECURSION Annie Calpe 11.12.04.

Slides:



Advertisements
Similar presentations
Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
Advertisements

Recursion.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Computer Science II Recursion Professor: Evan Korth New York University.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
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.
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)
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 12 Recursion, Complexity, and Searching and Sorting
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.
CIS 068 Welcome to CIS 068 ! Stacks and 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.
1 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
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.
Java Programming: Guided Learning with Early Objects Chapter 11 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.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
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.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.
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.
Recursion Powerful Tool
CS212: Data Structures and Algorithms
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Run-Time Environments Chapter 7
Topic 6 Recursion.
Chapter 15 Recursion.
Introduction to Recursion
Introduction to Recursion
Recursion Lakshmish Ramaswamy.
Recursion DRILL: Please take out your notes on Recursion
Decrease-and-Conquer Approach
Chapter 15 Recursion.
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
To understand recursion, you have to understand recursion!
Programming Fundamentals Lecture #7 Functions
Introduction to Computer Science - Alice
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
MSIS 655 Advanced Business Applications Programming
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion Chapter 11.
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Fundamentals of Programming
Recursion Data Structures.
Stack Frames and Functions
Module 1-10: Recursion.
Basics of Recursion Programming with Recursion
UNIT V Run Time Environments.
Lesson #6 Modular Programming and Functions.
Recursion.
CSC 143 Recursion.
ITEC324 Principle of CS III
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Presentation transcript:

RECURSION Annie Calpe 11.12.04

Overview Introduction Review: the Basics How it works - Examples - Factorial - Fibonacci Sequence - Sierpinski Curve Stacks Applications Design Considerations

Introduction to Recursion Recursion can be used to manage repetition. Recursion is a process in which a module achieves a repetition of algorithmic steps by calling itself. Each recursive call is based on a different, generally simpler, instance.

Introduction to Recursion Recursion is something of a divide and conquer, top-down approach to problem solving. - It divides the problem into pieces or selects out one key step, postponing the rest.

4 Fundamental Rules : Base Case : Always have at least one case that can be solved without recursion. Make Progress : Any recursive call must progress towards a base case. Always Believe : Always assume the recursive call works. Compound Interest Rule : Never duplicate work by solving the same instance of a problem in separate recursive calls.

Basic Form : void recurse () { recurse (); //Function calls itself } int main () recurse (); //Sets off the recursion

How does it work? The module calls itself. New variables and parameters are allocated storage on the stack. Function code is executed with the new variables from its beginning. It does not make a new copy of the function. Only the arguments and local variables are new. As each call returns, old local variables and parameters are removed from the stack. Then execution resumes at the point of the recursive call inside the function.

Recursion Trees A key tool for analyzing recursive algorithms is the recursion tree. The total processing time is related to the total # of nodes The necessary storage space is related to its height

To Build a Recursion Tree: root = the initial call Each node = a particular call Each new call becomes a child of the node that called it A tree branch (solid line) = a call-return path between any 2 call instances

Factorial Factorial (n): IF (n = 0) RETURN 1 ELSE RETURN n * Factorial (n-1) Calculates n*(n-1)*(n-2)*…*(1)*(1)

Fibonacci Numbers F (n) = F (n-1) + F (n-2) Fibonacci (n) IF (n <= 1) RETURN n ELSE RETURN Fibonacci (n-1) + Fibonacci (n-2) ** Inefficient use of recursion !!

Recursion Tree showing Fibonacci calls

Space Filling Curves A continuous mapping from a lower-dimensional space into a higher-dimensional one, using fractals. Fractals are shapes that occur inside other, similar shapes. A useful property of a space-filling curve is that it tends to visit all the points in a region once it has entered that region.

The Sierpinski Curve The “limiting curve” of an infinite sequence of curves numbered by an index n=1,2,3… It ends up covering every point in the region. Fills 2-D space (fills a plane using lines)

The Sierpinski Curve ZIG (n): if (n = 1) turn left, advance 1 else ZAG (n/2) ZAG (n): if (n = 1) turn right, advance 1 turn left else ZAG (n/2) ZIG (n/2)

ZIG(4) – ¼ Complete ** End of first ZIG (2) call ZIG (4) ZAG ZIG

ZIG(4) – ½ Complete ZIG (4) ZIG (2) ZAG (2) ZIG (2) ZAG (2) ZIG (1)

ZIG(4) – ½ Complete ** End of first ZAG (2) call ZAG ZIG ZAG ZIG ZIG

ZIG(4) – The Rest? No need to go further. Why? ANSWER: Because of Rule #3 - We’ve shown that the base case works as well as the next case.

Run-time Stack Use Recursion is controlled in a computer by means of a pushdown stack. A push = a new function call A pop = a completed execution of a function call Stack overflow is possible A move down = a push onto runtime stack A move up = a pop & a move down = a push

Some Uses For Recursion Numerical analysis Graph theory Symbolic manipulation Sorting List processing Game playing General heuristic problem-solving Tree traversals

Why use it? PROS CONS Clearer logic Often more compact code Often easier to modify Allows for complete analysis of runtime performance CONS Overhead costs

Summary Recursion can be used as a very powerful programming tool There is a tradeoff between time spent constructing and maintaining a program and the cost in time and memory of execution.

References The New Turing Omnibus – Dewdney Data Structures & Problem Solving in Java – Weiss Computing and Algorithm – Shackelford Recursion Tutorial – National University of Ireland, Dept of I.T.