0% found this document useful (0 votes)
64 views42 pages

Recursion: Compiled By: Dr. Mohammad Omar Alhawarat

This document discusses recursion through examples like lists, factorials, Fibonacci sequences, and the Towers of Hanoi problem. It defines recursion as a problem-solving process that breaks a problem into smaller identical problems until reaching a base case. It also discusses direct and indirect recursion, the phases of recursion including forward and backward phases, and the importance of having a base case to avoid infinite recursion.

Uploaded by

Abo Fawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views42 pages

Recursion: Compiled By: Dr. Mohammad Omar Alhawarat

This document discusses recursion through examples like lists, factorials, Fibonacci sequences, and the Towers of Hanoi problem. It defines recursion as a problem-solving process that breaks a problem into smaller identical problems until reaching a base case. It also discusses direct and indirect recursion, the phases of recursion including forward and backward phases, and the importance of having a base case to avoid infinite recursion.

Uploaded by

Abo Fawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

CHAPTER 02

Recursion Compiled by: Dr. Mohammad Omar Alhawarat


Recursive Definitions
2

 Consider the following list of numbers:


24, 88, 40, 37
 Such a list can be defined recursively:
A LIST is a: number
or a: number comma LIST
 That is, a LIST can be a number, or a number
followed by a comma followed by a LIST
 The concept of a LIST is used to define itself
Tracing the recursive definition of a list
3

LIST: number comma LIST


24 , 88, 40, 37

number comma LIST


88 , 40, 37

number comma LIST


40 , 37

number
37
What Is Recursion?
4

‫إﻧﮭﺎ ﻋﻣﻠﯾﺔ ﺣل ﻣﺷﻛﻠﺔ‬


 It is a problem-solving process
Recursion
 Breaks a problem into identical but smaller
problems ‫ﯾﻘﺳم اﻟﻣﺷﻛﻠﺔ إﻟﻰ ﻣﺷﺎﻛل ﻣﺗطﺎﺑﻘﺔ وﻟﻛﻧﮭﺎ أﺻﻐر‬
 Eventually you reach a smallest problem ‫ﻓﻲ اﻟﻧﮭﺎﯾﺔ ﺗﺻل إﻟﻰ أﻣﺷﺻﻛﻐﻠﺔر‬
 Answer is obvious or trivial ‫اﻟﺟواب واﺿﺢ أو ﺗﺎﻓﮫ‬

 Using that solution enables you to solve the previous


problems ‫ﯾﺗﯾﺢ ﻟك اﺳﺗﺧدام ھذا اﻟﺣل ﺣل اﻟﻣﺷﻛﻼت اﻟﺳﺎﺑﻘﺔ‬
 Eventually the original problem is solved
‫ﻓﻲ اﻟﻧﮭﺎﯾﺔ ﺗم ﺣل اﻟﻣﺷﻛﻠﺔ اﻷﺻﻠﯾﺔ‬
Recursive Thinking
5

 Recursion is a programming technique in which a


method can call itself in order to fulfill its purpose
 A recursive definition is one which uses the word or
concept being defined in the definition itself
 In some situations, a recursive definition can be an
appropriate way to express a concept
 Before applying recursion to programming, it is best
to practice thinking recursively
Infinite Recursion
6

 All recursive definitions must have a non-recursive part


 If they don't, there is no way to terminate the recursive
path ‫ ﻓﻼ ﺗوﺟد طرﯾﻘﺔ ﻹﻧﮭﺎء اﻟﻣﺳﺎر اﻟﻌودي‬، ‫إذا ﻟم ﯾﻔﻌﻠوا ذﻟك‬

 A definition without a non-recursive part causes infinite


recursion ‫ﺗﻌرﯾف ﺑدون ﺟزء ﻏﯾر ﺗﻛراري ﯾﺳﺑب اﻟﻌودﯾﺔ اﻟﻼﻧﮭﺎﺋﯾﺔ‬
 This problem is similar to an infinite loop -- with the
definition itself causing the infinite “looping”
 The non-recursive part often is called the base case
Parts of a Recursive Definition
7

Every recursive definition contains two parts:


 a base case, which is non-recursive and,

consequently, terminates the recursive application of


the rule.
 a recursive case, which reapplies a rule.
Direct vs. Indirect Recursion
8

 A method invoking itself is considered to be direct


recursion
 A method could invoke another method, which
invokes another, etc., until eventually the original
method is invoked again
 For example, method m1 could invoke m2, which
