If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Generate all combinations of M out N elements

Hi Everybody,

I require your help for finding a solution of a mathematical problem in C language. The brief details of the problem is as follows:

I have a global array of ‘n’ elements of type integer having some values. I want to write a function, which takes two int as a parameter one will be the size of the array i.e. ‘n’ and second parameter will be used to generate the combination (m) say if second parameter is ‘3’ the function has to fetch 3 elements of the array store it in some temp array or pointer and pass it on to some function after that again fetch 3 elements so on till the maximum limit is not reached which can be find out by nCm.
To be more clear consider the following example:
The Array is of 7 elements having elements data starting from 1 to 7. If m is 3 then no of temporary combinations will be nCm=35, which are as follows:
123 134 145 156 167
124 135 146 157
125 136 147
126 137
127

234 245 256 267
235 246 257
236 247
237

345 356 367
346 357
347

456 467
457

567

The same sequence is applied for any value of m be it 2,3,4,5 etc. It will be kind if anybody help me or send me the algorithm or program to perform this task.

Please note that the size of m is not known at design time it will be passed at a runtime.

Re: Generate all combinations of M out N elements

I'll take your challenge and come up with the following algorithm (this could be homework, so I won't give an example in code):

You want to display the combination of n items taken m at a time

1) For n total items, set up a set of n bits. The bits are numbered from 0 to n-1, so you have the following (assuming that n=7)

"0000000"

The 0th bit is the rightmost bit, and bit n-1 is the leftmost bit. Call this array of bits "bit".

2) Set up each element in your set in an array of n items. For example,
7 6 5 4 3 2 1.

Call this array "elements". So elements[0]=1, elements[n-1] = 7, etc.

3) Set bits 0 to m-1 to "1". So for example the bit array will now be "0000111" (assuming m=3).

4) For J = 0 to n Do
if bit[ J ] = 1 then output elements[ J ]
End For J
Output Carriage return (go to next line in output)

5) Get the next permutation of the "bit" array. If there is another permutation, go to step 4.

6) Quit

The idea of the algorithm is to generate all of the bit patterns with exactly 3 "on" bits. For each generation of the bit patterns, you output
the element corresponding to the "on" bits.

Now, how do you do permutations easily? No problem with C++ and the algorithm function called "next_permutation()". This program can be done in no time using this nice function.

If this must be 'C', then maybe you did a permutation progam/function previously. Since you are doing combinations, it makes sense that you might have done a permutation program. This is what code reuse is about.

If it is a 'C' program, you'll have to create your own little permutation function. Again, www.google.com searches, and a lot of textbooks have permuataion algorithms.

// Class to generate combinations given a vector&lt;T&gt;
// The total elements is the size of the passed in vector,
// The number of items to choose is nItems.
template &lt;class T&gt;
class CombinationGenerator
{
public:
CombinationGenerator(const vector&lt;T&gt;& ElVector, // Original items
int nItems=-1) : // Number of items to choose
m_Elements(ElVector), m_bNext(true)

So CombinationGenerator&lt;&gt; is a templated class. All you need to do is create an instance of "CombinationGeneraor&lt;your_data_type&gt;(Vector_of_your_data, nItems);" and keep calling "NextCombination" in a while() loop. Calling NextCombination() fills an array of the next combination, and returns true if a combination was found, or false if no more combinations exist.

Note that the main() program prints the number of combinations, just to prove to yourself (and myself) that this class actually works.