Prof. S.M. Lee Department of Computer Science. Answer:

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
Computer Science II Recursion Professor: Evan Korth New York University.
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.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
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.
Introduction to complexity Prof. Sin-Min Lee Department of Computer Science San Jose State University.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Prof. S.M. Lee Department of Computer Science.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
M180: Data Structures & Algorithms in Java
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Chapter 12 Recursion, Complexity, and Searching and Sorting
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 17: Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
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.
IB Computer Science Unit 5 – Advanced Topics Recursion.
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.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Recursion. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first.
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.
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.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion Chapter 2 Objectives Upon completion you will be able to:
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Chapter 15 Recursion.
Introduction to Recursion
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Java 4/4/2017 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Chapter 11.
Recursion Data Structures.
Functions Recursion CSCI 230
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 17: Recursion.
Module 1-10: Recursion.
Yan Shi CS/SE 2630 Lecture Notes
Presentation transcript:

Prof. S.M. Lee Department of Computer Science

Answer:

Recursion Recursion is more than just a programming technique. It has two other uses in computer science and software engineering, namely: as a way of describing, defining, or specifying things. as a way of designing solutions to problems (divide and conquer).

Recursion Recursion can be seen as building objects from objects that have set definitions. Recursion can also be seen in the opposite direction as objects that are defined from smaller and smaller parts. “Recursion is a different concept of circularity.”(Dr. Britt, Computing Concepts Magazine, March 97, pg.78)

Iterative Definition In general, we can define the factorial function in the following way:

Iterative Definition This is an iterative definition of the factorial function. It is iterative because the definition only contains the algorithm parameters and not the algorithm itself. This will be easier to see after defining the recursive implementation.

Recursive Definition We can also define the factorial function in the following way:

Iterative vs. Recursive Iterative 1 if n=0 factorial(n) = n x (n-1) x (n-2) x … x 2 x 1if n>0 Recursive factorial(n) = 1if n=0 n x factorial(n-1)if n>0 Function calls itself Function does NOT calls itself

Recursion To see how the recursion works, let ’ s break down the factorial function to solve factorial(3)

Breakdown Here, we see that we start at the top level, factorial(3), and simplify the problem into 3 x factorial(2). Now, we have a slightly less complicated problem in factorial(2), and we simplify this problem into 2 x factorial(1).

Breakdown We continue this process until we are able to reach a problem that has a known solution. In this case, that known solution is factorial(0) = 1. The functions then return in reverse order to complete the solution.

Breakdown This known solution is called the base case. Every recursive algorithm must have a base case to simplify to. Otherwise, the algorithm would run forever (or until the computer ran out of memory).

Breakdown The other parts of the algorithm, excluding the base case, are known as the general case. For example: 3 x factorial(2)  general case 2 x factorial(1)  general case etc …

Breakdown After looking at both iterative and recursive methods, it appears that the recursive method is much longer and more difficult. If that ’ s the case, then why would we ever use recursion? It turns out that recursive techniques, although more complicated to solve by hand, are very simple and elegant to implement in a computer.

Iteration vs. Recursion Now that we know the difference between an iterative algorithm and a recursive algorithm, we will develop both an iterative and a recursive algorithm to calculate the factorial of a number. We will then compare the 2 algorithms.

Iterative Algorithm factorial(n) { i = 1 factN = 1 loop (i <= n) factN = factN * i i = i + 1 end loop return factN } The iterative solution is very straightforward. We simply loop through all the integers between 1 and n and multiply them together.

Recursive Algorithm factorial(n) { if (n = 0) return 1 else return n*factorial(n-1) end if } Note how much simpler the code for the recursive version of the algorithm is as compared with the iterative version  we have eliminated the loop and implemented the algorithm with 1 ‘if’ statement.

How Recursion Works To truly understand how recursion works we need to first explore how any function call works. When a program calls a subroutine (function) the current function must suspend its processing. The called function then takes over control of the program.

How Recursion Works When the function is finished, it needs to return to the function that called it. The calling function then ‘ wakes up ’ and continues processing. One important point in this interaction is that, unless changed through call-by- reference, all local data in the calling module must remain unchanged.

How Recursion Works Therefore, when a function is called, some information needs to be saved in order to return the calling module back to its original state (i.e., the state it was in before the call). We need to save information such as the local variables and the spot in the code to return to after the called function is finished.

How Recursion Works To do this we use a stack. Before a function is called, all relevant data is stored in a stackframe. This stackframe is then pushed onto the system stack. After the called function is finished, it simply pops the system stack to return to the original state.

How Recursion Works By using a stack, we can have functions call other functions which can call other functions, etc. Because the stack is a first-in, last-out data structure, as the stackframes are popped, the data comes out in the correct order.

Basic Recursion What we see is that if we have a base case, and if our recursive calls make progress toward reaching the base case, then eventually we terminate. We thus have our first two fundamental rules of recursion:

Basic Recursion 1. Base cases: –Always have at least one case that can be solved without using recursion. 2. Make progress: –Any recursive call must make progress toward a base case.

Limitations of Recursion Recursion is a powerful problem-solving technique that often produces very clean solutions to even the most complex problems. Recursive solutions can be easier to understand and to describe than iterative solutions.

Main disadvantage of programming recursively The main disadvantage of programming recursively is that, while it makes it easier to write simple and elegant programs, it also makes it easier to write inefficient ones. when we use recursion to solve problems we are interested exclusively with correctness, and not at all with efficiency. Consequently, our simple, elegant recursive algorithms may be inherently inefficient.

