Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

MATH 224 – Discrete Mathematics
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Programming with Recursion
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
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.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
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.
Prof. S.M. Lee Department of Computer Science. Answer:
Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
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.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Comp 249 Programming Methodology Chapter 10 – Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
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.
Data Structures and Abstractions with Java, 4e Frank Carrano
Chapter 12 Recursion, Complexity, and Searching and Sorting
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Analysis of Algorithms
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
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 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
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.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Lecture 7. Solution by Substitution Method T(n) = 2 T(n/2) + n Substitute n/2 into the main equation 2T(n/2) = 2(2(T(n/4)) + n/2) = 4T(n/4) + n And T(n)
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.
Chapter 6 Recursion. Solving simple problems Iteration can be replaced by a recursive function Recursion is the process of a function calling itself.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
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.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Programming with Recursion
Algorithm Analysis 1.
Recursion CENG 707.
Recursion Topic 5.
COP 3503 FALL 2012 Shayan Javed Lecture 15
Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
Programming with Recursion
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Basics of Recursion Programming with Recursion
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recurrences.
Searching.
Presentation transcript:

Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4

Stephen P. Carl - CS 2421 Recursion Defined Recursion is a technique for defining data structures or algorithms in terms of themselves. A recursive algorithm is a form of decomposition where rather than choosing an arbitrary subtask of the problem to do, choose a simpler problem that has the same form as the original (self-similarity). A recursive definition has two parts: – the base case - a stopping condition – the recursive step - an expression of the computation or definition in terms of itself There may be one or more of each of these.

Stephen P. Carl - CS 2421 Example of a Recursive Definition There are many recursive definitions in mathematics. Consider the factorial function: n! = n * (n-1) * (n -2) * … * 2 * 1 The same function can be defined recursively by giving a base case and a recursive step: 0! = 1 (by definition) n! = n * (n - 1)! (the recursive step)

Stephen P. Carl - CS 2421 Recursive Functions A recursive function is a function which calls itself somewhere in the function body. Recursive functions are supported in most modern programming languages including C++ A language that supports recursion usually requires a system stack for tracking function call and return. In a recursive function, execution must “drive” computation to a base case so the recursion will stop. The recursive step is intended to ensure that the computation eventually terminates.

Stephen P. Carl - CS 2421 The Factorial Function in Java The recursive definition for factorial can be written in a very straightforward manner. Take some time to convince yourself that this method works: // precondition: n >= 0 public static int fact(int n) { Assert.pre(n >= 0); if (n < 2) // base case return 1; else // recursive step (call in bold) return (n * fact(n - 1)); }

Stephen P. Carl - CS 2421 Tracing a Recursive Function The behavior of fact when n = 5: fact(5) -> 5 * fact(4) fact(4) -> 4 * fact(3) fact(3) -> 3 * fact(2) fact(2) -> 2 * fact(1) fact(1) -> 1 We can view this as a process of driving the computation to the base case; next, we unwind.

Stephen P. Carl - CS 2421 Tracing a Recursive Function Eventually, the call fact(1) returns 1 to the function which called it, so that it can complete its calculation and return its result to the function which called it, and so on. This is called back-substitution, or unwinding the recursion. fact(1) -> 1 fact(2) -> 2 * 1 = 2 fact(3) -> 3 * 2 = 6 fact(4) -> 4 * 6 = 24 fact(5) -> 5 * 24 = 120 <<< the final answer

Stephen P. Carl - CS 2421 Recursion Under the Hood The execution environment sets up a call stack (or system stack) to store activation records for keeping track of function call and return. Therefore, each recursive call is represented by an activation record on the stack. The activation record at the top of the stack is active, and all other calls on the stack are said to be suspended. Each recursive-step call waits for the results of the next call so it can finish its own computation. In geeneral, recursion works by taking advantage of the system stack to keep track of the partial results computed by the successive recursive calls; these partial results are then back- substituted into the preceding calls through the normal operation of return values.

