473,387 Members | 1,501 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.

Algorithm to generate different combinations based on N numbers

Hi,

Probably this question was asked before, but what is the algorithm to
generate all combinations based on N numbers?

For example, for 3 elements = {1,2,3}, I'd like to generate a list of
(2^n - NULL):
1, 2, 3
1, 2
2, 3
1, 3
1,
2,
3,

For 4 elements = {1,2,3,4}, it'll be
1,2,3,4
1,2,3
1,2,4
1,3,4
2,3,4
1,2
1,3
1,4
2,3
2,4
3,4
1
2
3
4

Thanks,
P Ross
Jul 22 '05 #1
2 2937
* pe********@canada.com (Peter R) schriebt:

Probably this question was asked before, but what is the algorithm to
generate all combinations based on N numbers?


In C++ you can use std::bitset to access the bits of value. You can
alternatively use the shift operator << and bit-level and, &. The first
can be more clear, the latter more efficient.

The above assumes you use the straightforward algorithm of counting from
1 to 2^N-1, inclusive.

For details about this and other _algorithms_, please do ask in e.g.
[comp.programming], since that question is off-topic in this group.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

"Peter R" <pe********@canada.com> wrote in message news:f6*************************@posting.google.co m...
Hi,

Probably this question was asked before, but what is the algorithm to
generate all combinations based on N numbers?

For example, for 3 elements = {1,2,3}, I'd like to generate a list of
(2^n - NULL):
1, 2, 3
1, 2
2, 3
1, 3
1,
2,
3,

For 4 elements = {1,2,3,4}, it'll be
1,2,3,4
1,2,3
1,2,4
1,3,4
2,3,4
1,2
1,3
1,4
2,3
2,4
3,4
1
2
3
4

Thanks,
P Ross

For instance,

------ C++ code : BEGIN ------

#include <cassert>
#include <vector>
#include <set>
#include <iostream>
#include <sstream>
#include <iterator>
using namespace std;

typedef unsigned long ulong;

// ------------------------
struct less_functor
{
bool operator()(
const vector<int>& left_i,
const vector<int>& right_i
)
{
if (left_i.size() < right_i.size()) return true;
if (left_i.size() > right_i.size()) return false;

return (left_i < right_i);
}
};
// ------------------------
set<vector<int>, less_functor> get_permut (ulong);
set<vector<int>, less_functor> get_permut (const vector<int>&);
// ------------------------
set<vector<int>, less_functor> get_permut (ulong size_i)
{
vector<int> abt;
for (ulong i = 0; i < size_i; i++) abt.push_back (i + 1);
return get_permut (abt);
}
// ------------------------
set<vector<int>, less_functor > get_permut (const vector<int>& abt_i)
{
set<vector<int>, less_functor > ret_sv;
if (abt_i.size() == 0) return ret_sv;

if (abt_i.size() == 1)
{
ret_sv.insert (vector<int> (1, abt_i[0]));
return ret_sv;
}
for (ulong i = 0; i < abt_i.size(); i++)
{
vector<int> abt (abt_i);
abt.erase(remove(abt.begin(), abt.end(), abt_i[i]), abt.end());
const set<vector<int>, less_functor> tmp_sv (get_permut(abt));

for (set<vector<int>, less_functor>::const_iterator pos_iter = tmp_sv.begin();
pos_iter != tmp_sv.end();
pos_iter++
)
{
ret_sv.insert (*pos_iter);
}

}
ret_sv.insert (abt_i);

return ret_sv;

}
int main (int argc, char** argv)
{
const bool condition_argc (argc == 2);

cout << endl;
cout << " YOUR COMMAND LINE : ";
for (long i = 0; i < argc; i++)
{
cout << argv[i] << " ";
}
cout << endl;
cout << endl;

if (!condition_argc)
{
cout << ""
<< " USAGE : "
<< argv[0]
<< " <size of alphabet>"
<< endl;
return 1;
}

assert (condition_argc);

const long abt_size (atoi(argv[1]));
if (abt_size < 0)
{
cout << "ERROR : <size of alphabet> must be non-negative"
<< endl
<< "Actual value = "
<< abt_size
<< endl;
return 1;
}
const set<vector<int>, less_functor> out (get_permut (ulong (abt_size)));
for (set<vector<int>, less_functor>::const_iterator pos_iter = out.begin();
pos_iter != out.end();
pos_iter++
)
{
ostringstream oss;
copy (pos_iter->begin(), pos_iter->end(), ostream_iterator<int>(oss, ", "));
cout << oss.str().substr (0, oss.str().size() - 2) << endl;
}

return 0;
}
------ C++ code : END --------
--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #3

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

Similar topics

16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
3
by: spakka | last post by:
I'm useless at templates, but, inspired by this post: <hinnant-074D8A.11281207032005@syrcnyrdrs-01-ge0.nyroc.rr.com>, I'm trying to write an algorithm template <class BidirectionalIterator,...
8
by: cayblood | last post by:
Hello, I have been interested in something kind of like the next_permutation from the STL algorithm library, except that I want it to find possible combinations of vector elements. Here is a more...
5
by: Koen | last post by:
Hi, I am looking for an algorithm that figures out which numbers from a given set add up to another number. I am sure this has been done before, but I have no idea how such a calculation is...
3
by: Ryan | last post by:
I've been trying to find an algorithm that will output all of the possible combinations of items in an array. I know how to find the number of combinations for each set using nCr=n!/(r!(n-r)!) ...
8
by: jason | last post by:
Hello everyone, I am looking for an algorithm that would take an incremental value and map that to a case-inspecific alphanumeric string. However, I don't want the string to simply step through...
29
by: Roy Gourgi | last post by:
Hi, I am new to C#. I have the same time scheduling program written in C++ and it is 5 times faster than my version in C#. Why is it so slow as I thought that C# was only a little slower than...
16
by: a | last post by:
We are writing an app that assigns people to teams based on their curent score. Teams are 8 people, there are 2 teams. (i would like it to be flexible, but this is a start). I need an algorithm...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.