构造一个圆包含所有n位二进制数二进制数

2023-09-11 05:54:55 作者:誓言是蛊惑人心的

这个问题来自于一个采访: 如何构造一个圆圈(长度2的n次方01轮的序列)包含所有可能的n位二进制数,各数字出现一次且仅一次。例如,当n = 2时,其结果是:

  0--0
| |
1--1
 

数是{00,01,11,10}。所有可能的数字出现一次且仅一次。当n = 3,下面是一个例子回答:

  0--0--1
 / \
0 1
 \ /
  1--0--1
 

解决方案 n进制化十进制怎样转换

您需要构造二进制德布鲁因周期。维基百科的文章推荐了几种方法来做到这一点:

  

这De Bruijn序列可以通过利用n维德Bruijn的曲线图的汉弥尔顿路径通过k个码元(或等同地,一个(n的欧拉循环 - 1)维德Bruijn的图表)来构造。      

这是另一种结构包括串联起来,在字典顺序,所有的林登话,其长度划分ñ。

     

德布鲁因序列,也可以使用移位寄存器或者通过有限域构造。

The problem comes from an interview: How to construct a circle (01 round sequences with length 2^n) contains all possible n-bit binary number, each number appears once and only once. For example, when n=2, the result is:

0--0
|  |
1--1

the numbers are {00, 01, 11, 10}. All possible numbers appear once and only once. When n=3, here is an example answer:

  0--0--1
 /       \
0         1
 \       /  
  1--0--1

解决方案

You need to construct binary de Bruijn cycle. Wikipedia article suggests several ways to do it:

The De Bruijn sequences can be constructed by taking a Hamiltonian path of an n-dimensional De Bruijn graph over k symbols (or equivalently, a Eulerian cycle of a (n − 1)-dimensional De Bruijn graph).

An alternative construction involves concatenating together, in lexicographic order, all the Lyndon words whose length divides n.

De Bruijn sequences can also be constructed using shift registers or via finite fields.