Permute 2 2 2 6

This chapter describes functions for creating and manipulatingpermutations. 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 permutationwhich exchanges the last two elements of a four element vector.The corresponding identity permutation is .

  1. Permute 2 2 2 6 Puzzle
  2. Permute 2 2 2 64
  3. 2 Permute 2
  4. Permute 2 2 2 6 Puzzle Answer

Note that the permutations produced by the linear algebra routinescorrespond to the exchange of matrix columns, and so should be consideredas applying to row-vectors in the form rather thancolumn-vectors, when permuting the elements of a vector.

The functions described in this chapter are defined in the header filegsl_permutation.h.

Permute
  • The 'Left' and 'Right' halves of the table show which bits from the input key form the left and right sections of the key schedule state. Note that only 56 bits of the 64 bits of the input are selected; the remaining eight (8, 16, 24, 32, 40, 48, 56, 64) were specified for use as parity bits. Permuted choice 2 (PC-2).
  • In particular, they are called the permutations of five objects taken two at a time, and the number of such permutations possible is denoted by the symbol 5 P 2, read “5 permute 2.” In general, if there are n objects available from which to select, and permutations ( P ) are to be formed using k of the objects at a time, the number of.

Evalute the permutation 6 P 2 A permutation is a way to order or arrange a set or number of things The formula for a combination of choosing r ways from n possibilities is.

The Permutation struct¶

gsl_permutation

A permutation is defined by a structure containing two components, the sizeof the permutation and a pointer to the permutation array. The elementsof the permutation array are all of type size_t. Thegsl_permutation structure looks like this:

Permutation allocation¶

gsl_permutation * gsl_permutation_alloc(size_t n)

This function allocates memory for a new permutation of size n.The permutation is not initialized and its elements are undefined. Usethe function gsl_permutation_calloc() if you want to create apermutation which is initialized to the identity. A null pointer isreturned if insufficient memory is available to create the permutation.

gsl_permutation * gsl_permutation_calloc(size_t n)

This function allocates memory for a new permutation of size n andinitializes it to the identity. A null pointer is returned ifinsufficient memory is available to create the permutation.

void gsl_permutation_init(gsl_permutation * p)

This function initializes the permutation p to the identity, i.e..

void gsl_permutation_free(gsl_permutation * p)

This function frees all the memory used by the permutation p.

int gsl_permutation_memcpy(gsl_permutation * dest, const gsl_permutation * src)

This function copies the elements of the permutation src into thepermutation dest. The two permutations must have the same size.

Accessing permutation elements¶

The following functions can be used to access and manipulatepermutations.

size_t gsl_permutation_get(const gsl_permutation * p, const size_t i)

This function returns the value of the i-th element of thepermutation p. If i lies outside the allowed range of 0 to then the error handler is invoked and 0 is returned. An inline version of this function is used when HAVE_INLINE is defined.

int gsl_permutation_swap(gsl_permutation * p, const size_t i, const size_t j)

This function exchanges the i-th and j-th elements of thepermutation p.

Permutation properties¶

size_t gsl_permutation_size(const gsl_permutation * p)

This function returns the size of the permutation p.

size_t * gsl_permutation_data(const gsl_permutation * p)

This function returns a pointer to the array of elements in thepermutation p.

int gsl_permutation_valid(const gsl_permutation * p)

This function checks that the permutation p is valid. The nelements should contain each of the numbers 0 to n-1 once and onlyonce.

Permutation functions¶

void gsl_permutation_reverse(gsl_permutation * p)

This function reverses the elements of the permutation p.

int gsl_permutation_inverse(gsl_permutation * inv, const gsl_permutation * p)

This function computes the inverse of the permutation p, storingthe result in inv.

int gsl_permutation_next(gsl_permutation * p)

This function advances the permutation p to the next permutationin lexicographic order and returns GSL_SUCCESS. If no furtherpermutations are available it returns GSL_FAILURE and leavesp unmodified. Starting with the identity permutation andrepeatedly applying this function will iterate through all possiblepermutations of a given order.

int gsl_permutation_prev(gsl_permutation * p)

This function steps backwards from the permutation p to theprevious permutation in lexicographic order, returningGSL_SUCCESS. If no previous permutation is available it returnsGSL_FAILURE and leaves p unmodified.

Applying Permutations¶

The following functions are defined in the header files gsl_permute.hand gsl_permute_vector.h.

int gsl_permute(const size_t * p, double * data, size_t stride, size_t n)

This function applies the permutation p to the array data ofsize n with stride stride.

int gsl_permute_inverse(const size_t * p, double * data, size_t stride, size_t n)

This function applies the inverse of the permutation p to thearray data of size n with stride stride.

int gsl_permute_vector(const gsl_permutation * p, gsl_vector * v)

This function applies the permutation p to the elements of thevector v, considered as a row-vector acted on by a permutationmatrix from the right, . The -th column of thepermutation matrix is given by the -th column of theidentity matrix. The permutation p and the vector v musthave the same length.

int gsl_permute_vector_inverse(const gsl_permutation * p, gsl_vector * v)

This function applies the inverse of the permutation p to theelements of the vector v, considered as a row-vector acted on byan inverse permutation matrix from the right, . Notethat for permutation matrices the inverse is the same as the transpose.The -th column of the permutation matrix is given bythe -th column of the identity matrix. The permutation pand the vector v must have the same length.

int gsl_permute_matrix(const gsl_permutation * p, gsl_matrix * A)

This function applies the permutation p to the matrix A fromthe right, . The -th column of thepermutation matrix is given by the -th column of theidentity matrix. This effectively permutes the columns of A accordingto the permutation p, and so the number of columns of A mustequal the size of the permutation p.

int gsl_permutation_mul(gsl_permutation * p, const gsl_permutation * pa, const gsl_permutation * pb)

This function combines the two permutations pa and pb into asingle permutation p, where The permutation p is equivalent to applying pb first andthen pa.

Reading and writing permutations¶

The library provides functions for reading and writing permutations to afile as binary data or formatted text.

int gsl_permutation_fwrite(FILE * stream, const gsl_permutation * p)

This function writes the elements of the permutation p to thestream stream in binary format. The function returnsGSL_EFAILED if there was a problem writing to the file. Since thedata is written in the native binary format it may not be portablebetween different architectures.

int gsl_permutation_fread(FILE * stream, gsl_permutation * p)

This function reads into the permutation p from the open streamstream in binary format. The permutation p must bepreallocated with the correct length since the function uses the size ofp to determine how many bytes to read. The function returnsGSL_EFAILED if there was a problem reading from the file. Thedata is assumed to have been written in the native binary format on thesame architecture.

int gsl_permutation_fprintf(FILE * stream, const gsl_permutation * p, const char * format)

This function writes the elements of the permutation pline-by-line to the stream stream using the format specifierformat, which should be suitable for a type of size_t.In ISO C99 the type modifier z represents size_t, so'%zun' is a suitable format 1.The function returns GSL_EFAILED if there was a problem writingto the file.

int gsl_permutation_fscanf(FILE * stream, gsl_permutation * p)

This function reads formatted data from the stream stream into thepermutation p. The permutation p must be preallocated withthe correct length since the function uses the size of p todetermine how many numbers to read. The function returnsGSL_EFAILED if there was a problem reading from the file.

Permutations in cyclic form¶

A permutation can be represented in both linear and cyclicnotations. The functions described in this section convert between thetwo forms. The linear notation is an index mapping, and has alreadybeen described above. The cyclic notation expresses a permutation as aseries of circular rearrangements of groups of elements, orcycles.

For example, under the cycle (1 2 3), 1 is replaced by 2, 2 is replacedby 3 and 3 is replaced by 1 in a circular fashion. Cycles of differentsets of elements can be combined independently, for example (1 2 3) (45) combines the cycle (1 2 3) with the cycle (4 5), which is an exchangeof elements 4 and 5. A cycle of length one represents an element whichis unchanged by the permutation and is referred to as a singleton.

It can be shown that every permutation can be decomposed intocombinations of cycles. The decomposition is not unique, but can alwaysbe rearranged into a standard canonical form by a reordering ofelements. The library uses the canonical form defined in Knuth’sArt 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,

  1. Write all singleton cycles explicitly

  2. Within each cycle, put the smallest number first

  3. Order the cycles in decreasing order of the first number in the cycle.

For example, the linear representation (2 4 3 0 1) is represented as (14) (0 2 3) in canonical form. The permutation corresponds to anexchange of elements 1 and 4, and rotation of elements 0, 2 and 3.

