diff --git a/core-java-modules/core-java-lang-math/src/main/java/com/baeldung/powerset/PowerSetUtility.java b/core-java-modules/core-java-lang-math/src/main/java/com/baeldung/powerset/PowerSetUtility.java new file mode 100644 index 0000000000..fd4c1933f6 --- /dev/null +++ b/core-java-modules/core-java-lang-math/src/main/java/com/baeldung/powerset/PowerSetUtility.java @@ -0,0 +1,320 @@ +package com.baeldung.powerset; + +import com.google.common.collect.Sets; + +import javax.annotation.Nullable; +import java.util.AbstractSet; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Set; + +public class PowerSetUtility { + + private Map map = new HashMap<>(); + private List reverseMap = new ArrayList<>(); + + //Lazy Load PowerSet class + private static class PowerSet extends AbstractSet> { + private Map map = new HashMap<>(); + private List reverseMap = new ArrayList<>(); + private Set set; + + public PowerSet(Set set) { + this.set = set; + initializeMap(); + } + + abstract class ListIterator implements Iterator { + + protected int position = 0; + private int size; + + public ListIterator(int size) { + this.size = size; + } + + @Override + public boolean hasNext() { + return position < size; + } + } + + static class Subset extends AbstractSet { + private Map map; + private List reverseMap; + private int mask; + + public Subset(Map map, List reverseMap, int mask) { + this.map = map; + this.reverseMap = reverseMap; + this.mask = mask; + } + + @Override + public Iterator iterator() { + return new Iterator() { + int remainingSetBits = mask; + + @Override + public boolean hasNext() { + return remainingSetBits != 0; + } + + @Override + public E next() { + int index = Integer.numberOfTrailingZeros(remainingSetBits); + if (index == 32) { + throw new NoSuchElementException(); + } + remainingSetBits &= ~(1 << index); + return reverseMap.get(index); + } + }; + } + + @Override + public int size() { + return Integer.bitCount(mask); + } + + @Override + public boolean contains(@Nullable Object o) { + Integer index = map.get(o); + return index != null && (mask & (1 << index)) != 0; + } + } + + @Override + public Iterator> iterator() { + return new ListIterator>(this.size()) { + @Override + public Set next() { + return new Subset<>(map, reverseMap, position++); + } + }; + } + + @Override + public int size() { + return (1 << this.set.size()); + } + + @Override + public boolean contains(@Nullable Object obj) { + if (obj instanceof Set) { + Set set = (Set) obj; + return reverseMap.containsAll(set); + } + return false; + } + + @Override + public boolean equals(@Nullable Object obj) { + if (obj instanceof PowerSet) { + PowerSet that = (PowerSet) obj; + return set.equals(that.set);//Set equals check to have the same element regardless of the order of the items + } + return super.equals(obj); + } + + + private void initializeMap() { + int mapId = 0; + for (E c : this.set) { + map.put(c, mapId++); + reverseMap.add(c); + } + } + } + + public Set> lazyLoadPowerSet(Set set) { + return new PowerSet<>(set); + } + + public Set> recursivePowerSet(Set set) { + if (set.isEmpty()) { + Set> ret = new HashSet<>(); + ret.add(set); + return ret; + } + + T element = set.iterator().next(); + Set subSetWithoutElement = getSubSetWithoutElement(set, element); + Set> powerSetSubSetWithoutElement = recursivePowerSet(subSetWithoutElement); + + Set> powerSetSubSetWithElement = addElementToAll(powerSetSubSetWithoutElement, element); + + Set> powerSet = new HashSet<>(); + powerSet.addAll(powerSetSubSetWithoutElement); + powerSet.addAll(powerSetSubSetWithElement); + return powerSet; + } + + public Set> recursivePowerSetIndexRepresentation(Collection set) { + initializeMap(set); + Set> powerSetIndices = recursivePowerSetIndexRepresentation(0, set.size()); + return unMapIndex(powerSetIndices); + } + + private List> iterativePowerSetByLoopOverNumbersWithReverseLexicographicalOrder(int n) { + List> powerSet = new ArrayList<>(); + for (int i = 0; i < (1 << n); i++) { + List subset = new ArrayList<>(n); + for (int j = 0; j < n; j++) + subset.add(((1 << j) & i) > 0); + powerSet.add(subset); + } + return powerSet; + } + + private List> iterativePowerSetByLoopOverNumbersWithGrayCodeOrder(int n) { + List> powerSet = new ArrayList<>(); + for (int i = 0; i < (1 << n); i++) { + List subset = new ArrayList<>(n); + for (int j = 0; j < n; j++) { + int grayEquivalent = i ^ (i >> 1); + subset.add(((1 << j) & grayEquivalent) > 0); + } + powerSet.add(subset); + } + return powerSet; + } + + public Set> recursivePowerSetBinaryRepresentation(Collection set) { + initializeMap(set); + Set> powerSetBoolean = recursivePowerSetBinaryRepresentation(0, set.size()); + return unMapBinary(powerSetBoolean); + } + + public List> iterativePowerSetByLoopOverNumbers(Set set) { + initializeMap(set); + List> sets = iterativePowerSetByLoopOverNumbersWithReverseLexicographicalOrder(set.size()); + return unMapListBinary(sets); + } + + public List> iterativePowerSetByLoopOverNumbersMinimalChange(Set set) { + initializeMap(set); + List> sets = iterativePowerSetByLoopOverNumbersWithGrayCodeOrder(set.size()); + return unMapListBinary(sets); + } + + public static int getRankInLexicographicalOrder(List subset) { + int rank = 0; + for (int i = 0; i < subset.size(); i++) + if (subset.get(i)) + rank += (1 << (subset.size() - i - 1)); + return rank; + } + + public static List getSubsetForRankInLexicographicalOrder(int rank, int sizeOfSet) { + Boolean[] subset = new Boolean[sizeOfSet]; + for(int j = 0; j < sizeOfSet; j++) { + subset[sizeOfSet - j - 1] = ((rank & (1 << j)) > 0); + } + return Arrays.asList(subset); + } + + private Set> recursivePowerSetIndexRepresentation(int idx, int n) { + if (idx == n) { + Set> empty = new HashSet<>(); + empty.add(new HashSet<>()); + return empty; + } + Set> powerSetSubset = recursivePowerSetIndexRepresentation(idx + 1, n); + Set> powerSet = new HashSet<>(powerSetSubset); + for (Set s : powerSetSubset) { + HashSet subSetIdxInclusive = new HashSet<>(s); + subSetIdxInclusive.add(idx); + powerSet.add(subSetIdxInclusive); + } + return powerSet; + } + + private Set> recursivePowerSetBinaryRepresentation(int idx, int n) { + if (idx == n) { + Set> powerSetOfEmptySet = new HashSet<>(); + powerSetOfEmptySet.add(Arrays.asList(new Boolean[n])); + return powerSetOfEmptySet; + } + Set> powerSetSubset = recursivePowerSetBinaryRepresentation(idx + 1, n); + Set> powerSet = new HashSet<>(); + for (List s : powerSetSubset) { + List subSetIdxExclusive = new ArrayList<>(s); + subSetIdxExclusive.set(idx, false); + powerSet.add(subSetIdxExclusive); + List subSetIdxInclusive = new ArrayList<>(s); + subSetIdxInclusive.set(idx, true); + powerSet.add(subSetIdxInclusive); + } + return powerSet; + } + + private void initializeMap(Collection collection) { + int mapId = 0; + for (T c : collection) { + map.put(c, mapId++); + reverseMap.add(c); + } + } + + private Set> unMapIndex(Set> sets) { + Set> ret = new HashSet<>(); + for (Set s : sets) { + HashSet subset = new HashSet<>(); + for (Integer i : s) + subset.add(reverseMap.get(i)); + ret.add(subset); + } + return ret; + } + + private Set> unMapBinary(Collection> sets) { + Set> ret = new HashSet<>(); + for (List s : sets) { + HashSet subset = new HashSet<>(); + for (int i = 0; i < s.size(); i++) + if (s.get(i)) + subset.add(reverseMap.get(i)); + ret.add(subset); + } + return ret; + } + + private List> unMapListBinary(Collection> sets) { + List> ret = new ArrayList<>(); + for (List s : sets) { + List subset = new ArrayList<>(); + for (int i = 0; i < s.size(); i++) + if (s.get(i)) + subset.add(reverseMap.get(i)); + ret.add(subset); + } + return ret; + } + + private Set> addElementToAll(Set> powerSetSubSetWithoutElement, T element) { + Set> powerSetSubSetWithElement = new HashSet<>(); + for (Set subsetWithoutElement : powerSetSubSetWithoutElement) { + Set subsetWithElement = new HashSet<>(subsetWithoutElement); + subsetWithElement.add(element); + powerSetSubSetWithElement.add(subsetWithElement); + } + return powerSetSubSetWithElement; + } + + private Set getSubSetWithoutElement(Set set, T element) { + Set subsetWithoutElement = new HashSet<>(); + for (T s : set) { + if (!s.equals(element)) + subsetWithoutElement.add(s); + } + return subsetWithoutElement; + } +} diff --git a/core-java-modules/core-java-lang-math/src/test/java/com/baeldung/powerset/PowerSetUtilityUnitTest.java b/core-java-modules/core-java-lang-math/src/test/java/com/baeldung/powerset/PowerSetUtilityUnitTest.java new file mode 100644 index 0000000000..0a20c36692 --- /dev/null +++ b/core-java-modules/core-java-lang-math/src/test/java/com/baeldung/powerset/PowerSetUtilityUnitTest.java @@ -0,0 +1,225 @@ +package com.baeldung.powerset; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.hamcrest.collection.IsCollectionWithSize; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +public class PowerSetUtilityUnitTest { + + @Test + public void givenSet_WhenGuavaLibraryGeneratePowerSet_ThenItContainsAllSubsets() { + ImmutableSet set = ImmutableSet.of("APPLE", "ORANGE", "MANGO"); + Set> powerSet = Sets.powerSet(set); + Assertions.assertEquals((1 << set.size()), powerSet.size()); + MatcherAssert.assertThat(powerSet, Matchers.containsInAnyOrder( + ImmutableSet.of(), + ImmutableSet.of("APPLE"), + ImmutableSet.of("ORANGE"), + ImmutableSet.of("APPLE", "ORANGE"), + ImmutableSet.of("MANGO"), + ImmutableSet.of("APPLE", "MANGO"), + ImmutableSet.of("ORANGE", "MANGO"), + ImmutableSet.of("APPLE", "ORANGE", "MANGO") + )); + } + + @Test + public void givenSet_WhenPowerSetIsLazyLoadGenerated_ThenItContainsAllSubsets() { + Set set = RandomSetOfStringGenerator.generateRandomSet(); + Set> powerSet = new PowerSetUtility().lazyLoadPowerSet(set); + + //To make sure that the size of power set is (2 power n) + MatcherAssert.assertThat(powerSet, IsCollectionWithSize.hasSize((1 << set.size()))); + //To make sure that number of occurrence of each element is (2 power n-1) + Map counter = new HashMap<>(); + for (Set subset : powerSet) { + for (String name : subset) { + int num = counter.getOrDefault(name, 0); + counter.put(name, num + 1); + } + } + counter.forEach((k, v) -> Assertions.assertEquals((1 << (set.size() - 1)), v.intValue())); + } + + @Test + public void givenSet_WhenPowerSetIsCalculated_ThenItContainsAllSubsets() { + Set set = RandomSetOfStringGenerator.generateRandomSet(); + + Set> powerSet = new PowerSetUtility().recursivePowerSet(set); + + //To make sure that the size of power set is (2 power n) + MatcherAssert.assertThat(powerSet, IsCollectionWithSize.hasSize((1 << set.size()))); + //To make sure that number of occurrence of each element is (2 power n-1) + Map counter = new HashMap<>(); + for (Set subset : powerSet) { + for (String name : subset) { + int num = counter.getOrDefault(name, 0); + counter.put(name, num + 1); + } + } + counter.forEach((k, v) -> Assertions.assertEquals((1 << (set.size() - 1)), v.intValue())); + } + + @Test + public void givenSet_WhenPowerSetIsCalculatedRecursiveByIndexRepresentation_ThenItContainsAllSubsets() { + Set set = RandomSetOfStringGenerator.generateRandomSet(); + + Set> powerSet = new PowerSetUtility().recursivePowerSetIndexRepresentation(set); + + //To make sure that the size of power set is (2 power n) + MatcherAssert.assertThat(powerSet, IsCollectionWithSize.hasSize((1 << set.size()))); + //To make sure that number of occurrence of each element is (2 power n-1) + Map counter = new HashMap<>(); + for (Set subset : powerSet) { + for (String name : subset) { + int num = counter.getOrDefault(name, 0); + counter.put(name, num + 1); + } + } + counter.forEach((k, v) -> Assertions.assertEquals((1 << (set.size() - 1)), v.intValue())); + } + + @Test + public void givenSet_WhenPowerSetIsCalculatedRecursiveByBinaryRepresentation_ThenItContainsAllSubsets() { + Set set = RandomSetOfStringGenerator.generateRandomSet(); + + Set> powerSet = new PowerSetUtility().recursivePowerSetBinaryRepresentation(set); + + //To make sure that the size of power set is (2 power n) + MatcherAssert.assertThat(powerSet, IsCollectionWithSize.hasSize((1 << set.size()))); + //To make sure that number of occurrence of each element is (2 power n-1) + Map counter = new HashMap<>(); + for (Set subset : powerSet) { + for (String name : subset) { + int num = counter.getOrDefault(name, 0); + counter.put(name, num + 1); + } + } + counter.forEach((k, v) -> Assertions.assertEquals((1 << (set.size() - 1)), v.intValue())); + } + + @Test + public void givenSet_WhenPowerSetIsCalculatedIterativePowerSetByLoopOverNumbers_ThenItContainsAllSubsets() { + Set set = RandomSetOfStringGenerator.generateRandomSet(); + + List> powerSet = new PowerSetUtility().iterativePowerSetByLoopOverNumbers(set); + + //To make sure that the size of power set is (2 power n) + MatcherAssert.assertThat(powerSet, IsCollectionWithSize.hasSize((1 << set.size()))); + //To make sure that number of occurrence of each element is (2 power n-1) + Map counter = new HashMap<>(); + for (List subset : powerSet) { + for (String name : subset) { + int num = counter.getOrDefault(name, 0); + counter.put(name, num + 1); + } + } + counter.forEach((k, v) -> Assertions.assertEquals((1 << (set.size() - 1)), v.intValue())); + //To make sure that one subset is not generated twice + Assertions.assertEquals(powerSet.size(), new HashSet<>(powerSet).size()); + //To make sure that each element in each subset is occurred once + for (List subset : powerSet) { + Assertions.assertEquals(subset.size(), new HashSet<>(subset).size()); + } + } + + @Test + public void givenSet_WhenPowerSetIsCalculatedIterativePowerSetByLoopOverNumbersMinimalChange_ThenItContainsAllSubsetsInGrayOrder() { + + Set set = RandomSetOfStringGenerator.generateRandomSet(); + List> powerSet = new PowerSetUtility().iterativePowerSetByLoopOverNumbersMinimalChange(set); + + //To make sure that the size of power set is (2 power n) + MatcherAssert.assertThat(powerSet, IsCollectionWithSize.hasSize((1 << set.size()))); + //To make sure that number of occurrence of each element is (2 power n-1) + Map counter = new HashMap<>(); + for (List subset : powerSet) { + for (String name : subset) { + int num = counter.getOrDefault(name, 0); + counter.put(name, num + 1); + } + } + counter.forEach((k, v) -> Assertions.assertEquals((1 << (set.size() - 1)), v.intValue())); + //To make sure that one subset is not generated twice + Assertions.assertEquals(powerSet.size(), new HashSet<>(powerSet).size()); + //To make sure that each element in each subset is occurred once + for (List subset : powerSet) { + Assertions.assertEquals(subset.size(), new HashSet<>(subset).size()); + } + //To make sure that difference of consecutive subsets is exactly 1 + for(int i=1; i 0); + } + Assertions.assertEquals(i, PowerSetUtility.getRankInLexicographicalOrder(Arrays.asList(subset))); + } + } + + @Test + public void givenRanking_WhenPowerSetIsInLexicographicalOrder_ReturnTheSubset() { + int n = new Random().nextInt(5) + 5; //a number in [5, 10) + List> powerSet = new ArrayList<>(); + for(int i = 0; i < (1 << n); i++) { + powerSet.add(PowerSetUtility.getSubsetForRankInLexicographicalOrder(i, n)); + } + //To make sure that the size of power set is (2 power n) + MatcherAssert.assertThat(powerSet, IsCollectionWithSize.hasSize((1 << n))); + //To make sure that number of occurrence of each index is (2 power n-1) + Map counter = new HashMap<>(); + for (List subset : powerSet) { + for (int i = 0; i < subset.size(); i++) { + if(subset.get(i)) { + int num = counter.getOrDefault(i, 0); + counter.put(i, num + 1); + } + } + } + counter.forEach((k, v) -> Assertions.assertEquals((1 << (n - 1)), v.intValue())); + //To make sure that one subset is not generated twice + Assertions.assertEquals(powerSet.size(), new HashSet<>(powerSet).size()); + } + + static class RandomSetOfStringGenerator { + private static List fruits = Arrays.asList("Apples", "Avocados", "Banana", "Blueberry", "Cherry", "Clementine", "Cucumber", "Date", "Fig", + "Grapefruit"/*, "Grape", "Kiwi", "Lemon", "Mango", "Mulberry", "Melon", "Nectarine", "Olive", "Orange"*/); + + static Set generateRandomSet() { + Set set = new HashSet<>(); + Random random = new Random(); + int size = random.nextInt(fruits.size()); + while (set.size() != size) { + set.add(fruits.get(random.nextInt(fruits.size()))); + } + return set; + } + } +}