The following will generate an output like this - 0
0 1
0 1 2
0 1 2 3
0 1 3
0 2 3
0 3
1
1 2
1 2 3
1 3
2
2 3
3
using these indices you can generate from any given set.
class SetGenerator
{
public:
SetGenerator(size_t length)
: LENGTH(length)
{ indices.reserve(length); }
const vector<size_t>& generate_set()
{
if ( indices.empty() )
indices.push_back(0);
else if ( (LENGTH - 1) == indices[0])
{
indices.clear();
}
else
{
if ( LENGTH != (indices.back() + 1) )
{
indices.push_back(indices.back() + 1);
}
else
{
if ( (LENGTH-1) == ++indices.at(indices.size() - 2) || 2
== indices.size() )
indices.pop_back();
}
}
return indices;
}
private:
const size_t LENGTH;
vector<size_t> indices;
};
...
SetGenerator gen(5);
do
{
const vector<size_t> & idxs = gen.generate_set();
if ( idxs.empty() )
break;
copy(idxs.begin(), idxs.end(), ostream_iterator<size_t>(cout, " "));
cout<<'\n';
} while( true );
On Wed, Aug 26, 2009 at 12:20 PM, AKS <[email protected]> wrote:
>
> Hello fellas,
>
> i am lookin for an algorithm to find all the possible subsets in a
> given set
>
> So, if the Set is say, A={a,b,c} omit the null set
>
> o/p: --- {a} {b} {c} {a,b} {b,c} {a,c} {a,b,c} omit the
> null set
>
> regards,
> Abhijeet
> >
>
--
Yesterday is History.
Tomorrow is a Mystery.
Today is a Gift! That is why it is called the Present :).
http://sites.google.com/site/ramaswamyr
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---