Limitations of Recursion By using recursion, you can often write simple, short implementations of your solution. However, just because an algorithm can be implemented in a recursive manner doesn ’ t mean that it should be implemented in a recursive manner.

Limitations of Recursion Recursion works the best when the algorithm and/or data structure that is used naturally supports recursion. One such data structure is the tree (more to come). One such algorithm is the binary search algorithm that we discussed earlier in the course.

Limitations of Recursion Recursive solutions may involve extensive overhead because they use calls. When a call is made, it takes time to build a stackframe and push it onto the system stack. Conversely, when a return is executed, the stackframe must be popped from the stack and the local variables reset to their previous values – this also takes time.

Limitations of Recursion In general, recursive algorithms run slower than their iterative counterparts. Also, every time we make a call, we must use some of the memory resources to make room for the stackframe.

Limitations of Recursion Therefore, if the recursion is deep, say, factorial(1000), we may run out of memory. Because of this, it is usually best to develop iterative algorithms when we are working with large numbers.

Application One application of recursion is reversing a list. Before we implemented this function using a stack. Now, we will implement the same function using recursive techniques.

Fibonacci function: fibonacci(0) = 1 fibonacci(1) = 1 fibonacci(n) = fibonacci(n-1) + fibonacci(n-2) [for n>1] This definition is a little different than the previous ones because It has two base cases, not just one; in fact, you can have as many as you like. In the recursive case, there are two recursive calls, not just one. There can be as many as you like.

Execution of Code for f(3) x = 3 y = ? call f(2) y = 2 * 7 = 14 return y + 1 = 15 x = 2 y = ? call f(1) y = 2 * 3 = 6 return y + 1 = 7 x = 1 y = ? call f(0) y = 2 * 1 = 2 return y + 1 = 3 x = 0 y = ? return 1 copy of f value returned by call is 15

Conclusion A recursive solution solves a problem by solving a smaller instance of the same problem. It solves this new problem by solving an even smaller instance of the same problem. Eventually, the new problem will be so small that its solution will be either obvious or known. This solution will lead to the solution of the original problem.

The main benefits of using recursion as a programming technique are these: invariably recursive functions are clearer, simpler, shorter, and easier to understand than their non-recursive counterparts. the program directly reflects the abstract solution strategy (algorithm). From a practical software engineering point of view these are important benefits, greatly enhancing the cost of maintaining the software.

Consider the following program for computing the fibonacci function. int s1, s2 ; int fibonacci (int n) { if (n == 0) return 1; else if (n == 1) return 1; else { s1 = fibonacci(n-1); s2 = fibonacci(n-2); return s1 + s2; }

The main thing to note here is that the variables that will hold the intermediate results, S1 and S2, have been declared as global variables. This is a mistake. Although the function looks just fine, its correctness crucially depends on having local variables for storing all the intermediate results. As shown, it will not correctly compute the fibonacci function for n=4 or larger. However, if we move the declaration of s1 and s2 inside the function, it works perfectly. This sort of bug is very hard to find, and bugs like this are almost certain to arise whenever you use global variables to storeintermediate results of a recursive function.

Recursion is based upon calling the same function over and over, whereas iteration simply `jumps back' to the beginning of the loop. A function call is often more expensive than a jump.

The overheads that may be associated with a function call are: Space: Every invocation of a function call may require space for parameters and local variables, and for an indication of where to return when the function is finished. Typically this space (allocation record) is allocated on the stack and is released automatically when the function returns. Thus, a recursive algorithm may need space proportional to the number of nested calls to the same function.

Time: The operations involved in calling a function - allocating, and later releasing, local memory, copying values into the local memory for the parameters, branching to/returning from the function - all contribute to the time overhead.

If a function has very large local memory requirements, it would be very costly to program it recursively. But even if there is very little overhead in a single function call, recursive functions often call themselves many many times, which can magnify a small individual overhead into a very large cumulative overhead.

int factorial(int n) { if (n == 0) return 1; else return n * factorial(n-1); } There is very little overhead in calling this function, as it has only one word of local memory, for the parameter n. However, when we try to compute factorial(20), there will end up being 21 words of memory allocated - one for each invocation of the function:

factorial(20) -- allocate 1 word of memory, call factorial(19) -- allocate 1 word of memory, call factorial(18) -- allocate 1 word of memory,. call factorial(2) -- allocate 1 word of memory, call factorial(1) -- allocate 1 word of memory, call factorial(0) -- allocate 1 word of memory, at this point 21 words of memory

and 21 activation records have been allocated. return release 1 word of memory. return 1*1. -- release 1 word of memory. return 2*1. -- release 1 word of memory.

Iteration as a special case of recursion The first insight is that iteration is a special case of recursion. void do_loop () { do {... } while (e); } is equivalent to: void do_loop () {... ; if (e) do_loop(); } A compiler can recognize instances of this form of recursion and turn them into loops or simple jumps. E.g.: void do_loop () { start:...; if (e) goto start; } Notice that this optimization also removes the space overhead associated with function calls.

Most recursive algorithms can be translated, by a fairly mechanical procedure, into iterative algorithms. Sometimes this is very straightforward - for example, most compilers detect a special form of recursion, called tail recursion, and automatically translate into iteration without your knowing. Sometimes, the translation is more involved: for example, it might require introducing an explicit stack with which to `fake' the effect of recursive calls.

In general, there is no reason to incur the overhead of recursion when its use does not gain anything. Recursion is truly valuable when a problem has no simple iterative solution.