0% found this document useful (0 votes)
60 views

1.7 Recursion

This document discusses recursion in C programming. It defines recursion as solving a problem by solving smaller versions of the same problem. Recursion involves a function calling itself, either directly or indirectly through other functions. Factorial and Towers of Hanoi problems are provided as examples that can be solved recursively by breaking the problems down into smaller subproblems until reaching a base case. The document also explains how recursion works and compares recursion to iteration.

Uploaded by

Keerthi Vasan S
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)
60 views

1.7 Recursion

This document discusses recursion in C programming. It defines recursion as solving a problem by solving smaller versions of the same problem. Recursion involves a function calling itself, either directly or indirectly through other functions. Factorial and Towers of Hanoi problems are provided as examples that can be solved recursively by breaking the problems down into smaller subproblems until reaching a base case. The document also explains how recursion works and compares recursion to iteration.

Uploaded by

Keerthi Vasan S
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/ 23

1.

7 Recursion

1 Department of CSE
Objectives
• To learn the concept and usage of Recursion in C
• Examples of Recursion in C

2 Department of CSE
What is recursion?
• Sometimes, the best way to solve a problem is by solving a
smaller version of the exact same problem first

• Recursion is a technique that solves a problem by solving a


smaller problem of the same type

• a function that calls itself


Directly or
Indirectly (a function that is part of a cycle in the sequence of
function calls.)

3 Department of CSE
Pictorial representation of direct and indirect
recursive calls

f1 f1 f2 … fn

Direct recursive call Indirect recursive call

4 Department of CSE
Syntax
function_name(parameter list)
{

//‘c’ statements

function_name(parameter values) // recursive call

}

5 Department of CSE
Problems defined recursively
• There are many problems whose solution can be defined
recursively
Example: n factorial

1 if n = 0
n!= (recursive solution)
(n-1)!*n if n > 0

1 if n = 0
n!= (closed form solution)
1*2*3*…*(n-1)*n if n > 0
(also called as iterative
method)
6 Department of CSE
Coding the factorial function
• Iterative implementation
int Factorial(int n)
{
int fact = 1;

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


fact = fact * count;

return fact;
}

7 Department of CSE
Coding the factorial function
(An Example of Recursive Call)
• Recursive implementation

int Factorial(int n)
{
if (n==0) // base case
return 1;
else
return n * Factorial(n-1);
}

8 Department of CSE
Coding the factorial function (cont.)

9 Department of CSE
2*f(2)

2*f(1)

2*f(1)

=f(0)

=f(1)

=f(2)

=f(3)

10 Department of CSE
More Interesting Example
Towers of Hanoi

• Move stack of disks from one peg to another


• Move one disk at a time
• Larger disk may never be on top of smaller disk
11 Department of CSE
A Classical Case: Towers of Hanoi
• The towers of Hanoi problem involves moving a
number of disks (in different sizes) from one
tower (or called “peg”) to another.
– The constraint is that the larger disk can never be
placed on top of a smaller disk.
– Only one disk can be moved at each time
– Assume there are three towers available.

Source Temp Destination


12 Department of CSE
A Classical Case: Towers of Hanoi

13 Department of CSE
A Classical Case: Towers of Hanoi
• This problem can be solved easily by recursion.
• Algorithm:
if n is 1 then
move disk 1 from the source tower to the destination tower
else
1. move n-1 disks from the source tower to the temp tower.
2. move disk n from the source tower to the destination
tower.
3. move n-1 disks from the temp tower to the source tower.