Stephen P. Carl - CS 2421 Another Mathematical Example An important function in probability is the binomial coefficient, or choose, function, written C(n,k), and defined as: C(n,k) = n! / [(n - k)! * mk This is hard to compute because n! gets too large to represent as an integer even for small values of n. Another version is recursive (note: two base cases): C(n, 0) = 1, for n > 0 C(n, n) = 1, for n > 0 C(n, k) = C(n-1, mk + C(n-1, k-1), for n > k >= 0

Stephen P. Carl - CS 2421 The choose function in Java Here is a straightforward translation of the recursive definition into Java: // pre: n > 0 && n > k >= 0 public static int choose(int n, int k) { if (k == 0) // first base case return 1; else if (n == k) // second base case return 1; else // recursive step return choose(n-1, k) + choose(n-1, k-1); }

Stephen P. Carl - CS 2421 Recursion Trees A trace is one method of analyzing what a recursive function is doing. Another is to draw a recursion tree. In this method, we show each invocation of the function as a tree node and draw lines between each function invocation and the recursive calls it makes. The recursion tree can be annotated to show arguments to each function and the values they compute and return. The tree for fact gives us no new information, but drawing such a tree for the choose function is quite useful.

Stephen P. Carl - CS 2421 Recursion tree for choose() We derive the recursion tree for choose(4, 2);

Stephen P. Carl - CS 2421 Another example // pre: data[] is sorted // post: returns index if target in data, -1 otherwise public static int binarySearch(int[] data, int first, int last, int target) { if (first > last) // base case #1 return -1; int mid = (first+last)/2; if (data[mid] == target) // base case #2 return mid; else if (data[mid] > target) // rec step #1 return binarySearch(data, first, mid-1, target); else return binarySearch(data, mid+1, last, target); }

Stephen P. Carl - CS 2421 Types of Recursion Each of these simple examples illustrates a different kind of recursion: – The factorial and binarySearch methods are examples of linear recursion, in which only one recursive call is made per recursive step. – The choose method is an example of tree recursion, in which two (or more, in general) recursive calls are made in at least one recursive step. The recursion tree for choose exposes an inefficiency: some recursive calls do redundant work (though this is not always true for tree-recursive algorithms).

Stephen P. Carl - CS 2421 Recursion vs. Iteration For these simple examples, it is easy to come up with an iterative version of the same algorithm that will run faster. In fact recursion and iteration are related; languages without iteration simulate it using recursion, and vice versa. However, more interesting examples, such as the sorting routines we discuss later, do not have an obvious iterative solution. When a solution is discovered, it is often much longer than the recursive version. The same is true for algorithms based on recursively-defined data structures, such as the Binary Tree ADT.

Stephen P. Carl - CS 2421 Efficiency of Recursive Algorithms The efficiency of a recursive algorithm is not obvious and depends on the type and number of recursive calls performed. Methods used to determine efficiency include: – estimating number of operations done by counting the number of recursive calls from the trace or recursion tree and multiplying by the number of operations per call. – mathematically by using recurrence relations to model the performance of the function for any given input (usually studied in a discrete mathematics class). – Some functions require even more sophisticated mathematical tools to analyze.

Stephen P. Carl - CS 2421 Space Complexity Space complexity is a characterization of how much memory an algorithm requires as a function of input size. We can generally give an upper bound on the amount of memory any particular function will use. Many algorithms use a constant amount of space; for example, most sorting algorithms manipulate the values of an array in place, so the total amount of space used does not change during execution. Recursive methods save their state on the system stack, which is a bounded resource, so we must consider space complexity for these types of algorithms.

Stephen P. Carl - CS 2421 Examples of Space Complexity The fact method as written makes n total calls to perform fact(n), therefore, at most n activation records will be pushed on the stack. The binarySearch method makes a number of calls equal to or less than the log (base 2) of the size of the input array, so no more than log 2 (data.length) activation records will be pushed. In this case we may be able to do even better. To figure space complexity of the choose method, notice from the recursion tree that any path is made up of at most n calls (including the first). This means that at most n activation records will be on the stack at any given time during execution.

Stephen P. Carl - CS 2421 Space Complexity: Conclusions The space used by the recursive functions fact and choose (in terms of activation records on the stack) is proportional to the argument n, so we say that these functions have linear space complexity. The space used by the recursive function binarySearch is proportional to the logarithm of the array size, so it has logarithmic space complexity expressed in terms of the number of array elements.

Stephen P. Carl - CS 2421 Tail Recursion Tail recursion is defined as a recursive function that returns immediately after its recursive step - it does no computation with a result from a previous call. In fact and choose, results of one recursive call are returned to the calling function and used in a calculation; such functions are not tail-recursive. By contrast, binarySearch returns the value produced by the base case call, so there are no partial results to compute, and all back-substitutions are unnecessary. This is a tail-recursive function.

Stephen P. Carl - CS 2421 Tail Call Optimization The programmer or the compiler may make use of the tail-recursion property. If a function is tail recursive, it can be easily turned into an iterative function by the coder. However, some compilers automatically recognize this case and optimize the code for us. This amounts to reducing the space required to a constant amount by reusing the original call’s activation record. The compiler can do this because a tail recursive function call saves no partial results in the record.

Stephen P. Carl - CS 2421 Summary Many mathematical definitions and data structures are naturally recursive, as are many patterns observed in nature. Recursion can be expressed using recursive methods which have at least one base case along with at least one recursive call. Simple recursive methods may be slower and use more memory than equivalent iterative versions. However, we will see examples for which the recursive version is shorter, faster, and easier to understand.