CS212: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Factorial Recursion stack Binary Search Towers of Hanoi
Advertisements

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. 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)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
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 CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of 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.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion More on Project 2 Reading L&C 10.1 – 10.4.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
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 Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Program Development and Design Using C++, Third Edition
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.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Recursion.
Recursion.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion Salim Arfaoui.
Topic 6 Recursion.
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Abdulmotaleb El Saddik University of Ottawa
Chapter 15 Recursion.
More on Recursive Methods
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.
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.
Recursive Definitions
Algorithm Analysis (for Divide-and-Conquer problems)
Chapter 12 Recursion (methods calling themselves)
Chapter 14: Recursion Starting Out with C++ Early Objects
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.
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Unit 3 Test: Friday.
Chapter 11 Recursion.
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Recursion.
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
ICS103 Programming in C Lecture 11: Recursive Functions
Java Software Solutions Foundations of Program Design Sixth Edition
Recursion (part 1) October 25, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

CS212: Data Structures and Algorithms Lecture # 7 Recursion

Outline Towards Recursion Recursion Recursive Programming Rules of Recursion Exercises

Towards Recursion Factorial Function n != 1 if n == 0 Given a +ive integer n, n factorial is defined as the product of all integers between 1 and n, including n. n != 1 if n == 0 n != n*(n-1) * (n-2) * - - - - -* 1 if n > 0 Algorithm that accepts an integer n and returns the value of n! prod = 1; for(x = n; x > 0; x--) prod*= x; return(prod); This algorithm is an iterative algorithm

Towards Recursion Multiplying n by the product of all integers from n-1 to 1 yields the product of all integers from n to 1. For any n > 0, we see that n! equals n * (n-1)! n! = 1 if n == 0 n! = n(n-1)! if n > 0 Is this a recursive definition? A definition which defines an object in terms of a simpler case of itself, is called a recursive definition

Towards Recursion 1. 5! = 5 * 4! 2. - - - - - - - 4! = 4 * 3! 1. 5! = 5 * 4! 2. - - - - - - - 4! = 4 * 3! 3. - - - - - - - - - - - - - - 3! = 3 * 2! 4. - - - - - - - - - - - - - - - - - - - - - 2! = 2 * 1! 5. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1! = 1 * 0! 6. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -0! = 1 Solve from line 6 to 1. 6´- - - - - - - - - 0! = 1 5´- - - - - - - - - 1! = 1*0! = 1 * 1 = 1 4´- - - - - - - - - 2! = 2*1! = 2 * 1 = 2 3´- - - - - - - - - 3! = 3*2! = 3 * 2 = 6 2´- - - - - - - - - 4! = 4*3! = 4 * 6 = 24 1´- - - - - - - - - 5! = 5*4! = 5 *24 = 120

Towards Recursion Multiplication of Natural Number The product a * b, where a and b are positive integers, may be defined as a * b = a if b ==1 a * b = a * (b-1) + a if b > 1 e.g. 6 * 3 = 6 * 2 + 6 = 6 * 1 + 6 + 6 = 6 + 6 + 6 = 18

Towards Recursion Fibonnacci Sequence fib(0) = 0 fib(1) = 1 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . . . . . . . Each element is the sum of the two preceding elements with fib(0) = 0 fib(1) = 1 fib(n) = n if n ==0 or n ==1 fib(n) = fib(n-2) + fib(n-1) if n >= 2 e.g. Compute fib(5)

Towards Recursion Fib(5) Fib(3) Fib(1) Fib(2) Fib(0) Fib(4)

Towards Recursion All these problems have a recursive definition A definition which defines an object in terms of a simpler case of itself, is called a recursive definition

Recursion Every recursive definition has two parts, A Recursive part & A Non-Recursive part, also called as Base Case e.g. in the recursive definition of n! n! = 1 if n == 0 Base Case n! = n*(n-1) * (n-2) *…* 1 if n > 0 Recursive Part

Recursion The recursive part of the n! definition is used several times, terminating with the non-recursive part 1. 5! = 5 * 4! 2. - - - - - - - 4! = 4 * 3! 3. - - - - - - - - - - - - - - 3! = 3 * 2! 4. - - - - - - - - - - - - - - - - - - - - - 2! = 2 * 1! 5. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1! = 1 * 0! 6. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -0! = 1 Solve from line 6 to 1. 6´- - - - - - - - - 0! = 1 5´- - - - - - - - - 1! = 1*0! = 1 * 1 = 1 4´- - - - - - - - - 2! = 2*1! = 2 * 1 = 2 3´- - - - - - - - - 3! = 3*2! = 3 * 2 = 6 2´- - - - - - - - - 4! = 4*3! = 4 * 6 = 24 1´- - - - - - - - - 5! = 5*4! = 5 *24 = 120

Recursion All recursive definitions have to have a base case If they didn't, there would be no way to terminate the recursive path Such a definition would cause infinite recursion This problem is similar to an infinite loop

