Recursion.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Recursion COL 106.
Recursion.
Factorial Recursion stack Binary Search Towers of Hanoi
Programming Recursion.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
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 COMP171 Fall Recursion / Slide 2 Recursion * In some problems, it may be natural to define the problem in terms of the problem itself.
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
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.
Simple Recursion. COMP104 Lecture 35 / Slide 2 Recursion: Example 0 * What does the following program do? #include using namespace std; int fac(int n){
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
1 Chapter 1 RECURSION. 2 Chapter 1  Subprogram implementation  Recursion  Designing Recursive Algorithms  Towers of Hanoi  Backtracking  Eight Queens.
Chapter 14: Recursion Starting Out with C++ Early Objects
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
CMPE13 Cyrus Bazeghi Chapter 17 Recursion. CMPE13 What is Recursion? A recursive function is one that solves its task by calling itself on smaller pieces.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
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.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Recursion. Hanoi Tower Legend: Inside a Vietnamese temple there are three rods (labeled as r 1, r 2, and r 3 ), surrounded by n golden disks of different.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Basic Mathematics Chapter 1 (1.2 and 1.3) Weiss. Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition.
Math & Recursion COMP171 Fall Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition * Theorem.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
CS212: Data Structures and Algorithms
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion Version 1.0.
to understand recursion you must understand recursion
Chapter 15 Recursion.
Recursion: The Mirrors
Chapter 17 Recursion.
Recursion Chapter 12.
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Recursion and Logarithms
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
to understand recursion you must understand recursion
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.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Chapter 17 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
CS201: Data Structures and Discrete Mathematics I
Recursion Data Structures.
Announcements Final Exam on August 19th Saturday at 16:00.
Search,Sort,Recursion.
Chapter 17 Recursion.
Announcements Final Exam on December 25th Monday at 16:00.
Recursion: The Mirrors
Chapter 17 Recursion.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
CS 144 Advanced C++ Programming April 16 Class Meeting
Advanced Analysis of Algorithms
Presentation transcript:

Recursion

Introduction What does the program do? #include <iostream> using namespace std; int fac(int n){ int product; if(n <= 1) product = 1; else product = n * fac(n-1); return product; } void main(){ int number; cout << "Enter a positive integer : " << endl;; cin >> number; cout << fac(number) << endl;

Assume the number typed is 3. Trace it … Assume the number typed is 3. fac(3): has the final returned value 6 3<=1 ? No. product3 = 3*fac(2) product3=3*2=6, return 6, fac(2): 2<=1 ? No. product2 = 2*fac(1) product2=2*1=2, return 2, fac(1): 1<=1 ? Yes. return 1

