M
M
Matvey Tarasov2022-01-06 15:15:54
Java
Matvey Tarasov, 2022-01-06 15:15:54

How does this recursive algorithm work?

Hello!
The algorithm for calculating the binomial, as I understand it, is according to the Bernouli formula.
61d6dc43c5129742215868.png
It also uses a recurrent formula for finding the number of combinations
61d6dc7204063978128686.png
. The algorithm itself:

public double binomial1(int N, int k, double p) {
        if (N == 0 && k == 0) return 1.0;
        if (N < 0 || k < 0) return 0.0;
        return (1.0 - p) *binomial1(N-1, k, p) + p*binomial1(N-1, k-1, p);
    }


Questions:
How is it that we have (1.0 - p) and p in a recurrent expression without powers? After all, according to the Bernoulli formula, there should be. I understand that there is a recursion, apparently, they are multiplied by themselves several times, but I can’t unravel the tangle?
Are there any methods for analyzing such algorithms / formulas, and for creating these formulas. Somehow, after all, people get to write such code?

The code itself was taken from Sedgwick's book on algorithms , began to take a course on coursera.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Myclass, 2022-01-06
@Myclass

Do you understand the word degree ?
After all, it's just the n-th number of times the number is multiplied by itself. And the first part to the plus reproduces this. The second part after the plus is and is that plus from the formula.

N
Nikolay Savelyev, 2022-01-07
@AgentSmith

Such algorithms are easily analyzed and unraveled when you start to manually substitute values. 0, 1, 2,...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question