将 n 个可变高度图像拟合为 3 个(相似长度)列布局
我希望制作一个类似于 piccsy.com 的 3 列布局。给定许多宽度相同但高度不同的图像,有什么算法可以对它们进行排序以使列长度的差异最小?最好使用 Python 或 JavaScript...
非常感谢您提前提供的帮助!
马丁
I'm looking to make a 3-column layout similar to that of piccsy.com. Given a number of images of the same width but varying height, what is a algorithm to order them so that the difference in column lengths is minimal? Ideally in Python or JavaScript...
Thanks a lot for your help in advance!
Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有多少图像?
如果限制最大页面大小,并具有最小图片高度的值,则可以计算每页的最大图像数。在评估任何解决方案时您都需要这个。
我认为您提供的链接上有 27 张图片。
下面使用 Robin Green 之前提到的first_fit 算法,然后通过贪婪交换对此进行改进。
交换例程找到距离平均列高最远的列,然后系统地寻找其中一张图片与另一列中的第一张图片之间的交换,以最小化与平均值的最大偏差。
我使用了 30 张图片的随机样本,高度在 5 到 50 个“单位”范围内。在我的例子中,收敛速度很快,并且比first_fit算法有了显着的改进。
代码(Python 3.2:
输出:
好问题。
这是我在下面的单独评论中提到的反向排序的信息。
如果您有反向排序,这个额外的代码会附加到上面代码的底部(在“if”中) name == ...),将对随机数据进行额外的试验:
额外的输出显示了交换的效果:
How many images?
If you limit the maximum page size, and have a value for the minimum picture height, you can calculate the maximum number of images per page. You would need this when evaluating any solution.
I think there were 27 pictures on the link you gave.
The following uses the first_fit algorithm mentioned by Robin Green earlier but then improves on this by greedy swapping.
The swapping routine finds the column that is furthest away from the average column height then systematically looks for a swap between one of its pictures and the first picture in another column that minimizes the maximum deviation from the average.
I used a random sample of 30 pictures with heights in the range five to 50 'units'. The convergenge was swift in my case and improved significantly on the first_fit algorithm.
The code (Python 3.2:
The output:
Nice problem.
Heres the info on reverse-sorting mentioned in my separate comment below.
If you have the reverse-sorting, this extra code appended to the bottom of the above code (in the 'if name == ...), will do extra trials on random data:
The extra output shows the effect of the swaps:
这就是离线完工时间最小化问题,我认为这相当于多处理器调度问题。您拥有图像而不是作业,并且您拥有图像高度而不是作业持续时间,但这是完全相同的问题。 (它涉及空间而不是时间这一事实并不重要。)因此任何(大约)解决它们中任何一个的算法都可以。
This is the offline makespan minimisation problem, which I think is equivalent to the multiprocessor scheduling problem. Instead of jobs you have images, and instead of job durations you have image heights, but it's exactly the same problem. (The fact that it involves space instead of time doesn't matter.) So any algorithm that (approximately) solves either of them will do.
这是一个算法(称为首次拟合递减),它将以合理的方式为您提供非常紧凑的安排时间量。可能有更好的算法,但这非常简单。
(如果多列的高度相同(且最短),请选择任意一列。)
完成后,如果您不喜欢从高到低的外观,则可以根据自己的选择重新排列每列中的元素。
Here's an algorithm (called First Fit Decreasing) that will get you a very compact arrangement, in a reasonable amount of time. There may be a better algorithm but this is ridiculously simple.
(If multiple columns are the same height (and shortest) pick any one.)
When you're done, you can re-arrange the elements in the each column however you choose if you don't like the tallest-to-shortest look.
这是一个:
Here's one: