You are on page 1of 18

LECTURE # 03

INTRODUCTION TO
RECURSION

Instructor:
Ms.Dur-e-Shawar Agha
ROAD MAP
 Introductionto recursion
 Difference between iteration and recursion
 Example of recursion
 Tower of Hanoi
 Recursion Tree
TYPES OF ALGORITHM
RECURSION
 Any function which calls itself is called recursion.

OR

 A problem solving or programming technique in which a


method(function) can call itself in order to solve the
problem.
DIFFERENCE BETWEEN ITERATION AND
RECURSION

 Iteration uses a repetition statement and recursion uses a


selection statement.

 Iteration terminates when the loop continuation


condition fails. Recursion terminates when a base case is
reached.
PARTS OF RECURSIVE FUNCTION
 There are two main parts to recursive functions:
 General/Recursive/Inductive case: The case for which
the solution is expressed in terms of a smaller version of
itself function)
 Rules are given that allow for the construction of new
objects out of basic elements or the objects that have
already been constructed.
 There should be at least one general case otherwise no
recursion.
 Anchor/Ground/Base case: The case for which the
solution can be stated non-recursively. Here, a solid
solution is found.
EXAMPLE
 Let's take an example of recursion using the factorial for
a positive integer n factorial can be represented by:

int factorial(int n) { anchor / base case


if (n = = 0)
return 1;
else
return n * factorial (n – 1); Inductive Case
}
WORKING OF FACTORIAL

4!=4 * (3!)
  =4 * (3 * (2!))
  =4 * (3 * (2 * (1!)))
  =4 * (3 * (2 * (1 * (0!))))
  =4 * (3 * (2 * (1 * (1))))
RECURSION

Function
 fac(4) = 4 * fac (3)

 fac(3) = 4 * fac (2)

 fac(2) = 4 * fac (1)

 fac(1) = 4 * fac (0)

 fac(0) = 1

Return
 return 1 * 1 = 1

 return 2 * 1 = 2

 return 3 * 2 = 6

 return 4 * 6 = 24
PRACTICE QUESTION
 Find the output of the following recursive function
static int calc(int n) {
if (n==0)
return 0
else
return n + calc(n – 1);
}
calc (8) = ?
TOWER OF HANOI
 The tower of Hanoi (also called the tower of Brahma or
the Lucas tower) was invented by a French
mathematician Édouard Lucas in the 19th century. It is
associated with a legend of a Hindu temple where the
puzzle was supposedly used to increase the mental
discipline.
 Tower of Hanoi, is a mathematical puzzle which consists
of three towers (pegs) and more than one rings is as
depicted
TOWER OF HANOI
 These rings are of different sizes and stacked upon in an
ascending order, i.e. the smaller one sits over the larger
one. There are other variations of the puzzle where the
number of disks increase, but the tower count remains
the same.
2 DISCS OF TOWER OF HANOI
 Step 1: Move disc 1 from pole A to pole B.
 Step 2: Move disc 2 from pole A to pole C.

 Step 3: Move disc 1 from pole B to pole C.


MORE THAN 2 DISCS
 Step 1 : Move n-1 discs from pole 1 to pole 2.
 Step 2 : Move nth disk directly from pole 1 to pole 3.

 Step 3 : Move n-1 discs from pole 2 to pole 3.

 Base Case: When n=1


Move the disc from start to end pole.
 Recursive Case: When n > 1
3 DISCS TOWER OF HANOI
RECURSION TREES
 A recursion tree is useful for visualizing what happens
when a recurrence is iterated. It diagrams the tree of
recursive calls and the amount of work done at each call.
RECURSION TREES
 A recursion tree is useful for visualizing what happens
when a recurrence is iterated. It diagrams the tree of
recursive calls and the amount of work done at each call.
SUMMARY
 Recursion and its example

 Tower of Hanoi

 Recursion Tree

You might also like