Ms N Nashandi Dr SH Nggada Week 08 – Recursion. Outline We shall be covering the following What is a recursion? Iteration versus Recursion. Recursion.

Slides:



Advertisements
Similar presentations
More on Recursive Methods KFUPM- ICS Data Structures.
Advertisements

Computer Science 1620 Loops.
More on Recursive Recursion vs. Iteration Why Recursion?
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
ICS103 Programming in C Lecture 11: Recursive Functions
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
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.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
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.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Recursion Powerful Tool
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Introduction to Computer Programming
Topic: Recursion – Part 2
CprE 185: Intro to Problem Solving (using C)
REPETITION CONTROL STRUCTURE
Recursion DRILL: Please take out your notes on Recursion
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Decrease-and-Conquer Approach
More on Recursive Recursion vs. Iteration Why Recursion?
Lecture 7: Repeating a Known Number of Times
More on Recursive Methods
Repetition Structures (Loops)
Programming Fundamentals
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
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.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Lecture 17 Recursion part 1 Richard Gesick.
Programming application CC213
Announcements Final Exam on August 19th Saturday at 16:00.
Computing Fundamentals
Announcements Final Exam on December 25th Monday at 16:00.
Control Structures Part 1
Let’s all Repeat Together
Lecture 12 Recursion part 1 CSE /26/2018.
Self-Referencing Functions
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
EECE.2160 ECE Application Programming
Recursion.
7.2 Recursive Definitions of Mathematical Formulas
Presentation transcript:

Ms N Nashandi Dr SH Nggada Week 08 – Recursion

Outline We shall be covering the following What is a recursion? Iteration versus Recursion. Recursion 22016/01/03Ms N Nashandi and Dr SH Nggada

What is Recursion? Recursion refers to a function (or method) calling itself This could be a direct or indirect call Direct call Assuming you have a function named fun1, if fun1 calls itself within the code of fun1 then this is a direct call Indirect call Assuming fun1 calls another function named fun2 and fun2 in turn calls fun1 then this is an indirect call This could get complicated, as fun2 itself will experience an indirect call of itself, however, the process would have been initiated by fun1 2016/01/04NUST Advanced Programming3

Iteration versus Recursion Recursion just like Iteration, may lead to indefinite execution of code Therefore, as it is the case with iteration, a means to ending the recursion should exist in the function 2016/01/04NUST Advanced Programming4

Iteration versus Recursion An iteration has no means of calling itself but contains statements that refer back to the start of the loop. On the other hand, a recursion has a means to calling itself Recursion is usually used to simplify computation than it could be done with a iteration A more sophisticated recursion (as used in Quick sort) could divide problems into simpler parts (divide and conquer) Iteration and recursion both use repetition Iteration explicitly uses repetition structure while recursion achieves repetition via repeated method calls 2016/01/04NUST Advanced Programming5

Iteration versus Recursion Which is better? There is no clear answer Mathematicians prefer recursive approach Solutions are often shorter Note, good recursive solutions may be more difficult to design and test. Programmers seem to often prefer iterative solutions It seems more appealing to many Control stays within or local to loop and also appears less magical 2016/01/04NUST Advanced Programming6

Iteration versus Recursion Which Approach to Use It largely depends on the problem For instance, factorial problem which is often used to demonstrate the use of recursion is pretty artificial It doe not matter what approach you use because it is so simple The use of factorial though to demonstrate recursion helps in understanding the concept of recursion For many advanced data structures (e.g. trees), are more efficient if implemented using recursive approach If you are not sure which one is better for a given problem, you could use code analysis techniques to figure out which one to use 2016/01/04NUST Advanced Programming7

Recursion To understand the implementation of recursion, we shall use examples Example 1 The evaluation of a formula such as X n where X is a double (thus, could take an integer equivalent value) and n is an integer This simply refers to multiplying X by itself n times Example 2 The evaluation of the factorial of a given integer, i.e. X! This simply refers to the multiple of X and its preceding integers and stopping at 0. Note, 0! = /01/04NUST Advanced Programming8

