Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
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.
Recursion. Binary search example postponed to end of lecture.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
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.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
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.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
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.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
10/14/2015cosc237/recursion1 Recursion A method of defining a concept which refers to the concept itself A method of solving a problem by reducing it to.
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
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.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
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.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Lecture #3 Analysis of Recursive Algorithms
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
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.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Recursion CENG 707.
CprE 185: Intro to Problem Solving (using C)
Recursion DRILL: Please take out your notes on Recursion
Java 4/4/2017 Recursion.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Chapter 12 Recursion (methods calling themselves)
Programming application CC213
CS201: Data Structures and Discrete Mathematics I
Recursion Data Structures.
Functions Recursion CSCI 230
Module 1-10: Recursion.
Java Programming: Chapter 9: Recursion Second Edition
Yan Shi CS/SE 2630 Lecture Notes
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
ICS103 Programming in C Lecture 11: Recursive Functions
ITEC324 Principle of CS III
Presentation transcript:

Chapter 9: Recursion1 CHAPTER 9 RECURSION

Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions  Example : Simple and complex  Converting recursive to non-recursive equivalent functions Chapter 9: Recursion2

3 Simple Recursion  Concept: defining a solution in terms of a simpler version of the solution Recursion is often used instead of iteration  C permits a function to call itself.  We say that a function is calling itself when we can find a function call inside the body of the function with the same name as it has.  This function call is invoking itself.

Chapter 9: Recursion4 What is a recursive solution?  A solution of a problem is a recursive solution if it is expressible in a smaller version of itself and if ultimately a simple non recursive solution can not be found.  A recursive technique is an alternative to iteration technique (using loop), a commonly used technique by student due to its simplicity.

Chapter 9: Recursion5 A Recursive Solution  In order to implement a recursion to solve a problem, you need to consider some of these questions that are significantly useful: How can your problem being defined in term of smaller version of itself ? How does each recursive call diminish the size of the problem? What instance of the problem can serve as the case base? As the problem size diminishes, will you reach the case base?

A Recursive: Benefit & Cost  The Benefits: often your code will be shorter it is usually easier to define the solution recursively  and writing the code is just a matter of implementing the definition more elegant solution you may not need local variables to implement your solution (instead use parameter ‘passed by value’ properly) some solutions are only expressible recursively (or at least only easily expressible recursively)  this is true tree and graph operations, and search problems that require backtracking  The Cost/disadvantages: recursive solutions are often harder to debug (need good IDE debugger tool) recursion requires numerous function calling  this can lead to much poorer run-time performance takes up more memory space (on the run-time stack) in a few cases, solution may be much less efficient than an iterative solution

A Recursive: Examples of usage  Factorial: fact(n): if(n < 2) 1 else n * fact(n – 1)  Fibonacci: fib(n): if (n < 3) 1 else fib(n – 1) + fib(n – 2)  Finding largest item in an array: largest(a, n): if (n = = 0) return a[0] else return max(a[n], largest(a, n-1))  We can also find the largest using a binary search like strategy: largest(a, low, high):  if(low = = high) return a[low]  else return max(largest(a, low, (low+high)/2)), largest(a, (low+high)/2+1, high))  Writing elements backwards output_backward(S, n): if(n>=0)  output_backwards(S, n-1)  output character in S at position (n)

Chapter 9: Recursion8 int factorial (int n) { if (!n) return 1; return n * factorial(n-1); } SAME WITH int factorial2 (int n) { int i, fact = 1; for (i = 1; i <= n; i++) { fact *= i; } return fact; }

Program code Chapter 9: Recursion9 #include int largestBinarySearch(int a[], int low, int high); int largest(int a[], int n); void backwards(char[], int n); int factorial(int n) { if (n < 2) return 1; else return n * factorial(n - 1); } int main() { printf("Factorial 5 is %d\n", factorial(5)); printf("Fibonacci 10 is %d\n", fibonacci(10)); int array[5] = {3, 2, 1, 5, 4}; printf("The largest item in the array is %d\n", largest(array, 4)); printf("The largest item in the array is %d\n", largestBinarySearch(array, 0, 4)); char string[7] = "UNITEN"; backwards(string, 5); return (EXIT_SUCCESS); }

Program code Chapter 9: Recursion10 int fibonacci(int n) { if (n < 3) return 1; return fibonacci(n - 1) + fibonacci(n - 2); } int max(int x, int y) { return (x > y) ? x : y; } int largest(int a[], int n) { if (n == 0) return a[0]; else return max(a[n], largest(a, n - 1)); }

Program code Chapter 9: Recursion11 int largestBinarySearch(int a[], int low, int high) { if (low == high) return a[low]; else return max(largestBinarySearch(a, low, (low + high) / 2), largestBinarySearch(a, (low + high) / 2 + 1, high)); } void backwards(char S[], int n) { if (n > 0) backwards(S, n-1); printf("%c", S[n]); }

