Skip to content
Snippets Groups Projects
Commit 78d4de58 authored by Thiemo Belmega's avatar Thiemo Belmega
Browse files

Implement util to get all subsets of a given size out of a set

parent f12340e7
No related branches found
No related tags found
No related merge requests found
package de.unipotsdam.cs.toolup.util;
import java.util.*;
/**
* Created by Thiemo on 20.12.2015.
*/
public class SubsetUtil {
public static <T> Set<Set<T>> getAllSubsetsOfSize(Set<T> superSet, int size) {
List<T> superList = new ArrayList<>(superSet);
Set<Set<T>> setOfSubSets = new HashSet<>();
for (int i = 0; i < superList.size(); i++) {
T currentElement = superList.get(i);
if (size == 1) {
List<T> newSubList = Arrays.asList(currentElement);
setOfSubSets.add(new HashSet<>(newSubList));
} else {
setOfSubSets.addAll(createAllSubSetsWithCurrentElementRecursively(size, superList, currentElement));
}
}
return setOfSubSets;
}
/**
* Recursive method.
* <p>
* Example:
* If current element is "1" and superList is "1","2","3","4" and we are looking for sub sets of size 2;
* <p>
* - The method gets all sub sets of size 1 of the list "2","3","4", which are the sub sets {"2"}, {"3"} and {"4"}
* - Then the method adds the current element to each of these sub sets, which results
* in the sets {"1","2"},{"1","3"} and {"1","4"}
* - The method returns a set of all these created sub sets
*/
private static <T> Set<Set<T>> createAllSubSetsWithCurrentElementRecursively(int size, List<T> superList, T currentElement) {
Set<Set<T>> setOfSubSetsWithoutCurrentElement = getAllSmallerSubsetsWithoutCurrentElement(size, superList, currentElement);
Set<Set<T>> resultSetOfSubSets = new HashSet<>();
for (Set<T> subSetWithoutCurrentElement : setOfSubSetsWithoutCurrentElement) {
subSetWithoutCurrentElement.add(currentElement);
resultSetOfSubSets.add(subSetWithoutCurrentElement);
}
return resultSetOfSubSets;
}
private static <T> Set<Set<T>> getAllSmallerSubsetsWithoutCurrentElement(int size, List<T> superList, T currentElement) {
Set<T> setWithoutCurrentElement = new HashSet(superList);
setWithoutCurrentElement.remove(currentElement);
return getAllSubsetsOfSize(setWithoutCurrentElement, size - 1);
}
}
package de.unipotsdam.cs.toolup.util;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static junit.framework.Assert.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
@SuppressWarnings("ALL")
public class SubsetUtilTest {
List<String> superList = Arrays.asList("1", "2", "4", "3");
Set<String> superSet = new HashSet<>(superList);
@Test
public void testThatSubsetUtilReturnsAllSubSetsOfSize4() {
//arrange
//act
Set<Set<String>> subSets = SubsetUtil.getAllSubsetsOfSize(superSet, 4);
//assert
assertTrue(subSets.contains(superSet));
}
@Test
public void testThatSubsetUtilReturnsAllSubSetsOfSize3() {
//arrange
List<String> subList1 = Arrays.asList("1", "2", "4");
List<String> subList2 = Arrays.asList("1", "2", "3");
List<String> subList3 = Arrays.asList("1", "4", "3");
List<String> subList4 = Arrays.asList("3", "2", "4");
//act
Set<Set<String>> subSets = SubsetUtil.getAllSubsetsOfSize(superSet, 3);
//assert
assertTrue(subSets.contains(new HashSet(subList1)));
assertTrue(subSets.contains(new HashSet(subList2)));
assertTrue(subSets.contains(new HashSet(subList3)));
assertTrue(subSets.contains(new HashSet(subList4)));
}
@Test
public void testThatSubsetUtilReturnsAllSubSetsOfSize2() {
//arrange
//act
Set<Set<String>> subSets = SubsetUtil.getAllSubsetsOfSize(superSet, 2);
//assert
assertEquals(6, subSets.size());
}
@Test
public void testThatSubsetUtilReturnsAllSubSetsOfSize1() {
//arrange
List<String> subList1 = Arrays.asList("1");
List<String> subList2 = Arrays.asList("2");
List<String> subList3 = Arrays.asList("3");
List<String> subList4 = Arrays.asList("4");
//act
Set<Set<String>> subSets = SubsetUtil.getAllSubsetsOfSize(superSet, 1);
//assert
assertTrue(subSets.contains(new HashSet(subList1)));
assertTrue(subSets.contains(new HashSet(subList2)));
assertTrue(subSets.contains(new HashSet(subList3)));
assertTrue(subSets.contains(new HashSet(subList4)));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment