开发者

Finding optimal elements from an array in python

I need to devise an algorithm that finds me the most optimal elements from a list in python. I have a cd that holds 700mb. And an array of 300 randomly generated file sizes varying from 30 - 90 mb. It needs to fill the cd the most optimal way, t开发者_运维技巧hat minimum room is wasted ( looking through all possible ways) I guess it's similar to the knapsack problem, only that it has only 1 array and a limit. Since I'm totally new to the algorithm and datastructure scene, I have no idea how to implement this using python

Thanks in advance


As @payne points out in his comment, this is really the same as the knapsack problem. The solution is therefore a simple dynamic programming algorithm.

Say the files are arranged one after another in some order in a list. At first, you have the choice of either choosing to include the first file or skipping it. If you choose to include it, the space you have available will decrease by the size of that file. If you choose to skip it, the space available remains unchanged. Now, you can arrive to the second file in two states. In one, you have chosen the first file and thus have less space, while in the other you have skipped the first file and have more space. For each of these scenarios, you can again choose to include or skip over the second file.

Notice you can define your state simply by the file which you are considering at the moment and the available space that you have. Once you have moved past the last file or the space has run out, you have come to the end of that line of choices.

This yields a simple recurrence:

min_waste(index,space)={
   o if space=0     # no more space available, so 0 wastage

   space if index>=size(files) # no more files left, whatever is left is wasted

   min_waste(index+1,space)  if size(files[index])>space  # current file is too large skip ahead

   min( min_waste(index+1,space), min_waste(index+1,space-size(files[index])) ) otherwise
   # minimum of choosing this one and skipping ahead 
}

You can choose to implement this by filling up a table (i.e. 2D array) bottom up, or just write this up as a recursive function and memoize.

This gives you the minimum wastage, but not which files were selected to achieve it. But you can easily modify it to save information about the choice it makes in each state and use that to build up the series of choices from the starting state.


It is probably not efficient to find the absolute most optimal way. But you can use some rules of thumb, like take the largest files first, and then fill the remaining space with the first file that fits until the space is too small for any more to fit. See Bin Packing Problem. The optimal simple algorithm is First Fit Decreasing. Sort all the files by size from largest to smallest. Then place each file on the first cd where there is enough room for it to fit, until all the files are used up.

Edit

It is likely that all of the files put together do not exactly equal some number of cd's. For instance, if the total of the files is 1.6GB, that's two cd's with a little left over, even if they packed perfectly. So if you already know that 3 cd's are the minimum required, and you try a few combinations until you get it fitting on 3 cd's, why does it need to be optimized any more than that? You can't save any more cd's than the theoretical minimum.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