I have forgotten

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

# Cycles

Counts the number of cycles in a permutation.
Controller: CodeCogs
Contents

C++

## Cycles

 intcycles( 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> cycle </em> is a sequence of indices $\inline&space;&space;i_1,&space;i_2,&space;\ldots,&space;i_k$ such that

$i_p&space;=&space;\sigma(i_{p-1}),&space;\qquad&space;\forall&space;p&space;=&space;\overline{2,&space;k}$

and

$\sigma(i_k)&space;=&space;i_1$

This function calculates the number of cycles in a given permutation, using an efficient algorithm.

## Example:

#include <codecogs/maths/combinatorics/permutations/cycles.h>
#include <iostream>
int main()
{
int beta[9] = {2, 3, 9, 6, 7, 8, 5, 4, 1};
std::cout << "The number of cycles of the Beta permutation: ";
std::cout << Maths::Combinatorics::Permutations::cycles(9, beta);
std::cout << std::endl;
return 0;
}

## Output:

The number of cycles of the Beta 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 cycles 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.