Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.

Slides:



Advertisements
Similar presentations
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Advertisements

Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Binary search example postponed to end of lecture.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
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.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
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.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion More on Project 2 Reading L&C 10.1 – 10.4.
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
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.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Recursion Chapter 5.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Data Structures Using C++ 2E Chapter 6 Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Data Structures Using C++ 2E Chapter 6 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
1 Storage Classes, Scope, and Recursion Lecture 6.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
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. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
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.
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.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
Lecture - 8 On Stacks, Recursion. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
1 Programming for Engineers in Python Autumn Lecture 8: Recursion.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursion.
CS212: Data Structures and Algorithms
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion CENG 707.
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Algorithm Analysis (for Divide-and-Conquer problems)
Chapter 12 Recursion (methods calling themselves)
7.Recursion Recursion is the name given for expression anything in terms of itself. Recursive function is a function which calls itself until a particular.
Module 1-10: Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Recursion by Ender Ozcan

Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming defines a function in terms of itself. Advantage: Infinite set of possible sentences, designs or other data can be defined, parsed or produced by a finite computer program. Advantage: Infinite set of possible sentences, designs or other data can be defined, parsed or produced by a finite computer program.

Example: the natural numbers The canonical example of a recursively defined set is given by the natural numbers: The canonical example of a recursively defined set is given by the natural numbers: 0 is in N 0 is in N if n is in N, then n + 1 is in N if n is in N, then n + 1 is in N The set of natural numbers is the smallest set satisfying the previous two properties. The set of natural numbers is the smallest set satisfying the previous two properties. Here's an alternative recursive definition of N: Here's an alternative recursive definition of N: 0, 1 are in N; 0, 1 are in N; if n and n + 1 are in N, then n + 2 is in N; if n and n + 1 are in N, then n + 2 is in N; N is the smallest set satisfying the previous two properties. N is the smallest set satisfying the previous two properties.

