You are on page 1of 54

Chapter 4: Recursion

• Subprogram implementation
• Recursion
• Designing recursive algorithms
• Recursion removal
• Backtracking

1
Subprogram implementation
function FOO(X: real; Y: integer): real;
var A: array [1..10] of real;
N: integer;
begin

N := Y + 1;
X := A[N] ∗ 2;

end;

2
Subprogram implementation
• Code segment (static part)
• Activation record (dynamic part):
– Parameters
– Function results
– Local variables

3
Subprogram implementation
Return point
Prologue and system
FOO data
X
Statement
Y
executabl
e codes A

Epilogue N

Code segment Activation


record
4
Subprogram implementation

Code segment

1st 2nd N-th


call call call

Activation Activation Activation


record 1 record 2 record N

5
Recursion
An object
contains itself

6
Recursion
• A definition contains itself:
– Sibling(X, Y): X and Y have the same parents
– Cousin(X, Y): X’s and Y’s parents are siblings OR
cousins

7
Recursion
• Recursive algorithm is a repetitive
process that contains (call) itself:
– Direct: A → A
– Indirect: A → B → A

8
Recursion
• Does human thinking involve
recursion?

9
Factorial
Iterative algorithm

1 if n =
0
Factorial(n) =
n × (n − 1) × (n − 2) ×...× 3 × 2 × 1 if n
>0
Recursive algorithm

1 if n =
0
Factorial(n) =
n × (Factorial(n − 1)) if n >
10
0
Iterative Solution
Algorithm iterativeFactorial (val n <integer>)
Calculates the factorial of a number using a loop
Pre n is the number to be raised factorially
Return n! is returned
1 i=1
2 factN = 1
3 loop (i <= n)
1 factN = factN + 1
2 i=i+1
4 return factN
End iterativeFactorial
11
Recursive Solution
Algorithm recursiveFactorial (val n <integer>)
Calculates the factorial of a number using recursion
Pre n is the number to be raised factorially
Return n! is returned
1 if (n = 0)
1 factN = 1
2 else
1 factN = n × recursiveFactorial(n − 1)
3 return factN
End recursiveFactorial

12
Recursive Solution

Factorial(3) = 3 × Factorial(2) Factorial(3) = 3 ×2=6

Factorial(2) = 2 × Factorial(1) Factorial(2) = 2 ×1=2

Factorial(1) = 1 × Factorial(0) Factorial(1) = 1 ×1=1

Factorial(0) = 1

13
Recursive Solution
code
Algorithm recursiveFactorial (val n <integer>) segment

1 if (n = 0)
return recursiveFactorial()
1 factN = 1
2 else addres
1 factN = n × recursiveFactorial(n − s
1)
3 return factN
End recursiveFactorial activation
record
return address

factN

14
Recursive Solution

1 1

2 2 2

n 3 3 3 3
factN

stack 15
s
Recursive Solution

0
1

1 1
1

2 2 2
2

n 3 3 3 3
factN 6

stack 16
s
Designing Recursive
Algorithms
Recursive algorithm = recursive case + stopping
case

n × factorial(n − 1) factorial(0)

17
Designing Recursive
Algorithms
• Every recursive call must solve a part
of the problem or reduce the size of
the problem.

18
Designing Recursive
Algorithms
• Determine the recursive case
• Determine the stopping case
• Combine the recursive and stopping
cases

19
Designing Recursive
Algorithms
• Is the algorithm or data structures
naturally suited to recursion?
• Is the recursive solution shorter and
more understandable?
• Does the recursive solution run in
acceptable time and space limits?

20
Print List in Reverse

6 10 14 20

20 14 10
6

21
Print List in Reverse

6 10 14 20

20 14 10
6

22
Print List in Reverse
Algorithm printReverse (val list <pointer>)
Prints singly linked list in reverse
Pre list has been built
Post list printed in reverse
1 if (null list) stopping case
1 return
2 printReverse (list −> next) recursive case
3 print (list −> data)
End printReverse

23
Print List in Reverse
• Is the algorithm or data structures
naturally suited to recursion?
• Is the recursive solution shorter and
more understandable?
• Does the recursive solution run in
acceptable time and space limits?

