• https://me.yahoo.com
COST (GBP)
1.25
0.19
0

Breaks

Counts the number of breaks in a permutation.
Controller: CodeCogs

C++

Break Count

 intbreak_count( int n int* p )
Consider the permutation

$\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)$

A <em> break </em> is a pair of neighbors whose values differ by more than 1, i.e.
$|\sigma(i)&space;-&space;\sigma(i+1)|&space;\neq&space;1,&space;\qquad&space;i&space;\in&space;\{1,&space;2,&space;\ldots,&space;n&space;-&space;1\}$

This function calculates the number of breaks in a permutation, using the following algorithm:
• Starting with a permutation of order n.
• We prepend an element labeled 0 and append an element labeled $\inline&space;&space;n&space;+&space;1$.
• There are now $\inline&space;&space;n&space;+&space;1$ pairs of neighbors.
• We now search for indices $\inline&space;&space;i&space;\in&space;\{0,&space;1,&space;2,&space;\ldots,&space;n\}$ such that the above
inequality stands.

The identity permutation has a break count of 0. The maximum break count is $\inline&space;&space;n&space;+&space;1$.

Example:

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

Output:

The number of breaks of the Sigma permutation: 3

References:

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

Parameters

 n the size of the permutation p the actual permutation stored as an array

Returns

the number of breaks found in the permutation

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.