473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

powerset

I have a
vector<int> x;
and I would like to
call the function

foo( a_subset_of_x )

for ALL subsets of x (i.e. the powerset of x)

Is this possible in an easy way?
Aug 9 '05 #1
1 5991
Gunnar G wrote:

I have a
vector<int> x;
and I would like to
call the function

foo( a_subset_of_x )

for ALL subsets of x (i.e. the powerset of x)

Is this possible in an easy way?


Use a recursive function

#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> Set;

void PrintSet( int Count, Set& TheSet )
{
cout << Count << ": ";
for( size_t i = 0; i < TheSet.size(); ++i )
cout << TheSet[i] << " ";
cout << endl;
}

void Iterate( int& Count, int StartWith, Set& TheSet, Set& Helper )
{
PrintSet( Count, Helper );
Count++;

for( size_t i = StartWith; i < TheSet.size(); ++i ) {
Helper.push_back( TheSet[i] );
Iterate( Count, i + 1, TheSet, Helper );
Helper.pop_back();
}
}

void Iterate( Set& TheSet )
{
Set Tmp;
int Cnt = 1;
Iterate( Cnt, 0, TheSet, Tmp );
}

int main()
{
Set S;

S.push_back( 1 );
S.push_back( 2 );
S.push_back( 3 );
S.push_back( 5 );
S.push_back( 7 );
S.push_back( 11 );

Iterate( S );

return 0;
}
--
Karl Heinz Buchegger
kb******@gascad.at
Aug 9 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Raymond Hettinger | last post by:
I've gotten lots of feedback on the itertools module but have not heard a peep about the new sets module. * Are you overjoyed/outraged by the choice of | and & as set operators (instead of + and...
0
by: Irmen de Jong | last post by:
QOTW: "The best use for a bug report on comp.lang.python is as an object lesson for your grandchildren: 40 years from now you can search the archives for it, and tell the little darlings 'see? ...
0
by: Tim Rowe | last post by:
Has anybody written a good powerset function for that nice new set datatype we now have? I've found a powerset operator for Python lists on the net, but that depends on lists being indexable which...
7
by: Steve | last post by:
This post has two parts. First is my feedback on sets. (Hello? Last summer called, they want their discussion thread back...) Second is some questions about my implementation of a partition...
0
by: matt | last post by:
Hi, I would like to know how to implement a powerset operation in perl. My friend found the code: sub subsets { map { my $n = $_; ] } (1..2**($#_ + 1));
56
by: Paddy | last post by:
A work colleague circulated this interesting article about reducing software bugs by orders of magnitude: http://www.spectrum.ieee.org/WEBONLY/publicfeature/sep05/0905ext.html Some methods they...
6
by: Talin | last post by:
I've been using generators to implement backtracking search for a while now. Unfortunately, my code is large and complex enough (doing unification on math expressions) that its hard to post a...
44
by: Christoph Zwerschke | last post by:
In Python, it is possible to multiply a string with a number: >>> "hello"*3 'hellohellohello' However, you can't multiply a string with another string: >>> 'hello'*'world' Traceback (most...
5
by: jgentes | last post by:
I am looking for help creating a factorial like function, except it will take some user-defined array/list and concatenate the values into a user-defined length ... similar to substr, but not exactly...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.