Applied Algorithms (Lecture 17) Recursion Fall-23

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Recursion.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Programming with Recursion
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
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.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
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.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
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.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
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.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
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.
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)
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
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.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
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.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
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:
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.
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 is a computer programming technique that allows programmers to divide problems into smaller problems of the same type. ● You can.
Recursion Powerful Tool
Programming with Recursion
Recursion.
Recursion.
Recursion Ali.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Recursion CSE 2320 – Algorithms and Data Structures
Recursion Version 1.0.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Recursion Topic 5.
Topic 6 Recursion.
Chapter 15 Recursion.
Recursion DRILL: Please take out your notes on Recursion
Decrease-and-Conquer Approach
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion CSE 2320 – Algorithms and Data Structures
Programming with Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Important Notes Remaining Schedule: recall the 2 bad weather days (Jan 8th, Jan 17th) we will need to make up. In providing the 2 weeks needed for the.
Lecture 17 Recursion part 1 Richard Gesick.
CS201: Data Structures and Discrete Mathematics I
Unit 3 Test: Friday.
Recursion Taken from notes by Dr. Neil Moore
Search,Sort,Recursion.
Lecture 12 Recursion part 1 CSE /26/2018.
Yan Shi CS/SE 2630 Lecture Notes
Divide & Conquer Algorithms
Chapter 18 Recursion.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Recursion.
ITEC324 Principle of CS III
Presentation transcript:

Applied Algorithms (Lecture 17) Recursion Fall-23

Recursion Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be further divided into much smaller subproblems When the subproblems are small enough to solve directly the process stops A recursive algorithm is a problem solution that has been expressed in terms of two or more easier to solve subproblems

What is recursion? A procedure that is defined in terms of itself In a computer language a function that calls itself

Recursion A recursive definition is one which is defined in terms of itself. Examples: A phrase is a "palindrome" if the 1st and last letters are the same, and what's inside is itself a palindrome (or empty or a single letter) Rotor Rotator 12344321

Recursion The definition of the natural numbers: 1 is a natural number if n is a natural number, then n+1 is a natural number N =

Recursion in Computer Science 1. Recursive data structure: A data structure that is partially composed of smaller or simpler instances of the same data structure. For instance, a tree is composed of smaller trees (subtrees) and leaf nodes, and a list may have other lists as elements. a data structure may contain a pointer to a variable of the same type: struct Node { int data; Node *next; }; 2. Recursive procedure: a procedure that invokes itself 3. Recursive definitions: if A and B are postfix expressions, then A B + is a postfix expression.