24
Fibonacci Numbers
0 1 1 2 3 5 8 13 21 34

• Recursive case: Fib(n) = Fib(n − 1) + Fib(n


− 2)

• Stopping case: Fib(0) = 0 Fib(1) = 1

25
Fibonacci Numbers
Fib(n
)

Fib(n- Fib(n-
1) 2)

Fib(n- Fib(n- Fib(n- Fib(n-


2) 3) 3) 4)

Fib(n- Fib(n-
3) 4)

26
Fibonacci Numbers
Fib(4 5
)

Fib(3) 3 Fib(2) 2

Fib(2) 2 Fib(1) 1 Fib(1) Fib(0)


1 0

Fib(1) Fib(0)
1 0

27
Fibonacci Numbers

Algorithm fib (val num <integer>)


Calculates the nth Fibonacci number
Pre num is the ordinal of the Fibonacci number
Post returns the nth Fibonacci number
1 if (num = 0 OR num = 1) stopping case
1 return num
2 return (fib(n - 1) + fib(n - 2)) recursive case
End fib

28
Fibonacci Numbers
No Calls Time No Calls Time
1 1 < 1 sec. 11 287 < 1 sec.
2 3 < 1 sec. 12 465 < 1 sec.
3 5 < 1 sec. 13 753 < 1 sec.
4 9 < 1 sec. 14 1,219 < 1 sec.
5 15 < 1 sec. 15 1,973 < 1 sec.
6 25 < 1 sec. 20 21,891 < 1 sec.
7 41 < 1 sec. 25 242,785 1 sec.
8 67 < 1 sec. 30 2,692,573 7 sec.
9 109 < 1 sec. 35 29,860,703 1 min.
10 177 < 1 sec. 40 331,160,28 < 13
1 min.
29
The Towers of Ha Noi
Move disks from Source to Destination using
Auxiliary:
1. Only one disk could be moved at a time.
2. A larger disk must never be stacked above a smaller
one.
3. Only one auxiliary needle could be used for the
intermediate storage of disks.

Source Auxiliar Destinati


y on 30
The Towers of Ha Noi

Source Auxiliary Destination Source Auxiliary Destination

Source Auxiliary Destination Source Auxiliary Destination

31
The Towers of Ha Noi
move(n, A, C, B)

A B C A B C

move(n-1, A, B, move(1, A, C, move(n-1, B, C,


C) B) A)

A B C A B C

32
The Towers of Ha Noi
move(3, A, C, B)

move(1, A, C,
B)
move(2, A, B, A→ move(2, B, C,
C) A)
C
move(1, A, B, move(1, B, C,
move(1, A, C, C) move(1, C, B, move(1, B, A, A) move(2, C, B,
B)
A→ A) C)
B→ A)
A→ B C→ B→ C C→
C B A B

A B C
33
The Towers of Ha Noi
• Complexity:
T(n) = 2×T(n – 1) + 1

⇒ T(n) = O(2n)

34
The Towers Algorithm

Algorithm towers (val disks <integer>, val source


<character>,
val dest <character>, val auxiliary
<character>,
ref step <integer>)
Move disks from source to destination
Pre disks is the number of disks to be moved
Post steps for moves printed

35
The Towers Algorithm
1 print("Towers: ", disks, source, dest, auxiliary)
2 if (disks = 1)
1 print ("Step ", step, "Move from", source, "to", dest)
2 step = step + 1
3 else
1 towers (disks - 1, source, auxiliary, dest, step)
2 towers (1, source, dest, auxiliary, step)
3 towers (disks - 1, auxiliary, dest, source, step)
4 return
End towers

36
Recursion Removal
• Recursion can be removed using
stacks and iteration.

37
Recursion Removal

Algorithm P (val n <integer>)


1 if (n = 0)
1 print ("Stop")
2 else
1 Q(n)
2 P(n - 1)
3 R(n)
End P

38
Recursion Removal
Algorithm P (val n <integer>) Algorithm P (val n
<integer>)

1 if (n = 0) 1 createStack (s)
1 print ("Stop") 2 loop (n > 0)
2 else 1 Q(n)
1 Q(n) 2 push(s, n)
2 P(n - 1) 3 n=n-1
3 R(n) 3 print ("Stop")
End P 4 loop (not emptyStack
(s))
1 popStack(s, n)
2 R(n)
4
39
End P
Tail Recursion
• Recursive call is the last statement.

40
Tail Recursion
Algorithm P (val n <integer>)
1 if (n = 0)
1 print("Stop")
2 else
1 Q(n)
2 P(n - 1)
End P

41
Tail Recursion
Algorithm P (val n <integer>) Algorithm P (val n
<integer>)
1 if (n = 0)
1 print("Stop") 1 loop (n > 0)
2 else 1 Q(n)
1 Q(n) 2 n=n-1
2 P(n - 1) 2 print("Stop")
End P End P

42
Backtracking
• A process to go back to previous steps
to try unexplored alternatives.

43
Goal Seeking

6 7

4 5 8
start
1 2 3 9 10 11
goal
12 13 14 15 16

17 18

44
Eight Queens Problem
Place eight queens on the chess board in
such a way that no queen can capture
another.
Q
Q Q
Q
Q
45
Eight Queens Problem
1 2 3 4 1 2 3 4 1 2 3 1 2 3 4
4
1 Q 1 Q 1 Q 1 Q
2 2 Q 2 Q 2 Q
3 3 3 3 Q
4 4 4 4

1 2 3 4 1 2 3 4 1 2 3 1 2 3 4
4
1 Q 1 Q 1 Q 1 Q
2 2 Q 2 Q 2 Q
3 3 3 Q 3 Q
4 4 4 4 Q

46
Eight Queens Problem

Algorithm putQueen (ref board <array>, val r <integer>)


Place remaining queens safely from a row of a chess board
Pre board is 8×8 array representing a chess board
r is the row to place queens onwards
Post all the remaining queens are safely placed on the
board; or backtracking to the previous
rows is required

47
Eight Queens Problem
1 for every column c on the same row r
1 if (column c is safe)
1 place the next queen in column c
2 if (r < 8)
1 putQueen (board, r + 1)
3 else
1 output successful placement
4 remove the queen from column c
2 return
End putQueen

48
Eight Queens Problem
1 for every column c on the same row r
1 if (column c is safe)
1 place the next queen in column c board[r][c] = 1
2 if (r < 8)
1 putQueen (board, r + 1)
3 else
1 output successful placement
4 remove the queen from column c
2 return
End putQueen

49
Eight Queens Problem
1 for every column c on the same row r
1 if (column c is safe)
1 place the next queen in column c
2 if (r < 8)
1 putQueen (board, r + 1)
3 else
1 output successful placement
4 remove the queen from column c
2 return
End putQueen

50
Eight Queens Problem
1 for every column c on the same row r
1 if (column c is safe)
1 place the next queen in column c board[r][c] = 1
2 if (r < 8) usedCol[c] = 1
1 putQueen (board, r + 1) usedDR[r+c] = 1
3 else usedDL[r−c] = 1
1 output successful placement
4 remove the queen from column c
2 return
End putQueen

51
Eight Queens Problem
1 for every column c on the same row r
1 if (column c is safe) usedCol[c] is 0
1 place the next queen in column c usedDR[r+c] is 0
2 if (r < 8) usedDL[r−c] is 0
1 putQueen (board, r + 1)
3 else
1 output successful placement
4 remove the queen from column c
2 return
End putQueen

52
Eight Queens Problem
1 for every column c on the same row r
1 if (column c is safe)
1 place the next queen in column c
2 if (r < 8)
1 putQueen (board, r + 1)
3 else
1 output successful placement
4 remove the queen from column c board[r][c] = 0
2 return usedCol[c] = 0
End putQueen usedDR[r+c] = 0
usedDL[r−c] = 0

53
Eight Queens Problem
1 for every column c on the same row r
1 if (column c is safe)
1 place the next queen in column c Top-down
2 if (r < 8)
1 putQueen (board, r + 1)
+
3 else Stepwise
1 output successful placement
Refinement
4 remove the queen from column c
2 return
End putQueen

54

You might also like