The important property of the canonical form is that it can bereconstructed from the contents of each cycle without the brackets. Inaddition, by removing the brackets it can be considered as a linearrepresentation of a different permutation. In the example given abovethe permutation (2 4 3 0 1) would become (1 4 0 2 3). This mapping hasmany applications in the theory of permutations.

int gsl_permutation_linear_to_canonical(gsl_permutation * q, const gsl_permutation * p)

This function computes the canonical form of the permutation p andstores it in the output argument q.

int gsl_permutation_canonical_to_linear(gsl_permutation * p, const gsl_permutation * q)

This function converts a permutation q in canonical form back intolinear form storing it in the output argument p.

size_t gsl_permutation_inversions(const gsl_permutation * p)

This function counts the number of inversions in the permutationp. An inversion is any pair of elements that are not in order.For example, the permutation 2031 has three inversions, corresponding tothe pairs (2,0) (2,1) and (3,1). The identity permutation has noinversions.

size_t gsl_permutation_linear_cycles(const gsl_permutation * p)

This function counts the number of cycles in the permutation p, given in linear form.

size_t gsl_permutation_canonical_cycles(const gsl_permutation * q)

This function counts the number of cycles in the permutation q, given in canonical form.

Permute 2 2 2 6 Puzzle

Examples¶

The example program below creates a random permutation (by shuffling theelements of the identity) and finds its inverse.

Here is the output from the program:

The random permutation p[i] and its inverse q[i] arerelated through the identity p[q[i]]=i, which can be verifiedfrom the output.

The next example program steps forwards through all possible third orderpermutations, starting from the identity,

Here is the output from the program:

Permute 2 2 2 64

The permutations are generated in lexicographic order. To reverse thesequence, begin with the final permutation (which is the reverse of theidentity) and replace gsl_permutation_next() withgsl_permutation_prev().

References and Further Reading¶

The subject of permutations is covered extensively in the following,

  • Donald E. Knuth, The Art of Computer Programming: Sorting andSearching (Vol 3, 3rd Ed, 1997), Addison-Wesley, ISBN 0201896850.

For the definition of the canonical form see,

  • Donald E. Knuth, The Art of Computer Programming: FundamentalAlgorithms (Vol 1, 3rd Ed, 1997), Addison-Wesley, ISBN 0201896850.Section 1.3.3, An Unusual Correspondence, p.178–179.

Footnotes

1

In versions of the GNU C library prior to the ISO C99 standard,the type modifier Z was used instead.

  • DESCRIPTION

Math::Permute::Array - Perl extension for computing any permutation of an array. The permutation could be access by an index in [0,cardinal] or by iterating with prev, cur and next.

the output should be:

This module compute the i^{th} permutation of an array recursively. The main advantage of this module is the fact that you could access to any permutation in the order that you want. Moreover this module doesn't use a lot of memory because the permutation is compute. the cost for computing one permutation is O(n).

it could be optimize by doing this iteratively but it seems efficient. Thus this module doesn't need a lot of memory because the permutation isn't stored.

EXPORT

Permute [index, $ref_array]

Returns a reference on the index^{th} permutation for the array. This function should be called directly as in the example.

Apply_on_perms [func, $ref_array]

Applies the function on each permutation (this interface is efficient but limited).

new [ref_array]

Returns a permutor object for the given items.

next

Called on a permutor, it returns a reference on the array contening the next permutation.

2 Permute 2

prev

Called on a permutor, it returns a reference on the array contening the previous permutation.

cur

Called on a permutor, it returns a reference on the array contening the current permutation.

permutation [index, @array]

Called on a permutor, it returns a reference on a array contening index^{th} permutation for the array.

Permute 2 2 2 6 Puzzle Answer

cardinal

Called on a permutor, it returns the number of permutations

Internal functions

factorial [n]

returns the factorial of n. This is a internal function to calculate the number of permutations.

jean-noel quintin, <quintin_at_imag_dot_fr>

Copyright (C) 2012 by jean-noel quintin

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.12.4 or, at your option, any later version of Perl 5 you may have available.

To install Math::Permute::Array, copy and paste the appropriate command in to your terminal.

For more information on module installation, please visit the detailed CPAN module installation guide.