invokes m3, which invokes m1 again
 This is called indirect recursion
 It is often more difficult to trace and debug
Direct vs. Indirect Recursion
9

m1 m2 m3

m1 m2 m3

m1 m2 m3
Phases of Recursion
10

Forward Phase:
Every recursion has a forward phase in which a call at
every level, except the last, spins off a call to the next
level, and waits for the latter call to return control it.

 Backward Phase:
Every recursion has a backtracking phase in which a call
at every level, except the first, passes control back to the
previous level, at which point the call waiting at the
previous level wakes up and resumes its work.
Examples: Simple problems
11

 Summation
 Factorial
 Fibonacci
Summation
12

N N 1

i  N  i
i 1 i 1
N 2
 N  N 1   i
i 1
N 3
 N  N  1  N  2  i
i 1

 N  N 1  N  2    2 1
The sum of 1 to N, defined recursively
13

int sum (int num)


{
int result;

if (num == 1) Base case


result = 1;
else
result = num + sum(num-1);
Recursive case

return result;
}
Recursive calls to the sum method
14

result = 4 + sum(3)
main

sum(4) result = 3 + sum(2)


sum

result = 2 + sum(1)
sum(3)
sum

sum(2) result = 1
sum

sum(1)
sum
Factorial
15

 Mathematical formulas are often expressed


recursively ‫ﻏﺎﻟًﺑﺎ ﻣﺎ ﯾﺗم اﻟﺗﻌﺑﯾر ﻋن اﻟﺻﯾﻎ اﻟرﯾﺎﺿﯾﺔ ﺑﺷﻛل ﻣﺗﻛرر‬
 N!, for any positive integer N, is defined to be the
product of all integers between 1 and N inclusive
 This definition can be expressed recursively:
1! = 1
N! = N * (N-1)!
 A factorial is defined in terms of another factorial
until the base case of 1! is reached
Factorial Function
16

 N is 1 x 2 x 3 x ... x N – 1 x N
Computing the Factorial
17

 A recursive program implements a recursive definition.


‫ﯾﻘوم اﻟﺑرﻧﺎﻣﺞ اﻟﻌودي ﺑﺗﻧﻔﯾذ ﺗﻌرﯾف ﻣﺗﻛرر‬.

 The main work in writing a recursive method to solve a specific problem


is to define base cases.
Computing the Factorial (Cont.)
18
Fibonacci Sequence
19

0, if n == 0 base case
fibn = 1, if n == 1 base case
fibn – 1 + fibn – 2, if n > 1 recursive case
Fibonacci Sequence
20

 Definition of F(4) spins off


two chains of recursion,
one on F(3) and another
on F(2).
 Every chain either ends in
F(0) or F(1)
Computing the Fibonacci Sequence
21
Computing the Fibonacci Sequence (Cont.)
22

 Every call waits on two


subsequent calls.
 These waits are not
simultaneous.
 For every call there is a
wait-wakeup-wait-wakeup
cycle.
 Except the F(0) and F(1).
 There are two forward
phases and two
backtracking phases.
Avoiding Recursion
23

int fibo(int n)
{

if (n < 2)
return 1;
else
return fibo(n-1) + fibo(n-2);
}
Avoiding Recursion
24
Avoiding Recursion
25

int fibo(int n)
{
int[] f = new int[n];
f[0]=0;
f[1]=1;

for (int i=2; i<=n; i++)


f[i] = f[i-1] + f[i-2];

return f[n];
}
A Simple Solution to a Difficult Problem
26

 The Towers of Hanoi

26
Towers of Hanoi: An Application
27

 There are three towers or pegs, A, B,


and C, and a pile of disks of various
sizes.
 The disks start on peg A.
 Move all the disks from peg A to C,
using B as an intermediate.
 (a) only one disk can be moved at a
time.
 (b) a larger disk can never go on top
of a smaller one.
Towers of Hanoi: Base cases
28

 The smallest instance of the problem is when there is


only one disk in the stack.
 Simply move the disk from peg A to C.
 With 2 disks:
 Top disk moves to B
 Bottom disk moves to C.

 Top disk moves from B to C.


Towers of Hanoi: Recursive cases
29

 With three disks:


 We move two disks out of the way
from A to B.
 We move the bottom disk from A
to C.
 We move the two disks from B to
C.
 We are not allowed to move two
disks at a time.
 Sub-problems requires us to move
