# Permutation cycles in C

A cycle of a permutation is a subset of the elements that replace one another in sequence, until the last element is replaced by the first. For example, consider the permutation below:

$$\sigma=\begin{pmatrix} 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ 1 & 3 & 7 & 0 & 4 & 8 & 2 & 6 & 5\end{pmatrix}$$

We can find the cycles:
$$0 \rightarrow 1, 1 \rightarrow 3, 3 \rightarrow 0$$
$$2 \rightarrow 7, 7 \rightarrow 6, 6 \rightarrow 2$$
$$4 \rightarrow 4$$
$$5 \rightarrow 8, 8 \rightarrow 5$$

These can be written as:
$$(0, 1, 3)(2, 7, 6)(4)(5, 8)$$

It’s customary to omit cycles of length 1, so this would usually be written as
$$(0, 1, 3)(2, 7, 6)(5, 8).$$

To find the cycle decomposition of a permutation, we can use an algorithm that is very similar to depth-first search (DFS) on a graph. We begin a new search for each unvisited vertex (number), and visit its neighbour (image in the permutation) until we get back to the first vertex again.

Below is an implementation in C. The function permutation_cycles() takes a permutation in the form of integers starting from 0, its length, and a callback function that is called for each cycle found.

#include <stdlib.h>

typedef void(*cyclefn)(const unsigned int *, size_t);

void permutation_cycles_recursive(const unsigned int *permutation, unsigned int *visited,
unsigned int start, unsigned int current, unsigned int *path,
size_t length, cyclefn fun)
{
visited[current] = 1;
path[length] = current;
if (start == current && length > 0) {
fun(path, length);
}
else {
permutation_cycles_recursive(permutation, visited, start, permutation[current],
path, length + 1, fun);
}
}

void permutation_cycles(const unsigned int *permutation, size_t n, cyclefn fun)
{
unsigned int i;
unsigned int *visited = calloc(n, sizeof(unsigned int));
unsigned int *path = malloc(n * sizeof(unsigned int));
if (!(visited && path)) {
free(visited);
free(path);
return;
}
for (i = 0; i < n; i++) {
if (!visited[i]) {
permutation_cycles_recursive(permutation, visited, i, i,
path, 0, fun);
}
}
free(visited);
free(path);
}


Here is an example program that finds the cycle decomposition of the permutation shown above:

#include <stdio.h>

void print(const unsigned int *cycle, size_t length)
{
if (length > 1) {
unsigned int i;
putchar('(');
for (i = 0; i < length; i++) {
printf("%u", cycle[i]);
if (i < length - 1) {
printf(", ");
}
}
putchar(')');
}
}

int main(void)
{
unsigned int permutation[] = {1, 3, 7, 0, 4, 8, 2, 6, 5};
const size_t n = sizeof(permutation) / sizeof(permutation[0]);
permutation_cycles(permutation, n, print);
putchar('\n');
return 0;
}


The output:

(0, 1, 3)(2, 7, 6)(5, 8)