More on Recursive Recursion vs. Iteration Why Recursion?

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Types of Recursive Methods
Recursion Outline of the upcoming lectures: Review of Recursion
Factorial Recursion stack Binary Search Towers of Hanoi
More on Recursive Methods KFUPM- ICS Data Structures.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Recursion Outline of the upcoming lectures: –Review of Recursion –Types of Recursive Methods –Final Remarks on 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 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.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
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
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
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.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
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 Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
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.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
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.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
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.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
递归算法的效率分析. 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.
Ms N Nashandi Dr SH Nggada Week 08 – Recursion. Outline We shall be covering the following What is a recursion? Iteration versus Recursion. Recursion.
Recursion.
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Abdulmotaleb El Saddik University of Ottawa
More on Recursive Recursion vs. Iteration Why Recursion?
Unit 5 Recursion King Fahd University of Petroleum & Minerals
More on Recursive Methods
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Loops in C C has three loop statements: the while, the for, and the do…while. The first two are pretest loops, and the the third is a post-test loop. We.
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Definitions
Recursion Chapter 11.
Fundamentals of Programming
Announcements Final Exam on August 19th Saturday at 16:00.
Unit 3 Test: Friday.
Announcements Final Exam on December 25th Monday at 16:00.
When a function is called...
CSC 143 Recursion.
The structure of programming
Yan Shi CS/SE 2630 Lecture Notes
Dr. Sampath Jayarathna Cal Poly Pomona
ICS103 Programming in C Lecture 11: Recursive Functions
ITEC324 Principle of CS III
Presentation transcript:

More on Recursive Recursion vs. Iteration Why Recursion? Common Errors in Writing Recursive Methods:

Recursion vs. Iteration In general, an iterative version of a method will execute more efficiently in terms of time and space than a recursive version. This is because the overhead involved in entering and exiting a function in terms of stack I/O is avoided in iterative version. Sometimes we are forced to use iteration because stack cannot handle enough activation records - Example: power(2, 5000))

Why Recursion? Usually recursive algorithms have less code, therefore algorithms can be easier to write and understand - e.g. Towers of Hanoi. However, avoid using excessively recursive algorithms even if the code is simple. Sometimes recursion provides a much simpler solution. Obtaining the same result using iteration requires complicated coding - e.g. Quicksort, Towers of Hanoi, etc. Recursive methods provide a very natural mechanism for processing recursive data structures. A recursive data structure is a data structure that is defined recursively – e.g. Tree. Functional programming languages such as Clean, FP, Haskell, Miranda, and SML do not have explicit loop constructs. In these languages looping is achieved by recursion.

Why Recursion? Some recursive algorithms are more efficient than equivalent iterative algorithms. Example: public static long power1 (int x, int n) { long product = 1; for (int i = 1; i <= n; i++) product *= x; return product; } public static long power2 (int x, int n) { if (n == 1) return x; else if (n == 0)return 1; else { long t = power2(x , n / 2); if ((n % 2) == 0) return t * t; else return x * t * t; }

Common Errors in Writing Recursive Methods The method does not call itself directly or indirectly. Non-terminating Recursive Methods (Infinite recursion): No base case. The base case is never reached for some parameter values. int badFactorial(int x) { return x * badFactorial(x-1); } int anotherBadFactorial(int x) { if(x == 0) return 1; else return x*(x-1)*anotherBadFactorial(x -2); // When x is odd, we never reach the base case!! }

Common Errors in Writing Recursive Methods Post increment and decrement operators must not be used since the update will not occur until AFTER the method call - infinite recursion. Local variables must not be used to accumulate the result of a recursive method. Each recursive call has its own copy of local variables. public static int sumArray (int[ ] x, int index) { if (index == x.length)return 0; else return x[index] + sumArray (x, index++); } public static int sumArray (int[ ] x, int index) { int sum = 0; if (index == x.length)return sum; else { sum += x[index]; return sumArray(x,index + 1); }

Common Errors in Writing Recursive Methods Wrong placement of return statement. Consider the following method that is supposed to calculate the sum of the first n integers: When result is initialized to 0, the method returns 0 for whatever value of the parameter n. The result returned is that of the final return statement to be executed. Example: A trace of the call sum(3, 0) is: public static int sum (int n, int result) { if (n >= 0) sum(n - 1, n + result); return result; }

Common Errors in Writing Recursive Methods A correct version of the method is: Example: A trace of the call sum(3, 0) is: public static int sum(int n, int result){ if (n == 0) return result; else return sum(n-1, n + result); }

Common Errors in Writing Recursive Methods The use of instance or static variables in recursive methods should be avoided. Although it is not an error, it is bad programming practice. These variables may be modified by code outside the method and cause the recursive method to return wrong result. public class Sum{ private int sum; public int sumArray(int[ ] x, int index){ if(index == x.length) return sum; else { sum += x[index]; return sumArray(x,index + 1); }