If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: How to estimate the number of recursive calls

I wrote a program that gives me the numbes but for (100,50) it takes too long
I am sure that there is a formul

Of course, and the question is about finding that formula, rather than the specific answer.

Let's take the following approach: write a function f that expresses the number of recursive calls in terms of n and k.
Forgetting the check on the value of n and k for a minute, it should be obvious that f(n, k) = f(n - 1, k) + f(n - 1, k - 1).
You can expand this further in steps by substituting this formula for the elements on the right hand side. I.e. the second step yields f(n, k) = f(n - 2, k) + 2 * f(n - 2, k - 1) + f(n - 2, k - 2).
Repeat this for a few steps and then have a look at this: http://en.wikipedia.org/wiki/Pascal's_triangle
That should help you to find a more general formula for f(n, k). Once you have that, you can consider how to introduce the if condition in the function.

Cheers, D Drmmr

Please put [code][/code] tags around your code to preserve indentation and make it more readable.

As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Re: How to estimate the number of recursive calls

I tried before all of this and I think no one knows the answer in the world and wanna give up because it takes my time too much and spent enough time for this problem.

I think I have a solution.

Each call to f gives rise to two new calls to f recursively. One can think of the call pattern as an imaginary binary tree. Each call to f represents a node and the first f call in f goes to the left child node and the second to the right child node.

If k is equal to n or bigger then the recursion will be limited by n only. The imaginary binary tree will be complete and have a depth of n. The number of nodes, or rather f calls, will be 2^(n+1) - 1. Say n=1 for example that will give 3 f calls and if n=2 there are 7.

Now if k is made smaller than n it will start limiting the recursion. The imaginary binary tree will no longer be complete. Certain branches will be ignored because k becomes negative in some call paths and that will stop the recursion before it reaches the maximum depth determined by n. This happens each time the second call to f in f has been called k+1 times along a call path (the first call to f in f leaves k unchanged).

The question becomes: How many nodes, that is f calls, in the imaginary binary tree won't be visited because of k? If those are subtracted from the total number of nodes of the complete binary tree we have the number of actual f calls. An analysis is somewhat involved. If a node (f call) in the complete binary tree is on a path below more than k+1 right side child nodes (calls to the second f in f) then it will never be reached (because of the k limit) and should be subtracted from the total.

The result is simple and somewhat surprising. The number of never performed f calls can be found in Pascal's triangle! They're in a subtriangle defined by n and k. One simply add up all binominal coefficients of that subtriangle and double the sum.

All coefficients are added and the sum is doubled. That's the number of f calls that should be removed from the maximum total (which is 2^(n+1) - 1 according to the formula above). So we have f(4,1) = (2^(4+1) - 1) - 2*(1 + 1+3) = (32 - 1) - 2*5 = 21.

Since Pascal's triangle can be calculated in quadratic time an f(n,k) algorithm based on that will be O(N^2). On the other hand binominal coefficients quickly become very large so f(100, 50) is hardly tractable with standard hardware supported arithmetics.

Re: How to estimate the number of recursive calls

Originally Posted by nuzzle

I think I have a solution.

Each call to f gives rise to two new calls to f recursively. One can think of it as a binary tree. Each call to f represents a node and the first f call in f goes to the left and the second to the right.

If k is equal to n or bigger then the recursion will be limited by n only. The imaginary binary tree will be complete and have a depth of n. The number of nodes, or rather f calls, will be 2^(n+1) - 1. Say n=1 for example that will give 3 f calls.

Now if k is made smaller than n it will start limiting the recursion. The imaginary binary tree will no longer be complete. Certain branches will be ignore because k has become smaller than 0 for certain paths and the recursion will stop until it reaches the maximum depth sey by n. This happens each time the second call to f in f has been called k+1 times because then k becomes negative (the first call to f in f leaves k unchanged)

The question now becomes: How many nodes, that is f calls, in the imaginary binary tree won't be visited because of k? If those are subtracted from the total number of nodes of the complete binary tree we have the number of actual f calls.

An analysis is somewhat involved. If a node is reached after more than k+1 "right child" calls to f then it should be subtracted from the total. But the result is simple and somewhat surprising. The number of not performed f calls can found in Pascal's triangle. One simply cuts out a subtriangle and add up all coefficients.

You add up the numbers and double the sum. That's the number of nodes (f calls) that should be removed from the total which is 2^(n+1) - 1 according to the formula above. So we have f(4,1) = (2^(4+1) - 1) - 2*(1 + 1+3) = (32 - 1) - 2*5 = 21.

Since Pascal's triangle can be calculated in quadratic time the algorithm is O(N^2). On the other hand binominal coefficients quickly become very large so f(100, 50) is hardly tractable with standard arithmetic.

thank for your answer
but there is only one thing remains
I sent an mail to the writer of the book that I found this problem and he answered me

Code:

Hint: calculate the number of calls for some small values of N and k and consider functions that involve N! (N factorial) and k! (k factorial).

Re: How to estimate the number of recursive calls

Originally Posted by arira

where is the factorial in this formula?

Pascal's triangle consists of binominal coefficients (n|k) defined as n! / (k! * (n-k)!) so there are lots of factorials in the solution. I wouldn't say that hinting at factorials is such a great help really but maybe the author has some other solution approach in mind where they play a more prominent role.

In my solution binominals appear because they have a combinatorial interpretation. (n|k) tells how many ways there are to order k of n elements. There may be a deeper reason why they appear as a subtriangle of Pascal's triangle or maybe it's just coincidental. I couldn't say but it's interesting.

Re: How to estimate the number of recursive calls

Originally Posted by BioPhysEngr

Presumably there is some analytical solution.

Maybe the sum of binominals I have in my solution can be reduced to factorials but computationally it may be better to keep the binominals. It's because of the relationship with Pascal's triangle in this case. Factorials becomes awfully big very quickly and require lots of multiplications to compute. Binominals on the other hand stay smaller and if the recurrent Pascal's triangle algorithm is used only additions are needed. For example the factorial 20! is 2432902008176640000 while the binominal (20|10) is just 184756.

I looks like the OP is using Java and it sports a standard class for arbitrary-precision integers called BigInteger. Using BigInteger in combination with the very efficient recurrent Pascal's triangle algorithm would make it possible I think to find the exact solution to even something as big as f(100,50) in reasonable time.

Re: How to estimate the number of recursive calls

Originally Posted by nuzzle

Maybe the sum of binominals I have in my solution can be reduced to factorials but computationally it may be better to keep the binominals. It's because of the relationship with Pascal's triangle in this case. Factorials becomes awfully big very quickly and require lots of multiplications to compute. Binominals on the other hand stay smaller and if the recurrent Pascal's triangle algorithm is used only additions are needed. For example the factorial 20! is 2432902008176640000 while the binominal (20|10) is just 184756.

I looks like the OP is using Java and it sports a standard class for arbitrary-precision integers called BigInteger. Using BigInteger in combination with the very efficient recurrent Pascal's triangle algorithm would make it possible I think to find the exact solution to even something as big as f(100,50) in reasonable time.

I know what you mean but our purpose is to find a formula not a better way to calculate binomial
we can say in formula 50! and don't need to calculate it because it is formula

This was a nostalgic trip down memory lane. I haven't used Java or Eclipse for years.

BigInteger is something of a fossil. It's the only surviving reminder of a proud initiative called Java Grande. It was active at the peak of the Java hype age when there still was no limit to what Java could and should be used for. So much effort and now it's just a faint memory in the senile minds of a few elders who still remember Java in its hayday. So sad.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.