Computes the inversion sequence of a permutation.

View versions (1)

Interface

#include <codecogs/maths/combinatorics/permutations/inversion_sequence.h>

using namespace Maths::Combinatorics::Permutations;

For a given permutation

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

the inversion sequence ( \alpha ) is defined as

\alpha(i) = \mathrm{card} \{ j < i | \sigma(i) < \sigma(j) \},
\quad \forall i = \overline{1, n}
(2)

with \alpha(1) = 0.

This function calculates the inversion sequence of a permutation based on the above formula and returns its value as a C++ vector object.

Returns

the inversion sequence of the given permutation

Example:

#include <codecogs/maths/combinatorics/permutations/inversion_sequence.h>
#include <iostream>
int main()
{
  int sigma[5] = {3, 5, 1, 4, 2};
  std::vector<int> result = Maths::Combinatorics::Permutations::inversion_sequence(5, sigma);
  std::cout << "The inversion sequence of the Sigma permutation: ";
  std::cout << std::endl;
  for (int i = 0; i < 5; i++)
    std::cout << result[i] << "  ";
  std::cout << std::endl;
  return 0;
}

Output:

The inversion sequence of the Sigma permutation:
0  0  2  1  3

References

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

GPL Licence — free for non commercial use. See Licence details.