What would the 1st recursive call for this function look like? - c++

int Fun(int m, int n)
{
if(n==0)
{
return n + 2;
}
return Fun(n-1, m-1) + Fun(m-1,n-1) + 1;
}
I'm completely lost as to what the 1st case would visually look like for this function. I don't understand why the function has two parameters and why we only return 1 parameter at the end with our base case. What would be the process to work this out? Any input you want to use to explain to me is fine I was trying (3,3). Although, now that I'm thinking about it how would this function look like if one of the inputs was smaller than the other like (3,2) or (2,3)?

Note that return n + 2; simplifies to return 2;.
The function takes two arguments (parameters) and returns a single value. That's like the operation of adding two numbers that you were taught in your first year at school.
Whether or not Fun(n - 1, m - 1) is called before Fun(m - 1, n - 1) is not specified by the C++ standard. So I can't tell you what the first recursive call will look like. This gives compilers more freedom in making optimisation choices. Of course the order in which the functions are called has no effect on the eventual result.
The best way of analysing what happens in your particular case is to use a line by line debugger.

There is nothing special about recursive functions - they work exactly like non-recursive functions.
Fun(3,3) does this:
if(3==0)
{
return 3 + 2;
}
return Fun(2, 2) + Fun(2, 2) + 1;
It needs the value of Fun(2,2), which does this:
if(2==0)
{
return 2 + 2;
}
return Fun(1, 1) + Fun(1, 1) + 1;
And that needs Fun(1,1), which does
if(1==0)
{
return 1 + 2;
}
return Fun(0, 0) + Fun(0, 0) + 1;
and Fun(0,0) does
if(0==0)
{
return 0 + 2;
}
return Fun(-1, -1) + Fun(-1, -1) + 1;
which returns 2 since the condition is true.
So, Fun(1, 1) will do
return 2 + 2 + 1;
which is 5, and Fun(2,2) will do
return 5 + 5 + 1;
which is 11, and Fun(3,3) will do
return 11 + 11 + 1;
which is 23.
I'm sure you can work through other examples on your own.

Related

How does a recursive method count without a counter?

