• https://me.yahoo.com
COST (GBP)
0.84
3.47
0

# Ulam Distance

Computes the Ulam metric distance of two permutations.
Controller: CodeCogs

C++

## Ulam Distance

 intulam_distance( int n int* a int* b )[inline]
Consider two permutations of the same order $\inline&space;&space;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)$

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

$U&space;=&space;n&space;-&space;L(\sigma&space;\circ&space;\overline{\tau})$

where $\inline&space;&space;\overline{\tau}$ is the inverse of the $\inline&space;&space;\tau$ permutation.

This function calculates the value of $\inline&space;&space;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

### Authors

Lucian Bentea (August 2005)
##### Source Code

Source code is available when you agree to a GP Licence or buy a Commercial Licence.

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