Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Chapter 1 RECURSION. 2 Chapter 1  Subprogram implementation  Recursion  Designing Recursive Algorithms  Towers of Hanoi  Backtracking  Eight Queens."— Presentation transcript:

1 1 Chapter 1 RECURSION

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

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

4 Function implementation 4

5 #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

6 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

7 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

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; } 8

9 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

10 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

11 Function implementation 11

12 Function implementation 12

13 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

14 Recursion An object contains itself 14

15 Recursion 15

16 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

17 Recursion 17

18 Recursion 18

19 Recursion 19

20 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 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! = 5040 8! = 40320 9! = 362880 10! = 3628800

22 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 A4 1 1 1 0 3 A0 2 A4 1 1 3 A0 2 2 A4 6 3 A0

23 23

24 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 25

26 Recursion - Print List 26

27 Recursion - Print List 27 pTemp = pHead;

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

29 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

30 Recursion - Print List 30 AddrValue 8006 804810 10 814820 14 824830 20 8340 AddrValue 100800pHead Create List Print(pHead)

31 31 AddrValue 8006 804810 10 814820 14 824830 20 8340 AddrValue 100800pHead 200800head 204A0retAdd 6 output

32 32 AddrValue 8006 804810 10 814820 14 824830 20 8340 AddrValue 100800pHead 200800head 204A0retAdd 220810head 224A4retAdd 6 10 output

33 33 AddrValue 8006 804810 10 814820 14 824830 20 8340 AddrValue 100800pHead 200800head 204A0retAdd 220810head 224A4retAdd 230820head 234A4retAdd 6 10 14 output

34 34 AddrValue 8006 804810 10 814820 14 824830 20 8340 AddrValue 100800pHead 200800head 204A0retAdd 220810head 224A4retAdd 230820head 234A4retAdd 240830head 244A4retAdd 6 10 14 20 output

35 35 AddrValue 8006 804810 10 814820 14 824830 20 8340 AddrValue 100800pHead 200800head 204A0retAdd 220810head 224A4retAdd 230820head 234A4retAdd 240830head 244A4retAdd 2500head 254A4retAdd 6 10 14 20 output

36 36 AddrValue 8006 804810 10 814820 14 824830 20 8340 AddrValue 100800pHead 200800head 204A0retAdd 220810head 224A4retAdd 230820head 234A4retAdd 240830head 244A4retAdd 6 10 14 20 output

37 37 AddrValue 8006 804810 10 814820 14 824830 20 8340 AddrValue 100800pHead 200800head 204A0retAdd 220810head 224A4retAdd 230820head 234A4retAdd 6 10 14 20 output

38 38 AddrValue 8006 804810 10 814820 14 824830 20 8340 AddrValue 100800pHead 200800head 204A0retAdd 220810head 224A4retAdd 6 10 14 20 output

39 39 AddrValue 8006 804810 10 814820 14 824830 20 8340 AddrValue 100800pHead 200800head 204A0retAdd 6 10 14 20 output

40 40 AddrValue 8006 804810 10 814820 14 824830 20 8340 AddrValue 100800pHead 6 10 14 20 output

41 Recursion - Print List 41

42 Recursion - Print List 42

43 Designing Recursive Algorithms 43

44 Designing Recursive Algorithms 44

45 Designing Recursive Algorithms 45

46 Designing Recursive Algorithms 46

47 Designing Recursive Algorithms 47

48 Designing Recursive Algorithms 48

49 49

50 Designing Recursive Algorithms 50

51 51

52 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

53 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

54 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

55 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

56 Towers of Hanoi 56

57 Towers of Hanoi 57

58 Towers of Hanoi 58

59 Towers of Hanoi 59

60 Towers of Hanoi 60

61 Towers of Hanoi 61

62 Towers of Hanoi 62

63 Backtracking 63

64 Backtracking 64

65 Backtracking – Eight Queens problem 65

66 Backtracking – Eight Queens problem 66

67 Backtracking – Eight Queens problem 67

68 Backtracking – Eight Queens problem 68

69 Backtracking – Eight Queens problem 69

70 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

71 Backtracking – Eight Queens problem 71

72 Backtracking – Eight Queens problem 72

73 Backtracking – Eight Queens problem 73

74 Backtracking – Eight Queens problem 74

75 Backtracking – Eight Queens problem 75

76 Backtracking – Eight Queens problem 76

77 Backtracking – Eight Queens problem 77

78 Backtracking – Eight Queens problem 78


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

Similar presentations


Ads by Google