A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursion.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Analysis of Recursive Algorithms
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.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
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.
Data Structures Using C++ 2E Chapter 6 Recursion.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Data Structures Using C++ 2E Chapter 6 Recursion.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
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.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
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.
RECURSION.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
M180: Data Structures & Algorithms in Java
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Recursion Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
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.
A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma.
1 Section 5.5 Solving Recurrences Any recursively defined function ƒ with domain N that computes numbers is called a recurrence or recurrence relation.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
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.
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 Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
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.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
7. RECURSIONS Rocky K. C. Chang October 12, 2015.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
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.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 2 Objectives Upon completion you will be able to:
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
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 DRILL: Please take out your notes on 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.
Java 4/4/2017 Recursion.
Recursion Chapter 10.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion Chapter 11.
CS201: Data Structures and Discrete Mathematics I
Chapter 12 Supplement: Recursion with Java 1.5
ITEC324 Principle of CS III
Presentation transcript:

A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma

Objectives (Chapter 5) Definition of recursion and how it works Recursion tree Divide and Conquer Designing Recursive Algorithm Tail Recursion When Not to Use Recursion

Definition Recursion is the case when a function invokes itself or invokes a sequence of other functions, one of which eventually invokes the first function again  Suppose we have 4 functions: A, B, C and D Recursion: A → B → C → D → A Recursion is a feature of some programming languages, such as C++ and Java  No recursion feature in Cobol and Fortran

Tree of Subprogram Calls M() { A(); D(2); } A() { B(); C(); } B() {} C() { D(0); } D(int n) { if (n>0) D(n-1); }

Recursion Tree A recursion tree is a tree of subprogram calls that show recursive function calls Note that the tree shows the calls of functions  A function called from only one place, but within a loop executed more than once, will appear several times in the tree  If a function is called from a conditional statement that is not executed, then the call will not appear in the tree The total number of function calls is proportional to the total number of nodes of the recursion tree

Why Recursion Divide and conquer  To obtain the answer to a large problem, the large problem is often reduced to one or more problems of a similar nature but a smaller size  Subproblems are further divided until the size of the subproblems is reduced to some smallest, base case, where the solution is given directly without further recursion

A Mathematics Example The factorial function  Informal definition:  Formal definition:

 Problem: 4! 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) = 4 * (3 * (2 * (1 * 0!))) = 4 * (3 * (2 * (1 * 1))) = 4 * (3 * (2 * 1)) = 4 * (3 * 2) = 4 * 6 = 24

Every recursive process consists of two parts:  A smallest, base case that is processed without recursion;  A general method that reduces a particular case to one or more of the smaller cases, thereby eventually reducing the problem all the way to the base case. Do not try to understand a recursive algorithm by working the general case all the way down to the stopping rule  It may be helpful to work a small example Instead, only think about the correctness of the base bases and the recursive cases  If they are correct, then the recursive algorithm should be correct

 Example: Factorial int factorial (int n) /* Pre: n is an integer no less than 0 Post: The factorial of n (n!) is returned Uses: The function factorial recursively */ { if (n == 0) return 1; else return n * factorial (n - 1); }

 Example: Inorder traversal of a binary tree template void Binary_tree :: recursive_inorder(Binary_node *sub_root, void (*visit)(Entry &)) /* Pre: sub_root is either NULL or points to a subtree of the Binary_tree Post: The subtree has been traversed in inorder sequence Uses: The function recursive_inorder recursively */ { if (sub_root != NULL) { recursive_inorder(sub_root->left, visit); (*visit)(sub_root->data); recursive_inorder(sub_root->right, visit); }

The Hanoi Tower  A good example of solving a big problem using the divide and conquer and recursion technology Pp

Designing Recursive Algorithm Find the key step  Begin by asking yourself, “How can this problem be divided into parts?”  Once you have a simple, small step toward the solution, ask whether the remainder of the problem can be solved in the same or a similar way Find a stopping rule (base case)  The stopping rule is usually the small, special case that is trivial or easy to handle without recursion

Outline your algorithm  Combine the stopping rule and the key step, using an if statement to select between them Check termination  Verify that the recursion will always terminate All possible base cases are considered  Be sure that your algorithm correctly handles all possible base cases

Exercise  Write a recursive function for the following problem: Given a number n (n > 0), if n is even, calculate n. If n is odd, calculate n

About a recursion tree  The height of the tree is closely related to the amount of memory that the program will require  The total size of the tree reflects the number of times the key step will be done

Tail Recursion Definition  Tail recursion occurs when the last-executed statement of a function is a recursive call to itself Problem  Since the recursive call is the last action of the function, there is no need for recursion No difference in execution time for most compilers  Compiler will transform it into a loop  Functional programming often requires the transformation of a non-tail recursion into a tail recursion so that optimizations can be done

int sumto(int n){ if (n <= 0) return 0; else return sumto(n-1) + n; } int sumto1(int n, int sum){ if (n <= 0) return sum; else return sumto1(n-1,sum+n); }

Guidelines and Conclusions of Recursion When not to use recursion  Use the recursion tree to analyze  If a function call makes only one recursive call to itself, then its recursion tree is a chain In such a case, transformation from recursion to iteration is often easy and can save both space and time  If the recursion tree involves duplicate tasks, some data structure other than stack may be appropriate  Read pp

Example: Fibonacci Numbers Fibonacci numbers are defined by the recurrence relation Recursive solution int fibonacci(int n) /* fibonacci : recursive version */ { if (n <= 0) return 0; else if (n == 1) return 1; else return fibonacci(n − 1) + fibonacci(n − 2); } Problems  The results stored on the stack are discarded  There are lots of duplicate tasks in the tree

Non-recursive solution int fibonacci(int n) /* fibonacci : iterative version */ { int last_but_one; // second previous Fibonacci number, F i−2 int last_value; // previous Fibonacci number, F i−1 int current; // current Fibonacci number F i if (n <= 0) return 0; else if (n == 1) return 1; else { last_but_one = 0; last value = 1; for (int i = 2; i <= n; i++) { current = last_but_one + last_value; last_but_one = last_value; last_value = current; } return current; }

Recursion can always be replaced by iteration and stacks Conversely, any iterative program that manipulates a stack can be replaced by a recursive program without a stack

Analyzing Recursive Algorithms Often a recurrence equation is used as the starting point to analyze a recursive algorithm  In the recurrence equation, T(n) denotes the running time of the recursive algorithm for an input of size n We will try to convert the recurrence equation into a closed form equation to have a better understanding of the time complexity  Closed Form: No reference to T(n) on the right side of the equation  Conversions to the closed form solution can be very challenging

Example: Factorial int factorial (int n) /* Pre: n is an integer no less than 0 Post: The factorial of n (n!) is returned Uses: The function factorial recursively */ { if (n == 0) return 1; else return n * factorial (n - 1); } 1

 The time complexity of factorial(n) is:  T(n) is an arithmetic sequence with the common difference 4 of successive members and T(0) equals 2  The time complexity of factorial is O(n) 3+1: The comparison is included

Fibonacci numbers int fibonacci(int n) /* fibonacci : recursive version */ { if (n <= 0) return 0; else if (n == 1) return 1; else return fibonacci(n − 1) + fibonacci(n − 2); }

 The time complexity of fibonacci is:  Theorem (in Section A.4): If F(n) is defined by a Fibonacci sequence, then F(n) is  (g n ), where  The time complexity is exponential: O(g n )