14 Department of CSE
Tower of Hanoi Program
#include <stdio.h> /* PRE: n >= 0. Disks are arranged
small to large on the pegs a, b,
and c. At least n disks on peg
void move (int n, int a, int a. No disk on b or c is smaller
c, int b); than the top n disks of a.
POST: The n disks have been moved
from a to c. Small to large
int main() { order is preserved. Other disks
int disks; on a, b, c are undisturbed. */
printf ("How many disks?");
scanf ("%d", &disks); void move (int n, int a, int c, int
b) {
if (n > 0)
move (disks, 1, 3, 2); {
move (n-1, a, b, c);
return 0; printf ("Move one disk
from %d to %d\n", a, c);
} // main
move (n-1, b, c, a);
• Is pre-condition satisfied before
} // if (n > 0)

this call to move? return;


} // move

15 Department of CSE
Tower of Hanoi Program
#include <stdio.h> /* PRE: n >= 0. Disks are arranged
small to large on the pegs a, b,
and c. At least n disks on peg
void move (int n, int a, int a. No disk on b or c is smaller
c, int b); than the top n disks of a.
POST: The n disks have been moved
from a to c. Small to large
int main() { order is preserved. Other disks
int disks; on a, b, c are undisturbed. */
printf ("How many disks?");
scanf ("%d", &disks); void move (int n, int a, int c, int
b) {
if (n > 0)
move (disks, 1, 3, 2); {
move (n-1, a, b, c);
return 0; printf ("Move one disk
• If pre-condition
} // mainis satisfied here, is it still
from %d to %d\n", a, c);
move (n-1, b, c, a);
satisfied here? } // if (n > 0)

return; And here?


} // move

16 Department of CSE
Tower of Hanoi Program
#include <stdio.h> /* PRE: n >= 0. Disks are arranged
small to large on the pegs a, b,
and c. At least n disks on peg
void move (int n, int a, int a. No disk on b or c is smaller
c, int b); than the top n disks of a.
POST: The n disks have been moved
from a to c. Small to large
int main() { order is preserved. Other disks
int disks; on a, b, c are undisturbed. */
printf ("How many disks?");
scanf ("%d", &disks); void move (int n, int a, int c, int
b) {
if (n > 0)
move (disks, 1, 3, 2); {
move (n-1, a, b, c);
return 0; printf ("Move one disk
from %d to %d\n", a, c);
} // main
move (n-1, b, c, a);
If pre-condition is true and } // if (n > 0)
Can we reason that this
if n = 1, does move satisfy return; program correctly plays
the post-condition? } // move
Tower of Hanoi?

17 Department of CSE
Problems Suitable for Recursive Functions
• One or more simple cases of the problem have a
straightforward solution.
• The other cases can be redefined in terms of problems
that are closer to the simple cases.
• The problem can be reduced entirely to simple cases by
calling the recursive function.
– If this is a simple case
solve it
else
redefine the problem using recursion

18 Department of CSE
Splitting a Problem into Smaller Problems

• Assume that the problem of size 1 can be solved


easily (i.e., the simple case).
• We can recursively split the problem into a
problem of size 1 and another problem of size n-
1.
19 Department of CSE
Recursion vs. iteration
• Iteration can be used in place of recursion
– An iterative algorithm uses a looping construct
– A recursive algorithm uses a branching structure
• Recursive solutions are often less efficient, in terms of both
time and space, than iterative solutions
• Recursion can simplify the solution of a problem, often
resulting in shorter, more easily understood source code

20 Department of CSE
Recursion vs. Iteration (Contd…)

• Some simple recursive problems can be “unwound” into


loops
• But code becomes less compact, harder to follow!
• Hard problems cannot easily be expressed in non-
recursive code
• Tower of Hanoi
• Robots or avatars that “learn”
• Advanced games

21 Department of CSE
Try it Yourself
• Generate a fibonacci series using recursion
Recursive definition for {fn }:
INITIALIZATION: f0 = 0, f1 = 1
RECURSION : fn = fn-1+fn-2 for n > 1
• Finding the GCD
• Euclid’s algorithm makes use of the fact that
gcd(x,y ) = gcd(y, x mod y)
gcd (x,y) = x if y=0
gcd(y, x mod y) otherwise

22 Department of CSE
Summary
• Discussed so far the
• What is a recursion (function) ?
• What is the need for the recursive function?
• Writing recursive functions using C
• How a hard/difficult problem can be solved by recursion
• Comparison of Recursion with Iterative method

23 Department of CSE

You might also like