Distance-2 Constant-Weight: Chaining
Distance-2 Constant-Weight: Chaining
Distance-2 Constant-Weight: Chaining
2, FEBRUARY 1973
Abstract-The cyclic distance-2 chaining of constant-weight weight m, one can readily see that a distance-2 cyclic
codes has applications in A/D conversions as well as in combina- chaining of constant-weight n-vectors provides a means
torial problems involving the exhaustion of m-out-of-n combinations.
It is shown in this paper that such a chaining can be obtained from to achieve this goal.
the Gray code circuit and its transformations. Algorithms based on
several theorems derived have been developed and programmed in I I. SOME BASIC PROPERTIES
APL. Gray codes are related to conventional binary codes
in the following fashion: for a given binary n-vector
Index Terms-Constant-weight codes, enumeration of m-out-
of-n combinations, Gray codes, Hamiltonian circuits. b= (bn, * .*, b2,b1), there corresponds a Gray code
4= (gan . .*, g2, g.). The mapping is characterized by
I. INTRODUCTION the following relations:
A N n-dimensional binary Gray code G. is an ordered gn = bn
cyclic sequence of 2n binary n-vectors such that gi-bi+l + bi, mod 2 for i= 1,*y , n-1 .
analog-to-digital conversions to minimize the digital The integer value =1 bi2 -1 represented by the con-
error due to an ambiguous analog signal between two ventional binary code is also assigned as the value of the
adjacent quantum levels. In some A/D conversions, it is corresponding Gray code and this is denoted by VG(4).
desirable to incorporate additional error protection [4]- A Gray code may be conveniently represented by its
[6]. transition sequence T(Gn) [8]. The transition sequence
In this paper we are concerned with protection against for an n-dimensional Gray code is a cyclically ordered
asymmetrical errors, to which constant-weight codes listing of the bit positions that change as one proceeds
have been shown to be effective [71. Our objective here along the Gray code circuit. Normally, the starting code
is to construct a class of codes with the combined fea- vector is taken to be the all-0 n-vector. For example, the
tures of both Gray codes and constant-weight codes. transition sequence for a four-dimensional Gray code is
The problem is essentially the following: given a set of as follows:
(m) binary n-vectors of weight m, it is desired to order
them in a cyclic chain such that any two adjacent n-vec- T(G4) = 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 4.
tors in the chain differ in exactly two places. Before If the starting code vector is different from the all-0
going into the derivation of our solution to this prob- n-vector, then the same transition sequence T(Gn) will
lem, we wish to point out that such cyclic chaining of clearly result in a Gn' which is obtainable from G,n by
constant weight n-vectors has applications in other complementing all positions corresponding to the non-
areas. In combinatorial problems involving n compo- zero positiqns in the starting code vector. We shall show
nents, for instance, one may wish to exhaust totally or in what follows that if we take a set of code vectors con-
partially the (.) combinations of m components. Since sisting of all constant-weight vectors while preserving
each component may be associated with a large data set their order on the Gray code circuit, we will have a dis-
which may need further processing, a minimal replace- tance-2 cyclic chaining of constant-weight codes. We
ment of components between two consecutive combina- shall first make a few observations.
tions often leads to significant savings in both computa- If one takes a set of code vectors consisting.of all
tion and data retrieval time. The ideal situation is to n-vectors with a particular position constrained to a con-
enumerate all (f) combinations such that exactly one stant, that is, for some i, gi = 0 (or gi = 1), while preserv-
replacement is needed from one combination to the next. ing their order on the Gray code circuit, then the (n-1)
If one represents each combination by an n-vector of subcube resulting from the deletion of gt forms a Gray
code Gn-l' such that Gn-l' is identical to G.-, (or if
Manuscript received July 24, 1971; revised August 17, 1972. This gi= 1, G.-,' is identical to Gn-, except that gi-, is com-
paper was presented in part at the 5th Annual Princeton Conference plemented). One also observes that, in a transition se-
on Information Sciences and Systems, March 1971.
The authors are with the IBM T. J. Watson Research Center, quence between two adjacent occurrences of any i
Yorktown Heights, N. Y. 10598. (i= 1, * **, n), gi on the corresponding Gray code cir-
Authorized licensed use limited to: POLO BIBLIOTECARIO DI INGEGNERIA. Downloaded on May 06,2021 at 07:45:46 UTC from IEEE Xplore. Restrictions apply.
TANG AND LIU: DISTANCE-2 CYCLIC CHAINING 177
cuit must stay constant. Furthermore, between two than two, then there exist u and v, also n-vectors of
adjacent occurrences of any i in a transition sequence, weight m, which interleave with x and y on the Gray
every j #i, except one, must appear an even number of code circuit Gn.
times [8]. It can be shown that, with the constraint Proof: From the constant-weight property of these
gi 0, the resulting transition sequence can be obtained n-vectors, the Hamming distance between any pair of
by the following procedure: subsequences between the such n-vectors must be even with exactly half of the dis-
first and the second, the third and the fourth i's, etc., are tance resulting from 0-1 changes and the other half from
replaced by the numbers each of which appears exactly 1-0 changes. Since the Hamming distance between x
once in their corresponding subsequence. For gi = 1, the and y is assumed to be greater than two, it must be four
procedure is the same except that the subsequences are or more. Let the positions where x and y differ be in-
taken between the second and third i's, etc., as well as dexed by ii, i2, * *, i2k-1 i2k, where il > i2, ' * ', > i2k-1
-
the last and the first i's. As an example, let us consider >i2k, k>2, and let us consider the subcube circuit G2k'
G4 constrained by g2 = 0. The subsequences of the transi- obtained by constraining all other positions to the cor-
tion sequence T(G4) are shown in brackets: responding values in x and y. From earlier observations,
T(G4) = 1[2 1 3 1 2]1 4 1[2 1 3 1 2]1 4. the bit patterns at i1 and i2 in G2k' are the patterns of G2'
with each bit pattern repeated consecutively 22k-2 times.
Replacing these subsequences by 3's, we obtain the Since the Hamming distance between (xi,, xi2) and
transition sequence corresponding to G3', (Yil, yi2) is two, there must exist (uil, ui2) and (vil, vi2)
T(G3') = 1 3 1 4 1 3 1 4. interleaving (xil, xi2) and (yil, yi2) on G2'. Furthermore,
there are 22k-2 consecutive 2k-vectors on G2k' each start-
If g2 =1 we have different subsequences as shown below: ing out with one of the four bit pairs described above.
T(G4) = 1 2] 1 3 1 [2 1 4 1 2] 1 3 1 [2 1 4. Consider the set of 2k-vectors starting with ui1 and ui2.
One of the 2 2k-2 vectors takes the following form:
Replacing these subsequences by 4's, we obtain the ui1t ui2, (1 -uil), (1 -ui9), 1, 0, * . , 1, 0. Clearly this
-
transition sequence corresponding to G3", 2k-vector is of weight k, and thus u, the corresponding
T(G3") 1 3 1 4 1 3 1 4. n-vector (by restoring the constrained bits previously
defined in x and y) must be of weight m. The same argu-
Note that T(G3') and T(Gs') are the same, and are ment can be used to establish the existence of another
equivalent to, when reindexed, the ordinary T(G3). How- n-vector, v. Since the four sets of constrained n-vectors
ever, Gs' can be obtained from T(G3') with a starting were seen to interleave, so do x and y with respect to
vector of (000) while G3" can be obtained from the same u and v.
transition sequence with a different starting vector of Theorem 2: The set of (4 ) n-vectors of weight m, when
(001). Consequently, G3' can be obtained directly from chained according to the ordering on the Gray code G.,
GS' by complementing position 1. In general, if the con- has a Hamming distance of exactly two between every
straint is gi= 0, then G.-,', when reindexed, is the same pair of adjacent code vectors.
as G.-,. On the other hand, if the constraint is gi= 1 Proof: From Theorem 1, if any two n-vectors of
(i* 1), then G,,-1', when reindexed, is the same as G.- weight m differ by a Hamming distance of greater than
except that the position i-I is complemented. If, how- two, they cannot be adjacent on the distance-2 chain
ever, an arbitrary number of n-k positions are con- defined by the ordering of G. on the set of (8 ) n-vectors
strained, then by applying the above argument re- of weight m.
peatedly, one arrives at the following assertion: Gk',
when reindexed, is the same as Gk except that each posi- III. ALGORITHMS
tion immediately after (i.e., with a smaller index) a run In Section II we derived a solution to our problem,
of constrained positions should be complemented if this the distance-2 cyclic chaining of constant-weight codes.
run has an odd number of positions constrained to one. We shall next investigate methods of determinating the
One further observes that, since Gk' can be obtained subsequent constant-weight code vectors on a Gray code
from Gk by complementing at some bit positions, the circuit Gn given a starting code vector. One can obvi-
following basic property of a conventional Gray code ously generate the complete Gn and extract from it the
must be-retained in Gk': For any k'<k, the bit patterns code vectors with a desired weight, starting from the
of the highest k' positions in Gk' can be obtained from a given code. However, this straightforward method can
Gk' code (by further constraining Gk' -the lowest k-k' be shown to be rather inefficient as n becomes large.
positions) with each code vector repeated consecutively This is because we are interested in only (,) code vectors
2k-' times. We are now ready to prove the following out of 2n possible ones, and the ratio of (n) to 2" tends to
theorems. zero as n grows indefinitely.
Tkeorem 1: If x and y are two n-vectors of weight m, An immediate improvement can be obtained by ob-
and if the Hamming distance between x and y is greater serving that constant weightness in g implies that
Authorized licensed use limited to: POLO BIBLIOTECARIO DI INGEGNERIA. Downloaded on May 06,2021 at 07:45:46 UTC from IEEE Xplore. Restrictions apply.
178 IEEE TRANSACTIONS ON COMPUTERS, FEBRUARY 1973
Fig. 1. The tree of ending patterns for binary vectors corresponding to constant-weight Gray codes.
bi Ij=1 gj (mod 2) equals a constant (i.e., either 1 or 0). described above. We shall develop such a procedure in
One therefore needs to exhaust no more than 2n-1 code the following section.
vectors. Since this only reduces the amount of computa- If the set of constant weight Gray codes is trans-
tion by a factor of 2, it does not change the asymptotic formed into the corresponding set of binary codes, the
behavior of the procedure for large n. Substantial im- constraint of constant weightness becomes a constraint
provements can be obtained as a result of the following of constant number of changes between 0 and 1, and
theorem. vice versa (assuming the vector is augmented by a run
Theorem 3: Given x as an n-vector of weight m, let y of 0's at its most significant end). Given a binary code
be the next n-vector of weight m on the Gray code G, vector of m 1-0, 0-1 changes, our problem is to find
then there exists a uniquej such that VG(X) - VG(Y) 2 another vector with the same number of changes such
mod 2 , where .<j<max [m, n-m]. that the increment (mod 2") in the corresponding binary
The validity of this theorem will become evident in a value is the smallest. To do this, let us examine Case 1
subsequent analysis. As a result of Theorem 3, one can corresponding to the top branch of the tree shown in
envision the following algorithm to determine the sub- Fig. 1. In this case m is even and the binary vector has
sequent constant-weight code vectors. an ending pattern in its least significant position con-
Step 1: For the given weight m code vector g0, find its sisting of exactly k(k22) O's:
value VG(go). x ... x 1 0 -0,
Step 2: Add successively 2i, j = 1, 2, * , max [im, k O'S, -(k > 2)
n-rm] to V0(go) and find the smallest j =j' that results
in a Gray code vector of weight m. where x's indicate DON'T CARE bits. It is not difficult to
Step 3: Add 2P' to V0(g0) and from the sum (mod 2X) see that when we increase successively by 1 the value of
find the corresponding Gray vector gi. this vector, the first resultant binary vector to satisfy
Step 4: With go = gi, repeat Steps 1-3 until all (n ) vec- the constraint of constant 1-0, 0-1 change must have
tors are obtained. the following ending:
A more desirable algorithm would be based on a direct (k1 - 10
determination of j' without the trial and test procedure (kE-1)0'S.
Authorized licensed use limited to: POLO BIBLIOTECARIO DI INGEGNERIA. Downloaded on May 06,2021 at 07:45:46 UTC from IEEE Xplore. Restrictions apply.
TANG AND LIU: DISTANCE-2 CYCLIC CHAINING 179
1 110 ...O
k'Il's (k/2)(1O)s, k=even
0
(k'+2)0's (k/2)-l (lO)s
.10 ...10
then pi=j+I and p2=n.
(n- k) l's (k/2)(IO)s, k= m (n-k) O's (k/2)(10)s 2 (mad 2 Rule 2: m is odd.
6
x ,xlOl ... I
',-
x...xl10-... 01
2
a) If the ending is
k 1 's, ka2 (k-I1)l's
xOOl001 10I 1
xv. x 1 1 0 0 ...0
x x ... ... k
7_ 2
c
k tI's, kz>2 (k+l) 1l's j 0's
8 < ....~'2k-2
(k/2) (01)s, k =even (k/2) -1 (01)s then pi=1 and P2=j+ 2.
c X .. xlI Ol ... 101 . 01 X ...Xil ....l 101 .01.. Is
If the ending is
I k' I's (k/2) (01)s, k-even k?2 ('-l)0's (0/2)(01)s
o 10 x x00 ... 101,.01 ... nX lO...
0's
0,10
0 . 01 e x ..x 0 1 0 0 .0
k' l's (k/2)(01)s, k=even k>2 k' (k/2)(01)s
_ I I 01 .01 010 1 01...01 01 j O's
__'2k+__mo__2_
...
11 w
(n- k) I1'5 (k/2) (01)s k - m -1 (n-k-l)O's (k/2)(01)s to
then pi =j+ 1, P2-j+2.
b) If the ending is
In this case the increment of the binary values equals to
2k-1. This together with increments of other cases are x . ..x 0 1 1*.1
summarized in Table I. Since the cases considered in j= odd) l's
Table I are exhaustive (as depicted in Fig. 1), the valid-
ity of Theorem 3 is established. Moreover, an algo- then pi=max (1, j-1), P2=j+I.
rithmic implementation can be readily obtained. If the ending is
An analogous analysis can be carried out directly on x ...x 1 0 .. 0 1 1 ... 1 I
Gray code vectors. As a result, the two positions to be j'(>1)0's j(n= even)l's
complemented to chain a Gray code vector to the next
one can be directly determined by its ending pattern. then pl=j and P2=j+j'+2.
This is summarized in the following theorem. If the ending is
Theorem 4: Given a Gray code vector g = (gn, gn-i, x- I
x 1 1 0.. 0 1** 1 1
1
. gi)
gi of weight m, the next code vector of weight m
on the Gray code circuit can be obtained by comple- j'(> 1)O's j(= even) l's
menting two positions, Pi, P2 (Pl, P2= 1, 2, * *, n) in 4 then pi=j+ 1, P2=j+ji +2.
according to the following rules. If the vector is
Rule 1: m is even.
a) If the ending of 4 is 1 0 0 1 1.**1
x x 1 0 .-0 allO's j(=m-1)l's
O's then pi=j+1, p2=n.
then Pi=j and P2=j+1. Clearly, Theorem 4 can be readily translated into an
b) If the ending is algorithm for distance-2 cyclic chaining of constant-
weight codes.
x *xO 1 1**. 1 1 To demonstrate how this algorithm works, let us con-
j(= even) l's sider the following example. Suppose n = 7 and m =4.
The complete set of (7) = 35 code vectors generated by a
then pi=max (1,j-1) and p2=j+1l program implementing this algorithm is shown in Fig. 2.
If the ending is Notice the distance-2 cyclic chaining of these vectors.
x...x 0 1 0...0 1 1 ... 1 Let us take the 5th code vector (O 0 1 0 1 1 1) from
the list. Since m = 4 and the ending pattern is a run of 3
I'(.>)O's j(=odd)l's (odd) l's, this falls into the class described by the second
then pi=j and p2=i+i'+2. ending pattern considered in Rule lb) of Theorem 4.
Authorized licensed use limited to: POLO BIBLIOTECARIO DI INGEGNERIA. Downloaded on May 06,2021 at 07:45:46 UTC from IEEE Xplore. Restrictions apply.
ISO IEEE TRANSACTIONS ON COMPUTERS, FEBRUARY 1973
Authorized licensed use limited to: POLO BIBLIOTECARIO DI INGEGNERIA. Downloaded on May 06,2021 at 07:45:46 UTC from IEEE Xplore. Restrictions apply.