Click here to Skip to main content
Click here to Skip to main content

C++ Permutation Class

, 13 Aug 2011
Rate this:
Please Sign up or sign in to vote.
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:

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

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

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:

#include <span class="code-keyword"><iostream>
</span>
using namespace std;

#include <span class="code-string">"aipermutations.h"
</span>#include <span class="code-keyword"><boost/scoped_array.hpp>
</span>#include <span class="code-keyword"><stdlib.h>
</span>
#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)

About the Author

AngusEm
Software Developer (Senior)
Canada Canada
Angus March is a computer engineer in Ottawa.

Comments and Discussions

 
Questionnice PinmemberCIDev20-Sep-11 2:54 
QuestionYou need a template version? Pinmembermbue15-Aug-11 13:39 
AnswerRe: You need a template version? PinmemberAngusEm15-Aug-11 15:47 
GeneralRe: You need a template version? Pinmembermbue15-Aug-11 16:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 13 Aug 2011
Article Copyright 2011 by AngusEm
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid