1 Chapter 1 RECURSION. 2 Chapter 1  Subprogram implementation  Recursion  Designing Recursive Algorithms  Towers of Hanoi  Backtracking  Eight Queens.

Slides:



Advertisements
Similar presentations
C++ Programming:. Program Design Including
Advertisements

Recursion COL 106.
Recursion.
Programming 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.
Recursion COMP171 Fall Recursion / Slide 2 Recursion * In some problems, it may be natural to define the problem in terms of the problem itself.
Programming with Recursion
Recursion Gordon College CPS212
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
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.
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
Simple Recursion. COMP104 Lecture 35 / Slide 2 Recursion: Example 0 * What does the following program do? #include using namespace std; int fac(int n){
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Data Structures Using C++ 2E Chapter 6 Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
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.
Recursion Chapter 5.
1 CS 132 Spring 2008 Chapter 6 Recursion Read p Skip example 6-3 (Fibonacci), 6-4 (Hanoi) Read example 6-5 (p. 387)
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
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 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Recursion. Circular Definition Circular definition Circular definition Dialectic materialism is materialism that is dialectic. Dialectic materialism is.
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.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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.
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 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Chapter 6 Questions Quick Quiz
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
Function Recursion to understand recursion you must understand recursion.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
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.
Programming with Recursion
CS212: Data Structures and Algorithms
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 19: Recursion.
Chapter 15 Recursion.
Recursion DRILL: Please take out your notes on Recursion
Chapter 15 Recursion.
Recursion and Logarithms
Andy Wang Object Oriented Programming in C++ COP 3330
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Programming with Recursion
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Chapter 11.
CSC 143 Recursion.
Chapter 18 Recursion.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Presentation transcript:

1 Chapter 1 RECURSION

2 Chapter 1  Subprogram implementation  Recursion  Designing Recursive Algorithms  Towers of Hanoi  Backtracking  Eight Queens problem

Function implementation Code segment (static part) Activation record (dynamic part)  Parameters  Function result  Local variables  Return address 3

Function implementation 4

#include int maximum( int x, int y, int z) { int max = x; if ( y > max ) max = y; if ( z > max ) max = z; return max; } int main() { int a, b, c, d1, d2; A1 cout << "Enter three integers: "; A2 cin >> a >> b >> c; A3 d1 = maximum (a, b, c); A4 cout << "Maximum is: " << d1 << endl; A5 d2 = maximum (7, 9, 8); A5 cout << "Maximum is: " << d2 << endl; A7 return 0; } 5

Function implementation A4 Return Address Return value 7 9 x y 8 z max d1 = maximum (a, b, c); // a = 7; b = 9; c = 8 int maximum( int x, int y, int z) { int max = x; if ( y > max ) max = y; if ( z > max ) max = z; return max; } 6

Function implementation A4 Return Address Return value 7 9 x y 8 z 7 max d1 = maximum (a, b, c); // a = 7; b = 9; c = 8 int maximum( int x, int y, int z) { int max = x; if ( y > max ) max = y; if ( z > max ) max = z; return max; } 7

Function implementation A4 Return Address Return value 7 9 x y 8 z 9 max d1 = maximum (a, b, c); // a = 7; b = 9; c = 8 int maximum( int x, int y, int z) { int max = x; if ( y > max ) max = y; if ( z > max ) max = z; return max; } 8

Function implementation A4 Return Address Return value 7 9 x y 8 z 9 max d1 = maximum (a, b, c); // a = 7; b = 9; c = 8 int maximum( int x, int y, int z) { int max = x; if ( y > max ) max = y; if ( z > max ) max = z; return max; } 9

Function implementation A4 9 Return Address Return value 7 9 x y 8 z 9 max d1 = maximum (a, b, c); // a = 7; b = 9; c = 8 int maximum( int x, int y, int z) { int max = x; if ( y > max ) max = y; if ( z > max ) max = z; return max; } 10

Function implementation 11

Function implementation 12

Function implementation Stack frames:  Each vertical column shows the contents of the stack at a given time  There is no difference between two cases: when the temporary storage areas pushed on the stack come from different functions, and when the temporary storage areas pushed on the stack come from repeated occurrences of the same function. 13

Recursion An object contains itself 14

Recursion 15

Recursion Recursion is the name for the case when:  A function invokes itself, or  A function invokes a sequence of other functions, one of which eventually invokes the first function again. In regard to stack frames for function calls, recursion is no different from any other function call.  Stack frames illustrate the storage requirements for recursion.  Separate copies of the variables declared in the function are created for each recursive call. 16

Recursion 17

Recursion 18

Recursion 19

20 Recursion In C++, it’s possible for a function to call itself. Functions that do so are called seft-referential or recursive functions. In some problems, it may be natural to define the problem in terms of the problem itself. Recursion is useful for problems that can be represented by a simpler version of the same problem. Example: Factorial 1! = 1; 2! = 2*1 = 2*1! 3! = 3*2*1=3*2! …. n! = n*(n-1)! The factorial function is only defined for positive integers. n!=1 if n is equal to 1 n!=n*(n-1)! if n >1

