阅读 1305

一道算法题(回溯+剪枝)

题目

假设集合A有n个子集,每个子集有m个元素,元素都为正负数。找出任意相加为0的元素,将他们消掉,要求每次消掉的都是最大正数,且尽可能多的负元素。

分析

穷举行不通,因为穷举的时间复杂度将达到O(2^n),不可能跑完。而贪心算法可以简化问题,但不具备完整的说服力,不过可借鉴它的思想。所以,最终可选择回溯并结合剪枝,这样平均时间复杂度下降很多,但实际计算与真实数据有很大关系。

此外,为进一步解决计算量问题,且防止陷入无穷执行,引入三个阈值因子,可根据实际数据和业务情况调节。

  • threshold,组合数量阈值。

  • calculation_threshold,单个元素计算量阈值。

  • total_calculation_threshold,总计算量阈值。

分布式

任务中没有相互依赖性,天然支持分布式,只需要在取数那里分别取数即可。

效果说明

对数据进行升序排序,每轮进行一亿次(可配置)搜索后,很大很大概率已经能准确找到最想要的答案,时间也能很好控制。


start time : 1544195518997
[0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 22, 22, 22, 22, 23, 23, 24, 24, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 38]
[28, 28, 28, 28, 28, 28, 29, 29, 29, 30, 30, 31, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, 35, 35, 35, 36, 36, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 61]
[39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 44, 44, 44, 44, 44, 46, 46, 46, 46, 46, 47, 47, 49]
[47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48, 50, 50, 50, 50, 50, 51, 51, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 98]
[53, 53, 53, 54, 54, 54, 54, 55, 55, 55, 55, 56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 58, 58, 69, 99]
[58, 59, 59, 59, 59, 59, 59, 60, 60, 60, 61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 63, 63, 66]
[63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 64, 64, 64, 65, 65, 65, 66, 66, 66, 80]
[66, 66, 66, 66, 67, 67, 67, 68, 68, 68, 68, 69, 69, 69, 70, 87, 99]
[71, 71, 71, 71, 71, 71, 71, 71, 72, 72, 72, 73, 73, 73, 97]
end time : 1544195524950
counter :900000009
elapsed time : 5953ms

复制代码

github

github.com/sea-boat/al…

关注下面的标签,发现更多相似文章
评论