• https://me.yahoo.com
COST (GBP)
4.91
0.00
0

Permutation Lex

Progressively generates all the permutations of the given size, in lexicographic order.
Controller: CodeCogs

C++

Class PermutationLex

Consider the permutations of size n

$\sigma&space;=&space;\left(&space;\begin{array}{cccc}&space;1&space;&&space;2&space;&&space;\ldots&space;&&space;n&space;\cr&space;\sigma(1)&space;&&space;\sigma(2)&space;&&space;\ldots&space;&&space;\sigma(n)&space;\end{array}&space;\right)$

and

$\tau&space;=&space;\left(&space;\begin{array}{cccc}&space;1&space;&&space;2&space;&&space;\ldots&space;&&space;n&space;\cr&space;\tau(1)&space;&&space;\tau(2)&space;&&space;\ldots&space;&&space;\tau(n)&space;\end{array}&space;\right)$

Now consider the next two numbers in the numerical base $\inline&space;&space;n&space;+&space;1$, corresponding to each permutation

$N_{\sigma}&space;=&space;\sum_{i&space;=&space;0}^{n&space;-&space;1}&space;(n&space;+&space;1)^i&space;\sigma(n&space;-&space;i)&space;\qquad&space;N_{\tau}&space;=&space;\sum_{i&space;=&space;0}^{n&space;-&space;1}&space;(n&space;+&space;1)^i&space;\tau(n&space;-&space;i)$

Then $\inline&space;&space;\tau$ is said to be the lexicographic succesor of $\inline&space;&space;\sigma$ if and only if $\inline&space;&space;N_{\tau}&space;>&space;N_{\sigma}$.

This class progressively generates all the permutations of the given size, in lexicographic order, starting with the identical permutation.

Example:

#include <codecogs/maths/combinatorics/permutations/permutationlex.h>
#include <iostream>
int main()
{
Maths::Combinatorics::Permutations::PermutationLex P(7);
std::cout << "The first 5 lexicographic permutations of 7 elements:";
std::cout << std::endl;
for (int i = 0; i < 5; i++)
{
std::vector<int> alpha = P.getNext();
for (int j = 0; j < alpha.size(); j++)
std::cout << alpha[j] << " ";
std::cout << "\t rank = " << P.getRank();
std::cout << std::endl;
}
return 0;
}

Output:

The first 5 lexicographic permutations of 7 elements:
1 2 3 4 5 6 7    rank = 1
1 2 3 4 5 7 6    rank = 2
1 2 3 4 6 5 7    rank = 3
1 2 3 4 6 7 5    rank = 4
1 2 3 4 7 5 6    rank = 5

References:

SUBSET, a C++ library of combinatorial routines, http://www.csit.fsu.edu/~burkardt/cpp_src/subset/subset.html

Authors

Lucian Bentea (August 2005)
Source Code

Source code is available when you buy a Commercial licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.