| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This chapter describes functions for creating and manipulating
permutations. A permutation
is represented by an array of
integers in the range 0 to
, where each value
occurs once and only once. The application of a permutation
to a vector
yields a new vector
where
.
For example, the array
represents a permutation
which exchanges the last two elements of a four element vector.
The corresponding identity permutation is
.
Note that the permutations produced by the linear algebra routines
correspond to the exchange of matrix columns, and so should be considered
as applying to row-vectors in the form
rather than
column-vectors, when permuting the elements of a vector.
The functions described in this chapter are defined in the header file ‘gsl_permutation.h’.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A permutation is defined by a structure containing two components, the size
of the permutation and a pointer to the permutation array. The elements
of the permutation array are all of type size_t. The
gsl_permutation structure looks like this,
typedef struct
{
size_t size;
size_t * data;
} gsl_permutation;
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function allocates memory for a new permutation of size n.
The permutation is not initialized and its elements are undefined. Use
the function gsl_permutation_calloc if you want to create a
permutation which is initialized to the identity. A null pointer is
returned if insufficient memory is available to create the permutation.
This function allocates memory for a new permutation of size n and initializes it to the identity. A null pointer is returned if insufficient memory is available to create the permutation.
This function initializes the permutation p to the identity, i.e.
.
This function frees all the memory used by the permutation p.
This function copies the elements of the permutation src into the permutation dest. The two permutations must have the same size.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following functions can be used to access and manipulate permutations.
This function returns the value of the i-th element of the
permutation p. If i lies outside the allowed range of 0 to
then the error handler is invoked and 0 is returned.
This function exchanges the i-th and j-th elements of the permutation p.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns the size of the permutation p.
This function returns a pointer to the array of elements in the permutation p.
This function checks that the permutation p is valid. The n
elements should contain each of the numbers 0 to
once and only
once.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function reverses the elements of the permutation p.
This function computes the inverse of the permutation p, storing the result in inv.
This function advances the permutation p to the next permutation
in lexicographic order and returns GSL_SUCCESS. If no further
permutations are available it returns GSL_FAILURE and leaves
p unmodified. Starting with the identity permutation and
repeatedly applying this function will iterate through all possible
permutations of a given order.
This function steps backwards from the permutation p to the
previous permutation in lexicographic order, returning
GSL_SUCCESS. If no previous permutation is available it returns
GSL_FAILURE and leaves p unmodified.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function applies the permutation p to the array data of size n with stride stride.
This function applies the inverse of the permutation p to the array data of size n with stride stride.
This function applies the permutation p to the elements of the
vector v, considered as a row-vector acted on by a permutation
matrix from the right,
. The
-th column of the
permutation matrix
is given by the
-th column of the
identity matrix. The permutation p and the vector v must
have the same length.
This function applies the inverse of the permutation p to the
elements of the vector v, considered as a row-vector acted on by
an inverse permutation matrix from the right,
. Note
that for permutation matrices the inverse is the same as the transpose.
The
-th column of the permutation matrix
is given by
the
-th column of the identity matrix. The permutation p
and the vector v must have the same length.
This function combines the two permutations pa and pb into a
single permutation p, where
. The permutation
p is equivalent to applying
first and then pa.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The library provides functions for reading and writing permutations to a file as binary data or formatted text.
This function writes the elements of the permutation p to the
stream stream in binary format. The function returns
GSL_EFAILED if there was a problem writing to the file. Since the
data is written in the native binary format it may not be portable
between different architectures.
This function reads into the permutation p from the open stream
stream in binary format. The permutation p must be
preallocated with the correct length since the function uses the size of
p to determine how many bytes to read. The function returns
GSL_EFAILED if there was a problem reading from the file. The
data is assumed to have been written in the native binary format on the
same architecture.
This function writes the elements of the permutation p
line-by-line to the stream stream using the format specifier
format, which should be suitable for a type of size_t. On a
GNU system the type modifier Z represents size_t, so
"%Zu\n" is a suitable format. The function returns
GSL_EFAILED if there was a problem writing to the file.
This function reads formatted data from the stream stream into the
permutation p. The permutation p must be preallocated with
the correct length since the function uses the size of p to
determine how many numbers to read. The function returns
GSL_EFAILED if there was a problem reading from the file.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A permutation can be represented in both linear and cyclic notations. The functions described in this section convert between the two forms. The linear notation is an index mapping, and has already been described above. The cyclic notation expresses a permutation as a series of circular rearrangements of groups of elements, or cycles.
For example, under the cycle (1 2 3), 1 is replaced by 2, 2 is replaced by 3 and 3 is replaced by 1 in a circular fashion. Cycles of different sets of elements can be combined independently, for example (1 2 3) (4 5) combines the cycle (1 2 3) with the cycle (4 5), which is an exchange of elements 4 and 5. A cycle of length one represents an element which is unchanged by the permutation and is referred to as a singleton.
It can be shown that every permutation can be decomposed into combinations of cycles. The decomposition is not unique, but can always be rearranged into a standard canonical form by a reordering of elements. The library uses the canonical form defined in Knuth's Art of Computer Programming (Vol 1, 3rd Ed, 1997) Section 1.3.3, p.178.
The procedure for obtaining the canonical form given by Knuth is,
For example, the linear representation (2 4 3 0 1) is represented as (1 4) (0 2 3) in canonical form. The permutation corresponds to an exchange of elements 1 and 4, and rotation of elements 0, 2 and 3.
The important property of the canonical form is that it can be reconstructed from the contents of each cycle without the brackets. In addition, by removing the brackets it can be considered as a linear representation of a different permutation. In the example given above the permutation (2 4 3 0 1) would become (1 4 0 2 3). This mapping has many applications in the theory of permutations.
This function computes the canonical form of the permutation p and stores it in the output argument q.
This function converts a permutation q in canonical form back into linear form storing it in the output argument p.
This function counts the number of inversions in the permutation p. An inversion is any pair of elements that are not in order. For example, the permutation 2031 has three inversions, corresponding to the pairs (2,0) (2,1) and (3,1). The identity permutation has no inversions.
This function counts the number of cycles in the permutation p, given in linear form.
This function counts the number of cycles in the permutation q, given in canonical form.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The example program below creates a random permutation (by shuffling the elements of the identity) and finds its inverse.
|
Here is the output from the program,
$ ./a.out initial permutation: 0 1 2 3 4 5 6 7 8 9 random permutation: 1 3 5 2 7 6 0 4 9 8 inverse permutation: 6 0 3 1 7 2 5 4 9 8 |
The random permutation p[i] and its inverse q[i] are
related through the identity p[q[i]] = i, which can be verified
from the output.
The next example program steps forwards through all possible third order permutations, starting from the identity,
|
Here is the output from the program,
$ ./a.out 0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0 |
The permutations are generated in lexicographic order. To reverse the
sequence, begin with the final permutation (which is the reverse of the
identity) and replace gsl_permutation_next with
gsl_permutation_prev.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The subject of permutations is covered extensively in Knuth's Sorting and Searching,
For the definition of the canonical form see,
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated by Autobuild on September, 26 2007 using texi2html 1.78.