About the ordinances of the vectors of the -dimensional Boolean cube in accordance with their weights
The problem ”Given a Boolean function of variables by its truth table vector. Find (if exists) a vector of maximal (or minimal) weight, such that .” arises in computing the algebraic degree of Boolean functions or vectorial Boolean functions called S-boxes. The solutions to this problem have useful generalizations and applications. To find effective solutions we examine the ways of ordering the vectors of the Boolean cube in accordance with their weights. The notion ”-th layer” of the -dimensional Boolean cube is involved in the definition and examination of the ”weight order” relation. It is compared with the known relation ”precedes”. We enumerate the maximum chains for both relations. An algorithm that generates the vectors of the -dimensional Boolean cube in accordance with their weights is developed. The lexicographic order is chosen as a second criterion for an ordinance of the vectors of equal weights. The algorithm arranges the vectors in a unique way called a weight-lexicographic order. It is represented by the serial numbers of the vectors, instead of the vectors itself. Its time and space complexities are , i.e., of linear type with respect to the size of the output. The obtained results are summarized and added as a new sequence (A294648) in the OEIS.
Keywords: Boolean cube, binary vector, serial number, lexicographic order, weight order, maximum chains enumerating, weight-lexicographic order generating, power set generating, ranking
The binary vectors (binary words, binary sequences, bit strings, etc.) play an important role in all areas of Discrete mathematics and Computer science. The set of all -dimensional binary vectors is often called an -dimensional Boolean cube (hypercube). The most natural order of its vectors is the lexicographic order, which is a total order. There are other important orders—for example, in a Gray code (various types, considered exhaustively in the survey of C. Savage ), or in accordance with the relation ”precedence” which is a partial order. Here we consider a similar one—order in accordance with the weights of the binary vectors. Our study of this order is motivated by searching for efficient solutions to the following problem: ”Given a Boolean function of variables by its Truth Table vector, denoted by . Find (if exists) a vector of maximal (or minimal) weight, such that .”. This problem arises in computing the algebraic degree of Boolean functions or vectorial Boolean functions (called S-boxes) [4, 5]. The solutions to this problem have useful generalizations and applications that are commented here. The most natural way to solve this problem is to perform an exhaustive (linear) search: for any vector it checks whether and selects the one with a maximal (resp. minimal) weight. Since the values of correspond to the lexicographic order of the vectors of the -dimensional Boolean cube, such solution needs checks. However, if the search checks the values of in accordance with the vectors’ weights, the search will finish after finding the first vector , such that . Once the desired order of the vectors has been obtained, this approach needs operations. This order can be obtained by an algorithm that: (1) computes the vectors’ weights and (2) sorts the vectors in accordance with their weights. So it needs at least operations.
The ways for ordering the vectors of the Boolean cube in accordance with their weights are examined here. The necessary basic notions concerning the Boolean cube and their properties are given in Section 2. In Section 3 the key notion ”-th layer” of the Boolean cube is involved in discussing the ways for an ordinance of the vectors of the Boolean cube in accordance with their weights. The corresponding relation ”precedes by weight” is defined, investigated and compared with the known relation ”precedes”. The maximum chains for the Partially Ordered Sets (POSets) determined by both relations are enumerated (the corresponding notes are appended to the sequences A051459 and A000142 in the OEIS ). A special way for an ordinance of the vectors in weight order (WO) is defined in the second part of Section 3. It is represented by the serial numbers of the vectors instead of the vectors themselves. Theorem 6 shows that the lexicographic order is the second criterion for an ordinance of the vectors of equal weights in their WO. So a unique order is obtained and it is called a Weight-Lexicographic Order (WLO). An algorithm that generates the sequence of the serial numbers of all -dimensional binary vectors in WLO is created. It is represented in Section 4. The essential parts of its code (in the C programming language) and some results of its performance are given. These results are summarized and added as a new sequence (A294648) in the OEIS . The correctness of the algorithm is shown and its time and space complexities are evaluated—they are both . Some concluding remarks are given in the last section.
2 Basic notions and properties
After studying numerous books and textbooks, we could not find a whole topic (part or chapter), devoted to the -dimensional Boolean cube. The notions and the assertions given in this part can be found in most of the sources cited here. However, they are scattered in different parts and have different names and notations. So we represent the necessary basic notions about the Boolean cube and its properties following .
Here denotes the set of natural numbers. We consider that and is the set of positive natural numbers.
Usually, the -dimensional Boolean cube is defined as , i.e., the set of all -dimensional binary vectors. So their number is . The following alternative, inductive and constructive definition is more useful for us further.
1) The set is called one-dimensional Boolean cube and its elements and are called one-dimensional binary vectors.
2) Let , be the -dimensional Boolean cube and be its -dimensional binary vectors.
3) The -dimensional Boolean cube is built by taking the vectors of twice: firstly, each vector of is prefixed by zero, and thereafter each vector of is prefixed by one, i.e.,
Figure 1 shows how the vectors of the -dimensional Boolean cube are obtained following the definition.
Let be an arbitrary vector. The natural number is called a serial number of the vector . In other words is the natural number whose -digit binary representation is .
Let be an arbitrary vector. A weight (or Hamming weight) of is the natural number , equal to the number of non-zero coordinates of , i.e., .
For arbitrary vectors and the relation lexicographic precedence is defined as follows: , if or , such that , but . When we say that lexicographically precedes and write .
It is easy to verify that the relation is reflexive, antisymmetric and transitive. So is a partial order in the cube. Furthermore, each pair of vectors are comparable with respect to , i.e., either , or holds—this property is called a totality. So is a total order in . This means that its vectors can be ordered (or sorted) lexicographically in a unique way in the sequence , , , such that , for all , and , for all , and for any . This order is called also a standard order.
The vectors of , obtained in accordance with Definition 1 are in lexicographic order, for any .
Following Definition 1, the proof of the theorem by induction is easy.
1) For the assertion is obvious.
2) Suppose that for arbitrary integer the vectors of are in lexicographic order.
3) We consider the vectors of . The first half of them are obtained from by adding zero in the beginning of each of them. In accordance with the inductive suggestion, they are in lexicographic order. The second half of them are obtained by adding one in the beginning of each vector of and so they are in lexicographic order too. Finally, since each vector from the first half begins with zero, it precedes lexicographically each vector from the second half (because it begins with one). Therefore the vectors of are in lexicographic order.
So the theorem holds for any . ∎
Let the vectors of be in lexicographic order. Then:
1) The serial numbers of the vectors form the sequence of natural numbers: . So, if and only if .
2) The weights of the vectors in the second half of are obtained by adding 1 to the weights of corresponding vectors from the first half of the cube.
The proof of this theorem is analogous to the proof of Theorem 1. The theorem states the bijection between the vectors in lexicographic order and their serial numbers. It also shows the relation between the vectors in lexicographic order and their weights. Its assertion is illustrated in Figure 2.
Let and be arbitrary vectors of . A Hamming distance between and is the natural number equal to the number of coordinates in which and differ. If , then and are called adjacent, or more precisely adjacent in -th coordinate, if they differ in this coordinate only. If , the vectors and are called opposite to each other.
The graph of the -dimensional boolean cube is defined as , where (i.e., the vectors of the cube are vertices of ) and (i.e., each pair adjacent vectors are connected by an edge). The graphs are shown in Figure 3. The geometric reasons that determine the name cube (or more precisely hypercube) can be seen in the figure.
Besides the lexicographic order and the order of the vectors of in a Gray code, another important order is given by the following relation.
The precedence relation is denoted by and it is defined as follows: for arbitrary vectors , , if . When we say that precedes and write . When or the vectors and are called comparable, and otherwise—incomparable.
It is easy to verify that is reflexive, antisymmetric and transitive. So is a partial order in . In other words is a partially ordered set (POSet) with respect to and it is denoted by or simply by . Since not all pairs , are comparable (for example, all vectors of equal weights are incomparable), is not a total order, unlike the lexicographic order.
The vector is called a minimal element of the POSet , if , for any . Analogously, the vector is called a maximal element of with respect to , if , for any . So, the zero vector (i.e., the all zeros vector of -coordinates, denoted by further) and the unit vector (i.e., the all ones vector of -coordinates, denoted by further) are the minimal and the maximal element of the POSet , correspondingly. If any pair of vectors of the subset are comparable, they can be ordered in a unique way in a chain, for example , such that , for , and , for , and for . So is a totally ordered set. A chain that is not a proper subset of any other chain is a maximal chain. For example, , , , is a maximal chain in , whereas , , , is not a maximal chain in —see Figure 3. The maximal chain should contain the minimal and the maximal element of the POSet with respect to the corresponding relation. Each chain of the greatest possible size is called a maximum (or longest) chain.
Let be a given set, , and . The vector , defined as:
for , is called a characteristic vector of the set .
Let be an -element set, , and be the power set of . Let be a function defined as follows: , where is the characteristic vector of , for any . Then is a bijection.
The proof of the theorem is easy and we omit it. The theorem states that the vectors of the -dimensional Boolean cube are bijectively related to the subsets of a given -dimensional set by the notion of ”characteristic vector”. Furthermore, the function from Theorem 3 bijectively relates (maps) the bitwise operations on the binary vectors to the operations on the subsets of a given -element set as follows: (disjunction) and (union); (conjunction) and (intersection); (negation) and (complement); (sum modulo 2, XOR) and (symmetric difference), correspondingly. These properties are generalized in the following theorem [12, 8, 13, 7, 3].
Let be an -element set, . Then the Boolean algebras , and are isomorphic.
The bijection from Theorem 3 concerns the relations (defined on a given universal set , ) and (defined on ). For arbitrary , having characteristic vectors , correspondingly, it is easy to prove that:
Thus is an isomorphism between the POSets and , that preserves the relations and the orders corresponding to them. This property is illustrated in Figure 6 by the graphs of the corresponding relations, for .
These important structural properties are used for:
Computer representations of sets by binary vectors or arrays and performance of the basic operations on them—see [9, 1, 2, 10, 12, 19, 11, 13, 16, 14, 17, 18, 6, 22], etc. The serial numbers of the vectors of are used for ranking the subsets of by the notion characteristic vector and this is the most natural ranking function. We note that in this way, the lexicographic order of the vectors (i.e. the natural numbers ) corresponds to the reverse lexicographic order of the subsets of .
The following exposition is related to all these applications.
3 Ordinances of the vectors of the Boolean cube in accordance with their weights
We start with the following key notion.
For an arbitrary , the set of all -dimensional binary vectors of weight is called a -th layer of the -dimensional Boolean cube. We denote it by .
3.1 The weight-order relation
Figure 3 illustrates the notion of layer from Definition 8. All vectors in the same horizontal level in the figure form the corresponding layer of the cube. Since coordinates can be chosen (and filled in with ones) among coordinates in ways, hence , for . These numbers (i.e., binomial coefficients) form the -th row of Pascal’s triangle and it is well-known that . Obviously, the family of all layers is a partition of the -dimensional Boolean cube into layers. Moreover, the sequence of layers is an order of the vectors of in accordance with their weights. This means that when and , then precedes in the sequence of layers, and when , then and there is no precedence between them. More precisely, the corresponding relation can be defined as follows: for arbitrary , if . We want to be reflexive and so we set . When we say that ” precedes by weight ” and write also . It is easy to verify that is a partial order in and we refer to it as a Weight-Order (WO) further.
The vectors of can be rearranged in ways, for . Thus we obtain ways for WO of the vectors of . The product values obtained for are , correspondingly. They form the sequence A051459 in the OEIS , which is defined (by Yuval Dekel, Nov 15 2003) very shortly as ”Number of orderings of the subsets of a set with elements that are compatible with the subsets’ sizes; i.e., if , are two subsets with then ”. This description corresponds to the assertion of Theorem 3 and to the notion WO, since the vectors in the layer are characteristic vectors of all -element subsets of an -element set, for . In addition, we conclude that is the number of:
all maximum chains in the POSet ;
all possible topological orders (or sorts) of the directed acyclic graph defined by the same POSet.
The corresponding comments were added to A051459.
Let us consider the connection between the relations and . We note that always implies . However, does not imply in the general case. Here is a simple example that confirms this assertion—if and , then , whereas and are incomparable with respect to the relation ””. Therefore .
Now we shall enumerate the maximum chains in the POSet . The following assertion will help for this goal.
Let be an arbitrary vector of the layer , for some integer . Then has adjacent vectors in the layer and also adjacent vectors in the layer .
We consider an arbitrary vector and we assume that contains units in the coordinates , where . The set of all vectors adjacent to is partitioned into two subsets. The first one contains all vectors , such that , i.e., exactly one of the coordinates is inverted to zero and all remaining coordinates are the same. So they are elements of and there are such vectors. The second subset contains all vectors , such that , i.e., all coordinates are ones and exactly one of the remaining coordinates is inverted to one. Analogously, all these vectors are elements of and their number is . ∎
The number of maximum chains in the POSet is equal to , for any .
Obviously, the length of any maximum chain is equal to the number of layers in , which is . Let be a maximum chain. Starting from the vector and following Lemma 1, there are possible ways to choose the vector which is adjacent to . There are possible ways to choose a vector which is adjacent to , etc. There are ways to choose a vector which is adjacent to , etc. Finally, the last vector can be chosen in a unique way. Applying the multiplication rule we obtain that maximum chains can be obtained. ∎
The values of , for form the sequence A000142 (called Factorial numbers) in the OEIS . Among its numerous comments, only one corresponds to the assertion of Theorem 5. It was done on Feb 05 2006 by Rick L. Shepherd as follows: ”The number of chains of maximal length in the power set of 1, 2, …, n ordered by the subset relation.”. Beside the assertion of Theorem 5, we added one more comment to the sequence A000142—it contains the number of all shortest paths (obtained by Breadth First Search, for example) between the nodes and in the graph .
3.2 The weight-lexicographic order relation
For the problem formulated in Section 1 the WO of the vectors of is sufficient. However, we need the serial numbers of the vectors in the sequence of layers instead of the vectors themselves. So, we shall represent the WO by a sequence with the serial numbers of the vectors in the layers, in accordance with Theorem 2. For that purpose, for an arbitrary layer of , we denote by the sequence of serial numbers, corresponding to the vectors of . If denotes the sequence of all serial numbers, corresponding to the vectors in the sequence of layers , then represents a WO of the vectors of . Briefly, we refer to as a WO sequence of . One of all possible WO sequences deserves a special attention. For its consideration, we need the following operation on a sequence of integers.
Let and be a sequence of integers. We define the operation addition of the natural number to the sequence as follows: .
1) The WO sequence of the one-dimensional Boolean cube is .
2) Let be the WO sequence of the -dimen-sional Boolean cube.
3) The WO sequence of the -dimensional Boolean cube , is defined as follows:
and it corresponds to the layer ;
and it corresponds to the layer ;
, for . Here is a concatenation of two sequences: the sequence is taken (or copied) firstly, and the sequence follows after it. The sequence corresponds to the layer .
The corresponding recursive definition of is:
|If ,||, where:|
The last two definitions resemble the definition of Pascal’s triangle. As we noted, the length of , for . Instead of the rule used in Pascal’s triangle, we use a similar rule . The next theorem clarifies it.
Let be the WO sequence, obtained in accordance with Definition 10, for an arbitrary . Then, the serial numbers in the sequence determine a lexicographic order of the vectors of the corresponding layer , for .
We prove the theorem by induction on , , following Definition 10.
1) For the assertion is obvious.
2) Suppose that the theorem holds, for an arbitrary integer , and the sequence is obtained in accordance with Definition 10.
3) Let be the sequence, obtained in accordance with Definition 10. For and , the corresponding layers and are in lexicographic order. Furthermore, and are in their right places in . Let be one of the rest of the subsequences in , for an arbitrary integer , . In accordance with Definition 10, is a concatenation of two subsequences: and , placed in that order. So, the layer corresponding to is partitioned into two groups. The first one consists of all vectors of , that begin with zero. Hence their serial numbers coincide with these in the sequence . It corresponds to the layer , whose vectors are in lexicographic order, in accordance with the inductive suggestion. So the vectors in the first group are also in lexicographic order. The second group includes all vectors of that begin with one. So their serial numbers are obtained by an addition of the integer to the serial numbers of the sequence . Following the inductive suggestion, the vectors of the corresponding layer are in lexicographic order and therefore the vectors in the second group are also in lexicographic order. Moreover, each vector from the first group precedes lexicographically each vector from the second group. Therefore, the sequence determines a lexicographic order in the corresponding layer . This conclusion holds for any integer , , and so the theorem is proven. ∎
Theorem 6 states that Definition 10 determines a second criterion for ordering in the WO of the Boolean cube—this is the lexicographic order. Since it is a unique total order for each subsequence , , a unique total weight order for the sequence is obtained. We call it a Weight-Lexicographic Order (WLO). It is represented by the corresponding WLO sequence .
4 The WLO algorithm
We developed an algorithm called WLO algorithm that computes the sequence for a given input . The algorithm uses an array for the binomial coefficients from Pascal’s triangle (i.e., the lengths of the subsequences), and one more array where the beginning of each subsequence is computed and stored. The values in these two arrays are computed firstly. The code of the corresponding function is given below in the C programming language.
The WLO algorithm is based on Definition 10. Starting from it computes consecutively the sequences in the array seqs as follows.
Some results obtained by the algorithm, for , are given in Table 1.
|2||0, 1, 2, 3|
|3||0, 1, 2, 4, 3, 5, 6, 7|
|4||0, 1, 2, 4, 8, 3, 5, 6, 9, 10, 12, 7, 11, 13, 14, 15|
|5||0, 1, 2, 4, 8, 16, 3, 5, 6, 9, 10, 12, 17, 18, 20, 24, 7, 11, 13, 14, 19, 21, 22, 25,|
More results can be seen in the OEIS , sequence A294648.
Figure 6 summarizes some of discussed results and illustrates:
the bijection between subsequences of and the layers of ;
the bijection between the vectors of and the subsets of (see Theorem 3);
the isomorphism between the POSets and , by the graphs of the corresponding relations.
Like we said, the WLO algorithm is based on Definition 10 and it follows its steps. This fact, Theorem 6 and the notes after it determine its correctness. Let us consider the time complexity of the algorithm. The time for filling in both additional arrays (for Pascal’s triangle and for the beginning of each subsequence) is . The function fill_in_seqs in Listing 2 runs as follows. On the -th step, , it copies generally values from to , and also it adds the constant to members of and stores them in . So, it performs assignments and summations, i.e., operations generally on the -th step. Therefore, the time complexity of the algorithm is
So, the time complexity of the WLO algorithm is of an exponential type with respect to the size of the input . It can not be better since it produces an output of exponential size. What is more important is that the algorithm has a linear time complexity with respect to the size of the output.
Let us consider the space complexity of the WLO algorithm. For clarity, in Listing 2 we use a two-dimensional array of size and hence, the space complexity is . We recall that the existence of is sufficient to obtain . So, instead of the square array we can use:
Two one-dimensional static arrays of size —for the existing sequence and for the new sequence . After we obtain , we change the role of the arrays to obtain the next sequence , and so on.
One-dimensional arrays of size which are created dynamically in the -th step, for .
In both cases the space complexity of the WLO algorithm reduces to .
Trying to find an efficient solution to the problem formulated in Section 1 we considered the weight order of the vectors of the Boolean cube. We examined this order more generally and we obtained solutions to two enumeration problems, concerning the POSets and . The corresponding notes were added to the sequences A051459 and A000142 in the OEIS . We defined one special WO—the WLO and proved that it is a unique total order. Based on this, we developed the WLO algorithm that generates all -dimensional binary vectors in accordance with their weights, where the lexicographical order is chosen as a second criterion. The vectors of the cube in WLO have very compact representation by their serial numbers. Following the obtained results, a new sequence (A294648) was appended to the OEIS .
Our next goal is to perform tests and to evaluate the efficiency of the proposed solution to the initial problem. Moreover, the bijection between the -dimensional Boolean cube and the power set of a given -element set (Theorem 3) means that the WLO algorithm can have more general applications. For example, it can be used in solving problems related to representing and generating the power set of a given set, or some of its subsets (for example, -element subsets, or combinations), etc., as it is shown in Figure 6. In such cases, if the elements of a given set are in lexicographic order, the WLO sequence of their characteristic vectors implies a cardinality order of the subsets. However, the corresponding subsets of equal cardinalities will be in a reverse lexicographic order.
The author is grateful for the partial support from the Research Fund of the University of Veliko Tarnovo, Bulgaria, under Contract FSD-31-303-05/16.03.2018.
-  A. V. Aho, J. E. Hopcroft and J. D. Ullman, The Design and Analysis of Computer Algorithms, Addison-Wesley Publishing Company, 1974.
-  A. V. Aho, J. E. Hopcroft and J. D. Ullman, Data Structures and Algorithms, Addison-Wesley Publishing Company, 1983.
-  V. Bakoev, Discrete mathematics: Sets, Relations, Combinatorics, KLMN, Sofia, 2014. (in Bulgarian)
-  C. Carlet, Boolean Functions for Cryptography and Error Correcting Codes, in: Y. Crama, P. L. Hammer (Eds.), Boolean Models and Methods in Mathematics, Computer Science, and Engineering, Cambridge Univ. Press, 2010, pp 257–397.
-  C. Carlet, Vectorial Boolean Functions for Cryptography, in: Y. Crama, P. L. Hammer (Eds.), Boolean Models and Methods in Mathematics, Computer Science, and Engineering, Cambridge Univ. Press, 2010, pp 398–469.
-  T. Cormen, Ch. Leiserson, R. Rivest and Cl. Stein, Introduction to Algorithms, Third Edition, 2009, The MIT Press.
-  R. Garnier and J. Taylor, Discrete Mathematics for New Technology, Second Edition, IOP Publishing Ltd, 2002.
-  R. Grimaldi, Discrete and Combinatorial Mathematics. An Applied Introduction, Fifth Edition, Addison-Wesley, 2004.
-  D. Knuth, The art of computer programming, Volume 4A: Combinatorial Algorithms, Part 1, Addison-Wesley, 2011.
-  T. Koshy, Discrete Mathematics with Applications, Academic Press, 2013.
-  D. Kreher and D. Stinson,Combinatorial algorithms: generation, enumeration and search, CRC Press LLC, 1999.
-  O. Kuznetsov O., Discrete mathamatics for engineers, Sixth Edition, Lan, St. Peterburg-Moskow-Krasnodar, 2006. (in Russian)
-  K. N. Manev, Introduction to Discrete Mathematics, Fourth Edition, KLMN, Sofia, 2007. (in Bulgarian)
-  A. Nijenhuis and H. Wilf, Combinatorial Algorithms for Computers and Calculators, Second Ed., Academic Press, 1978.
-  S. Pemmaraju and S. Skiena, Computational Discrete Mathematics: Combinatorics and Graph Theory with Mathematica, Cambridge Univ. Press, 2003.
-  E. Reingold, J. Nievergelt and N. Deo, Combinatorial algorithms, Theory and practice, Prentice-Hall, New Jersey, 1977.
-  K. Rosen, Discrete Mathematics and its Applications, Seventh Edition, McGraw-Hill, 2012.
-  K. Rosen (Ed. in Chief), J. Michaels, J. Gross, J. Grossman and D. Shier, Handbook of Discrete and Combinatorial Mathematics, CRC Press, 2000.
F. Ruskey, Combinatorial Generation. Working Version (1j-CSC 425/ 520), 2003. Accessible online at
-  C. Savage, A Survey of Combinatorial Gray Codes, SIAM Review, 39(4) (1997), 605–629.
-  N. J. A. Sloane, The On-Line Encyclopedia of Integer Sequences (OEIS), 2009. Published electronically at http://oeis.org/
-  S. Skiena, The Algorithm Design Manual, Second Edition, Springer, 2008.