Mathematics and generic programming (7) replacement algorithm

Mathematics and generic programming    https://blog.csdn.net/nameofcsdn/article/details/110448717

table of Contents

One, mapping, transformation, replacement

1. Mapping

2. Transform

3. Replacement

2. Transformation group, permutation group, symmetry group (full permutation group)

1. Transformation group

2. Permutation group

3. Symmetry group (full permutation group)

3. Cayley's theorem

Four, transposition

Fifth, the replacement is broken down into cycles

Six, exchange two intervals

Seven, rotation (rotation)

Eight, use loops to perform rotation

Nine, upside down


One, mapping, transformation, replacement

1. Mapping

Set A to set B can define a mapping.

According to whether it is injective or surjective, mapping can be divided into four types: non-injective and non-injective, injective and non-surjective, surjective and non-injective, and one-to-one mapping.

2. Transform

The mapping of set A to itself is called transformation.

Naturally, there are injective transformations, surjective transformations and one-to-one transformations on the set A.

One-to-one mapping, one-to-one transformation, identity mapping, and identity transformation all have the same meaning.

3. Replacement

If A is a finite set, then the transformation on A is called permutation.

Standard notation:

\left(\begin{array}{llll} 1 & 2 & 3 & 4 \\ 2 & 4 & 1 & 3 \end{array}\right)

Simplified notation:

(2 4 1 3)

 

2. Transformation group, permutation group, symmetry group (full permutation group)

1. Transformation group

The group that consists of several transformations as elements and compound as an operation is called transformation group.

2. Permutation group

The group that consists of several permutations as elements and compound as a calculation is called permutation group.

3. Symmetry group (full permutation group)

The group formed by all permutations of n elements is called the symmetry group Sn, also called the total permutation group.

According to the definition, the symmetry group has the following properties:

Binary operation: composition (composition, the operation is associative)

Inverse operation: inverse permutation

Unit element: identity permutation

 

3. Cayley's theorem

Cayley's theorem: All groups with n elements are isomorphic with a subgroup of the symmetry group Sn.

// TODO: The proof of this theorem has not been studied much.

 

Four, transposition

Transposition is the replacement of the i-th and j-th elements (i≠j) and keep the rest of the elements unchanged.

Swap in C++ has the same meaning as transposition.

The swap operation only requires that its parameters meet the Semiregular concept, and it doesn't have to be Regular.

Transposition Lemma: Any kind of permutation is the product of several transpositions.

 

Fifth, the replacement is broken down into cycles

The permutation can be broken down into several cycles.

This decomposition can be written as (235614)=(1235)(46)

A cycle containing one element is called a trivial cycle.

Each non-trivial loop of length k requires k+1 assignment operations to complete.

Performing any replacement method in situ requires n-u+v assignment operations, where n is the number of elements, and u and v are the number of trivial and non-trivial cycles in the replacement method.

 

Six, exchange two intervals

You can use first0 and last0 to represent the left and right bounds of the first interval, and use first1 to represent the left bound of the second interval:

In order to be able to call the swap operation on the data, we must ensure that the value type of I0 is the same as the value type of I1. Since the concept has not been formally integrated into the C++ language, we temporarily express this requirement with comments in the code.

Here is an important programming principle: the principle of type separation.

If two pieces of data may be of different types, then don't assume that they must be the same type.

The above two iterator types can be different, so we can exchange a section of the linked list and a section of the array.

 

Seven, rotation (rotation)

Transpose n elements according to the value of k (k≥0) in the following form: (k mod n, k+1 mod n,..., k+n-2 mod n, k+n-1 mod n) Called k rotations of n elements.

Rotating Gries-Mills algorithm:

If the rotate algorithm can return the new value of the parameter m when the algorithm is executed, it will be helpful for many calculation scenarios, because the value reflects the position of the first element before the rotation and the position after the rotation. With this value, we can achieve identity permutation by rotate(f, rotate(f, m, l), l).

The number of assignments for the algorithm is 3 (n-gcd(n, k))

The Gries-Mills algorithm only needs to advance one position each time it moves, that is, the algorithm can be used for data structures such as singly linked lists.

 

Eight, use loops to perform rotation

The k rotations of n elements contain gcd (k, n) cycles, so only n + gcd (k, n) assignments are required.

The rotation algorithm implemented with the help of loops needs to perform jumps with a span greater than 1, which means that it has higher requirements on the iterator than the former. In other words, it requires the iterator to support random access.

First we implement a single loop:

template <ForwardIterator I, Transformation F>
void rotate_cycle_from(I i, F from) {
    ValueType<I> tmp = *i;
    I start = i;
    for (I j = from(i); j = start; j = from(j)) {
        *i = *j, i = j;
    }
    *i = tmp;
}

The rotate_cycle_from function can complete the loop where the iterator i is. From is the iterator for jump access. From(i) can be understood as: calculate where the element at position i comes from.

Use loops to perform rotation:

 

Nine, upside down

Inversion (reverse) is to replace a list containing k elements, so that the 0th and k-1th elements are interchanged, the 1st and k-2th elements are interchanged, and so on.

Use inversion to achieve rotation:

Guess you like

Origin blog.csdn.net/nameofcsdn/article/details/112448779