Computes the Ulam metric distance of two permutations.

View versions (1)

Interface

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

using namespace Maths::Combinatorics::Permutations;

Consider two permutations of the same order n

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

and

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

If we let L(\alpha) be the length of the longest ascending subsequence of a permutation \alpha, then the Ulam metric distance between \sigma and \tau is given by:

U = n - L(\sigma \circ \overline{\tau})
(3)

where \overline{\tau} is the inverse of the \tau permutation.

This function calculates the value of U based on the formula given above.

References

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

Example 1

#include <codecogs/maths/combinatorics/permutations/ulam_distance.h>
#include <iostream>
int main()
{
  int sigma[5] = {1, 2, 3, 4, 5}, tau[5] = {5, 2, 1, 4, 3},
  distance = Maths::Combinatorics::Permutations::ulam_distance(5, sigma, tau);
  std::cout << "The Ulam metric distance of Sigma and Tau is: ";
  std::cout << std::endl << distance << std::endl;
  return 0;
}

Output:

The Ulam metric distance of Sigma and Tau is:
3

Parameters

n
the size of the permutations
a
the first permutation
b
the second permutation

Returns

the value of the Ulam metric distance between a and b
GPL Licence — free for non commercial use. See Licence details.