Pages

Showing posts with label Recursion. Show all posts
Showing posts with label Recursion. Show all posts

Saturday, January 21, 2012

The Subset Problem and it's recursive implementation


Suppose we have a set S with n number of elements. How many different ways can you select k  elements from the set S. So we solve the problem using recursion. Here is a video explaining the problem and below that is the recursive implementation of the subset problem.



Implementation:

int C(int n,int k) {
   if (k==0 || k==n)
     return 1;
   return C(n-1,k-1) + C(n-1,k);
}