1 chapter 1 recursion. 2 chapter 1 subprogram implementation recursion designing recursive...
TRANSCRIPT
![Page 1: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/1.jpg)
1
Chapter 1
RECURSION
![Page 2: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/2.jpg)
2
Chapter 1
Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens problem
![Page 3: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/3.jpg)
Function implementation
Code segment (static part) Activation record (dynamic part)
Parameters Function result Local variables Return address
3
![Page 4: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/4.jpg)
Function implementation
4
![Page 5: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/5.jpg)
Function implementation#include <iostream.h>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
![Page 6: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/6.jpg)
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
![Page 7: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/7.jpg)
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
![Page 8: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/8.jpg)
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
![Page 9: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/9.jpg)
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
![Page 10: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/10.jpg)
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
![Page 11: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/11.jpg)
Function implementation
11
![Page 12: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/12.jpg)
Function implementation
12
![Page 13: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/13.jpg)
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
![Page 14: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/14.jpg)
Recursion
An object contains itself
14
![Page 15: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/15.jpg)
Recursion
15
![Page 16: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/16.jpg)
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
![Page 17: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/17.jpg)
Recursion
17
![Page 18: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/18.jpg)
Recursion
18
![Page 19: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/19.jpg)
Recursion
19
![Page 20: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/20.jpg)
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: Factorial1! = 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 1n!=n*(n-1)! if n >1
![Page 21: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/21.jpg)
21
Example :#include <iostream.h>
#include <iomanip.h>
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 factorialunsigned 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! = 36288010! = 3628800
![Page 22: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/22.jpg)
Factorial(3)unsigned long Factorial( unsigned long number ){A1 if (number < 1) // base caseA2 return 1;A3 else // recursive caseA4 return number * Factorial( number - 1 );A5}
22
3A0
3A0
2A4
3A0
2A4
1A4
3A0
2A411
A410
A4
3A0
2A411
A4
3A022
A4
63
A0
![Page 23: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/23.jpg)
23
![Page 24: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/24.jpg)
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
![Page 25: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/25.jpg)
25
![Page 26: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/26.jpg)
Recursion - Print List
26
![Page 27: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/27.jpg)
Recursion - Print List
27
pTemp = pHead;
![Page 28: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/28.jpg)
Recursion - Print List A list is
empty, or consists of an element and a sublist, where
sublistis a list.
28
pHeadpHead
![Page 29: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/29.jpg)
Recursion - Print ListAlgorithm Print(val head<pointer>)
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
![Page 30: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/30.jpg)
Recursion - Print List
30
Addr Value
800 6
804 810
810 10
814 820
820 14
824 830
830 20
834 0
Addr Value
100 800 pHead
Create List
Print(pHead)
![Page 31: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/31.jpg)
31
Addr Value
800 6
804 810
810 10
814 820
820 14
824 830
830 20
834 0
Addr Value
100 800 pHead
200 800 head
204 A0 retAdd
6
output
![Page 32: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/32.jpg)
32
Addr Value
800 6
804 810
810 10
814 820
820 14
824 830
830 20
834 0
Addr Value
100 800 pHead
200 800 head
204 A0 retAdd
220 810 head
224 A4 retAdd
6 10
output
![Page 33: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/33.jpg)
33
Addr Value
800 6
804 810
810 10
814 820
820 14
824 830
830 20
834 0
Addr Value
100 800 pHead
200 800 head
204 A0 retAdd
220 810 head
224 A4 retAdd
230 820 head
234 A4 retAdd
6 10 14
output
![Page 34: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/34.jpg)
34
Addr Value
800 6
804 810
810 10
814 820
820 14
824 830
830 20
834 0
Addr Value
100 800 pHead
200 800 head
204 A0 retAdd
220 810 head
224 A4 retAdd
230 820 head
234 A4 retAdd
240 830 head
244 A4 retAdd
6 10 14 20
output
![Page 35: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/35.jpg)
35
Addr Value
800 6
804 810
810 10
814 820
820 14
824 830
830 20
834 0
Addr Value
100 800 pHead
200 800 head
204 A0 retAdd
220 810 head
224 A4 retAdd
230 820 head
234 A4 retAdd
240 830 head
244 A4 retAdd
250 0 head
254 A4 retAdd
6 10 14 20
output
![Page 36: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/36.jpg)
36
Addr Value
800 6
804 810
810 10
814 820
820 14
824 830
830 20
834 0
Addr Value
100 800 pHead
200 800 head
204 A0 retAdd
220 810 head
224 A4 retAdd
230 820 head
234 A4 retAdd
240 830 head
244 A4 retAdd
6 10 14 20
output
![Page 37: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/37.jpg)
37
Addr Value
800 6
804 810
810 10
814 820
820 14
824 830
830 20
834 0
Addr Value
100 800 pHead
200 800 head
204 A0 retAdd
220 810 head
224 A4 retAdd
230 820 head
234 A4 retAdd
6 10 14 20
output
![Page 38: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/38.jpg)
38
Addr Value
800 6
804 810
810 10
814 820
820 14
824 830
830 20
834 0
Addr Value
100 800 pHead
200 800 head
204 A0 retAdd
220 810 head
224 A4 retAdd
6 10 14 20
output
![Page 39: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/39.jpg)
39
Addr Value
800 6
804 810
810 10
814 820
820 14
824 830
830 20
834 0
Addr Value
100 800 pHead
200 800 head
204 A0 retAdd
6 10 14 20
output
![Page 40: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/40.jpg)
40
Addr Value
800 6
804 810
810 10
814 820
820 14
824 830
830 20
834 0
Addr Value
100 800 pHead6 10 14 20
output
![Page 41: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/41.jpg)
Recursion - Print List
41
![Page 42: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/42.jpg)
Recursion - Print List
42
![Page 43: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/43.jpg)
Designing Recursive Algorithms
43
![Page 44: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/44.jpg)
Designing Recursive Algorithms
44
![Page 45: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/45.jpg)
Designing Recursive Algorithms
45
![Page 46: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/46.jpg)
Designing Recursive Algorithms
46
![Page 47: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/47.jpg)
Designing Recursive Algorithms
47
![Page 48: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/48.jpg)
Designing Recursive Algorithms
48
![Page 49: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/49.jpg)
49
![Page 50: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/50.jpg)
Designing Recursive Algorithms
50
![Page 51: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/51.jpg)
51
![Page 52: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/52.jpg)
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
![Page 53: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/53.jpg)
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!
Oops!Oops!
53
![Page 54: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/54.jpg)
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!Oops!No termination No termination
conditioncondition
Oops!Oops!
54
![Page 55: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/55.jpg)
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
![Page 56: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/56.jpg)
Towers of Hanoi
56
![Page 57: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/57.jpg)
Towers of Hanoi
57
![Page 58: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/58.jpg)
Towers of Hanoi
58
![Page 59: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/59.jpg)
Towers of Hanoi
59
![Page 60: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/60.jpg)
Towers of Hanoi
60
![Page 61: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/61.jpg)
Towers of Hanoi
61
![Page 62: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/62.jpg)
Towers of Hanoi
62
![Page 63: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/63.jpg)
Backtracking
63
![Page 64: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/64.jpg)
Backtracking
64
![Page 65: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/65.jpg)
Backtracking – Eight Queens problem
65
![Page 66: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/66.jpg)
Backtracking – Eight Queens problem
66
![Page 67: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/67.jpg)
Backtracking – Eight Queens problem
67
![Page 68: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/68.jpg)
Backtracking – Eight Queens problem
68
![Page 69: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/69.jpg)
Backtracking – Eight Queens problem
69
![Page 70: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/70.jpg)
Backtracking – Eight Queens problembool 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
![Page 71: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/71.jpg)
Backtracking – Eight Queens problem
71
![Page 72: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/72.jpg)
Backtracking – Eight Queens problem
72
![Page 73: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/73.jpg)
Backtracking – Eight Queens problem
73
![Page 74: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/74.jpg)
Backtracking – Eight Queens problem
74
![Page 75: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/75.jpg)
Backtracking – Eight Queens problem
75
![Page 76: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/76.jpg)
Backtracking – Eight Queens problem
76
![Page 77: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/77.jpg)
Backtracking – Eight Queens problem
77
![Page 78: 1 Chapter 1 RECURSION. 2 Chapter 1 Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649e535503460f94b4924a/html5/thumbnails/78.jpg)
Backtracking – Eight Queens problem
78