I am writing methods for a binary search tree and am having trouble understanding the basics of recursion. I found a method that checks for the size of the binary search tree and I see how it it going through each element of the tree, but I don't understand how it is counting the size exactly. Can someone please explain this to me?
Here is the method:
unsigned long BST::sizeHelper(BSTNode* r){
if (r == NULL){
return 0;
} else {
return (sizeHelper(r->left) + sizeHelper(r->right) + 1); //+1 for the root
}
}
I see the return statement, but I don't see any indication of how it is counting the elements as it goes through them.
Upon each return, the method adds at least one to the total size.
For example, consider the following tree:
(I'm bad at drawing, so I stole one online)
Steps are as follow:
Start from A, return size(B) + size(C) + 1.
For B, return size(D) + 0 + 1. (0 because B has no right child, i.e. NULL)
For D, return 0 + 0 + 1. size(D) = 1.
Now going back, size(B) = 1 + 1 = 2.
For C, return size(E) + size(F) + 1.
Similar to D, size(E) = size(F) = 1.
Going back again, size(C) = 1 + 1 + 1 = 3.
Finally, size(A) = 2 + 3 + 1 = 6.

C++ Understanding recursive function

I need some help with understanding this recursive function. It is returning 101 when n = 5 but I do not understand why. Here is the function:
string RecursiveMystery(int n) {
string s = "1";
if (n % 2 == 0) {
s = "0";
}
if (n < 2) {
return s;
}
return RecursiveMystery(n / 2) + s;
So when RecursiveMystery(5), it should go into the end return function RecursiveMystery(5 / 2) which is equal to 0 + 1 which is 01 (because s = 1 at the time of RecursiveMystery(5)). I'm stuck with understanding how it is still returning 101.
If you call RecursiveMystery(5), it returns RecursiveMystery(2) + "1". So we have to evaluate RecursiveMystery(2), which returns RecursiveMystery(1) + "0". And RecursiveMystery(1) returns `"1".
Therefore
RecursiveMystery(5) = RecursiveMystery(2) + "1" = RecursiveMystery(1) + "0" + "1" = "1" + "0" + "1" = "101"
Some more infos about the RecursiveMystery-method. It computes the binary representation of the number n. Basically it writes a 1 at the end, if n is odd, and a 0, if n is even. And n/2 is just the number n without the last digit (in binary representation ).
Stage 1:
Run the function with the different inputs you need to see what the results are:
RecursiveMystery(5)
return RecursiveMystery(2) + "1"; // Gets to recursive call
// So look at what RecursiveMystery(2) does
RecursiveMystery(2)
return RecursiveMystery(1) + "0"; // Gets to recurive call
// So look at what RecursiveMystery(1) does
RecursiveMystery(1)
return "1"; // Return early as n < 2
Stage 2
So now lets expand the top level manually
RecursiveMystery(5)
return RecursiveMystery(2) + "1";
=> RecursiveMystery(5)
return RecursiveMystery(1) + "0" + "1";
=> RecursiveMystery(5)
return "1" + "0" + "1";
=> RecursiveMystery(5)
return "101";
Your function starts out at 5, and keeps s as "1", then calls recursively with n = 2, and turns s to "0", then calls itself recursively one more time (because 2 is not below 2), with n as 1. And this time it's the last call, with s remaining "1".
Unwinding the calls back to the original one, you get "1"+"0"+"1".
if you want it to return "01" change the if statement
from
if (n < 2)
to
if (n <= 2)

C++ Recursive Febonacci Function

Below is my code. I tried to print the Fibonacci recursive function [at the very end] but it gave me segment fault. What is wrong with my code? I spent like 3 hours on this and couldn't figure it out. Can someone please be kind enough to give me some help? Thanks
int fibonacci (int x) {
if (x == 0) {
return 0;
}
else if (x == 1) {
return 1;
}
else {
return (fibonacci(x-1) + fibonacci (x + 2));
}
}
Most probably you're getting the segfault because your stack grows without control, and that's because of this line:
return (fibonacci(x-1) + fibonacci (x + 2));
calling fibonacci() with a value greater than the original causes the non-recursive case to never be reached, causing the stack to eventually overflow, or trying to do so, as the SO will detect this and terminates your process.
So, rewrite that line as this:
return (fibonacci(x-1) + fibonacci (x - 2));
Currently the recursion never ends because:
return (fibonacci(x-1) + fibonacci (x + 2));
should be
return (fibonacci(x-1) + fibonacci (x - 2));
The current code leads to the stack running out of memory because you keep adding function calls infinitely and at the point where the stack is out of memory you get the crash. You can see what is happening when you follow the flow of execution with a sample number:
fibonnaci(2) = fibonacci(1)+fibonacci(4)
= 1 + (fibonacci(3)+fibonacci(6))
= 1 + (((fibonacci(2)+fibonacci(5))+(fibonacci(5)+fibonacci(8)))
= 1 + .......
As you can see this never actually terminates.

Simple c++ recursive explaination needed

I am currently trying to wrap my head around recursion so I picked a c++ textbook and began to read. The first couple of pages in the chapter on recursion were easy to understand but then I got to an item that doesn't make sense to me.
int height(node *p)
{
if(p==NULL)
return 0;
else{
return 1 + max(height(p->llink),height(p->rlink));
}
If max gives me the greatest of two values, how does max get its arguments from what height it's returning.
If anyone could help I would greatly appreciate it.....
To understand recursion you have to think recursively:
you can understand that an empty tree has height 0
you can understand that a generic non-empty tree has height 1 + the height of the longest subtree (which can be the one starting from the left or from the right)
Starting from this you can trivially understand the code. If you draw the tree you will see what happens. If you have for example
A
/ \
B C
/ \
D E
height(A) will return 1 + max(height(B), height(C))
height(B) will return 1 + max(height(D), height(E))
height(C) will return 1 + max(height(NULL), height(NULL)) = 1
height(D) will return 1 + max(height(NULL), height(NULL)) = 1
height(E) will return 1 + max(height(NULL), height(NULL)) = 1
so
height(A) = 1 + max(height(B), height(C)) =
= 1 + max(1 + max(height(D),height(E)), 1) =
= 1 + max(1 + 1, 1) = 1 + max(2, 1) = 3
(I omitted calls to height(NULL) because they are trivially 0 and otherwise it would have been too much verbose.)
The arguments to function are evaluated before function call.
So your example equivalent could look like the following (which maybe makes more sense?):
int height(node *p)
{
if(p==NULL)
return 0;
else{
int heightLeftSubtree = height(p->llink);
int heightRightSubtree = height(p->rlink);
return 1 + max(heightLeftSubtree, heightRightSubtree);
}
}

Fibonacci Function Question

I was calculating the Fibonacci sequence, and stumbled across this code, which I saw a lot:
int Fibonacci (int x)
{
if (x<=1) {
return 1;
}
return Fibonacci (x-1)+Fibonacci (x-2);
}
What I don't understand is how it works, especially the return part at the end: Does it call the Fibonacci function again? Could someone step me through this function?
Yes, the function calls itself. For example,
Fibonacci(4)
= Fibonacci(3) + Fibonacci(2)
= (Fibonacci(2) + Fibonacci(1)) + (Fibonacci(1) + Fibonacci(0))
= ((Fibonacci(1) + Fibonacci(0)) + 1) + (1 + 1)
= ((1 + 1) + 1) + 2
= (2 + 1) + 2
= 3 + 2
= 5
Note that the Fibonacci function is called 9 times here. In general, the naïve recursive fibonacci function has exponential running time, which is usually a Bad Thing.
This is a classical example of a recursive function, a function that calls itself.
If you read it carefully, you'll see that it will call itself, or, recurse, over and over again, until it reaches the so called base case, when x <= 1 at which point it will start to "back track" and sum up the computed values.
The following code clearly prints out the trace of the algorithm:
public class Test {
static String indent = "";
public static int fibonacci(int x) {
indent += " ";
System.out.println(indent + "invoked with " + x);
if (x <= 1) {
System.out.println(indent + "x = " + x + ", base case reached.");
indent = indent.substring(4);
return 1;
}
System.out.println(indent + "Recursing on " + (x-1) + " and " + (x-2));
int retVal = fibonacci(x-1) + fibonacci(x-2);
System.out.println(indent + "returning " + retVal);
indent = indent.substring(4);
return retVal;
}
public static void main(String... args) {
System.out.println("Fibonacci of 3: " + fibonacci(3));
}
}
The output is the following:
invoked with 3
Recursing on 2 and 1
invoked with 2
Recursing on 1 and 0
invoked with 1
x = 1, base case reached.
invoked with 0
x = 0, base case reached.
returning 2
invoked with 1
x = 1, base case reached.
returning 3
Fibonacci of 3: 3
A tree depiction of the trace would look something like
fib 4
fib 3 + fib 2
fib 2 + fib 1 fib 1 + fib 0
fib 1 + fib 0 1 1 1
1 1
The important parts to think about when writing recursive functions are:
1. Take care of the base case
What would have happened if we had forgotten if (x<=1) return 1; in the example above?
2. Make sure the recursive calls somehow decrease towards the base case
What would have happened if we accidentally modified the algorithm to return fibonacci(x)+fibonacci(x-1);
return Fibonacci (x-1)+Fibonacci (x-2);
This is terribly inefficient. I suggest the following linear alternative:
unsigned fibonacci(unsigned n, unsigned a, unsigned b, unsigned c)
{
return (n == 2) ? c : fibonacci(n - 1, b, c, b + c);
}
unsigned fibonacci(unsigned n)
{
return (n < 2) ? n : fibonacci(n, 0, 1, 1);
}
The fibonacci sequence can be expressed more succinctly in functional languages.
fibonacci = 0 : 1 : zipWith (+) fibonacci (tail fibonacci)
> take 12 fibonacci
[0,1,1,2,3,5,8,13,21,34,55,89]
This is classic function recursion. http://en.wikipedia.org/wiki/Recursive_function should get you started. Essentially if x less than or equal to 1 it returns 1. Otherwise it it decreases x running Fibonacci at each step.
As your question is marked C++, I feel compelled to point out that this function can also be achieved at compile-time as a template, should you have a compile-time variable to use it with.
template<int N> struct Fibonacci {
const static int value = Fibonacci<N - 1>::value + Fibonacci<N - 2>::value;
};
template<> struct Fibonacci<1> {
const static int value = 1;
}
template<> struct Fibonacci<0> {
const static int value = 1;
}
Been a while since I wrote such, so it could be a little out, but that should be it.
Yes, the Fibonacci function is called again, this is called recursion.
Just like you can call another function, you can call the same function again. Since function context is stacked, you can call the same function without disturbing the currently executed function.
Note that recursion is hard since you might call the same function again infinitely and fill the call stack. This errors is called a "Stack Overflow" (here it is !)
In C and most other languages, a function is allowed to call itself just like any other function. This is called recursion.
If it looks strange because it's different from the loop that you would write, you're right. This is not a very good application of recursion, because finding the n th Fibonacci number requires twice the time as finding the n-1th, leading to running time exponential in n.
Iterating over the Fibonacci sequence, remembering the previous Fibonacci number before moving on to the next improves the runtime to linear in n, the way it should be.
Recursion itself isn't terrible. In fact, the loop I just described (and any loop) can be implemented as a recursive function:
int Fibonacci (int x, int a = 1, int p = 0) {
if ( x == 0 ) return a;
return Fibonacci( x-1, a+p, a );
} // recursive, but with ideal computational properties
Or if you want to be more quick but use more memory use this.
int *fib,n;
void fibonaci(int n) //find firs n number fibonaci
{
fib= new int[n+1];
fib[1] = fib[2] = 1;
for(int i = 3;i<=n-2;i++)
fib[i] = fib[i-1] + fib[i-2];
}
and for n = 10 for exemple you will have :
fib[1] fib[2] fib[3] fib[4] fib[5] fib[6] fib[7] fib[8] fib[9] fib[10]
1 1 2 3 5 8 13 21 34 55``