Normal (non-recursive) functions Recursive function int main() { one(…); } void one(…) { … two(…) void two(…) { three(…) void three(…) { int main() { fac(3); } int fac(int n){ int product; if(n <= 1) product = 1; else product = n * fac(n-1); return product; Functions are calling DIFFERENT functions One function (three) is the last ‘stopping function’ … calling the SAME function (with different parameters) … The ‘stopping function’ is already included as a ‘condition’

Recursive function A recursive function is just a function which is calling one (or more) other functions which happen to be the same!!! Though the function is the same, the ‘parameters’ are always different and ‘smaller’. There is always at least one stopping case to terminate. It is a kind of ‘loop’, even more powerful as a general problem-solving technique! --- thinking recursively!

Recursion: problem solving, therefore a programming technique Remember: The general top-down programming and problem solving: A complex problem is often easier to solve by dividing it into several smaller parts (by top-down analysis), each of which can be solved by itself. Recursion is one way to decompose a task into smaller subtasks. At least one of the subtasks is a smaller example of the same task. The smallest example of the same task has a non-recursive solution. Example: The factorial function n! = n * (n-1) * (n-2) * ... * 1 or n! = n * (n-1)! and 1! = 1

Recursion vs. Iteration (non-recursive) A recursive solution may be simpler to write (once you get used to the idea) than a non-recursive solution. But a recursive solution may not be as efficient as a non-recursive solution of the same problem. To iterate is human, to recurse, divine!

Iterative Factorial // Non-recursive factorial function // Compute the factorial using a loop int fac(int n){ int k, product; if(n <=1) product = 1; product = 1; for(k=1; k<=n; k++) product = k*product; return product; }

Other recursive examples Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... where each number is the sum of the preceding two. Recursive definition: F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2)

Everything is recursive … Linear search Length of a string Min, max of an array Selection sort Bubble sort … Binary search: Compare search element with middle element of the array: If not equal, then apply binary search to half of the array (if not empty) where the search element would be.

For n elements in an array: Start from the first element while (not yet finished) do the current element move to the next one do-something(n) If 0 or 1 element, just do it else (decompose into first element and the n-1 remaining elements) do the first element do-something(n-1)

Sum of an array Write a recursive function that computes the sum of all elements in an array of integers. What is the summation of n elements? int summation(int a[], int size){ int sum; if(size==0) sum=0; else sum=summation(a,size-1)+a[size-1]; return sum; }

Product of an array Write a recursive function that computes the product of an array of integers. What is the product of n elements? int product(int a[], int size) { int prod; if(size==0) prod=1; else prod=product(a,size-1)*a[size-1]; return prod; }

Recursion general form How to write recursively? function-type function(parameters) { function-type value; if(stopping conditions) value = stopping value; else value = g(function((revised parameters))); return value; }

Exponential How to write exp(int x, int y) recursively? int exp(int x, int y) { int power; if(y==0) power = 1; else power = x * exp(x, y-1); return power; }

Counting the number of zeros Write a recursive function that counts the number of zero digits in a non-negative integer zeros(10200) returns 3 int zeros(int n){ int z; if (n<10) if (n==0) z=1; else z=0; else z=zeros(n/10)+zeros(n%10); return z; } n/10  the number n with the last digit removed n%10  the last digit of n

Find factors Write a recursive function to determine how many factors m are part of n. For example, if n=48 and m=4, then the result is 2 (48=4*4*3). int factors(int n, int m){ int f; if(n%m != 0) f=0; else f=1+factors(n/m, m); return f; }

Summary natural consequence of ‘top-down’ analysis ‘everything’ is recursive Decomposition-composition Let’s trace a few examples … zeros(123009840) asum(A,5) exp(6,4) …

A more subtle example: the max of an array max(n): start from the first element: max=the-first while (not yet finished) pick up new-element max=maximumOfTwo(max,new-element) move to the next element max(n): if one-element, max = one-element else decompose n elements into: 1-element and n-1 element m1 = the 1-element m2 = max(n-1) compose the final solution: max = maximumOfTwo(m1,m2) int max2(int m1,int max2) {return (m1>m2)?m1:m2;}; int max(int A[], int n) { int max=A[0]; int i=1; while (i<n) { max=max2(max,A[i]); i=i+1; } return max; int max2(int m1,int max2) {return (m1>m2)?m1:m2;}; int max(int A[], int size) { int max,m2; if (size==1) max=A[size-1]; else m2=max(A,size-1); max=max2(A[size-1],m2); return max; }

Efficiency The same code, but no intermediate storage ‘m2’, int max2(int m1,int max2) {return (m1>m2)?m1:m2;}; int max(int A[], int size) { int max; if (size==1) max=A[size-1]; else max=max2(max(A,size-1),A[size-1]); return max; } int max(int A[], int size) { int max; if (size==1) max=A[size-1]; else if(max(A,size-1)>A[size-1])) max=max(A,size-1); else max=A[size-1]; return max; } int max(int A[], int size) { if (size==1) return A[size-1]; else if(max(A,size-1)>A[size-1])) return max(A,size-1); else return A[size-1]; } The same code, but no intermediate storage ‘m2’, it will have more recursive calls … inefficient!

Fibonacci (quite inefficient) int fibonacci(int n) { int f; if(n==0) f=0; else if (n==1) f=1; else f= fibonacci(n-1)+fibonacci(n-2); return f; } How many calls?

(Iterative) Fibonacci int fibonacci(int n) { int fib; int fibm1=1; int fibm2=0; if(n==0) fib=0; else if (n==1) fib=1; int j; for(j=2;j<=n;j++) { fib=fibm1+fibm2; fibm2=fibm1; fibm1=fib; } return fib;

Linear search Input: an array of integers A, and a value Output: the position of the value in the array if one element, check it! else decompose into 1-element and n-1 elements check the 1-element search(n-1)

int search(int A[], int size, int value) { int pos; if (size==1) if(A[size-1]==value) pos=1; else pos=-1; else if(A(size-1]==value) pos=size-1; else pos=search(A,size-1,value); return pos; }

(Binary) Search in a sorted array Input: a sorted array of integers A, and a value Output: the position of the value in the array

Binary search Input: a sorted array of integers A, and a value Output: the position of the value in the array if the middle element == value, done! else if the middle element > value do the same in the first half of the array else do the same in the second half of the array }

int bsearch(int data[],lower,upper,value) { int pos; if (lower<=upper) { mid=(lower+upper)/ 2; if (data[mid] == value) pos=mid; else if (data[mid]>value) pos=bsearch(data,lower,mid–1,value); else pos=bsearch(data,mid+1,upper,value); } return pos;

(iterative) Binary search: int bsearch(int data[], int size, int value) { int lower, middle, upper; bool found; lower = 0; upper = size - 1; found=false; position=-1; while ((lower<=upper) && (!found)) { middle = (lower+upper)/2; if (data[middle] == value) { found=true; position=middle; } else { if (data[middle] > value) upper = middle - 1; else lower = middle + 1; return position; int main() { const int size=8; int data[size] = {1,5,6,7,9,10,17,30}; int value; cin >> value; cout << “position of the value is “ << bsearch(data,size,value) << endl; }

The tower of Hanoi! Move a stack of disks of different sizes from one rod to another through a third one: - only one disk is moved each time - always smaller ones on top of bigger ones Check any webpage!

// move n disks from A to C via B void tower(int n, char A, char B, char C) { if (n==1) move(1,A,C); else {tower(n-1,A,C,B); move(n,A,C); tower(n-1,B,A,C)}; } void move(int k, char X, char Y) { cout << “move disc ” << k << “ from “ << X << “ to “ Y “ << endl;

trace tower(3,A,B,C) …

There are systematic ways of de-recursing a function Hanoi tower is easy to write in recursive form, but very hard to write in iterative from! LISP (scheme) language (for AI, Fortran for computing): functional programming language intrinsically recursive (loops are rarely used)

For n elements in an array: Start from the first element While (not yet finished) do the current element move to the next one DoSth(n) If 0 or 1 element, just do it else decompose into first element and the n-1 remaining elements (trivially) do the first element DoSth(n-1) compose the solution of n elements

Palindrome in an array of integers or characters of known size…

bool palindrom(int a[],lower,upper) { bool pal=false; if (lower>=upper) pal=true; else pal=(a[lower]==a[upper]) && palindrom(a,lower+1,upper-1); return pal; } int main() { … palindrom(a,0,10);