21 Example : #include unsigned long Factorial( unsigned long ); int main(){ for ( int i = 0; i <= 10; i++ ) cout << setw( 2 ) << i << "! = " << Factorial( i ) << endl; return 0; } // Recursive definition of function factorial unsigned long Factorial( unsigned long number ){ if (number < 1) // base case return 1; else // recursive case return number * Factorial( number - 1 ); } The output : 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = ! = ! = ! =

Factorial(3) unsigned long Factorial( unsigned long number ){ A1 if (number < 1) // base case A2 return 1; A3 else // recursive case A4 return number * Factorial( number - 1 ); A5} 22 3 A0 3 2 A4 3 A0 2 A4 1 3 A0 2 A A0 2 A A0 2 2 A4 6 3 A0

23

Recursion We must always make sure that the recursion bottoms out:  A recursive function must contain at least one non- recursive branch.  The recursive calls must eventually lead to a non- recursive branch. 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)! and 1! = 1 24

25

Recursion - Print List 26

Recursion - Print List 27 pTemp = pHead;

Recursion - Print List A list is  empty, or  consists of an element and a sublist, where sublistis a list. 28 pHead

Recursion - Print List Algorithm Print(val head ) Prints Singly Linked List. Pre headpoints to the first element of the list needs to be printed. Post Elements in the list have been printed. Uses recursive function Print. A1.if(head= NULL) // stopping case A1.1.return A2.write (head->data) A3.Print(head->link) // recursive case A4.End Print 29

Recursion - Print List 30 AddrValue AddrValue pHead Create List Print(pHead)

31 AddrValue AddrValue pHead head 204A0retAdd 6 output

32 AddrValue AddrValue pHead head 204A0retAdd head 224A4retAdd 6 10 output

33 AddrValue AddrValue pHead head 204A0retAdd head 224A4retAdd head 234A4retAdd output

34 AddrValue AddrValue pHead head 204A0retAdd head 224A4retAdd head 234A4retAdd head 244A4retAdd output

35 AddrValue AddrValue pHead head 204A0retAdd head 224A4retAdd head 234A4retAdd head 244A4retAdd 2500head 254A4retAdd output

36 AddrValue AddrValue pHead head 204A0retAdd head 224A4retAdd head 234A4retAdd head 244A4retAdd output

37 AddrValue AddrValue pHead head 204A0retAdd head 224A4retAdd head 234A4retAdd output

38 AddrValue AddrValue pHead head 204A0retAdd head 224A4retAdd output

39 AddrValue AddrValue pHead head 204A0retAdd output

40 AddrValue AddrValue pHead output

Recursion - Print List 41

Recursion - Print List 42

Designing Recursive Algorithms 43

Designing Recursive Algorithms 44

Designing Recursive Algorithms 45

Designing Recursive Algorithms 46

Designing Recursive Algorithms 47

Designing Recursive Algorithms 48

49

Designing Recursive Algorithms 50

51

Designing Recursive Algorithms Price for recursion:  calling a function consumes more time and memory than adjusting a loop counter.  high performance applications (graphic action games, simulations of nuclear explosions) hardly ever use recursion. In less demanding applications recursion is an attractive alternative for iteration (for the right problems!) 52

Designing Recursive Algorithms If we use iteration, we must be careful not to create an infinite loop by accident: for(int incr=1; incr!=10;incr+=2)... int result = 1; while(result >0){... result++; } Oops! Oops! 53

Designing Recursive Algorithms Similarly, if we use recursion we must be careful not to create an infinite chain of function calls: int fac(int numb){ return numb * fac(numb-1); } Or: int fac(int numb){ if (numb<=1) return 1; else return numb * fac(numb+1); } Oops! No termination condition Oops! 54

Towers of Hanoi  Only one disc could be moved at a time  A larger disc must never be stacked above a smaller one  One and only one extra needle could be used for intermediate storage of discs 55

Towers of Hanoi 56

Towers of Hanoi 57

Towers of Hanoi 58

Towers of Hanoi 59

Towers of Hanoi 60

Towers of Hanoi 61

Towers of Hanoi 62

Backtracking 63

Backtracking 64

Backtracking – Eight Queens problem 65

Backtracking – Eight Queens problem 66

Backtracking – Eight Queens problem 67

Backtracking – Eight Queens problem 68

Backtracking – Eight Queens problem 69

Backtracking – Eight Queens problem bool Queens::unguarded(int col) const { int i; bool ok = true; for (i = 0; ok && i < count; i++) ok = !queen_square[i][col]; for (i = 1; ok && count - i >= 0 && col - i >= 0; i++) ok = !queen_square[count - i][col - i]; for (i = 1; ok && count - i >= 0 && col + i < board_size; i++) ok = !queen_square[count - i][col + i]; return ok; } 70

Backtracking – Eight Queens problem 71

Backtracking – Eight Queens problem 72

Backtracking – Eight Queens problem 73

Backtracking – Eight Queens problem 74

Backtracking – Eight Queens problem 75

Backtracking – Eight Queens problem 76

Backtracking – Eight Queens problem 77

Backtracking – Eight Queens problem 78