two disks from one beg to another,
which we already know how to do.
‫ وھو ﻣﺎ ﻧﻌرﻓﮫ‬، ‫ﺗﺗطﻠب اﻟﻣﺷﺎﻛل اﻟﻔرﻋﯾﺔ ﻣﻧﺎ ﻧﻘل ﻗرﺻﯾن ﻣن ﺗﺳول إﻟﻰ آﺧر‬
‫ﺑﺎﻟﻔﻌل ﻛﯾف ﻧﻔﻌل‬
Towers of Hanoi: Recursive cases(Cont.)
30

 Two-disk problem moved two disks from A to C.


 Three-disk problem, the first two-disk sub-problem
moves disks from A to B.
 Second two-disk sub-problem moves disks form B to C.

 Providing the source destination, and intermediate pegs


can generalize the sub-problem.
Towers of Hanoi: All together
31

 Recursive definition of towers of Hanoi solution for


any n.
 Solve the towers of Hanoi problem for n – 1, with
source peg A, destination peg B, intermediate peg C.
 Move a disk from A to C.

 Solve the towers of Hanoi problem for n – 1, with


source peg B, destination peg C, intermediate peg A.
Towers of Hanoi: An example
32

• The sequence of moves


for solving the Towers of
Hanoi problem with three
disks.
Continued →
Towers of Hanoi: An example (Cont.)
33

• (Continued) The sequence of


moves for solving the Towers
of Hanoi problem with three
disks.
Towers of Hanoi: An example (Cont.)
34

• (Continued) The smaller


problems in a recursive
solution for four disks
Towers of Hanoi: The algorithm
35

Algorithm to solve Towers of Hanoi Puzzle

Algorithm solveTowers (numberOfDisks, startPole, tempPole, endPole)


if (numberOfDisks == 1)
Move disk from startPole to endPole
else
{
solveTowers (numberOfDisks - 1, startPole, endPole, tempPole)
Move disk from startPole to endPole
solveTowers (numberOfDisks - 1, tempPole, startPole, endPole)
}

35
Recursion vs. Iteration
36

 Comparison of elements of a
loop and a recursive function

Loop Recursive Method


loop control variable method input
loop exit condition base case
loop entry condition recursive case
loop body method body
Recursion vs. Iteration
37

 Just because we can use recursion to solve a


problem, doesn't mean we should
 For instance, we usually would not use recursion to
solve the sum of 1 to N
 The iterative version is easier to understand (in fact
there is a formula that is superior to both recursion
and iteration in this case)
 You must be able to determine when recursion is the
correct technique to use
Recursion vs. Iteration
38

 Every recursive solution has a corresponding


iterative solution
 For example, the sum of the numbers between 1
and N can be calculated with a loop
 Recursion has the overhead of multiple method
invocations
 However, for some problems recursive solutions are
often more simple and elegant than iterative
solutions
Drawbacks of Recursion
39

 Stack space that is used to implement it.


 Every recursive method call produces a new instance of
the method, with a new set of local variables (including
parameters).
 Computing the factorial of a number.
 Local information pertaining to each of the calls to fact(N),
fact(N-1), etc. all the way down to fact(2) is stored on stack.
 fact (N) would consume O(N) worth of stack space.
‫ﻋﯾوب‬

Drawbacks of Recursion (Cont.)


40

 Certain computations may be performed


redundantly. ‫ﯾﻣﻛن إﺟراء ﺣﺳﺎﺑﺎت ﻣﻌﯾﻧﺔ ﺑﺷﻛل‬
‫ﻣﺗﻛرر‬
 The Fibonacci sequence.
 Incomputing F(4), F(2) is computed twice.
 F(5)?
 F(3) is computed twice, which involves a computation of F(2), and
there is another computation of F(2) by itself.
 Things thus get worse as we recursively compute the
Fibonacci sequence for bigger and bigger numbers.
 Not to mention the stack space used.
Drawbacks of Recursion (Cont.)
41

 One has to weigh the simplicity of code delivered


by recursion against its drawbacks.
 When iterative solution is obvious.
 There are several problems for which such iterative
solutions are not obviously forthcoming.
Summary
42

 Defining the base case is vital.


 Building on the base case(s) to solve a problem results in
defining the recursive case.
 It is not always right to use recursive solutions. Applying
recursive on simple problems may result in inefficiency in
terms of time and redundancy (Fibonacci).
 Recursion is simple, efficient, and elegant solution if
applied to the right problem (Towers of Hanoi).

You might also like