Recursion How can we translate recursive definition into a computer program? Recursion is a fundamental programming technique that can provide a solution to the problems that have a recursive definition

Recursive Programming A method/function which can invoke itself, if set up that way, is called a recursive method/function The code of a recursive method/function must be structured to handle both the base case and the recursive case

Recursive Programming Each call to the method sets up a new execution environment, with new parameters and local variables As always, when the method completes, control returns to the method that invoked it (which may be an earlier invocation of itself)

Recursive Programming Consider again the recursive definition of the factorial of a positive integer. n! = 1 if n == 0 Base Case n! = n*(n-1) * (n-2) * …* 1 if n > 0 Recursive Part We can write the recursive function using this definition as int factorial(int n) { if(n == 0) //Base Case return 1; else return n * factorial(n-1); //Recursion }

Recursive Programming main factorial(3) factorial(2) factorial(1) 2*factorial(1) 3 * factorial(2) return 1 return 6 1*factorial(0) factorial(0) return 2

Recursive Programming Multiplication of natural numbers: The product a * b, where a and b are positive integers, may be defined as a * b = a if b ==1 a * b = a * (b-1) + a if b > 1 int multiplication(int a, int b) { if(b == 1) return a; else if(b > 1) return multiplication(a, b-1) + a; }

Recursive Programming Fibonnacci Sequence fib(n) = n if n ==0 or n ==1 fib(n) = fib(n-2) + fib(n-1) if n >= 2 int fib(int n) { if(n == 0 || n == 1) return n; else return fib(n-2) + fib(n-1); }

Hanoi Tower Problem Statement Given N discs (of strictly decreasing size) stacked on one needle and two empty needles, it is required to stack all the discs onto a second needle in decreasing order of size The third needle may be used as temporary storage The movement of the discs is restricted by the following rules: Only one disc may be moved at a time A disc may be moved from any needle to any other At no time may a larger disc rest upon a smaller disc

Hanoi Tower Needle A Needle B Needle C (start of (interme- (completion problem) diate) of problem) Solution Steps: 1. Move N –1 discs from A to B 2. Move disc N from A to C 3. Move N –1 discs from B to C

Hanoi Tower void tower(char source, char destination, char intermediate, int n) { if(n==1) { cout<<"Move disk 1 from needle "<<source<<" to needle "<<destination<<endl; } else { tower(source, intermediate, destination, n-1); cout<<"Move disk "<<n<<" from needle "<<source<<" to needle "<<destination<<endl; tower(intermediate, destination, source, n-1);

Hanoi Tower Move disk 3 from A to C tower(‘A’, ‘C’, ‘B’, 3); //A is source, C is destination //B is intermediate 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

Rules of Recursion Base Case Making Progress There must be a way out for a recursive algorithm It must not generate an infinite sequence of calls on itself Without a non-recursive exit, no recursive function can ever be computed Making Progress For the cases that are to be solved recursively, the recursive call must always be to a case that makes progress toward a base case

Rules of Recursion Example 1- - - - - - int Bad(int N) 2- - - - - - { 3- - - - - - - - -if(N == 0) 4- - - - - - -- - - - - return 0; 5- - - - - - -- --else 6- - - - - - - - - - - - return Bad (N / 3+1) + N - 1; 7- - - - - - }

Rules of Recursion Design Rule Assume that all the recursive calls work Never duplicate work by solving the same instance of a problem in separate recursive calls Example: Fibonacci Numbers

Exercises Example void print_out(int N) { if(N < 10) cout<<N; A +ive integer to be printed Assume that only I/O routines available will take a single digit number and output it to the terminal Write a recursive algorithm to print the number digit by digit void print_out(int N) { if(N < 10) cout<<N; else print_out(N/10); cout<<(N%10); }

Exercises The problem of computing the sum of all the numbers between 1 and any positive integer N can be recursively defined as: i = 1 N N-1 N-2 = N + = N + (N-1) + = etc.

Exercises int sum(int n) { if(n==1) return n; else return n + sum(n-1); }

Exercises Ackerman's function is defined recursively on a non negative integers as follows A(m, n) = n + 1 if m = = 0 A(m, n) = A(m-1, 1) if m != 0, n = = 0 A(m, n) = A(m-1, A(m, n-1)) if m != 0, n != 0

Exercises int ackerman(int m, int n) { if(m==0) return n+1; if(m!=0&&n==0) return ackerman(m-1, 1); if(m!=0&&n!=0) return ackerman(m-1, ackerman(m,n-1)); }

Exercises The Greatest Common Divisor (GCD) of two positive integers x and y is defined as GCD(x, y) = y if (y <= x && x % y ==0) GCD(x, y) = GCD(y, x) if (x < y) GCD(x, y) = GCD(y, x % y) otherwise

Exercises int GCD(int x,int y) { if (y <= x && x % y ==0) return y; else if (x < y) return GCD(y,x); else return GCD(y, x % y); }