Introduction

As each programmer must have experienced, often you can modify a function only a little to meet the new requirement. Here I present such an example for permutation -- to enumerate all element arrangements for an ascending ordered list. For instance, for a string “abc” where 'a'<'b'<'c', we have permutations “abc”, “acb”, “bac”, “bca”, “cab”, and “cba”, while for a half ordered “cab”, the result is “cab” and “cba”. The following function picked from the STL header file “algorithm” shows how to generate the next permutation from the previous one.

do v.insert(v.end(), s);
while (next_permutation(s.begin(), s.end()));

Where s is a work string for character permutation and v is a vector to collect permuted s iteratively. In practice, we may meet some permutation variations, two of which are then discussed in this article.

With Non-Ordered Elements

First, consider a permutation variation in a list without a predicate defined for element comparison, in other words, a list without intrinsic order. So, the algorithm cannot rely on the comparisons by the “less than” operator “<” in next_permutation(). For example, either from string “abc” or “cab”, we always want all six permutations as mentioned above.

For this, I adapt the STL function to _next_permutation() by adding the third “map” parameter as shown in the following:

In the caller StlPermutation(), if an input is considered as non-ordered when bOrdered is false, I set a position map that acts as a media for an artificial (simulated) comparison. Then, if this pMap is passed into _next_permutation(), I use comparison (*pMap)[*i]<(*pMap)[*j] for a non-ordered situation, instead of *i<*j. Now, just two condition changes there make it a dual function.

A Recursive Solution

Another way for non-ordered permutation is using recursion. Although not so efficient as iteration, it is easier to construct naturally mirroring the problem. I create a recursive function as follows, more concise than Steinhaus-Johnson-Trotter algorithm.

In this RecPermutation(), I strip each character aside, make a recursive call for the rest of the string, and once it returns, concatenates that character with permuted results. Obviously, this is more comprehensible than _next_permutation().

With Repeated Elements

Sometimes, we see a variation of non-ordered permutation where repeated elements are allowed. For instance, given “aab” or “aba”, the desired permutation pattern might be “aab”, “aba”, and “baa”, but from RecPermutation(), we still get six strings with each of the three appearing twice. Also, by a little modification of RecPermutation(), I achieved this method in the following function:

As you see, I add the second parameter bAllowRepeated, and when this flag is false, I check the stripped character to skip repeated one if any. This simply enhances RecPermutation() as an alternative usage. Try to imagine altering an iteration function this way – really not easy!

Test and Comparison

Surely, you can search online for more permutation solutions. Among them, it’s worthy of mentioning this solution, created by Phillip Fuchs. There the iterative algorithm is pretty impressive and works efficiently for a non-ordered and non-repeated element list. I included his Example2 in my test program to examine an input "ijabcdefgh" as shown below:

Also, I made a comparison using the Permute.exe release build in my 2.2GHz P4 XP laptop, as shown in the following table:

As expected, the ordered _next_permutation() generates only part of permutations for the partially ascending "ijabcdefgh", while the non-ordered _next_permutation() generates all. The recursive RecPermutation() takes 45 seconds, not efficient as STL iteration (6 seconds), while Phillip’s example is a bit better than _next_permutation(). However, only the enhanced RecPermutation() excludes redundant permutations in a repeated element list, where the additional expense looks trivial.

your article is most interresting
i consider to use the algorithm for
the folowing:
given array = seria of integers in
ascending order (arbitrary length),

create a function with:
input :-p_n a number between 0 and
number of possible permutations of
the array.
- sl = a number of integers to
select from the array.
output : - a uniqu set of sl selected
integers , according to p_n (each value
in p_n will produce a different set of
integers).

Please refer to Mr. Phillip Paul Fuchs' sites: http://www.geocities.com/permute_it/sjt_algo.html or http://www.geocities.com/permute_it/01example.html, where the arrays are implemented. If you want to collect the all Permutations - "a set of sl selected integers", replace the display(a, j, i); But this may exhausts the memory if the size of your list is a little bit large.