I want to find all the integer subsets that sum n via backtracking
For example for the integers:
1 2 3 4 5 6 7
and n = 7
I want to ouput
1 2 4
1 6
2 5
3 4
7
I think that I should pass the position in the integer array that I'm evaluating as argument, but I'm stuck writing the rest of th开发者_StackOverflow中文版e logic.
My code so far:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
/**
*
* @author talleres
*/
public class Main {
int sum (TreeSet<Integer>ts, int temp) {
int sum=0;
for (Integer i: ts){
sum +=i;
}
return sum+temp;
}
static HashSet<TreeSet<Integer>> alternatives = new HashSet <TreeSet<Integer>>();
static ArrayList<TreeSet<Integer>> subsets = new ArrayList <TreeSet<Integer>>();
static TreeSet<Integer> getNextSubset (){
TreeSet<Integer> alternative = new TreeSet<Integer>();
if (!alternatives.contains(alternative)){
return alternative;
}
else return null; // BEWARE!!
}
static void findSubsets (ArrayList<Integer> numbers, int amount, int index){
TreeSet <Integer> subset = new TreeSet<Integer>();
int temp = numbers.get(index); //initialize alternative
if (temp<=amount)
subset.add(temp);
if (temp==amount)
subsets.add(subset);
}
public static void main(String[] args) throws IOException {
// TODO code application logic here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("inset integers");
ArrayList<Integer> numeros = new ArrayList<Integer>();
String line=br.readLine();
while (!line.equals("")){
numeros.add (Integer.parseInt(line));
line = br.readLine();
}
Collections.sort(numeros);
System.out.println("insert the amount the subsets should sum");
line = br.readLine();
int amount = Integer.parseInt(line);
ArrayList<Integer> accum = new ArrayList<Integer>();
findSubsets(numeros, amount, 0);
}
}
Here's some pseudo code for you to work with:
Set<Set<Integer>> subsets(Set<Integer> remaining, int n) {
results = new HashSet<Set<Integer>>();
if (n == 0)
results.add(empty set);
for each i in remaining
newRemaining = remaining \ {i}
for each subresult in subsets(newRemaining, n - i)
results.add(subresult + {i})
return results
}
Should work for negative numbers as well. (uhm, actually will work. I implemented it and tested it before writing the pseudo code :-)
I might be tempted to do this in a recursive function. It feels straightforward. It might not be the best, but it will work well.
This is very much in pseudo-code and assumes the numbers are 1..END. If you are given a list, sorting and then using list[i] would be appropriate.
find(int curpos,int cursum,int sumleft,char output[])
{
if (sumleft == 0)
print(output);
if (curpos > sumleft)
return;
for(i=curpos;i<=TARGET && i<=sumleft)
find(i+1,cursum+i,sumleft-i,output+i."+%d")
}
main()
{
char output[100];
find(1,0,TARGET,"");
}
精彩评论