我正在寻找一种算法,将不同大小的项目列表拆分为“N”个大小相似的组。
具体来说,我正在使用 C# 开发一个 ASP.NET 站点,其中有一个(数据库检索的)字符串列表。字符串的长度各不相同。我有一组需要显示字符串的列。我需要一种算法来找到最平衡的集合(项目顺序无关),以允许最终的列尽可能平衡。
抽象示例:
创建 3 列。
要分发的项目:
- Item A - height 5
- Item B - height 3
- Item C - height 7
- Item D - height 2
- Item E - height 3
期望的输出:
Column 1: Item A, Item D
Column 2: Item C
Column 3: Item B, Item E
I'm seeking an algorithm to split a list of items of varying sizes into "N" number of similarly-sized groups.
Specifically, I'm working on an ASP.NET site in C# where I have a (database-retrieved) list of strings. The strings are of varying lengths. I have a set of columns which need to display the strings. I need an algorithm that will find the most balanced sets (item order is irrelevant) to allow the final columns to be as balanced as possible.
Abstracted Example:
Creating 3 columns.
Items to distribute:
- Item A - height 5
- Item B - height 3
- Item C - height 7
- Item D - height 2
- Item E - height 3
Desired output:
Column 1: Item A, Item D
Column 2: Item C
Column 3: Item B, Item E
发布评论
评论(7)
最快的方法可能是将每个新项目插入到最小的列表中(其中“最小”是列表中所有项目的大小之和)。
The quickest thing to do is probably just insert each new item into the smallest list (where "smallest" is the sum of the sizes of all the items in the list).
这似乎是包装盒(或装箱)问题的一个变体,您尝试将可变大小的物品集合放入尽可能少的容器中:
http://en.wikipedia.org/wiki/Bin_packing_problem
根据你的项目集的大小,你可能可以相当简单地暴力破解一个解决方案,看看用于尺寸差异最小的组合。对于较大的集合,这成为一个相当困难的问题,并且您可能会更好地使用“简单”算法,让您接近一个好的答案。
This seems like a variant of the Packing Boxes (or Bin Packing) problem, which is where you try to fit a collection of variable sized items into as few containers as possible:
http://en.wikipedia.org/wiki/Bin_packing_problem
Depending on the size of your set of items, you could probably brute force a solution fairly simply, looking for the combination with the smallest difference between sizes. For larger sets this becomes quite a difficult problem, and you might be better with a "simple" algorithm that gets you somewhere close to a good answer.
看看作业车间调度算法,其中我们有许多不同大小的作业分散在机器上,使总生产时间最短。
Have a look at job shop scheduling algorithms where we have a number of jobs of varying sizes to be distrubted over machines so that the total production time is minimal.
这是另一个版本,可以创建所需长度的组。
Here's the other version which can create groups of desired length.
尝试一些非常非常基本的
方法如果您需要按两个元素进行分组,则可以使用此方法。您可以将其更改为对元素进行分组,直到达到预定义值(例如 10)。也许我会将其他版本发布到。
Try something very very basic
This method can be used in case you need to group by two elements. You can change it to group elements till a predefined value is reached (e.g. 10). Probably I'll post the other version to.
如果您有两列,这听起来像是分区问题的应用。该问题是 NP 完全问题,但存在伪多项式时间的动态规划解决方案。
http://en.wikipedia.org/wiki/Partition_problem
如果您将列数增加到超过二、则不存在伪多项式时间解。
http://en.wikipedia.org/wiki/3-partition_problem
If you have two columns, this sounds like an application of the Partition Problem. The problem is NP-complete, but there is a dynamic programming solution that is pseudo-polynomial time.
http://en.wikipedia.org/wiki/Partition_problem
If you increase the number of columns beyond two, then there is no pseudo-polynomial time solution.
http://en.wikipedia.org/wiki/3-partition_problem
这是实现接受的答案的通用代码:
首先对项目进行排序(从大到小),这将改善很多
Here is the generic code that implement the accepted answer:
sort the items first (from big to small), that will improve a lot