Recursive Combination Solution using Dynamic Programming

Status
Not open for further replies.
Hey everyone,

I'm writing a Recursurive solution for doing Combinations using Dynamic Programming and using a apvector (same as a regular vector) as my auxilirary data holder. Here's the code I have so far (works fine, no need to fix):

code:<HR>
Code:
apmatrix<double> table(1000,1000,0);

double comb3(int n, int r){
  if ( n == r || r == 0) return 1;
  if ( n == 1 || n - r == 1) return n;
  if (table [n][r] == 0) table[n][r] = comb3(n-1,r - 1) + comb3(n-1,r);
  return table[n][r];
}

Now, I showed this to my teacher and he said it was very good except there are a few base cases that I could add that would speed my code up. The thing is, I can't really think of any. If anyone could suggest a few, either in code or in math, that'd be great.

Noah
 

Citrus538

Ars Legatus Legionis
12,722
Maybe, he meant something like:

code:<HR>
Code:
apmatrix<double> table(1000,1000,0);
double comb3(int n, int r)
{  
  if ( n == r || r == 0) 
    return 1;
  if (n == 2 || n - r == 2) 
    return table[n - 1][r - 1] + table[n - 1][r];


  if (table [n][r] == 0)
    table[n][r] = comb3(n-1,r - 1) + comb3(n-1,r);

  return table[n][r];
}

(edit: doh, no html in code blocks! I'm sure you can see what I did though, second if statement and second return)

Of course this will not return the same result (you'll have to fiddle with it a bit), so I don't know if this will wind up being faster.

He could have meant "find an earlier occurring base case", which I tried to do. This depends on what garauntees there are on the initial inputs though. . .is a 2x2 matrix valid? Or must it be at least 3x3? Can it be 1x1? 1x3?
 
Maybe you could use the fact that comb(n, r) == comb(n, n - r), and transform the "r" parameter on the fly to use the smallest of both values. Don't know if that would really save you something in the end, but I think it might. Try it and see :)

Edit: What I meant:
code:<HR>
Code:
double comb3(int n, int r)
{
    // Your base cases, as before
    if (r > (n / 2))
        r = n - r;
    // the rest stays the same
}
You should also check (at least initially in a wrapper function) that r is valid for the given n. It is often assumed in statistics that the value of this function will be 0 if the input is invalid.
 
This is also on the comb(n,n-r)=comb(n,r) front ...

In the lines
code:<HR>
Code:
if ( n == r || r == 0) return 1;
if ( n == 1 || n - r == 1) return n;
the case comb(n,1) isn't covered directly ... perhaps that was a typo on the second line, and instead should be the following?
code:<HR>
Code:
if ( r == 1 || n - r == 1) return n;

Provided that 0<=r<=n, the n==1 case is covered in the first test.

An alternative way of doing the comb(n,n-r)==comb(n,r) test could be
to add another assignment in the recursive step:
code:<HR>
Code:
if (table[n][r]==0)
    table[n][r]=table[n][n-r]=comb3(n-1,r-1)+comb(n-1,r);

I think that this may be more efficient for smaller tables (cache) and certain branch-averse architectures, but you'd have to run and see :)
 
Status
Not open for further replies.