Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Computer Science II Recursion Professor: Evan Korth New York University.
Recursion. Binary search example postponed to end of lecture.
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
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
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 CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
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.
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.
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.
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 and Implementation of Functions
Recursion Chapter 5.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
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.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion, pt. 1 The Foundations. What is Recursion? Recursion is the idea of solving a problem in terms of itself. – For some problems, it may not possible.
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 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Recursion Chapter 11. How it works “A recursive computation solves a problem by using the solution of the same problem with simpler inputs” Big Java,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Math Review 1.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Pei Zheng, Michigan State University 1 Chapter 8 Recursion.
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.
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.
CS212: Data Structures and Algorithms
Recursion.
Chapter 15 Recursion.
Recursion Lakshmish Ramaswamy.
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Recursion 12-Nov-18.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Recursive Thinking Recursive Programming
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Recursion 29-Dec-18.
RECURSION Annie Calpe
Applied Discrete Mathematics Week 9: Integer Properties
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion.
Recursion.
Presentation transcript:

Recursion

What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline

A recursive method is a method that directly or indirectly calls itself. What is recursion? 5! = 5 × 4 × 3 × 2 × 1 n! = n × n − 1 × n − 2 ×... × 1 (n − 1)! = n − 1 × n − 2 ×... × 1 factorial(n) = n × factorial(n − 1) factorial(1) = 1 Example: definition of factorials

What happens if we call int x = factorial(4)?. Example: recursive implementation of factorial { if (n == 1) return 1; return n * factorial(n - 1); } public int factorial(int n) return 4 * factorial (3); factorial(4)= 24 return 3 * factorial (2); factorial(3)= 6 return 2 * factorial (1); factorial(2) = 2 Return 1; factorial(4)= 1 Eventually, x is assigned the return value of factorial(4), i.e. 24

How can a method F solve a problem by calling itself? The key: F calls itself on a simpler problem! Problems solvable by recursion can be reduced into one or more smaller and similar problems. In general, recursion has 2 cases: –Induction case: solved by reducing the problem. –Base case: simple enough, requires no more reduction. The basics of recursion

The rules of recursion Always have at least one case that can be solved without recursion. 1. Base case To avoid infinite recursion, any recursive call should progress towards the base case. 2. Make progress Always assume that the recursive call works. 3. You gotta believe Never duplicate work by solving the same instance of a problem in separate recursive calls. 4. Compound interest rule

Example: –Consider factorial(n). –Assume factorial(n − 1) computes the correct result. –What must be done to get factorial(n) from factorial(n − 1)? –Multiply by n. This is true whether you’re computing factorial(4) or factorial(400000).You don’t have to trace this times to convince yourself! “Believing” in the induction case

What happens if we call bad(n) with any value of n > 0? Infinite recursion! How NOT to do recursion { if (n == 0) return 0; return bad(n * 3); } public static int bad (int n)

The Java Virtual Machine keeps track of method calling using activation records. An activation record contains relevant info, such as: –Values of parameters –Values of local variables –Which line of code was being executed Every time a method call is made, a new activation record is created. What would make a good data structure for a collection of these records? A Stack! How does recursion work?

When method S is called, an activation record for S is pushed onto the stack, making it the currently active method. When the method returns, the stack is popped and the activation record that is the new top of the stack contains the restored values. OutlineActivation record stack

On “my” computer, calling s(12516) can’t be handled! Why? The computer runs outs of memory storing the activation record stack! A simple loop would have worked... Too much recursion! { if (n == 1) return 1; else return s (n - 1); } public static long s (int n)

How do we prove this is true? We can easily test it for N = 1, perhaps even for 2 <= N <= 10. However, this is not proof that the theorem holds for all N >= 1. Induction: the mathematical basis for recursion For P any integer N >= 1, the sum of the first N integers, given by,,equals N(N + 1)/2. Theorem Mathematical induction is a method for proving theorems. Example:

Prove it. Proving the theorem with induction

The Fibonacci sequence F 0, F 1,..., F i is a very famous sequence of numbers. It is defined as follows: –F0 = 0 –F1 = 1 –F i = F i-1 + F i-2 Thus: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,... Fibonacci numbers

This works. However, it’s really slow. Computing F40 takes almost a minute on some modern PCs! To compute fibo(n), we recursively compute fibo(n-1) and fibo(n- 2). However, we already computed fibo(n-2) in the process of computing fibo(n-1). Computing Fibonacci recursively { if( n <= 1 ) return n; else return fibo( n - 1 ) + fibo( n - 2 ); } public static long fibo(int n)

F40 makes over 300 million recursive calls. The growth rate is exponential! Remember the last rule of recursion: Never duplicate work by solving the same instance of a problem in separate recursive calls. The solution: remember the previous Fibonacci numbers. Wasting time...

Storing the result of previous computation: this technique is called dynamic programming or memorization. There is a more (space) efficient implementation. How? Remembering the previous numbers { if (n <= 1) return n; long result[] = new long[n + 1]; result[0] = 0; result[1] = 1; for (int ii = 2; ii <= n; ii++) result[ii] = result[ii - 1] + result[ii - 2]; return result[n]; } public static long fibo2 (int n)

Remembering just the last two numbers { if (n <= 1) return n; long fib1 = 0; long fib2 = 1; long result; for (int ii = 2; ii <= n; ii++) {result = fib2 + fib1; fib1 = fib2; fib2 = result;} return result; } public static long fibo3 (int n)

An efficient recursive implementation { return fiboHelp(0,1,n) } public static long fibo4 (int n) { if (n == 0) return x; else if ( n == 1 ) return y; else return fiboHelp (y, x+y, n-1); } static long fiboHelp (long x, long y, int n)

Recursion is a powerful problem solving tool and has many uses. The foundation of recursive functions is mathematical induction, a way of proving theorems. The rules of recursion: –Base case. –Progress towards the base case. –Always assume the recursive call works. –Never duplicate work by solving the same instance of a problem in separate recursive calls. Dynamic programming: saving the results of previous computation. Summary