Example: Factorial Computing 4!, requires a call to Fact(4) Computing 4!, requires a call to Fact(4) Fact(4)  returns Fact(3)*46*4=24 Fact(4)  returns Fact(3)*46*4=24 Fact(3)  returns Fact(2)*32*3=6 Fact(3)  returns Fact(2)*32*3=6 Fact(2)  returns Fact(1)*21*2=2 Fact(2)  returns Fact(1)*21*2=2 Fact(1)  returns 1 Fact(1)  returns 1 integer Fact ( integer X ) { if X < 0 then return -1; // invalid arg if X = 1 then return 1; return Fact(X-1) * X; }

Basic steps of recursive programs Initialize the algorithm. Termination Criteria – check to see whether the current value(s) being processed match the base case. If so, process and return the value. Redefine the answer in terms of a smaller or simpler sub-problem or sub-problems. Run the algorithm on the sub-problem. Combine the results in the formulation of the answer. Return the results.

Example – Sum of n numbers Suppose we have a list of n numbers, and we want to sum them. Suppose we have a list of n numbers, and we want to sum them. 1.Initialize the algorithm. This algorithm's seed value is the first number to process and is passed as a parameter to the function. 2.Check for the base case. The program needs to check and see if the list is empty. If so, we return zero because the sum of all members of an empty list is zero. 3.Redefine the answer in terms of a simpler sub-problem. We can define the answer as the sum of the rest of the list plus the contents of the current number. To determine the sum of the rest of the list, we call this function again with the next number. 4.Combine the results. After the recursive call completes, we add the value of the current node to the results of the recursive call.

Linked List datanext 3 datanext 159 datanext 78 headtail Linked list is a basic data structure, used to hold a sequence of items in memory

C implementation using linked lists int sum_list(struct list_node *l) { if(l == NULL) return 0; return l.data + sum_list(l.next); }

Comparing loops with recursive functions PropertiesLoops Recursive functions Repetition Execute the same block of code repeatedly to obtain the result; signal their intent to repeat by either finishing the block of code or issuing a continue command. Execute the same block of code repeatedly to obtain the result; signal their intent to repeat by repeat by calling themselves. Terminating conditions In order to guarantee that it will terminate, a loop must have one or more conditions that cause it to terminate and it must be guaranteed at some point to hit one of these conditions. In order to guarantee that it will terminate, a recursive function requires a base case that causes the function to stop recursing. State Current state is updated as the loop progresses. Current state is passed as parameters.

To Loop or Not To Loop Rule of thumb for programmers If you can solve a problem using iteration, avoid recursion If you can solve a problem using iteration, avoid recursion

Fibonacci Sequence The original problem that Fibonacci investigated (in the year 1202) was about how fast rabbits could breed in ideal circumstances (assuming they don’t die). The original problem that Fibonacci investigated (in the year 1202) was about how fast rabbits could breed in ideal circumstances (assuming they don’t die).

n th Fibonacci Number fib(n) = fib(n – 1) + fib (n – 2) for n>1, and fib(0)=0, fib(1)=1. fib(n) = fib(n – 1) + fib (n – 2) for n>1, and fib(0)=0, fib(1)=1. C program computing n th fibonacci number: C program computing n th fibonacci number: int fib(int n) { if (n == 0) return 0; if (n == 1) return 1; return fib(n-1) + fib(n-2); }

n th Fibonacci Number-Revisited fib(5) fib(4)fib(3) fib(2)fib(1)fib(2) fib(1)fib(0)fib(1)fib(0)fib(1)fib(2) fib(1)fib(0) Try to avoid solving overlapping sub-problem instances

Recursive vs. Iterative Solution int fib(int n) { if (n == 0) return 0; if (n == 1) return 1; return fib(n-1) + fib(n-2); } int fib(int n) { int i, *fib=new int[n]; fib[0]=0; fib[1]=1; for (i=2; i<n; i++) fib[i]=fib[i-1]+fib[i-2]; return fib[n]; } Running time is ~ a(  n +  n ) Running time is ~ cn

Tower of Hanoi Puzzle Puzzle was invented by the French mathematician Edouard Lucas in Puzzle was invented by the French mathematician Edouard Lucas in We are given a tower of 8 disks, initially stacked in increasing size on one of three pegs. The objective is to transfer the entire tower to one of the other pegs, moving only one disk at a time and never a larger one onto a smaller. We are given a tower of 8 disks, initially stacked in increasing size on one of three pegs. The objective is to transfer the entire tower to one of the other pegs, moving only one disk at a time and never a larger one onto a smaller.

Recursive Solution Call the three pegs Src (Source), Aux (Auxiliary) and Dst (Destination). Call the three pegs Src (Source), Aux (Auxiliary) and Dst (Destination). SrcAuxDst n disks

Recursive Solution – Step 1 Move the top n-1 disks from Src to Aux (using Dst as an intermediary peg) Move the top n-1 disks from Src to Aux (using Dst as an intermediary peg) n-1 disks SrcAuxDst

Recursive Solution – Step 2 Move the bottom disks from Src to Dst Move the bottom disks from Src to Dst SrcAuxDst

Recursive Solution – Step 3 Move n-1 disks from Aux to Dst (using Src as an intermediary peg) Move n-1 disks from Aux to Dst (using Src as an intermediary peg) SrcAuxDst

Pseudocode Solve(n, Src, Aux, Dst) Solve(n, Src, Aux, Dst) if n is 0 return if n is 0 return Solve(n-1, Src, Dst, Aux) Solve(n-1, Src, Dst, Aux) Move from Src to Dst Move from Src to Dst Solve(n-1, Aux, Src, Dst) Solve(n-1, Aux, Src, Dst)

Analysis of the Algorithm I Assume than T(n) is the number of steps required to move n disks from Src to Dst Assume than T(n) is the number of steps required to move n disks from Src to Dst SrcAuxDst n disks

Analysis of the Algorithm II Moving the top n-1 disks from Src to Aux will take T(n-1) steps Moving the top n-1 disks from Src to Aux will take T(n-1) steps n-1 disks SrcAuxDst

Analysis of the Algorithm III Moving the bottom disk from Src to Dst will take a single step Moving the bottom disk from Src to Dst will take a single step SrcAuxDst

Analysis of the Algorithm IV Moving n-1 disks from Aux to Dst will take T(n-1) steps Moving n-1 disks from Aux to Dst will take T(n-1) steps SrcAuxDst

Overall Complexity T(n) = 2T(n-1) + 1 {T(n-1) = 2T(n-2) + 1 } T(n) = 2T(n-1) + 1 {T(n-1) = 2T(n-2) + 1 } T(n) = 2 2 T(n-2) substitute T(n-1) T(n) = 2 2 T(n-2) substitute T(n-1) T(n) = 2 3 T(n-3) T(n) = 2 3 T(n-3) … T(n) = 2 n-1 T( n-(n-1)) +… T(n) = 2 n-1 T( n-(n-1)) +… T(1)=1 T(n) = 2 n-1 +… (geometric series) T(n) = 2 n-1 +… (geometric series) T(n) = 2 n - 1 T(n) = 2 n - 1