You are on page 1of 2

Chapter I Recursion (review) mathematical puzzle.

It consists of three rods, and a


number of disks of different sizes which can slide onto
A recursive definition is one that uses the concept any rod. The puzzle starts with the disks in a neat stack
or thing that is being defined as part of the definition. in ascending order on one rod, the smallest at the top,
For example: An “ancestor” is either a parent or an thus making a conical shape. The objective of the puzzle
ancestor of a parent. A “directory” is a part of a disk is to move the entire stack from the first rod to the
drive that can hold files and directories. In mathematics, second rod (using the third as a spare rod), obeying the
a “set” is a collection of elements, which can following rules:
themselves be sets. - Only one disk may be moved at a time.
Recursive definitions can describe very complex - Each move consists of taking the upper disk
situations with just a few words. A definition of the term from one of the rods and sliding it onto another
“ancestor” without using recursion might be something rod, on top of the other disks that may already
like “a parent, or a grandparent, or a great- be on that rod.
grandparent, or a great-great-grandparent, and so on.” - No disk may be placed on top of a smaller disk.
Recursion can be used as a programming
technique. We call an algorithm recursive if it calls
itself (either directly or indirectly). A recurrence can be
direct or indirect. It is direct if it contains a reference to
itself, and it is indirect if two methods are mutually
calling each other. To say that a subroutine calls itself
directly means that its definition contains a subroutine
call statement that calls the subroutine that is being
defined. To say that a subroutine calls itself indirectly
means that it calls a second subroutine which in turn
calls the first subroutine (either directly or indirectly). A
recursive subroutine can define a complex task in just a
few lines of code.

1. General structure of recursive method Figure 1: moving 3 disks


A recursive algorithm always consists of two parts:
the base case and the recursive case. The base criterion
(or base case) decides which of them has to be executed
next. Roughly speaking, if the problem is small enough,
it is solved by the base case directly. [A base case for a
recursive algorithm is a case that is handled directly,
rather than by applying the algorithm recursively].
If the problem it is too big, it is broken up into
smaller sub-problems that have the same structure as the
original, and the algorithm is recalled for these parts.
The process obviously ends when all arising sub-
problems “melt away”.

2. Examples of recursive methods


Factorial of a number
Consider a positive integer n. n! = n(n-1)!. A
recursive algorithm that computes n! is given below:
static int factorial(int n){
if(n==0) //base case
return 1;
else
return n*factorial(n-1);
}

Towers of Hanoi
A typical example of a recursive solution is the
problem known as the Towers of Hanoi, which is a Figure 2: moving 4 disks

Recursion
1
The recursive solution of moving n disks from the - r = 19 – 5; a = 14
first rod to the second using the third as spare location - r = 14 – 5; a = 9
can be defined in three steps as demonstrated in Figure - r = 9 – 5; a = 4;
3 below. - since a=4<b, r = 4
Hence the following recursive definition:
//PRE-CONDITION a>=b
static int remainder(int a, int b){
if(a<b)
return a;
else{
a = a-b;
return remainder(a, b);
}
}

Euclid’s algorithm
Figure 3: Recursive solution Euclid’s algorithm computes the Greatest Common
Divisor (GCD) of two positive integers. It is based on
Suppose you know how to solve the problem for n-1 two very simple observations that the GCD of numbers
discs, then solving it for n discs is simple: a, b satisfies:
- Move the (n-1) top-part of the tower to stick 3. 𝑔𝑐𝑑(𝑎, 𝑏) = 𝑔𝑐𝑑(𝑎, 𝑎 + 𝑏)
- Move the n’th (largest) disc to stick 2. {
𝑔𝑐𝑑(𝑎, 𝑏) = 𝑏 𝑖𝑓 𝑏 𝑑𝑖𝑣𝑖𝑑𝑒𝑠 𝑎
- Move the (n-1) tower from stick 3 to stick 2 The recursive version of GCD algorithm is given below
- Furthermore, we know how to solve the static int gcd(int a, int b){
problem for n=1 [just by moving the disk from int c = a%b;
stick 1 to stick 2]. if(c == 0)
Here is a version of the subroutine hanoi that will print return b;
out step-by-step instructions for solving the problem. else
static void Hanoi(int disks, int from, int to, int spare){ return gcd(b, c);
if (disks = = 1) { }
/*There is only one disk to be moved.
Just move it.*/ Exercise 1
System.out.println("Move a disk 𝑛
1. Give a recursive function to compute(𝑝).
from stack number "+ from + " to 𝑛
stack number " + to); Hint: 0
= 1 ;
} 𝑛
= 1 ;
else { 𝑛
// Move all but one disk to the spare stack, (𝑛𝑝) = (𝑛−1
𝑝
)+(𝑛−1
𝑝−1
); ∀ 1≤p≤ n-1
// then move the bottom disk, then put all the
// other disks on top of it. Exercise 2
Hanoi (disks-1, from, spare, to); The Fibonacci sequence is defined by the following
System.out.println("Move a disk relation:
from stack number "+ from + " to 0 = i=0
stack number " + to); Fi = 1 = i=1
Hanoi (disks-1, spare, to, from); Fi-1+Fi-2, otherwise ∀ i≥2
}
} 1. Give an iterative function that evaluates the
You can call the subroutine as follow: Hanoi(N, 1, 2, 3), Fibonacci sequence.
where is the number of rods. [Hint: static int Fibonacci(int n)]
2. Propose a recursive version of Fibonacci
Computing the remainder of the division of a by b
It is possible to determine the remainder of the division Exercise 3
of an integer a by b through a recursive function. the Suppose we are playing a lottery game: we have 90 balls
remainder r of the division of a by b can be obtained as in a pot numbered from 1 to 90, and we draw 5 of them.
follow: r = a – b, a = r … until a < b Write a function that lists all possible draws. Modify
Suppose for example that a=29 and b=5 your pseudo-code so that it works for any n balls and
- r = 29 – 5; a = 24 any k(≤n) drawn of them. (We call this the k
- r = 24 – 5; a = 19 combination of n elements.)

Recursion
2

You might also like