Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

C++ Permutation Class

4.17/5 (3 votes)
13 Aug 2011CPOL2 min read 26.7K   312  
A C++ class that can perform k-permutations of n on a set

Introduction

This class can perform permutations on a set, but not the kind of permutations that the STL next_permutation() function does, which are permutations of n, but k-permutations of n.

Background

There is some confusion over the meaning of "permutation" so for clarity, you might want to read the Wikipedia article on permutations. Note that this does not calculate the number of permutations there are in given parameters of k and n but returns all the subsets that form the permutations of a set.

Using the Code

Currently, you do not get to choose the set. AiPermutations presumes that your set will be all integers between 0 and n inclusive. If that does not suit your data, then you can treat the arrays returned as holding the indexes to the array of your set. So, if your set would look something like:

C++
char myset[] = { 'A', 'B', 'C', 'D' };

then you can convert the returned subset into a subset of your liking with:

C++
for(int i = 0; i < k; i++)
	mysubset[i] = myset[ returnedsubset[i] ];

Other than that, usage is very simple. Just plug the k and n parameters into the constructor, allocate an elementtype-array of size k and pass it to AiPermutations::next(), which you repeatedly call until it returns false. Here's an example:

C++
#include <iostream>

using namespace std;

#include "aipermutations.h"
#include <boost/scoped_array.hpp>
#include <stdlib.h>

#define K 3
#define N 6

int fact(int n) {
	int nAccumulator = 1;
	for(int i = 2; i <= n; i++)
		nAccumulator *= i;
	return nAccumulator;
}

int main(int argc, char **argv) {
	AiPermutations perm(K, N);
	boost::scoped_array<AiPermutations::elementtype> 
		permutation(new AiPermutations::elementtype[K]);

	int nPermutationCount = 0;
	while (perm.next(permutation.get())) {
		nPermutationCount++;
		cout << "got: (";
		for(int i = 0; i < K - 1; i++)
			cout << permutation[i] << ", ";
		cout << permutation[K - 1] << ')' << endl;
	}

	cout << "Finally, " << nPermutationCount << " 
	permutations when it should have been: " << fact(N)/fact(N - K) << endl;

	return EXIT_SUCCESS;
}

Robustness

This class is not designed around it. It will not throw exceptions or return errors (when built in release mode) for absurd parameters or inadequately allocated arrays in method parameters. Furthermore, the code will behave unpredictably when processing trivial examples, such as a k = 0, even if they are mathematically sound.

Portability

It should be portable in ANSI C and requires STL. So far I've built it under:

  • gcc 4.4.3 (Ubuntu 10.04)
  • Visual Studio 2005 (WinXP)

Watch for...

...a template version that allows the user to use sets of his own type. This should not be difficult, since the concept is as difficult as the example above, however if a set was provided with duplicate elements, then non-unique subsets would be produced, which probably could not be managed easily.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)