Recursive Data Structures Linked lists and trees are recursive data structures: struct Node { int data; Node *next; }; struct TreeNode { TreeNode *left; TreeNode * right; Recursive data structures suggest recursive algorithms.

A mathematical look We are familiar with How about f(x) = 3x+5 How about f(x) = 3x+5 if x > 10 or f(x) = f(x+2) -3 otherwise

Calculate f(5) f(x) = 3x+5 if x > 10 or f(x) = f(x+2) -3 otherwise = 38 But we have not determined what f(5) is yet!

Calculate f(5) f(x) = 3x+5 if x > 10 or f(x) = f(x+2) -3 otherwise = 38 Working backwards we see that f(5)=29

Series of calls f(5) f(7) f(9) f(11)

Recursion Recursion occurs when a function/procedure calls itself. Many algorithms can be best described in terms of recursion. Example: Factorial function The product of the positive integers from 1 to n inclusive is called "n factorial", usually denoted by n!: n! = 1 * 2 * 3 .... (n-2) * (n-1) * n

Recursive Definition of the Factorial Function 1, if n = 0 n * (n-1)! if n > 0 n! = 5! = 5 * 4! 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 * 0! = 5 * 24 = 120 = 4 * 3! = 4 * 6 = 24 = 3 * 2! = 3 * 2 = 6 = 2 * 1! = 2 * 1 = 2 = 1 * 0! = 1

Recursive Definition of the Fibonacci Numbers The Fibonacci numbers are a series of numbers as follows: fib(1) = 1 fib(2) = 1 fib(3) = 2 fib(4) = 3 fib(5) = 5 ... fib(n) = 1, n <= 2 fib(n-1) + fib(n-2), n > 2 fib(3) = 1 + 1 = 2 fib(4) = 2 + 1 = 3 fib(5) = 2 + 3 = 5

What is the value of BadFactorial(2)? Recursive Definition int BadFactorial(n){ int x = BadFactorial(n-1); if (n == 1) return 1; else return n*x; } What is the value of BadFactorial(2)? We must make sure that recursion eventually stops, otherwise it runs forever:

Using Recursion Properly For correct recursion we need two parts: 1. One (ore more) base cases that are not recursive, i.e. we can directly give a solution: if (n==1) return 1; 2. One (or more) recursive cases that operate on smaller problems that get closer to the base case(s) return n * factorial(n-1); The base case(s) should always be checked before the recursive calls.

Recursion for efficient programming As a preliminary exercise, try to program the following sequence... 111,112,113,121,122,123,131,132,133, 211,212,213,221,222,223,231,232,233, 311,312,313,321,322,323,331,332,333. ...using For loops. Answer: It's as simple as:- for i = 1 to 3 for j = 1 to 3 for k = 1 to 3 print i, j, k

Recursion for efficient programming void Func(n) { for i = 1 to 3 A[n] = i if (n < 3) Func(n+1) else print A[1], A[2], A[3] } A[] is simply an array of integers. The function should be invoked with initial n value as 1, i.e., Func(1). Trace the program to figure out the output.

Towards an Efficient Approach To improve efficiency, we write the entire algorithm to output the first m terms in a recursive way, rather than use recursion in just the subroutine The key to using a recursive approach to solving a problem is to work out how to solve the problem, assuming that a simpler case of the same problem has already been solved. So, for our sequence problem, we need to work out how to output the first m terms, assuming that we already have an algorithm to output the first m - 1 terms.

Towards an Efficient Approach This can be done in the following way: Using the algorithm we already have, output the first m - 1 terms-omit this step if m = 1 Evaluate t(m), the mth term of the sequence Output the value of t(m) Before converting this approach to pseudo code, look at how step 2 is to be achieved. It's silly to use the recursive function algorithm to evaluate t(m), since this would cause precisely the type of inefficiency we're trying to eliminate.

Counting Digits Recursive definition Example digits(n) = 1 if (–9 <= n <= 9) 1 + digits(n/10) otherwise Example digits(321) = 1 + digits(321/10) = 1 +digits(32) = 1 + [1 + digits(32/10)] = 1 + [1 + digits(3)] = 1 + [1 + (1)] = 3

Counting Digits in C++ int numberofDigits(int n) { if ((-10 < n) && (n < 10)) return 1 else return 1 + numberofDigits(n/10); }

Recursion If you want to compute f(x) but can’t compute it directly Assume you can compute f(y) for any value of y smaller than x Use f(y) to compute f(x) For this to work, there has to be at least one value of x for which f(x) can be computed directly (e.g. these are called base cases)

Evaluating Exponents Recurisivley int power(int k, int n) { // raise k to the power n if (n == 0) return 1; else return k * power(k, n – 1); }

Divide and Conquer Using this method each recursive subproblem is about one-half the size of the original problem If we could define power so that each subproblem was based on computing kn/2 instead of kn – 1 we could use the divide and conquer principle Recursive divide and conquer algorithms are often more efficient than iterative algorithms

Evaluating Exponents Using Divide and Conquer int power(int k, int n) { // raise k to the power n if (n == 0) return 1; else{ int t = power(k, n/2); if ((n % 2) == 0) return t * t; else return k * t * t; }

Selection Sort void selectionSort (double[] A, int lo, int hi) { // A[0]..A[lo-1] contain smallest // values in A, in ascending order if (lo < hi) { swap(A, lo, findMiniumum(A, lo, hi); selectionSort(A, lo + 1, hi); }

Stacks Every recursive function can be implemented using a stack and iteration. Every iterative function which uses a stack can be implemented using recursion.

Disadvantages May run slower. May use more space. Compilers Inefficient Code May use more space.

Advantages More natural. Easier to prove correct. Easier to analysis. More flexible.