0% found this document useful (0 votes)
51 views12 pages

Recursion

Recursion is a technique for solving problems by breaking them down into smaller subproblems of the same type. Recursive functions call themselves to solve these subproblems. Examples of recursively defined problems include factorials and combinations. A recursive function must have a base case to stop recursion and a general case that calls itself on a smaller input until the base case is reached.

Uploaded by

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

Recursion

Recursion is a technique for solving problems by breaking them down into smaller subproblems of the same type. Recursive functions call themselves to solve these subproblems. Examples of recursively defined problems include factorials and combinations. A recursive function must have a base case to stop recursion and a general case that calls itself on a smaller input until the base case is reached.

Uploaded by

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

Recursion

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
When you turn this into a program, you end
up with functions that call themselves
(recursive functions)
int f(int x)
{
int y;
 
if(x==0)
return 1;
else {
y = 2 * f(x-1);
return y+1;
}
}
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
Coding the factorial function
• Recursive implementation
int Factorial(int n)
{
if (n==0) // base case
return 1;
else
return n * Factorial(n-1);
}
Coding the factorial function
(cont.)
• Iterative implementation
int Factorial(int n)
{
int fact = 1;
 
for(int count = 2; count <= n; count++)
fact = fact * count;
 
return fact;
}
Another example:
n choose k (combinations)
• Given n things, how many different sets of size
k can be chosen?

n n-1 n-1
= + , 1 < k < n (recursive solution)
k k k-1
n n!
= , 1 < k < n (closed-form solution)
k k!(n-k)!
with base cases:
n n
= n (k = 1), = 1 (k = n)
1 n
n choose k (combinations)
int Combinations(int n, int k)
{
if(k == 1) // base case 1
return n;
else if (n == k) // base case 2
return 1;
else
return(Combinations(n-1, k) + Combinations(n-1, k-1));
}
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
How do I write a
recursive function?
• Determine the size factor
• Determine the base case(s)
(the one for which you know the answer)
• Determine the general case(s)
(the one where the problem is expressed as a
smaller version of itself)
• Verify the algorithm
(use the "Three-Question-Method")

You might also like