* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Lecture-13
Survey
Document related concepts
Transcript
Programming Fundamental
Instructor Name:
Lecture-13
Today’s Lecture
What is Recursion
Recursive Function
General Form of Recursive Methods
Recursive Function Call Understanding with Example
2
Recursion
What is Recursion?
A recursive function is a function that calls itself.
The process of solving a problem by reducing it to smaller version of itself
is called recursion.
Recursion is very powerful way to solve certain problems for which the
solution would otherwise be very complicated.
There are many problems and specific areas where you can see the
repetitive behaviour (pattern) or you can find a thing, which can be
modelled in such a way that it repeats itself.
Once you understand recursive methods, they are often simpler to write
than their iterative equivalents
We will begin by studying the form of general recursive methods and
comparing iterative and recursive solution.
3
Understanding Recursion
Handshake Problem
There are n people in a room. If each person shakes hands once with every
other person. What is the total number h(n) of handshakes?
h(n) = h(n-1) + n-1
h(4) = h(3) + 3
h(3) = h(2) + 2
h(2) = 1
h(n): Sum of integer from 1 to n-1 = n(n-1) / 2
4
Understanding Recursion
Fund Raising : Iteration vs Recursion
Problem: Collect $1,000.00 for charity
Assumption: Everyone is willing to donate a penny
Iterative Solution
Visit 100,000 people, asking each for a penny
Recursive Solution
If you are asked to collect a penny, give a penny to the person who asked
you for it
Otherwise
Visit 10 people and ask them to each raise 1/10th of the amount of
money that you have been asked to raise
Collect the money that they give you and combine it into one bag
Give it to the person who asked you to collect the money
5
Recursion
A recursive function is a function that calls itself.[
Recursive problem-solving approaches have a number of elements
in common.
Recursive Function has two parts
(i) Base Case(s).
The function actually knows how to solve only the simplest
case(s),
If the function is called with a base case, the function simply
returns a result.
(ii) Complex Case(s)
If the function is called with a more complex problem, It
typically divides the problem into two conceptual pieces.
a) A piece that the function knows how to do and
b) A piece that it does not know how to do
Complex Case(s)
To make recursion feasible, the latter piece must resemble the
original problem
But be a slightly simpler or slightly smaller version.
Function launches (calls) a fresh copy of itself to work on the
smaller problem this is referred to as a recursive call and is also
called the recursion step.
The recursion step often includes the keyword return,
General Form of Recursive Methods
Solve(Problem)
{
if (Problem is minimal/not decomposable: a base case)
solve Problem directly; i.e., without recursion
else {
(1) Decompose Problem into one or more similar,
strictly smaller subproblems: SP1, SP2, ... , SPN
(2) Recursively call Solve (this method) on each
subproblem: Solve(SP1), Solve(SP2),..., Solve(SPN)
(3) Combine the solutions to these subproblems into a
solution that solves the original Problem
}
}
8
General Form of Recursive Methods
A recursive definition has two parts:
the base case - a stopping condition
the recursive step - an expression of the computation or definition in
terms of itself
There are many recursive definitions in mathematics. Consider the
factorial function:
n! = n * (n-1) * (n -2) * … * 2 * 1
The same function can be defined recursively by giving a base case and a
recursive step:
0! = 1 (by definition)
n! = n * (n - 1)! (the recursive step)
9
Understanding Recursive Methods
In some problems, it may be natural to define the problem in terms of
the problem itself.
Recursion is useful for problems that can be represented by a simpler
version of the same problem.
Example: the factorial function
6! = 6 * 5 * 4 * 3 * 2 * 1
We could write:
6! = 6 * 5!
In general, we can express the factorial function as follows:
n! = n * (n-1)!
Is this correct? Well… almost.
The factorial function is only defined for positive integers. So we
should be a bit more precise:
n! = 1
(if n is equal to 1)
n! = n * (n-1)!
(if n is larger than 1)
10
Understanding Recursive Methods
Factorial Function
The C++ equivalent of this definition:
int fac(int numb){
if(numb<=1)
return 1;
else
return numb * fac(numb-1);
}
11
Understanding Recursive Methods
Assume the number typed is 3, that is, numb=3.
fac(3) :
3 <= 1 ?
fac(3) = 3 * fac(2)
No.
fac(2) :
2 <= 1 ?
No.
fac(2) = 2 * fac(1)
fac(1) :
1 <= 1 ? Yes.
return 1
fac(2) = 2 * 1 = 2
return fac(2)
fac(3) = 3 * 2 = 6
return fac(3)
fac(3) has the value 6
int fac(int numb){
if(numb<=1)
return 1;
else
return numb * fac(numb-1);
}
12
Understanding Recursive Methods
Assume the number typed is 4, that is, numb=4.
int fac(int numb){
if(numb<=1)
return 1;
else
return numb * fac(numb-1);
}
13
Understanding Recursive Methods
Assume the number typed is 5, that is, numb=5.
14
Factorial Function Iterative Vs Recursive
For certain problems (such as the factorial function), a recursive solution often
leads to short and elegant code. Compare the recursive solution with the
iterative solution:
Recursive solution
int fac(int numb){
if(numb<=1)
return 1;
else
return numb*fac(numb-1);
}
Iterative solution
int fac(int numb){
int product=1;
while(numb>1){
product *= numb;
numb--;
}
return product;
}
15
Understanding Recursive Methods
Fibonacci Sequence
The Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The next number is found by adding up the two numbers before it.
The 2 is found by adding the two numbers before it (1+1)
Similarly, the 3 is found by adding the two numbers before it (1+2),
And the 5 is (2+3),
and so on!
Example: the next number in the sequence above is 21+34 = 55
16
Understanding Recursive Methods
Fibonacci Sequence – Recursive Definition
F(0) = 0;
F(1) = 1;
F(number) = F(number-1)+ F(number-2);
C++ Recursive function for Fibonacci sequence
int fib(int number)
{
if (number == 0) return 0;
if (number == 1) return 1;
return (fib(number-1) + fib(number-2));
}
17
Fibonacci Sequence - Recursive Methods
18
Fibonacci Sequence - Recursive Methods
• Assume the input number is 4, that is, num=4:
fib(4):
4 == 0 ? No; 4 == 1? No.
fib(4) = fib(3) + fib(2)
fib(3):
3 == 0 ? No; 3 == 1? No.
fib(3) = fib(2) + fib(1)
fib(2):
2 == 0? No; 2==1? No.
fib(2) = fib(1)+fib(0)
fib(1):
1== 0 ? No; 1 == 1? Yes.
fib(1) = 1;
return fib(1);
int fib(int num)
{
if (num == 0) return 0;
if (num == 1) return 1;
return
(fib(num-1)+fib(num-2));
}
19
Fibonacci Sequence - Recursive Methods
Trace a Fibonacci Number
fib(0):
0 == 0 ? Yes.
fib(0) = 0;
return fib(0);
fib(2) = 1 + 0 = 1;
return fib(2);
fib(3) = 1 + fib(1)
fib(1):
1 == 0 ? No; 1 == 1? Yes
fib(1) = 1;
return fib(1);
fib(3) = 1 + 1 = 2;
return fib(3)
20
Fibonacci Sequence - Recursive Methods
Trace a Fibonacci Number
fib(2):
2 == 0 ? No; 2 == 1? No.
fib(2) = fib(1) + fib(0)
fib(1):
1== 0 ? No; 1 == 1? Yes.
fib(1) = 1;
return fib(1);
fib(0):
0 == 0 ? Yes.
fib(0) = 0;
return fib(0);
fib(2) = 1 + 0 = 1;
return fib(2);
fib(4) = fib(3) + fib(2)
= 2 + 1 = 3;
return fib(4);
21
Example
The Fibonacci Series
long fab (long);
main()
{
int i;
Cin>>I;
cout<<"fabonaci"<<i<<"="<<fab(i)<<endl;
getch();
}
long fab(long x)
{
if (x==1 || x==0)
return x;
else
return (fab(x-1)+fab(x-2));
}
Important (Iteration & Recursion)
If we use iteration, we must be careful not to create an infinite loop by
accident:
for(int incr=1; incr!=10;incr+=2)
...
Oops!
int result = 1;
while(result >0){
...
result++;
}
Oops!
Similarly, if we use recursion we must be careful not to create an infinite
chain of function calls:
int fac(int numb){
return numb * fac(numb-1);
}
Oops!
int fac(int numb){
if (numb<=1)
return 1;
else
return numb * fac(numb+1);
}
Oops!
23
Important (Iteration & Recursion)
Recursion
We must always make sure that recursion bottoms out
A recursive function must contains at least one non recursive branch
The recursive call must eventually leads to non recursive branch
Recursion is one way to decompose a task into smaller subtasks. At least
one of the subtasks is a smaller example of the same task.
The smallest example of the same task has a non-recursive solution.
Example: The factorial function
n! = n * (n-1)! and 1! = 1
24
Home Practice
Home Practice - Recursion
Write a recursive solution to compute sum of first n integers
Write a recursive solution to computer sum of first n odd and even integers
Write a recursive solution that counts the numbers of zero digits in an
integer
for example zeros(10200) returns 3
Write a exponential Functional that recursively computes
np
An important function in probability is the binomial coefficient, or choose,
function, written B(n,k), and defined as:
B(n,k) = n! / k!(n - k)! n≥0, 0<=k≤n
Write recursive solution based on following recursive formula
B(n, k) = B(n − 1, k − 1) + B(n − 1, k).
25
26