2

I need a way to generate all unique sub sets of length k of an array, similar to itertools.combinations() in python. I am trying to get these sets for association mining until the support is less than 2. Any sets passed 2 are proving to be a headache. Also, cannot use any STL functions for this.

Any insight would be appreciated.

Closest I could find to what my problem is here: Generate all subsets of size k (containing k elements) in Python

P.S each element in the set (array) are integers.

in main:

int temp = 0;
int setsize = 2;
int **testarray = subsets(itemset,itemsetSize,setsize);
for (int i = 0; i < 1300; i++)
{
    for(int j = 0; j < 10; j++)
    {
        cout<<testarray[i][j];
    }
}
cout<<endl;

in functions:

int** subsets (int inputArray[],int arraySize, int k)
{
    size_t n = arraySize;

    int **resultArray = 0;
    resultArray = new int *[1000];

    size_t i = (1 << k) -1;

    while( !(i >> n) ) 
    {
        int pushBack = 0;
        int pushBack2 = 0;
        int v[500];
        for (size_t j = 0; j<n; j++)
        {
            if (i & (1 << j))
            {
                v[pushBack] = inputArray[j];
                pushBack++;
            }
        }
        resultArray[pushBack2] = v;
        pushBack2++;
        i = (i+(i&(-i)))|(((i^(i+(i&(-i))))>>2)/(i&(-i)));          
    }
    return resultArray;
}
Community
  • 1
  • 1
that_guy
  • 2,313
  • 4
  • 33
  • 46

1 Answers1

0
#include <vector>
#include <iostream>
using namespace std;

vector<vector<int>> subsets(const vector<int>& input, int k)
{
    size_t n = input.size();

    vector<vector<int>> result;

    size_t i = (1 << k) - 1;

    while( !(i >> n) ) 
    {
        vector<int> v;

        for (size_t j = 0; j < n; j++)
           if (i & (1 << j))
                v.push_back(input[j]);

        result.push_back(v);

        i = (i+(i&(-i)))|(((i^(i+(i&(-i))))>>2)/(i&(-i))); 
    }

    return result;
}

int main()
{
    auto out = subsets({5,2,8,14,3,9,11}, 3);
    for (auto x : out)
    {
        for (auto y : x)
            cout << y << " ";
        cout << endl;
    }
}

Output:

5 2 8 
5 2 14 
5 8 14 
2 8 14 
5 2 3 
5 8 3 
2 8 3 
5 14 3 
2 14 3 
8 14 3 
5 2 9 
5 8 9 
2 8 9 
5 14 9 
2 14 9 
8 14 9 
5 3 9 
2 3 9 
8 3 9 
14 3 9 
5 2 11 
5 8 11 
2 8 11 
5 14 11 
2 14 11 
8 14 11 
5 3 11 
2 3 11 
8 3 11 
14 3 11 
5 9 11 
2 9 11 
8 9 11 
14 9 11 
3 9 11 
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • 4
    `(i+(i&(-i)))|(((i^(i+(i&(-i))))>>2)/(i&(-i)))` i'm puking operators right now. – mfontanini Apr 22 '12 at 20:22
  • I tried to convert it so it would be working with arrays, however I don't seem to be getting all permutations... I have edited the post to show what I have come up with. – that_guy Apr 22 '12 at 21:11
  • Attempt 2, I realized that I was doing it wrong and I think I got it right this time, however I get a read violation because in function "subsets" it evaluates to false in the "while( !(i >> n) )". Not sure why... – that_guy Apr 23 '12 at 00:07