0

I'm trying to figure out how to implement an algorithm to find a power set given a set, but I'm having some trouble. The sets are actually vectors so for example I am given Set<char> set1{ 'a','b','c' }; I would do PowerSet(set1); and I would get all the sets but if I do Set<char> set2{ 'a','b','c', 'd' }; I would do PowerSet(set2) and I would miss a few of those sets.

Set<Set<char>> PowerSet(const Set<char>& set1)
{
    Set<Set<char>> result;
    Set<char> temp;
    result.insertElement({});
    int card = set1.cardinality();
    int powSize = pow(2, card);
    
    for (int i = 0; i < powSize; ++i)
    {
        for (int j = 0; j < card; ++j)
        {
            if (i % static_cast<int> ((pow(2, j)) + 1))
            {
                temp.insertElement(set1[j]);
                result.insertElement(temp);
            }
        }
        temp.clear();
    }
    return result;
}

For reference:

  • cardinality() is a function in my .h where it returns the size of the set.
  • insertElement() inserts element into the set while duplicates are ignored.
  • Also the reason why I did temp.insertElement(s[j]) then result.insertElement(temp) is because result is a set of a set and so I needed to create a temporary set to insert the elements into then insert it into result.
  • clear() is a function that empties the set.
  • I also have removeElem() which removes that element specified if it exists, otherwise it'll ignore it.
Jasmine
  • 49
  • 6
  • 1
    How are you calling clear on set1, which is const? Also, logic wise, should that not be temp.clear() instead? – Wutz Oct 24 '21 at 05:48
  • Note: `pow` operates in floating point and the result of floating-point computations can be imprecise. In this case you're after exponents of two, so you shouldn't have any problems before the result exceeds the capacity of `int`, but I've had some nasty surprises in the past. – user4581301 Oct 24 '21 at 05:48
  • 2
    Why don't you want to use left shift? – John Kugelman Oct 24 '21 at 05:49
  • @Wutz Yes, I meant to do temp.clear() I made a mistake typing it out. – Jasmine Oct 24 '21 at 05:54
  • @JohnKugelman I know (i & (1 < – Jasmine Oct 24 '21 at 05:56
  • Please create a [mre] that we can run. Finding mistakes that turn out to be transcription errors is a bad use of our time, and it's quite possible there are mistakes in the other `Set` methods that we can't see. – John Kugelman Oct 24 '21 at 05:56
  • *Why* are you trying to figure out another method? – John Kugelman Oct 24 '21 at 05:57
  • "without using left shift bit" What kind of restriction is that? Where does it come from? Use the best tool for the job. – n. m. could be an AI Oct 24 '21 at 07:35

1 Answers1

1

Your if test is nonsense -- it should be something like

if ((i / static_cast<int>(pow(2,j))) % 2)

you also need to move the insertion of temp into result after the inner loop (just before the temp.clear()).

With those changes, this should work as long as pow(2, card) does not overflow an int -- that is up to about card == 30 on most machines.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226