0

I've been working on a problem in which I've to take union of n sets and see if all the sets are disjoint. I'm using

set_union

A draft for my code is as follows:

vector<int> I; // Vector to store union of all sets
vector<vector<int> >A; // Vectors whose union are to be taken
/* Read A */
repeat i from 0 to n-1
{
    I_size=I.size();
    Ai_size=A[i].size();
    I.resize(I_size+Ai_size);
    I=vector<int>(I.begin(),set_union(I.begin(),I.end(),A[i].begin(),A[i].end(),I.begin()));
    if(*(--I.end())==0) // If two sets contain some common term, last element of I will be 0
        Notify that the sets cannot be united // Break from the loop in case any two sets have common terms
}

I'm breaking from the loop in case last element of I is 0 because none of the vectors in A will ever contain a 0. The above code doesn't works. It gives some unknown runtime error. Please suggest where I'm going wrong?

Actual code:

vector<vector<int> >A;
int n;

int SetIntersection()
{
    vector<int> I;
    for(int i=0; i<n; i++)
    {
        int I_size=I.size();
        int Ai_size=A[i].size();
        I.resize(I_size+Ai_size);
        I=vector<int>(I.begin(),set_union(I.begin(),I.end(),A[i].begin(),A[i].end(),I.begin()));
        if(*(--I.end())==0)
            return 0;
    }
return 1;
}

int main()
{
        int x;
        cin>>n;
        for(int i=0; i<n; i++)
        {
            cin>>x;
            A[i].push_back(x);
        }
        if(SetIntersection()==1)
            cout<<"Sets are disjoint";
        else
            cout<<"Sets are not disjoint";
return 0;
}
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
CPPCoder
  • 155
  • 1
  • 1
  • 10
  • Could you post the actual code in which you get the error? – yizzlez Mar 16 '14 at 14:17
  • @awesomeyi I'm getting error due to this part, I'm pretty sure that rest of the part of my program is fine! There's some problem related to declaring I as 0 size vector(I guess). because when I initialise I with some random initial size say 100, it runs fine although set union isn't still correct – CPPCoder Mar 16 '14 at 14:20
  • The vectors A will always be in sorted order – CPPCoder Mar 16 '14 at 14:32
  • You're violating a requirement/precondition for `set_union`: [set.union]/2 "*Requires:* The resulting range shall not overlap with either of the original ranges." – dyp Mar 16 '14 at 15:41
  • This `*(--I.end())` looks like it wants to be `I.back()`. – dyp Mar 16 '14 at 15:42
  • Also, where do you add/create the elements of `A` that you later modify via `A[i].push_back(x)`? – dyp Mar 16 '14 at 15:46
  • @dyp How I am I violating the condition? – CPPCoder Mar 16 '14 at 16:03
  • @dyp I've declared A in global scope – CPPCoder Mar 16 '14 at 16:04
  • 1
    In `set_union(I.begin(),I.end(),A[i].begin(),A[i].end(),I.begin())`, the output range (specified by the last iterator passed) overlaps with the first input range (specified by the first two iterators). You've declared `A` in global scope, but didn't add any elements to it. `operator[]` for `std::vector` does not create/add new elements, it just modifies already existing elements. – dyp Mar 16 '14 at 16:20

0 Answers0