I am trying to do a homework in math which is find a subset of collection {1,2,..,n} where n is a number given in the code, I cannot get it done with the sublist so I need to get your help with a math programming.
For example for n = 2:
[12]
[1][2]
It have 2 elements.
For example for n = 3:
[1][2][3]
[12][3]
[13][2]
[23][1]
[123]
It have 5 elements.
It have five elements.
For n = 4:
[1][2][3][4]
[12][3][4]
[13][2][4]
[14][2][3]
[23][1][4]
[24][1][3]
[34][1][2]
[12][34]
[13][24]
[14][23]
[123][4]
[124][3]
[134][2]
[234][1]
[1234]
It have 15 elements.
Do you have any ideas how to get it done?
I have tried many possibilites with LinkedHashSet and sublist inside loop but I had no clue for over 2h how to get it done, the output like this.
Is there any library in the Java to get this output? I would do that by manuall way, but there ahve to be different way.
import org.paukov.combinatorics.*;
import org.paukov.combinatorics.util.ComplexCombinationGenerator;
public class Main {
public static void main(String args[]){
// create a vector (A, B, B, C)
ICombinatoricsVector<String> vector = Factory.createVector(new String[] { "1", "2", "3"});
// Create a complex-combination generator
Generator<ICombinatoricsVector<String>> gen = new ComplexCombinationGenerator<String>(vector, 1);
Generator<ICombinatoricsVector<String>> gen2 = new ComplexCombinationGenerator<String>(vector, 2);
Generator<ICombinatoricsVector<String>> gen3 = new ComplexCombinationGenerator<String>(vector, 3);
// Iterate the combinations
for (ICombinatoricsVector<ICombinatoricsVector<String>> comb : gen) {
System.out.println(ComplexCombinationGenerator.convert2String(comb) + " - " + comb);
}
for (ICombinatoricsVector<ICombinatoricsVector<String>> comb : gen2) {
System.out.println(ComplexCombinationGenerator.convert2String(comb) + " - " + comb);
}
for (ICombinatoricsVector<ICombinatoricsVector<String>> comb : gen3) {
System.out.println(ComplexCombinationGenerator.convert2String(comb) + " - " + comb);
}
}
}
([1, 2, 3]) - CombinatoricsVector=([CombinatoricsVector=([1, 2, 3], size=3)], size=1)
([1],[2, 3]) - CombinatoricsVector=([CombinatoricsVector=([1], size=1), CombinatoricsVector=([2, 3], size=2)], size=2)
([2, 3],[1]) - CombinatoricsVector=([CombinatoricsVector=([2, 3], size=2), CombinatoricsVector=([1], size=1)], size=2)
([2],[1, 3]) - CombinatoricsVector=([CombinatoricsVector=([2], size=1), CombinatoricsVector=([1, 3], size=2)], size=2)
([1, 3],[2]) - CombinatoricsVector=([CombinatoricsVector=([1, 3], size=2), CombinatoricsVector=([2], size=1)], size=2)
([1, 2],[3]) - CombinatoricsVector=([CombinatoricsVector=([1, 2], size=2), CombinatoricsVector=([3], size=1)], size=2)
([3],[1, 2]) - CombinatoricsVector=([CombinatoricsVector=([3], size=1), CombinatoricsVector=([1, 2], size=2)], size=2)
([1],[2],[3]) - CombinatoricsVector=([CombinatoricsVector=([1], size=1), CombinatoricsVector=([2], size=1), CombinatoricsVector=([3], size=1)], size=3)
([1],[3],[2]) - CombinatoricsVector=([CombinatoricsVector=([1], size=1), CombinatoricsVector=([3], size=1), CombinatoricsVector=([2], size=1)], size=3)
([3],[1],[2]) - CombinatoricsVector=([CombinatoricsVector=([3], size=1), CombinatoricsVector=([1], size=1), CombinatoricsVector=([2], size=1)], size=3)
([3],[2],[1]) - CombinatoricsVector=([CombinatoricsVector=([3], size=1), CombinatoricsVector=([2], size=1), CombinatoricsVector=([1], size=1)], size=3)
([2],[3],[1]) - CombinatoricsVector=([CombinatoricsVector=([2], size=1), CombinatoricsVector=([3], size=1), CombinatoricsVector=([1], size=1)], size=3)
([2],[1],[3]) - CombinatoricsVector=([CombinatoricsVector=([2], size=1), CombinatoricsVector=([1], size=1), CombinatoricsVector=([3], size=1)], size=3)
This is combinatorics. See more information about the structure you need to understand, i.e. a permutation without repetition, also called a combination.
You might be interested in combinatoricslib, a Java library on Google Code, which you could use for your program.
You could also try to solve it without a library. That should not be too difficult. You'd need to use recursion I think.
I tested with List partitions:
And the result was
I think that is what you were looking for. It needs some tweaking though (maybe ordering and adding the initial list elements like
[1],[2],[3],[4]
).