## To calculate subset of a set of variables

## To calculate subset of a set of variables

(OP)

Is there any method available in Boost graph liberary to calculate subsets of length N of a SET? if no please guide me how i can calculate it.

e.g.

vector type set = { a, b, c, d, e, f, g, h, i, j }

subsets of size n=2

{a, b}, {a, c} .....{b, c}.........................

subsets of size n=3

{a, b, c}, {b, c, d} .....{ c, d, e}.........................

susets of size n=4

...............

............

I tried to solve it but it is not giving all subsets

vector<char> vars;

vars.push_back('b');

vars.push_back('c');

vars.push_back('d');

vars.push_back('e');

vars.push_back('f');

vars.push_back('g');

vars.push_back('h');

int counter = 0, n=3 ; // n = {0 ,1,2,3 ....} size of the subset

cout << " \n Y = " << vars.at(0)<<"\n";

for (int i = 1; i < vars.size(); i++)

{

counter++;

cout<<" \n i = "<<i<<" counter = "<<counter;

if (i >= 1 && counter <= n){

cout << " \n ........................cond variables = " << vars.at(i);

continue;

}

if ( counter == n+1)

{

counter = 0;

i = i - n;

}

}

e.g.

vector type set = { a, b, c, d, e, f, g, h, i, j }

subsets of size n=2

{a, b}, {a, c} .....{b, c}.........................

subsets of size n=3

{a, b, c}, {b, c, d} .....{ c, d, e}.........................

susets of size n=4

...............

............

I tried to solve it but it is not giving all subsets

vector<char> vars;

vars.push_back('b');

vars.push_back('c');

vars.push_back('d');

vars.push_back('e');

vars.push_back('f');

vars.push_back('g');

vars.push_back('h');

int counter = 0, n=3 ; // n = {0 ,1,2,3 ....} size of the subset

cout << " \n Y = " << vars.at(0)<<"\n";

for (int i = 1; i < vars.size(); i++)

{

counter++;

cout<<" \n i = "<<i<<" counter = "<<counter;

if (i >= 1 && counter <= n){

cout << " \n ........................cond variables = " << vars.at(i);

continue;

}

if ( counter == n+1)

{

counter = 0;

i = i - n;

}

}

## RE: To calculate subset of a set of variables

## CODE

typedef std::vector<char> V;

std::ostream& vout(std::ostream& os, const V& v)

{

for (size_t i = 0; i < v.size(); i++)

os << v[i];

return os;

}

void app(const V& org, const V& pre, size_t k, size_t n, std::ostream& os)

{

if (n <= 1) {

for (size_t i = k; i < org.size(); i++)

vout(os,pre) << org[i] << '\n';

} else {

size_t n1 = n - 1;

for (size_t i = k; i < org.size() - n1; i++) {

V s(pre);

s.push_back(org[i]);

app(org,s,i+1,n1,os);

}

}

}

} // namespace

void subs(const std::vector<char>& org, size_t n, std::ostream& os)

{

if (n <= org.size()) {

V pre;

app(org,pre,0,n,os);

}

}

int main()

{

std::vector<char> v;

v.push_back('a');

v.push_back('b');

v.push_back('c');

v.push_back('d');

v.push_back('e');

v.push_back('f');

v.push_back('g');

v.push_back('h');

v.push_back('i');

v.push_back('j');

subs(v,3,std::cout);

return 0;

}

## RE: To calculate subset of a set of variables

## RE: To calculate subset of a set of variables

I want to populate a vector on each combination. Then i will pass this vector to my statistical test. and then repopulate it with new combination.

so, how i can deal with,

for (size_t i = k; i < org.size(); i++)

vout(os,pre) << org[i] << '\n';

Again thanks

## RE: To calculate subset of a set of variables

There are lots of methods to do that.

For example, define

## CODE

## CODE

void app(const V& org, const V& pre, size_t k, size_t n, C& c)

{

if (n <= 1) {

for (....) { // push the next combination into c

V v(pre);// instead of printing...

v.push_back(org[i]);

c.push_back(v);

}

} ...

}

As you see no needs in vout helper here.

## CODE

{

C c;

V v;

// initialize v

subs(v,3,c);

// Done. c.size() - number of combinations... etc...