×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

To calculate subset of a set of variables

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;
    }

}

RE: To calculate subset of a set of variables

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;
}
 

RE: To calculate subset of a set of variables

(OP)
Dear thanks it works fine.

RE: To calculate subset of a set of variables

(OP)
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[i] << '\n';


Again thanks

RE: To calculate subset of a set of variables

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!
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close