簡體   English   中英

非遞歸格雷碼算法的理解

[英]Non-recursive Grey code algorithm understanding

這是算法書中的任務。

問題是我完全不知道從哪里開始!

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 1,2 00 01 11 10等。

許多問題

1)我知道,對於n = 1,我可以從0 1開始嗎?

2)我應該如何理解“從所有0的n位字符串開始”?

3)“上一位字符串”? 哪個字符串是“上一個”? 以前的意思是從較低的n位? (例如,對於n = 2,先前是n = 1的那個)?

4)如果只有翻轉操作,我如何將1位字符串轉換為2位字符串?

這讓我很困惑。 到目前為止我唯一理解的“人”方法是:從低位獲取集合,復制它們,反轉第2集,向第1集中的每個元素添加0,在第2集中添加1對每個元素。 完成(例如: 0 1 - > 0 1 | 0 1 - > 0 1 | 1 0 - > 00 01 | 11 10 - > 11 01 11 10完成。

謝謝你的幫助

所有四個問題的答案都是這個算法不是以較低的n值開始。 它生成的所有字符串都具有相同的長度,並且從第(i-1)-th字符串生成i-th (對於i = 1,...,2 n -1)字符串。

這是n = 4的第幾步:

從G 0 = 0000

為了生成G 1 ,在G 0中翻轉第0-th位,因為0是1 = 0001 b的二進制表示中的最低有效位1的位置。 G 1 = 0001

為了產生G 2,翻轉1-st G中1位,因為1是的至少顯著的位置1中的2 = 0010 b中的二進制表示。 G 2 = 0011

為了生成G 3 ,在G 2中翻轉第0-th位,因為0是3 = 0011 b的二進制表示中的最低有效位1的位置。 G 3 = 0010

為了生成G 4 ,在G 3中翻轉第2-nd位,因為2是4 = 0100 b的二進制表示中的最低有效位1的位置。 G 4 = 0110

為了生成克5,翻轉0-th中G 4位,如0是的至少顯著的位置1中的5 = 0101 b中的二進制表示。 G 5 = 0111

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM