How to explain how this algorithm works? I need to write an article about this but I can't explain why this recursion works fine.
It defines the number of partitions of a given integer
function p(sum,largest):
if largest==0: return 0
if sum==0: return 1
if sum<0: return 0
return p(sum, largest-1) + p(sum-largest, largest)
(call: p(n,n))
Thank you very much.
This function is the same as $F(n, k)$ which is the number of partitions of $n$ such that no element in the partition is greater than $k$ (assuming $k <= n$).
Using this function $F(n, n)$ denotes the number of partitions of $n$ as no element in the partition of $n$ can be greater than $n$.
The base conditions $F(0, k) = 1$ and $F(-m, k) = 0$ are by the definition of the partition function $P(n)$ which represents the total number of partitions of $n$ ($m > 0$). $F(n, 0) = 0$ as $n$ cannot be partitioned such that no element in the partition is greater than $0$.
Now as for the last statement in the function, think of $F(n, k)$ - the number of partitions of $n$ such that no element in a partition is greater than $k$ - as the union of two mutually exclusive sets, first set containing all the partitions such that all elements in a partition are less than $k$ and the second set containing all the partitions such that at least one element in each partition is equal to $k$.
Clearly the cardinality of first set is $F(n, k-1)$ and the cardinality of the second set is $F(n-k, k)$ because we have already chosen $k$ as an element in a partition so now we have to partition $n-k$.
Clearly the the above two sets are mutually exclusive therefore,
$F(n, k) = F(n, k-1) + F(n-k, k) $