void AlgoMMPCbar::subs(const std::vector<unsigned int>& org, const std::vector<unsigned int>& pre, size_t k, size_t n, SubSets& c){
if (n <= 1) {
for(size_t i = k; i < org.size(); i++){
std::vector<unsigned int> v(pre);// instead of printing...
v.push_back(org.at(i));
c.push_back(v);
}
} else {
size_t n1 = n - 1;
for(size_t i = k; i != org.size() - n1; i++){ //
std::vector<unsigned int> s(pre);
s.push_back(org.at(i));
subs(org,s,i+1,n1,c);
}
}
}
void AlgoMMPCbar::computeSubSets(const std::vector<unsigned int>& org, size_t& n, SubSets& c){
c.clear(); // clear previous data
std::vector<unsigned int> pre;
pre.reserve(n+1); // for performance
if (n==0)
c.push_back(pre);
else
subs(org,pre,0, n, c);
}
The above code used to generate subsets of size n for further test/processing. But i never need to test all these generated subsets (in worst case it will check them all). The main time consuming part of the program is the subset generation. Now i want to transform the above functionality to generate subsets one by one (not all at once, so, i can stop further subset generation any time).
Please share your expertise to transform the above functionality in a function like subset.next(), to save computational time.
Thanks in advance.
say
indmaintains the indexes for elements in the subset in an increasing order, i.e.find the smallest
jsuch thatyou may go to the next subset by
note that the new
indarray is still sorted. You may easily show that starting withyou will generate all the subsets iterating through above procedure. you can have a fast code if you use some tricks for ‘maintaining’ the value of
jin above rather than doing a linear search.