这是算法书中的任务。
问题是我完全不知道从哪里开始!
Trace the following non-recursive algorithm to generate the binary reflexive
Gray code of order 4. Start with the n-bit string of all 0’s.
For i = 1, 2, ... 2^n-1, generate the i-th bit string by flipping bit b in the
previous bit string, where b is the position of the least significant 1 in the
binary representation of i.
所以我知道 1 位的格雷码应该是0 1
2位00 01 11 10
等。
许多问题
1)我知道对于 n = 1 我可以从 开始0 1
吗?
2)我应该如何理解“从全0的n位字符串开始”?
3)“前一个位串”?哪个字符串是“前一个”?以前的意思是来自较低的 n 位?(例如对于 n=2,前一个是来自 n=1 的那个)?
4) 如果唯一的操作是翻转,我如何将 1 位字符串转换为 2 位字符串?
这让我很困惑。到目前为止,我理解的唯一“人类”方法是:从较低的 n 位获取集合,复制它们,反转第二个集合,将 0 添加到第一个集合中的每个元素,添加 1 做第二个集合中的每个元素。完成(例如:0 1
-> 0 1 | 0 1
-> 0 1 | 1 0
-> 00 01 | 11 10
->11 01 11 10
完成。
谢谢你的帮助