|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionCombination is the way of picking a different unique smaller set from a bigger set, without regard to the ordering (positions) of the elements (in the smaller set). This article teaches you how to find combinations. First, I show you the technique to find combinations. Next, I will go on to explain how to use my source code. The source includes a recursive template version and a non-recursive template version. At the end of the article, I will show you how to find permutations of a smaller set from a bigger set, using both Before all these, let me first introduce to you the technique of finding combinations. The TechniqueThe notations used in this article
ExplanationLet me explain using a very simple example: finding all combinations of 2 from a set of 6 letters {A, B, C, D, E, F}. The first combination is AB and the last is EF. The total number of possible combinations is: n!/(r!(n-r)!)=6!/(2!(6-2)!)=15 combinations. Let me show you all the combinations first: AB
AC
AD
AE
AF
BC
BD
BE
BF
CD
CE
CF
DE
DF
EF
If you can't spot the pattern, here it is: AB | AB
A | AC
A | AD
A | AE
A | AF
---|----
BC | BC
B | BD
B | BE
B | BF
---|----
CD | CD
C | CE
C | CF
---|----
DE | DE
D | DF
---|----
EF | EF
The same thing goes to combinations of any number of letters. Let me give you few more examples and now you can figure them out yourself. Combinations of 3 Letters from {A, B, C, D, E} (Set of 5 Letters) The total number of possible combinations is: 10 A B C
A B D
A B E
A C D
A C E
A D E
B C D
B C E
B D E
C D E
Combinations of 4 Letters from {A, B, C, D, E, F} (Set of 6 Letters). The total number of possible combinations is: 15. A B C D
A B C E
A B C F
A B D E
A B D F
A B E F
A C D E
A C D F
A C E F
A D E F
B C D E
B C D F
B C E F
B D E F
C D E F
I'm thinking if you would have noticed by now, the number of times, a letter appears. The formula for the number of times, a letter appears in all possible combinations is n!/(r!(n-r)!) * r / n == c * r / n. Using the above example, it would be 15 * 4 / 6 = 10 times. All the letters {A, B, C, D, E, F} appear 10 times as shown. You can count them yourself to prove it. Source Code SectionPlease note that all the combination functions are now enclosed in the stdcomb namespace. The Recursive WayI have made a recursive function, The function defined in combination.h as below: template <class RanIt, class Func> void recursive_combination(RanIt nbegin, RanIt nend, int n_column, RanIt rbegin, RanIt rend, int r_column,int loop, Func func); The parameters prefixed with 'n' are associated with n sequence, while r-prefix are r sequence related. As an end user, you need not bother about those parameters. What you need to know is The typical way of filling out the parameters is Just for your information, the maximum number of depth of recursion done is r+1. In the last recursion (r+1 recursion), each new combination is formed. Examples of using The Non-Recursive WayIf you have misgivings about using the recursive method, there is a non-recursive template function for you want to choose. (Actually there are 2.) The parameters are even simpler than the recursive version. Here's the function definition in combination.h template <class BidIt>
bool next_combination(BidIt n_begin, BidIt n_end,
BidIt r_begin, BidIt r_end);
template <class BidIt>
bool next_combination(BidIt n_begin, BidIt n_end,
BidIt r_begin, BidIt r_end, Prediate Equal );
And its reverse counterpart version: template <class BidIt> bool prev_combination(BidIt n_begin, BidIt n_end, BidIt r_begin, BidIt r_end); template <class BidIt> bool prev_combination(BidIt n_begin, BidIt n_end, BidIt r_begin, BidIt r_end, , Prediate Equal ); The parameters, You can peruse the source code for these 2 functions in combination.h and its examples in next_comb_ex.cpp and prev_comb_ex.cpp, if you want. A typical way of using do { //Do what you want here. } while(next_combination(n.begin(),n.end(),r.begin(),r.end() ); Certain Conditions must be Satisfied in Order for next_combination() to Work
When the above conditions are not satisfied, results are undetermined even if the Return ValueWhen Some information about next_combination() and prev_combination()
Examples of how to use these 2 functions are in next_comb_ex.cpp and prev_comb_ex.cpp. So what we can do with next_combination()?With The formula for total number of permutations of r sequence, picked from n sequence is : n!/(n-r)! We can call do
{
//do your processing on the new combination here
while(next_permutation(r2.begin(),r2.end()))
{
//do your processing on the new permutation here
}
}
while(next_combination(n.begin(),n.end(),r.begin(),r.end() ));
However, I must mention that there exists a limitation for the above code. The n and r sequences must be sorted in ascending order in order to work. This is because do
{
//do your processing on the new combination here
for(cnt i=0;cnt<24;++cnt)
{
next_permutation(r2.begin(),r2.end());
//do your processing on the new permutation here
}
}
while(next_combination(n.begin(),n.end(),r.begin(),r.end() ));
However this method requires you to calculate the number of permutations beforehand. So how do I prove they are distinct permutations?There is a pair <iterator, bool> insert(const value_type& _Val); The proof.cpp is written for this purpose, using STL If you are interested, you can proceed to read the 2nd part of the article: Combinations in C++, Part 2 DownloadsPlease build them in release build! History
| ||||||||||||||||||||