子集树应用篇
对于需要从一些中找子集的问题都可以用子集树解决
为高效快速找到我们需要的子集我们需要对子集树进行剪枝操作
*题目1:*有一组整数,请选择一部分整数,让选择的和剩下的整数,他们的和的差最小
public class childTree04 {
static int[] arr = {12,52,60,13,32,28,38};
static int[] x = new int[arr.length];
static int[] bestx = new int[arr.length];
static int r = 0;//元素总和
static int min = Integer.MAX_VALUE;//最下差值
public static void main(String[] args) {
for (int i = 0; i < arr.length; i++) {
r += arr[i];
}
backstrace(arr, 0);
System.out.println("min:" + min);
System.out.println(Arrays.toString(bestx));
/*for (int i = 0; i < bestx.length; i++) {
if(bestx[i] == 1){
System.out.println(arr[i]);
}
}*/
}
private static void backstrace(int[] arr, int i) {
if(i == arr.length){
int sum = 0;
for (int j = 0; j < arr.length; j++) {
if(x[j] == 1){
sum += arr[j];
}
}
// sum
int ret = Math.abs(sum - (r-sum));
if(ret < min){
min = ret;
for (int j = 0; j < x.length; j++) {
bestx[j] = x[j];
}
}
} else {
x[i] = 1;
backstrace(arr, i+1);
x[i] = 0;
backstrace(arr, i+1);
}
}
}
一组2n个整数序列,选择其中n个整数,和序列中剩下的n个整数和的差值最小。
import java.util.Arrays;
public class childTree02 {
static int[] arr = {1,2,3,4};
static int[] x = new int[arr.length];
static int[] bestx = new int[arr.length];
static int r = 0; // 没有选择的整数的和
static int min = Integer.MAX_VALUE;
static int choice = 0;//选择的次数
public static void main(String[] args) {
for (int i = 0; i < arr.length; i++) {
r += arr[i];
}
backstrace(arr, 0);
System.out.println("min:" + min);
System.out.println(Arrays.toString(bestx));
/*for (int i = 0; i < bestx.length; i++) {
if(bestx[i] == 1){
System.out.println(arr[i]);
}
}*/
}
private static void backstrace(int[] arr, int i) {
if(i == arr.length){
if(choice != arr.length/2){
return;
}
int sum = 0;
for (int j = 0; j < arr.length; j++) {
if(x[j] == 1){
sum += arr[j];
}
}
// sum
int ret = Math.abs(sum - r);
if(ret < min){
min = ret;
for (int j = 0; j < x.length; j++) {
bestx[j] = x[j];
}
}
} else {
if(choice < arr.length/2){ // 剪左子树,当前的左边不满足则减去左边arr[i],
choice += 1;
r -= arr[i];
x[i] = 1;
backstrace(arr, i+1);
r += arr[i];
choice -= 1;
}
if(choice < arr.length/2){ // 剪右子树
x[i] = 0;
backstrace(arr, i+1);
}
}
}
}
下面以轮船装载问题来体会剪枝的高效性
题目:有两艘轮船c1,c2,分别可装载40t,50t,一组质量为w1,w2,w3…的东西装进两所轮船,如何装载最合理?(最好先将装载多的那组装满,剩余装进下一组)
import java.util.Arrays;
/*
* 重量:w1,w2, ......wn
* 两艘轮船容量c1,c2
* w1+w2+..........+wn<=c1+c2
*
* */
public class 轮船装载问题 {
static int[]w={12,18,21,14,9,10};
static int c1=50;
static int c2=34;
static int[]x=new int[w.length];//标记状态
static int []bestx=new int[w.length];//装载的分配数组
static int bestx1=Integer.MIN_VALUE;//最优的结果
static int c1w=0;
static int r=0;
public static void main(String[] args) {
for(int i=0;i<w.length;i++){
r+=w[i];
}
jude(0);
//整个装载情况打印
System.out.println(Arrays.toString(bestx));
//c1装载的情况打印
System.out.println("c1"+" "+bestx1);
for(int i=0;i<bestx.length;i++){
if(bestx[i]==1){
System.out.print(w[i]+" ");
}
}
System.out.println();
//c2装载的情况打印
System.out.println("c2"+" "+(c1+c2-(c1-bestx1)-bestx1));
for(int i=0;i<bestx.length;i++){
if(bestx[i]==0){
System.out.print(w[i]+" ");
}
}
}
public static void jude(int i){
if(i==w.length){
if(bestx1<c1w){
bestx1=c1w;
for(int k=0;k<x.length;k++){
bestx[k]=x[k];
}
}
}else {
r-=w[i];
if(c1w+w[i]<=c1){
c1w+=w[i];
x[i] =1;
jude(i+1);
c1w-=w[i];
}
if(c1w+r>bestx1){
x[i]=0;
jude(i+1);
}
r+=w[i];
}
}
}
0-1背包问题
问题:给定N个物品和一个背包。物品i的重量是Wi,其价值位Vi ,背包的容量为C。问应该如何选择装入背包的物品,使得放入背包的物品的总价值为最大?
分析:在不超过背包重量的前提下使得价值达到最大,显然,放入背包的物品,是N个物品的所有子集的其中之一。N个物品中每一个物品,都有选择、不选择两种状态。因此,只需要对每一个物品的这两种状态进行遍历。
代码如下:
public class childTree03 {
static int[] w = {5, 8, 7};//各个物品的重量
static int[] v = {12, 9, 13};//物品所对应的价值
static int c = 18;//背包中重量
static int choiceW = 0;//选择物品重量
static int choiceV = 0;//选择物品价值
static int[] x = new int[w.length];//用于记录选择和不选择的值
static int[] bestx = new int[w.length];//选择的重量
static int bestvalue = Integer.MIN_VALUE;//背包的最大价值
static int r = 0;//总价值
public static void main(String[] args) {
for (int i = 0; i < w.length; i++) {
r += v[i];
}
getvaluemax(0);
System.out.println("bestxvalue:"+bestvalue);
System.out.println(Arrays.toString(bestx));
}
public static void getvaluemax(int i) {
if (i == w.length) {
// for (int k = 0; k < w.length; k++) {
// if (x[k] == 1) {
// System.out.println(x[k] + " ");
// }
if (choiceV > bestvalue) {
bestvalue = choiceV;
for (int k = 0; k < bestx.length; k++) {
bestx[k] = x[k];
}
}
} else {
r -= v[i];//v[i]:除去v[i]之外它的左子树上元素和
//当前的重量加上之前的重量
if (choiceW + w[i] <= c) {
choiceW += w[i];
choiceV += v[i];
x[i] = 1;
getvaluemax(i + 1);
//从递归函数中出来说明元素未被放入则要减去
choiceW -= w[i];
choiceV -= v[i];
}
//当前除了右孩树,它的孩子的总价值加上
if (choiceV + r> bestvalue) {//r:此时不包含i的价值,出当前结点外的做还
x[i] = 0;
getvaluemax(i + 1);
}
//回溯时要加上
r+=v[i];
}
}
}
结果为:
"C:\Program Files\Java\jdk1.8.0_121\bin\java.exe" -javaagent:C:\IDEA\lib\idea_rt.jar=32632:C:\IDEA\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;C:\IDEAproject\HellloWorld\out\production\HellloWorld" 数据结构之子集树.childTree03
bestxvalue:25
[1, 0, 1]
Process finished with exit code 0