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

1.题目

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

2.分析

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

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

  • threshold,组合数量阈值。

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

  • total_calculation_threshold,总计算量阈值。

3.分布式

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

4.效果说明

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

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

5.示例

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * 
 * @author seaboat
 * @date 2018-12-08
 * @version 1.0
 * <pre><b>email: </b>849586227@qq.com</pre>
 * <pre><b>blog: </b>http://blog.csdn.net/wangyangzhizhou</pre>
 * <p>集合A有n(万级别)个子集,每个子集有m(万级别)个元素,元素都为正负数,找出任意相加为0的元素,将他们消掉,要求每次消掉的都是最大正数,且尽可能多的负元素。</p>
 */
public class SetSum {

	private static boolean flag[];
	private static int length = 0;
	private static List<Integer> longestArrays = new ArrayList<Integer>();
	private static int longestSize = 0;
	private static boolean isGet = false;
	private static long counter = 0;
	private static long total_counter = 0;
	private static long threshold = 200;
	private static long calculation_threshold = 100000000;
	private static long total_calculation_threshold = 100000000000L;

	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		Random r = new Random();
		System.out.println("start time : " + start);
		int[] po_array = { 1000, 2000, 1500, 1200, 1800, 1100, 1300, 1400, 1600, 1700 };
		int[] ne_array = new int[500];
		for (int i = 0; i < ne_array.length; i++)
			ne_array[i] = -r.nextInt(100);
		//1、abs 
		for (int i = 0; i < ne_array.length; i++)
			ne_array[i] = -ne_array[i];
		//2、sort
		Arrays.sort(po_array);
		Arrays.sort(ne_array);

		for (int i = po_array.length - 1; i > 0; i--) {
			flag = new boolean[ne_array.length];
			isGet = false;
			length = 0;
			longestSize = 0;
			counter = 0;
			longestArrays.clear();
			doSum(ne_array, 0, po_array[i]);

			//not enough data, we break it directly.
			if (longestArrays.size() == 0)
				break;

			System.out.println(longestArrays);

			//clear the elements we have got.
			for (int value : longestArrays)
				ne_array = remove(ne_array, value);
			Arrays.sort(ne_array);
		}
		System.out.println("end time : " + System.currentTimeMillis());
		System.out.println("counter :" + total_counter);
		System.out.println("elapsed time : " + (System.currentTimeMillis() - start) + "ms");
	}

	static void doSum(int[] array, int index, int sum) {
		if (counter > calculation_threshold)
			return;
		if (total_counter > total_calculation_threshold)
			return;
		counter++;
		total_counter++;
		if (isGet)
			return;
		if (sum == 0) {
			if (longestSize < length) {
				longestArrays.clear();
				longestSize = length;
				for (int i = 0; i < array.length; ++i) {
					if (flag[i])
						longestArrays.add(array[i]);
				}
			}
			if (length > threshold) {
				System.out.println("we have got a satisfying answer!");
				isGet = true;
				return;
			}
		} else {
			if (index == array.length)
				return;
			else {
				flag[index] = true;
				length++;
				if (sum - array[index] >= 0)
					doSum(array, index + 1, sum - array[index]);
				flag[index] = false;
				length--;
				if (sum >= 0)
					doSum(array, index + 1, sum);
			}
		}
	}

	private static int[] remove(int[] arr, int num) {
		int[] tmp = new int[arr.length - 1];
		int idx = 0;
		boolean hasRemove = false;
		for (int i = 0; i < arr.length; i++) {
			if (!hasRemove && arr[i] == num) {
				hasRemove = true;
				continue;
			}
			tmp[idx++] = arr[i];
		}
		return tmp;
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值