Suppose we have a collection of n different subsets of the s

Suppose we have a collection of n different subsets of the set { 1, 2, ..., n } and they are in some arbitrary order, that is, we have subsets S1, S2, ..., Sn, but how many and which elements are in each of these subsets is entirely arbitrary. Suppose also that we have another subset S\' of { 1, 2, ..., n }.

Express a brute-force algorithm that determines whether S\' equal to one of the subsets in the collection.

Solution

For all sets from S1 to Sn
{
   if Compare(Si,S\')
   {
   print(\"Equal to subset \"+i)
   }
}
Compare(S1,S2){
   l1 = len(S1);
   l2 = len(S2);
   if(l1!=l2)
   {
       return false;
   }
   hat=0;
   ans=0;
   for(i=0;i<l1;i++)
   {
       for(j=0;j<l2;j++)
       {
           if(S1[i]==S2[j])
           {
               S2[j] *= -1;
               hat=1;
               break;
           }
       }
       if(hat==1)
       {
           hat=0;
       }
       else
       {
           ans=1;
       }
   }
   if(ans==1)
   {
       return false;
   }
   else
   {
       return true;
   }
}

Suppose we have a collection of n different subsets of the set { 1, 2, ..., n } and they are in some arbitrary order, that is, we have subsets S1, S2, ..., Sn,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site