Recursion To understand the implementation of recursion, we shall use examples We shall use 2 implementations for each example Iteration Recursion 2016/01/04NUST Advanced Programming9

Recursion Example 1 - X n Iteration 2016/01/04NUST Advanced Programming10 int main() { double X; // base number int n; // power number cout << "Enter Base Number: "; // prompts the user to enter base cin >> X; // reads base number cout << "Enter Power: "; // prompts the user to enter power cin >> n; // reads power double result = 1; // holds the result of the evaluation // Calculates X raised to power of n for (int i = 1; i <= n; i++) { result = result * X; } cout << X << " to the power of " << n << " is " << result << endl; system("pause"); // pauses execution and waits for key stroke return 0; // returns to the operating system }

Recursion Example 1 - X n Iteration 2016/01/04NUST Advanced Programming11 Example execution

Recursion Example 1 - X n Recursion 2016/01/04NUST Advanced Programming12 int main() { double X; // base number int n; // power number cout << "Enter Base Number: "; // prompts the user to enter base cin >> X; // reads base number cout << "Enter Power: "; // prompts the user to enter power cin >> n; // reads power double result; // holds the result of the evaluation result = Power(X, n); // calls the recursive function cout << X << " to the power of " << n << " is " << result << endl; system("pause"); // pauses execution and waits for key stroke return 0; // returns to the operating system }

Recursion Example 1 - X n Recursion 2016/01/04NUST Advanced Programming13 double Power(double X, int n) { if (n == 0) return 1; // returns 1 if the power is 0 else return X * Power(X, n - 1); // performs recursive evaluation } Recursive function – Power(X, n)

Recursion Example 1 - X n Recursion 2016/01/04NUST Advanced Programming14 Example execution

Recursion Example 1 - X n The if statement provides for the value 1 to be returned if n is zero, and in all other cases, it returns the result of the expression, X*Power(X, n-1) The expression causes a further call to the function Power() with current value of n reduced by 1 Thus, the else clause in the if statement provides the essential mechanism necessary to avoid an indefinite sequence of recursive function calls Clearly, if the value of n is other than zero within the function Power(), a further call to the function Power() occurs For any given value of n other than 0, the function calls itself n times 2016/01/04NUST Advanced Programming15

Recursion Example 2 – X! Iteration 2016/01/04NUST Advanced Programming16 int main() { int X; // base number cout << "Enter Number: "; // prompts the user to enter base cin >> X; // reads base number int result = 1; // holds the result of the evaluation and intialises it to 1 for (int i = X; i >= 1; i--) // from X down to 1; could be the other way round { result = result * i; // performs the evaluations } cout << X << " factorial is " << result << endl; // prints out the result system("pause"); // pauses execution and waits for key stroke return 0; // returns to the operating system }

Recursion Example 2 – X! Iteration 2016/01/04NUST Advanced Programming17 Example execution

Recursion Example 2 – X! Recursion 2016/01/04NUST Advanced Programming18 int main() { int X; // base number cout << "Enter Number: "; // prompts the user to enter base cin >> X; // reads base number int result = Factorial(X); // calls the recursive function for factorial cout << X << " factorial is " << result << endl; // prints out the result system("pause"); // pauses execution and waits for key stroke return 0; // returns to the operating system }

Recursion Example 2 – X! Recursion 2016/01/04NUST Advanced Programming19 int Factorial(int X) { if (X == 0) return 1; else return X * Factorial(X - 1); }

Recursion Example 2 – X! Recursion 2016/01/04NUST Advanced Programming20 Example execution

Recursion From the examples, it could be deduced that iteration exclusively uses repetition while Recursion uses selection to achieve repetition 2016/01/04NUST Advanced Programming21

Further Reading Find out to use recursion for more complex problems or data structures /01/03Ms N Nashandi and Dr SH Nggada

End of Lecture 23 ? 2016/01/03Ms N Nashandi and Dr SH Nggada