Programming and Data Structures

Slides:



Advertisements
Similar presentations
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Advertisements

Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
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.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
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.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
1 Recursion Recursive method –Calls itself (directly or indirectly) through another method –Method knows how to solve only a base case –Method divides.
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.
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:
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.
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.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Recursion Definition Recursion is the computer programming process, whereby a method calls itself.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Recursion.
CS212: Data Structures and Algorithms
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion CENG 707.
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.
Introduction to Recursion
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Abdulmotaleb El Saddik University of Ottawa
RECURSION.
Chapter 15 Recursion.
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Formatted and Unformatted Input/Output Functions
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Chapter 11.
CS201: Data Structures and Discrete Mathematics I
Recursion Data Structures.
Functions Recursion CSCI 230
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.
Recursion When performing a repetitive task either: a loop recursion
Recursion: The Mirrors
CSC 143 Recursion.
ECE 103 Engineering Programming Chapter 18 Iteration
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

Programming and Data Structures Debasis Samanta Computer Science & Engineering Indian Institute of Technology Kharagpur Spring-2017

Lecture #6 Recursion in C CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Today’s Discussion… What is Recursion? Some Examples Recursion - Mechanism of Execution Recursion versus Iteration Function Call Implementation CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Concept of Recursion Lecture #05: © DSamanta CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Recursion A process by which a function calls itself repeatedly. Such a function is called a recursive function Recursion may be direct or cyclically in a chain Direct recursion. When a function f(…) calls f(…). Cyclically in a chain recursion. f1(…) calls f2(…) , f2(…) calls f3(…) . . . fi(…) calls f1(…) CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Some Examples of Recursion Example 3: GCD of two positive integers gcd(10, 15) = 5, gcd(11, 13) = 1 gcd(m,n) = gcd(m-n,n), if m>n else gcd(m,n-m) Example 4: Recursion formula T(n) = n + 2×T(n-1) T(100) = ? Example 4: Tower of Hanoi Move n disks from A C = move (n-1) disks from A to B + move the disk from A to C + move (n-1) disk from B to C CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Some Examples of Recursion Example 1: Factorial calculation n! = n× (n-1)×(n-2) ×…×3×2×1 n! = n×(n-1)! factorial(n) = n×factorial(n-1) Example 2: Fibonacci number sequence 1, 1, 2, 3, 5, 8, 13, 21, ….. ni = ni-1 + ni-2 fibonacci(n) = fibonacci(n-1) + fibonacci(n-2) CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Important Points to be Noted For a function to be written in recursive form, two conditions are to be satisfied: Condition 1 It should be possible to express the problem in recursive form. Condition 2 The problem statement must include a stopping condition. CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Important Points to be Noted It should be possible to express the problem in recursive form. Examples factorial(n) = n×factorial(n-1) fibonacci(n) = fibonacci(n-1) + fibonacci(n-2) gcd(m,n) = if m>n gcd(m-n, n) else gcd(m, n-m) T(n) = n + 2×T(n-1) CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Important Points to be Noted The problem statement must include a stopping condition. Examples factorial(n) = n×factorial(n-1) factorial(0) = 1 fibonacci(n) = fibonacci(n-1) + fibonacci(n-2) fibonacci(0) = 0; fibonacci(1) = 1 gcd(m,n) = if m>n gcd(m-n,n) else gcd(m,n-m) gcd(m, m) = m T(n) = n + 2×T(n-1) T(0) = 0 CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Some Examples of Recursion CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Recursive Function for n! fact(0) = 1 fact(n) = n × fact(n-1), if n > 0 #include <stdio.h> int fact(int n) { if (n == 0) return 1; else return (n * fact(n-1)); } void main() int x; scanf(“%d”, &x); printf (“Factorial of %d is: %d”, x, fact(x)); CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Factorial Execution When a recursive program is executed, the recursive function calls are not executed immediately. They are kept aside (on a stack) until the stopping condition is encountered. The function calls are then executed in reverse order. CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Factorial Execution if (4 = = 0) return (1); 24 if (4 = = 0) return (1); else return (4 * fact(3)); 6 if (3 = = 0) return (1); else return (3 * fact(2)); 2 if (2 = = 0) return (1); else return (2 * fact(1)); 1 int fact (int n) { if (n = = 0) return (1); else return (n * fact(n-1)); } if (1 = = 0) return (1); else return (1 * fact(0)); CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Factorial Execution First, the function calls will be processed fact(4) = 4 * fact(3) fact(3) = 3 * fact(2) fact(2) = 2 * fact(1) fact(1) = 1 * fact(0) The actual values return in the reverse order fact(0) = 1 fact(1) = 1 * 1 = 1 fact(2) = 2 * 1 = 2 fact(3) = 3 * 2 = 6 fact(4) = 4 * 6 = 24 CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Recursive Function for F(n) fibo(0) = 0; fibo(1) = 1 fibo(n) = fibo(n-1) + fibo(n-2), if n > 2 #include <stdio.h> int fibo(int n) { if (n < 2) return n; else return (fibo(n-1) + fibo(n-2)); } void main() int x; scanf(“%d”, &x); printf (“%d-th Fibonacci number is %d \n”, x, fibo(x)); CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Fibonacci Series Execution int f(int n) { if (n < 2) return (n); else return (f(n-1) + f(n-2)); } f(0) = 0 f(1) = 1 f(n) = f(n-1)+f(n-2),if n>1 f(4) f(3) f(2) f(1) f(2) f(0) f(1) f(1) f(0) CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Tracing Execution called 9 times How many times is the function called when evaluating f(4) ? Inefficiency: Same thing is computed several times. f(4) f(3) f(2) f(1) f(2) f(0) f(1) f(1) f(0) called 9 times CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Recursive Function for gcd(m,n) gcd(m,m) = m; gcd(m,n) = gcd(m-n, n) if m > n = gcd(m, n-m) else #include <stdio.h> int gcd(int m, int n) { if (m == n) return m; else if (m > n) return (gcd(m-n, n); else return gcd(m, n-m); } void main() int x, y; scanf(“x = %d, y = %d”, &x, &y); printf (“GCD of %d and %d is %d”, x, y, gcd(x, y)); CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

