Created
June 21, 2014 05:40
-
-
Save gabhi/e88071134878ce71a96d to your computer and use it in GitHub Desktop.
powerset - array combination of r elements from n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
List<List<String>> powerSet = new LinkedList<List<String>>(); | |
for (int i = 1; i <= args.length; i++) | |
powerSet.addAll(combination(Arrays.asList(args), i)); | |
System.out.println(powerSet); | |
} | |
public static <T> List<List<T>> combination(List<T> values, int size) { | |
if (0 == size) { | |
return Collections.singletonList(Collections.<T> emptyList()); | |
} | |
if (values.isEmpty()) { | |
return Collections.emptyList(); | |
} | |
List<List<T>> combination = new LinkedList<List<T>>(); | |
T actual = values.iterator().next(); | |
List<T> subSet = new LinkedList<T>(values); | |
subSet.remove(actual); | |
List<List<T>> subSetCombination = combination(subSet, size - 1); | |
for (List<T> set : subSetCombination) { | |
List<T> newSet = new LinkedList<T>(set); | |
newSet.add(0, actual); | |
combination.add(newSet); | |
} | |
combination.addAll(combination(subSet, size)); | |
return combination; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment