Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion Just be thankful you're not getting all the government you're.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Factorial Recursion stack Binary Search Towers of Hanoi
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Recursion Definition: A method that calls itself. Recursion can be direct or indirect. Indirect recursion involves a method call that eventually recalls.
Computer Science II Recursion Professor: Evan Korth New York University.
Recursion. Binary search example postponed to end of lecture.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Programming with Recursion
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
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.
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.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
Run time vs. Compile time
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
1 Chapter 7 Recursion. 2 What Is Recursion? l Recursive call A method call in which the method being called is the same as the one making the call l Direct.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion More on Project 2 Reading L&C 10.1 – 10.4.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
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.
Prof. S.M. Lee Department of Computer Science. Answer:
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Compiler Construction
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
1 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
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.
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
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.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
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.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
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.
Recursion Powerful Tool
Programming with Recursion
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Recursion Lakshmish Ramaswamy.
Programming with Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion Data Structures.
Module 1-10: Recursion.
Recursion Taken from notes by Dr. Neil Moore
Yan Shi CS/SE 2630 Lecture Notes
Chapter 5 Recursion.
Recursion © Dave Bockus.
Presentation transcript:

Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion Just be thankful you're not getting all the government you're paying for. Will Rogers (1879-1935)

Recursion recursion in Mathematics factorial Fibonacci numbers define infinite set with finite definition in Computer Science syntax rules finite definition, infinite language

Recursive Methods recursive method a method that, directly or indirectly, invokes itself factorial derived from Mathematical definition 1 for n=0 for n>0, involves a method call method called, this method suspended value returned & used in expression long result Fibonacci numbers derived from mathematical definition for n>1, involves two method calls first call, then second call then expression