GCD Execution gcd(m,m) = m; gcd(m,n) = gcd(m-n, n) if m > n = gcd(m, n-m) else int gcd(int m, int n) { if (m == n) return m; else if(m > n) return (gcd(m-n,n); else return gcd(m, n-m); } g(10,15) g(10, 5) g(5,5) CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Performance Comparison of GCDs gcd(m,n) = m, if m = n = gcd(m-n,n) if m>n = gcd(m,n-m) else gcd(m,n) = m, if (m = n) or n = 0; = gcd(n,m) if n>m = gcd(m%n,m) int gcd(int m, int n) { if (m == n) return m; else if(m > n) return (gcd(m-n,n); else return gcd(m, n-m); } int gcd(int m, int n) { if ((m == n)|| (n == 0)) return m; else if(n > m) return (gcd(n,m); else return gcd(m, n%m); } Compare the performance of the above-mentioned two versions of gcd calculations CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Recursive Function for T(n) T(n) = n + 2 × T(n-1), if n > 0 #include <stdio.h> int T(int n) { if (n == 0) return 0; else return (n + 2*T(n-1)); } void main() int x; scanf(“%d”, &x); printf (“T(%d”)= %d”, x, T(x)); CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Towers of Hanoi Problem 1 2 3 4 5 A B C CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Towers of Hanoi Problem The problem statement Initially all the disks are stacked on the A pole. Required to transfer all the disks to the C pole. Only one disk can be moved at a time. A larger disk cannot be placed on a smaller disk. C pole is used for temporary storage of disks. CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Towers of Hanoi Problem Recursive statement of the general problem of n disks Step 1: Move the top (n-1) disks from A to B Step 2: Move the largest disk from A to C. Step 3: Move the (n-1) disks from B to C. CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Towers of Hanoi Problem #include <stdio.h> void move(int n, char A, char B, char C); int main() { int n; /* Number of disks */ scanf (“%d”, &n); move (n, ‘A’, ‘B’, ‘C’); return 0; } void move (int n, char A, char B, char C) if (n > 0) { move (n-1, A, C, B); printf (“Move disk %d from %c to %c \n”, n, A, C); move (n-1, B, C, A); return; CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Towers of Hanoi – Execution 3 Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B Move disk 3 from A to C Move disk 1 from B to A Move disk 2 from B to C 4 Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to R Move disk 3 from A to B Move disk 1 from C to L Move disk 2 from C to B Move disk 4 from A to C Move disk 1 from B to C Move disk 2 from B to A Move disk 1 from C to A Move disk 3 from B to C 5 Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B Move disk 3 from A to C Move disk 1 from B to A Move disk 2 from B to C Move disk 4 from A to B Move disk 2 from C to A Move disk 3 from C to B Move disk 5 from A to C Move disk 3 from B to A Move disk 4 from B to C How many moves are required for n disks? CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Points to be Noted Every recursive program can also be written without recursion. Recursion is used for programming convenience, not for performance enhancement. Sometimes, if the function being computed has a nice recurrence form, then a recursive code may be more readable. Performance warning! Speed: Avoid Fibonacci-style recursive programs, which result in an exponential “explosion” of calls. Memory: Recursive calls take consume additional memory. CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Recursion Versus Iteration Repetition Iteration: explicit loop Recursion: repeated function calls Termination Iteration: Loop condition fails Recursion: Base case recognized Both can have infinite loops Be careful about the termination condition Balance Choice between performance (iteration) and good software engineering (recursion). CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

How are function calls implemented? The following applies in general, with minor variations that are implementation dependent. The system maintains a stack in memory. Stack is a last-in first-out structure. Two operations on stack, push and pop. Whenever there is a function call, the activation record gets pushed into the stack. Activation record consists of the return address in the calling program, the return value from the function, and the local variables inside the function. CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Function calls implementation main() { …….. x = gcd (a, b); } int gcd (int x, int y) { …….. return (result); } Local Variables Activation record STACK Return Value Return Addr Before call After call After return CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Before call Call ncr Call fact fact returns ncr returns main() { …….. x = ncr (a, b); } int ncr (int n, int r) { return (fact(n)/ fact(r)/fact(n-r)); } int fact (int n) { ……… return (result); } 3 times 3 times LV2, RV2, RA2 LV1, RV1, RA1 LV1, RV1, RA1 LV1, RV1, RA1 Before call Call ncr Call fact fact returns ncr returns CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

What happens for recursive calls? What we have seen …. Activation record gets pushed into the stack when a function call is made. Activation record is popped off the stack when the function returns. In recursion, a function calls itself. Several function calls going on, with none of the function calls returning back. Activation records are pushed onto the stack continuously. Large stack space required. Activation records keep popping off, when the termination condition of recursion is reached. CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Example:: main() calls fact(3) { int n; n = 3; printf (“%d \n”, fact(n) ); } int fact (n) int n; { if (n = = 0) return (1); else return(n * fact(n-1)); } CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

TRACE OF THE STACK DURING EXECUTION 1 RA .. fact n = 1 n = 1 n = 1 fact returns to main main calls fact - - 1*1 = 1 RA .. fact RA .. fact RA .. fact n = 2 n = 2 n = 2 n = 2 n = 2 - - - - 2*1 = 2 RA .. fact RA .. fact RA .. fact RA .. fact RA .. fact n = 3 n = 3 n = 3 n = 3 n = 3 n = 3 n = 3 - - - - - - 3*2 = 6 RA .. main RA .. main RA .. main RA .. main RA .. main RA .. main RA .. main CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

? Trace the activation records for the following version of Fibonacci sequence. #include <stdio.h> int f (int n) { int a, b; if (n < 2) return (n); else { a = f(n-1); b = f(n-2); return (a+b); } } main() { printf(“Fib(4) is: %d \n”, f(4)); Local Variables (n, a, b) Return Value X Return Addr (either main, or X, or Y) Y main CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Any question? You may post your question(s) at the “Discussion Forum” maintained in the course Web page. CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Problems to Ponder… Add a counter to count the number of function calls occurs in each of the sample recursive programs discussed in this lecture slides. If a local variable is defined in side the body of recursive function, what will happen to the values of that variables? Formulate each of the following algebraic formulas in recursive forms. sum = a[0] + a[1] + a[2] + … + a[size], where a[size] is an array of integer with size size. y = 1−𝑥+ 𝑥 2 2! − 𝑥 3 3! + 𝑥4 4! −∙∙∙ −1 𝑛 𝑥 𝑛 𝑛1 𝑦= 𝑥 𝑛 , where x is any floating point number and n is a positive integer. Find the values of y using i) iteration and ii) recursion Find the product of n floating point numbers. The numbers should be read from the keyboard. You should not use any looping construct. [Hint: use recursion and decide a suitable sentinel for termination of recursion.] CS 10001 : Programming and Data Structures Lecture #05: © DSamanta

Problems for Practice… You can check the Moodle course management system for a set of problems for your own practice. Login to the Moodle system at http://cse.iitkgp.ac.in/ Select “PDS Spring-2017 (Theory) in the link “My Courses” Go to Topic 6: Practice Sheet #06 : Recursion in C Solutions to the problems in Practice Sheet #06 will be uploaded in due time. CS 10001 : Programming and Data Structures Lecture #01: © DSamanta

Spend few minutes and then enjoy the study. If you try to solve problems yourself, then you will learn many things automatically. Spend few minutes and then enjoy the study. CS 10001 : Programming and Data Structures Lecture #05: © DSamanta