n -bit codes, in which any two adjacent codes differ in exactly one bit.
- empty sequence.
- sequence of gray codes of order
.
- sequence
written in reverse order. Then the sequence
is obtained by assigning a zero to the left of each code in the sequence
and the units on the left to each code
and their subsequent unification, i.e.
. It is easy to verify that the property of the Gray codes is in the sequence
constructed in this way is not disturbed. 
.
.
Actually, as mentioned in the proof, the Gray code of order n is obtained from the Gray code of order n-1 by assigning zero to the left (this corresponds to the function call
and assignment before it) and the reverse Gray code of the order n-1 by assigning one to the left (this corresponds to the function call
and assignment before him).
is a complete binary tree of height n . Consequently, the total number of function calls when generating Gray codes of order n is nothing more than
, which is equal to the number of all possible n- bit codes. Therefore, the algorithm is optimal.
in which only binary codes are left with the number of units equal to k , are arranged in the order of minimal change — with a difference of exactly two digits — are obtained from each other by replacing one element. Therefore, we proceed to the further study of the properties of symmetrically reflected gray codes.
- the string obtained by concatenating the symbol x ( m times) and concatenating the symbol y ( k times). Example:
.
- a sequence of all n- bit codes with k units, in which the codes are in the order in which they are located in the sequence
.
- a sequence of all n- bit codes with k units, in which the codes are in the order in which they are located in the sequence
.
units in sequence
has the appearance
and the last one is
or
, if a
.
and
.
- true (it is easy to check the formulas for the two codes 0 and 1).
and any
. Recall the formula for proving the existence of Gray codes:
. From here, we get that the first code with
units in sequence
with a zero assigned to the left is the first code with
units in sequence
. With
we will get a single code with
unit for which the formula is also true. With
latest code with
units in sequence
there is a first code with
unit and unit assigned to the left in the sequence
. With
the formula is also true. 
differ in exactly two digits.
and
.
- obvious (only one possible code in the sequence).
and
. With
or
we have only one code in the sequence
. With
neighboring words are completely in
or in
by induction hypothesis, they differ exactly in two digits. It remains to check one pair of adjacent codes in
latest code with
units in
and the first code with
units in
(last with
unit in
with the unit assigned to the left). By the lemma, with
they have a look
and
respectively, and at
-
and
. It is not difficult to see that they differ exactly in two digits. 
leave only those binary codes in which there are exactly k ones, then they will be arranged in the order of minimal change (each following one is obtained from the previous one by replacing one element).
is given by the following recurrence relation:
. This can be verified by induction.
is given by the following recurrence relation:
. Also verified by induction.
I would like to draw attention to the fact that the output of a binary code also implies the filling of the remaining digits with ones or zeros, depending on the value of u and v .
. We get the final asymptotics
. This is a rather rough estimate, but it is any better.
, especially when the value of k tends to n : the algorithm is almost linear.
will be binary, but not complete. Let us make a parallel assessment of complexity in terms of trees: each sheet corresponds to the receipt of some binary code, and the distance to the sheet in the tree does not exceed n . Evaluation
tells us that we simply sum up the upper bound for the lengths - n - of all the paths to the sheets. But many of these paths have a length less than n , and even more paths intersect with each other. This should make us think that the asymptotics of the above algorithm is actually much better.Source: https://habr.com/ru/post/200806/
All Articles