Principles and Rules Base cases: you must always have some base cases, which can be solved without recursion. Solving the recursive function f(x) = 2f(x-1) + x2 Example 1 Note a base case generally kills the recursion by not allowing future recursive calls. Base Case: since at this point recursion is not needed to determine a return value. int f (int x) { if (x == 0) return 0 else return 2*f(x-1)+x*x

Principles and Rules. Example 2 Head B C Head PrintList (node ptr){ if (ptr != null) { print(ptr.data); PrintList(ptr.next); } } If statement prevents recursive part from being called.

Principles and Rules.. Making progress: For the cases that are solved recursively, the recursive call must always be to a case that makes progress toward a base case. int f (int x) { if (x == 0) return 0 else return 2*f(x-1)+x*x X is reduced toward the base case. PrintList (node ptr){ if (ptr != null) { print(ptr.data); PrintList(ptr.next); } } We assume that ptr.next will take us toward the end of the list, hence a null pointer.

Method Execution method invocation suspend method pass parameters execute called method reactivate calling method storage for local variables & formal parameters separate storage for each invocation? if not one invocation interferes with another – poor abstraction somewhat more complex than original memory model

Memory Management requirements new storage for locals & parameters return point could be called from more than one place allocate storage on demand at method call activation record (AR) block of storage for method invocation to use represents method activation storage for all local variables, parameters, return point and other allocated at call, freed at return method execution note: locals become undefined at each call

Activation Record Stack method call/return pattern LIFO ARs are stacked push at call pop at return top AR stack visible memory allocation stack represented in an array (memory) hardware support code fixed size stack grows/shrinks in method/return call pattern heap dynamic allocation of objects may grow/shrink according to allocation demands

What the Machine does - Call Entry Each program has a “Call Stack”. When a method is called the state of the system is stored in a Call Frame. This includes all local variables and state information. It is like taking a snap shot of the system. Method B{…} Method A { Call Method B } Main(){ Call Method A Call Stack with Call Frames for each suspend process Local Variables and Parameters are declared. Current state of Method A is stored when Method B is Called, including local variables System state and local variables of Main Method Local Variables and Parameters are declared. Current state of Method B is stored when Method C is Called, including local variables Method B is called, forcing Method A to be suspended Method A is Called

What the Machine does - Call Return When a method returns: The call frame is removed from the stack Local variables of previous AR are restored. System continues from this returned state. Since a call frame stores a system state, we can say a history is also stored. Previous values, Previous locations. History is a state of suspended animation which can be woke at the correct time.

Example of Recursion PrintList (node ptr){ if (ptr != null) { print(ptr.data); PrintList(ptr.next); } } We wish to print the list forward and backward using recursion. D A B C Head The result of the code would be: A B C D D C B A

PrintList (node ptr){ if (ptr != null) { print(ptr.data); Entry Ptr => Node A PrintList (node ptr){ if (ptr != null) { print(ptr.data); PrintList(ptr.next); } } Ptr.Next => Node C Ptr.Next => Null Ptr.Next => Node D Ptr.Next => Node B A B C D D C B A D A B C Head Notice that B C & D are simply a sub-problem of the entire list

Pattern of Recursive Execution ARs for recursive methods handled same as non-recursive may be more than one AR for the same method on the stack top AR is for the currently executing invocation of the method e.g. factorial initial call (factorial(n)) say n=3 first invocation begins first recursive call 2 more recursive calls just before first return after first return 2 more returns return to main understanding recursion abstraction

Recognizing Recursive Problems You have the potential for a recursive solution if A mathematical function is defined recursively. Mathematical induction is used to prove the function (data structure). The problem is composed of sub-problems with the same properties as the main problem. Is there a trivial case? Does the sub-problem tend toward the trivial case? Onion Test Look at an onion Remove the outer layer Does it still look like an Onion? Yes, recursion may be possible. No, Iterative solution needed.

Recursive Algorithms termination requirements trivial case reduction proof reduction leads to trivial case proof of termination factorial trivial case: n=0 reduction: factorial(n-1) proof Fibonacci numbers trivial cases: n=0, n=1 reduction: fibonacci(n-1), fibonacci(n-2)

Recursion vs Iteration iterative algorithm always exists consider machine code can translate recursive algorithm into iterative replace by loop stack of state e.g. tail recursion don’t even need stack iterative factorial different point of view e.g. Fibonacci numbers generate in sequence

Examples Koch curve space filling curve transformation of a straight line order & length recursion 4 Koch curves of lower order Koch snowflake image recognition e.g. MRI image array of cells locate single connected area neighbors avoiding duplication algorithm and output Site with lots of good examples

Recursion v.s. Iteration. comparison factorial O(n) vs O(n) Fibonacci O(n) vs O(2n) See next slide penalties for recursion space time advantages of recursion recursive algorithm sometimes simpler programmer productivity maintenance basic rule look for iterative solution when not easily found or complex consider recursion

Fibonacci Numbers n = 3 the first instance of fib calculates: fib(n-1) ==> [fib(2) and [fib(n-2) ==> fib(1)]] line 1 fib(2) results in instances fib(1) and fib(0) line 2 Notice: Instance of fib(1) is now known form line 2 But fib(1) in line 1 has yet to be processed So fib(1) is recalculated. Results in: Slow algorithms Increases the magnitude unnecessarily.

Call Tree for fib(5) http://www2.hig.no/~algmet/animate.html

Design Rule Design rule: Assume that all recursive calls work. In general the machine uses its own stack to organize the calls. Very difficult to trace the calls. Important to ensure the code adheres to the first two principles, this rule then takes care of itself.

Applying of Recursion indicators variety of cases trivial case can express general case in terms of reduction e.g. recursive structures sequentially-linked structures head & tail sequential traversal trivial case – empty list reduction – apply to tail LISP

Fibonacci Numbers fib( int n) { if (n <= 1) return 1; else return fib(n-1) + fib(n-2); } 3 2 1 1 13 5 8

Koch Curve Order 0 Order 1 Order 2 Order 3

Koch Curve. The snowflake is drawn as 3 sides, or a triangle. /** This method uses the koch method to draw the snowflake as a ** triangle with each side being a Koch curve of order 4. */ private void draw ( ) { int i; yertle.left(Math.PI/2); yertle.forward(30); yertle.forward(40); yertle.right(Math.PI); yertle.penDown(); for ( i=1 ; i<=3 ; i++ ) { koch(4,80); yertle.right(2*Math.PI/3); }; yertle.penUp(); }; // draw The snowflake is drawn as 3 sides, or a triangle. Each side of the triangle will be represented by an order 4 Koch Curve After each side is drawn we rotate (2Pi/3) radians, representing the exterior angle of a triangle.

Koch Curve.. /** This method draws a Koch curve of specified order and length. ** ** @param order order of curve ** @param len length of curve. */ private void koch ( int order, double len ) { if ( order == 0 ) { yertle.forward(len); } else { koch(order-1,len/3); yertle.left(Math.PI/3); yertle.right(2*Math.PI/3); }; }; // koch Order 0 curve is the base case, so draw a straight line. Notice no recursion. A line segment in a Koch curve is represented by 4 units As we recurse the order is reduced tending toward the base case. Each of those line segments are again represented by 4 line segments, Recursing allows these sub-segments to be drawn.

Image Scan As each cell is evaluated, the count is returned. First base case, if we hit the edge of the image. private int cellCount ( int r, int c ) { if ( r<0 | r>=h | c<0 | c>=w ) { return 0; } else { if ( image[r][c] == ' ' | image[r][c] == '*' ) { image[r][c] = '*'; return cellCount(r+1,c-1) + cellCount(r+1,c ) + cellCount(r+1,c+1) + cellCount(r ,c-1) + 1 + // (r,c) cellCount(r ,c+1) + cellCount(r-1,c-1) + cellCount(r-1,c ) + cellCount(r-1,c+1); }; }; // cellCount Second base case, if we find a space or a cell we have already processed. Else we found a cell which has not been process, so mark it as processed *. Recurse to each of the adjacent cells. Count the cell found as +1 Progress toward the base case is based on the assumption that the cells in the cluster are finite, thus have an edge.

The End