31.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture 12 4.2.2002.

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.
Back to Sorting – More efficient sorting algorithms.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursion.
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.
CSE 326 Analyzing Recursion David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion. Binary search example postponed to end of lecture.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
Recursion.
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.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
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.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Recursion.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
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 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
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 CMPE231, Spring 2012 Aleaxander G. Chefranov 1.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
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.
Functions-Recall 1. 2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2:
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
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.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
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.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any.
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.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Recursion. What is Recursion? Method of solving problems Alternative to iteration All recursive solutions can be implemented iteratively Characteristic...
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
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:
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Recursion.
Chapter 19: 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.
Chapter 15 Recursion.
Data Structures and Programming Techniques
Recursion Chapter 12.
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Data Structures and Programming Techniques
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
CS201: Data Structures and Discrete Mathematics I
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.
Chapter 18 Recursion.
Recursive Algorithms 1 Building a Ruler: drawRuler()
ITEC324 Principle of CS III
Presentation transcript:

Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture

Sudeshna Sarkar, IIT Kharagpur 2 Recursion a + (a+1) + (a+2) b = a + ((a+1) + (a+2) b) sum(a,b) = a + sum(a+1,b) Base case ? factorial (n) = n*factorial(n-1) Base case ?

Sudeshna Sarkar, IIT Kharagpur 3 Recursion int sum (int a, int b) { int temp; if (a>b) return 0; temp=sum(a+1,b); return a+temp; } int factorial (int n) { int temp; if (n<=1) return 1; temp=factorial(n-1); return n*temp; }

Sudeshna Sarkar, IIT Kharagpur 4 Recursion main () {... x = sum(1,4);... } int sum (int a, int b) { int temp; if (a>b) return 0; temp=sum(a+1,b); return a+temp; } 1 4 sum(a,b) { temp=sum(2,4); return 1+temp; } sum(2,4) { temp=sum(3,4); return 2+temp; } sum(3,4) { temp=sum(4,4); return 3+temp; } sum(4,4) { temp=sum(5,4); return 4+temp; } sum(5,4) { return 0; }

Sudeshna Sarkar, IIT Kharagpur 5 Recursion main () {... x = sum(1,5);... } int sum (int a, int b) { int temp; if (a>b) return 0; temp=sum(a+1,b); return a+temp; } sum(1,4) { temp=sum(2,4); return 1+temp; } sum(2,4) { temp=sum(3,4); return 2+temp; } sum(3,4) { temp=sum(4,4); return 3+temp; } sum(4,4) { temp=0; return 4; }

Sudeshna Sarkar, IIT Kharagpur 6 Recursion main () {... x = sum(1,5);... } int sum (int a, int b) { int temp; if (a>b) return 0; temp=sum(a+1,b); return a+temp; } sum(1,4) { temp=sum(2,4); return 1+temp; } sum(2,4) { temp=sum(3,4); return 2+temp; } sum(3,4) { temp=4; return 3+4; }

Sudeshna Sarkar, IIT Kharagpur 7 Recursion main () {... x = sum(1,5);... } int sum (int a, int b) { int temp; if (a<=b) return 0; temp=sum(a+1,b); return a+temp; } sum(1,4) { temp=sum(2,4); return 1+temp; } sum(2,4) { temp=7; return 2+7; }

Sudeshna Sarkar, IIT Kharagpur 8 Recursion main () {... x = sum(1,5);... } int sum (int a, int b) { int temp; if (a<=b) return 0; temp=sum(a+1,b); return a+temp; } sum(1,4) { temp=9; return 1+9; }

Sudeshna Sarkar, IIT Kharagpur 9 void foo () { char ch; scanf (“%c”, &ch); if (ch==‘\n’) return; foo (); printf (“%c”, &ch); } What will the program output on the following input ? PDS \n

Sudeshna Sarkar, IIT Kharagpur 10 Example int sumSquares (int m, int n) { if (m<n) /* Recursion */ return sumSquares(m, n-1) + n*n; else /* Base Case */ return n*n ; } int sumSquares (int m, int n) { if (m<n) /* Recursion */ return m*m + sumSquares(m+1, n); else /* Base Case */ return m*m ; }

