Sunday, 12 April 2015

Finding Subsets

I often use programming to solve problems and automate tasks. Recently, I wanted to solve some simple optimization problem. I decided to generate all possible subsets in a list.
When I was thinking about this problem, I saw recursion.
Say you have a list of length n.
You can ask what the list of subsets of list n-1 is and then add element x
take that list of sets and add the removed element to generate an added set.

For example, take a list of 3 elements (A, B, C)
remove A.
Find the subset of 2 elements (B, C)
you get ((B, C), (B), and (C))
now generate the added set (B, C, A), (B, A), (C, A)
You should return (B, C, A), (B, A), (C, A), (B, C), (B), and (C))

The base case is when there is one element (empty is not a subset in this definition).

So I implemented this and found it to be slow. It's not supprising though. In a set of n elements, there are a set of 2^n  - 1
Based on this, there are at least 2^n base cases to evaluate. 2^n grows very fast and the problem really grows quickly. 

For optimization, I think the best way is to think about whether a variant of pruning could work. Based on the problem at hand, if smaller set meets the cut, the larger set may but if smaller set does not meet the cut, larger set will not work. I don't have to construct larger sets if the smaller set does not satisfy the condition. Depending on the exact conditions (which are the variables), there may be only a small number of possibilities.I'll see if this speed up, but I will probably need some additional optimizations. 

I'm thinking of trying to take the "best" choice at every step with a hint of randomness by also trying a few random elements. I think it's a variation of the finding a global minimum type algorithm. From my understanding, one of the ways to find minimum is by following the gradient, which only finds (possibly) a local minimum. In these algorithms, where you start is very important since you might be trapped in a local minimum and cannot find the global minimum due to this trapping. One of the methods used in such problems is to start the problem in many different "starting values". So for example, if you had the following function, if you start at the right hand side, you're going to find a local minimum, while if you start at the left hand side, you will find the global minima. If you start 50 different values between -5 to +5 increments, probably you will find the global minima. In this method though, you will obviously need to compute 50 times... which will take 50x more time.

One of the issues with approximations is that you compromise accuracy... But it stays practical and feasible. That's what's needed in science. In nature, I'm more of a pure scientist (meaning that I just enjoy the science because it's interesting rather than the practical benefits that come from it). But after working on "real" projects, I've realized that the world doesn't want pure science. They want results that are profitable or in some way useful. At first I was a little disappointed because to me science is like art - it's about making brilliant logic or experimental design to probe the world. At the end though, I think I've gotten kind of used to this need for practicality. 

No comments:

Post a Comment