Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To calculate subset of a set of variables 1

Status
Not open for further replies.

amanyasin

Technical User
Feb 7, 2003
28
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;
}

}
 
Alas, I know nothing about Boost library graph components but this improvisation works fine:
Code:
namespace { // Locals:
    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;
}
 
Dear i need your help more.

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 << '\n';


Again thanks
 
I was trying to sent this post many hours ago but tek-tiks server was down...

There are lots of methods to do that.
For example, define
Code:
typedef std::vector<V> C; // vector of combinations
Change functions signatures and modify this loop body:
Code:
void subs(const std::vector<char>& org, size_t n, C& c)

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);
        }
    } ...
}
Now you have a vector of all combinations. Analyze it as you wish...
As you see no needs in vout helper here.
Code:
int main()
{
    C c;
    V v;
    // initialize v
    subs(v,3,c);
    // Done. c.size() - number of combinations... etc...
Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top