Sudeshna Sarkar, IIT Kharagpur 11 Example int sumSquares (int m, int n) { int middle ; if (m==n) return m*m; else { middle = (m+n)/2; return sumSquares(m,middle) + sumSquares(middle+ 1,n) ; } m midmid+1 n

Sudeshna Sarkar, IIT Kharagpur 12 Call Tree sumSquares(5,10) sumSquares(5,7) sumSquares(5,10)sumSquares(8,10) sumSquares(5,6) sumSquares(7,7) sumSquares(8,9) sumSquares(10,10) sumSquares(5,5) sumSquares(6,6)sumSquares(8,8) sumSquares(9,9)

Sudeshna Sarkar, IIT Kharagpur 13 Annotated Call Tree sumSquares(5,10) sumSquares(5,7) sumSquares(5,10)sumSquares(8,10) sumSquares(5,6) sumSquares(7,7) sumSquares(8,9) sumSquares(10,10) sumSquares(5,5) sumSquares(6,6)sumSquares(8,8) sumSquares(9,9)

Sudeshna Sarkar, IIT Kharagpur 14 Trace sumSq(5,10) = (sumSq(5,7) + sumSq(8,10)) = (sumSq(5,6) + (sumSq(7,7)) + (sumSq(8,9) + sumSq(10,10)) = ((sumSq(5,5) + sumSq(6,6)) + sumSq(7,7)) + ((sumSq(8,8) + sumSq(9,9)) + sumSq(10,10)) = (( ) + 49) + (( ) + 100) = ( ) + ( ) = ( ) = 355

Sudeshna Sarkar, IIT Kharagpur 15 Recursion : The general idea Recursive programs are programs that call themselves to compute the solution to a subproblem having these properties : 1. the subproblem is smaller than the overall problem or, simpler in the sense that it is closer to the final solution 2. the subproblem can be solved directly (as a base case) or recursively by making a recursive call. 3. the subproblem’s solution can be combined with solutions to other subproblems to obtain the solution to the overall problem.

Sudeshna Sarkar, IIT Kharagpur 16 Think recursively Break a big problem into smaller subproblems of the same kind, that can be combined back into the overall solution : Divide and Conquer

Sudeshna Sarkar, IIT Kharagpur 17 Exercise 1. Write a recursive function that computes x n, called power (x, n), where x is a floating point number and n is a non- negative integer. 2. Write an improved recursive version of power(x,n) that works by breaking n down into halves, squaring power(x, n/2), and multiplying by x again if n is odd. 3. To calculate the square root of x (a +ve real) by Newton’s method, we start with an initial approximation a=x/2. If |x- a  a|  epsilon, we stop with the result a. Otherwise a is replaced with the next approximation, (a+x/a)/2. Write a recursive method, sqrt(x) to compute square root of x by Newton’s method.

Sudeshna Sarkar, IIT Kharagpur 18 Common Pitfalls Infinite Regress : a base case is never encountered a base case that never gets called : fact (0) running out of resources : each time a function is called, some space is allocated to store the activation record. With too many nested calls, there may be a problem of space. int fact (int n) { if (n==1) return 1; return n*fact(n-1); }

Sudeshna Sarkar, IIT Kharagpur 19 Tower of Hanoi ABC

Sudeshna Sarkar, IIT Kharagpur 20 Tower of Hanoi ABC

Sudeshna Sarkar, IIT Kharagpur 21 Tower of Hanoi ABC

Sudeshna Sarkar, IIT Kharagpur 22 Tower of Hanoi ABC

Sudeshna Sarkar, IIT Kharagpur 23 void towers (int n, char from, char to, char aux) { if (n==1) { printf (“Disk 1 : %c -> &c \n”, from, to) ; return ; } towers (n-1, from, aux, to) ; printf (“Disk %d : %c  %c\n”, n, from, to) ; towers (n-1, aux, to, from) ; }

Sudeshna Sarkar, IIT Kharagpur 24 Disk 1 : A -> C Disk 2 : A -> B Disk 1 : C -> B Disk 3 : A -> C Disk 1 : B -> A Disk 2 : B -> C Disk 1 : A -> C Disk 4 : A -> B Disk 1 : C -> B Disk 2 : C -> A Disk 1 : B -> A Disk 3 : C -> B Disk 1 : A -> C Disk 2 : A -> B Disk 1 : C -> B towers (4, ‘A’, ‘B’, ‘C’) ;

Sudeshna Sarkar, IIT Kharagpur 25 Recursion may be expensive ! Fibonacci Sequence: fib (n) = n if n is 0 or 1 fib (n) = fib (n-2) + fib(n-1) if n>= 2. int fib (int n){ if (n==0 or n==1) return 1; return fib(n-2) + fib(n-1) ; }

Sudeshna Sarkar, IIT Kharagpur 26 Call Tree fib (5) fib (3)fib (4) fib (1) fib (2)fib (1)fib (2) fib (0) fib (3) fib (1) fib (2) fib (0) fib (1)

Sudeshna Sarkar, IIT Kharagpur 27 Iterative fibonacci computation int fib (int n){ if (n <= 1) return n; lofib = 0 ; hifib = 1 ; for (i=2; i<=n; i++) { x = lofib ; lofib = hifib ; hifib = x + lofib; } return hifib ; } i = x = lofib= hifib=

Sudeshna Sarkar, IIT Kharagpur 28 Merge Sort To sort an array of N elements, 1. Divide the array into two halves. Sort each half. 2. Combine the two sorted subarrays into a single sorted array Base Case ?

Sudeshna Sarkar, IIT Kharagpur 29 Binary Search revisited To search if key occurs in an array A (size N) sorted in ascending order: mid = N/2; if (key == N/2) item has been found if (key < N/2) search for the key in A[0..mid-1] if (key > N/2) search for key in A[mid+1..N] Base Case ?