Chapter 9: Recursion12 Comparison: Iterative Functions  As mentioned earlier, we can use a recursion technique as an alternative to the iteration technique.  Assume we want to calculate the summation of iteration technique  We can use iteration technique to accomplish this task int cal (int n) { int j=1; while ( j < n) { sum = sum + j; j++; }

Chapter 9: Recursion13 Comparison: Recursive Functions recursion technique  Alternatively, we can use recursion technique to solve the problem. int cal( int n) { if (n == 1 ) return 1; else return ( n + cal(n-1)); }

Comparison: Recursive Functions (con’t) Chapter 9: Recursion14 N=4 => = 10 “ return ( n + cal(n-1))” cal (3) 3 + cal (2) 2 + cal (1) cal (3) 3 + cal (2) 2 + cal (1) 1 a) Sequence of recursive calls 1 returned = 3 is returned = 6 is returned = 10 is returned Final value = 10 is returned b) Value returned from each recursive call (backtracking)

Chapter 9: Recursion15 A Recursive: Simple example1 #include void print_integers(int); int main( ) { int number; printf(“Enter an integer: “); scanf(“%d”, number); print_integers(number); } void print_integers(int n) { if (n>=1) { printf(“%d\n”, n); print_integers(n-1); }

Chapter 9: Recursion16 A Recursive: Simple example1 (con’t)  The function print_integers also has a function call in its body to itself.  The parameter in the statement print_integers(n-1); is 1 less than n, the value in the previous call.  In other words, the problem that the function is expected to solve is smaller or simpler version of the previous problem.  Thus we can say that the problem size is gradually diminishing.

Chapter 9: Recursion17 A Recursive: Simple example2  It is important to understand how a recursive function works. Assuming you have a factorial function as below int Fact ( int n) { if ( n == 1) return 1;i else return (n * Fact ( n-1));ii }

Chapter 9: Recursion18  You can find the value of factorial 4 by calling Fact(4), but how does this function works and produce the output of Fact(4). Fact(4) = ( 4 * Fact(3)) = ( 4 * (3 * Fact(2))) = (4 * ( 3 * (2 * Fact(1)))) = (4 * (3 * ( 2 * 1))) = (4 * (3 * (2))) = (4 * (6)) = 24 A Recursive: Simple example2 (con’t)

Chapter 9: Recursion19 Using graphical representation, this process can be illustrated as below Fact (4) i ii 4 * Fact (3) Fact 3 i ii. 3 * Fact (2) Fact 2 i ii. 2 * Fact (1) Fact 2 i ii. 2 * 1 Fact 3 i ii. 3 * 2 Fact 1 i return 1 ii Fact 4 i ii 4 * 6 Tracing a Recursion Function

Chapter 9: Recursion20 A Recursive: Simple example3  Assuming we want to develop a recursive solution to calculate the value of X n. Firstly, we may need to study the formal notation solution of this problem. We can write the solution of X n as below X 0 = 1…………………(i) base case statement X n = X. X n-1 …………(ii) recursive statement  Statement (i) will serve as our base case statement and the statement (ii) will serve as the recursive statement.

Chapter 9: Recursion21 A Recursive: Simple example3 (con’t)  So by having the definition, we can develop the recursive function as below. int pow ( int X, int n) { if ( n == 0) return 1; else return (X * pow ( X,n-1)); }

A Recursive: Simple example3 (con’t)  Exercise: Trace example3 using a graphical representation as discussed in slide no. 18. The initial value of variable X=2 and n=3 What is the final output returned. Chapter 9: Recursion22

Chapter 9: Recursion23 Example 4: Fibonacci numbers – Iteration technique int fibonacci(int seq_num) { int first = 0, second = 1, count = 3, fibo; if(seq_num == 0) fibo = first; else if(seq_num == 1) fibo = second; else while(count <= seq_num) { fibo = first + second; first = second; second = fibo; count++; } return fibo; } Fibonacci - a series of numbers created by adding the last two numbers in the series to produce the next number in the series, i.e., 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc.

Chapter 9: Recursion24 Example 4: Fibonacci numbers - Recursive technique int fibonacci(int seq_num) { if(seq_num == 0) return 0; else if(seq_num == 1) return 1; else return fibonacci(seq_num -1) + fibonacci(seq_num -2); }  This has simplified the iteration technique in the previous slide

25 (fib 5) (fib 3) (fib 4) (fib 1) (fib 2) (fib 2) (fib 3) (fib 0) (fib 1) (fib 1) (fib 2) (fib 0)(fib 1) Tree of sub problems: Fibonacci Chapter 9: Recursion

26 SUMMARY  This chapter has introduced you; How to construct simple recursion How recursion can simplify the iteration technique in certain cases A few examples on recursion  Fibonacci  Factorial  Power of