Xiang Huang

Gray's Binary Code

2023-03-26


I will teach recursion and induction next week and I will use Gray’s code as an example. I asked ChatGPT about it. Summary:

  1. The programs ChatGPT gave are correct. But it did make a mistake: it’s example test cases output does not correctly correspond to its program. The order of reflected codes and non-reflected codes were switched. I pointed that out and ChatGPT fixed it.
  2. The inductive proof it wrote misses did not discuss the adjacent codes (last one in the non-reflected codes and the first one in the reflected codes). I pointed out and ChatGPT seemed to try to fixed it but mainly just copy my words.
  3. The space complexity $O(1)$ ChatGPT claimed is not true.

Below is the conversation I have with ChatGPT on Gray’s code.

Person:

ChatGPT:

  1. If n = 1, the Gray code is ['0', '1'].
  2. Otherwise, generate the Gray code of length n-1 recursively.
  3. Reflect the Gray code of length n-1 around its middle (i.e., reverse the order of the elements).
  4. Add a 0 prefix to each element of the reflected Gray code.
  5. Add a 1 prefix to each element of the original Gray code of length n-1.
  6. Concatenate the two lists obtained in steps 4 and 5 to obtain the Gray code of length n.

Here is the implementation of the above algorithm in Python:

1
2
3
4
5
6
7
8
9
def gray_code(n):
    if n == 1:
        return ['0', '1']
    else:
        prev_code = gray_code(n-1)
        reflected_code = prev_code[::-1]
        zero_prefixed = [f'0{c}' for c in reflected_code]
        one_prefixed = [f'1{c}' for c in prev_code]
        return zero_prefixed + one_prefixed

To use this function, you can simply call gray_code(n) where n is the length of the Gray code you want to generate. For example:

1
2
result = gray_code(3)
print(result)

This will output:

1
['000', '001', '011', '010', '110', '111', '101', '100']

which are all the possible Gray codes of length 3.

Note that this algorithm has a time complexity of O(2^n) since it generates all possible Gray codes of length n.

Person:

ChatGPT:

The basic idea is to start with the Gray code of length 1, and then generate the Gray code of length n by iteratively inserting a new bit to the left of the previous sequence, while reflecting the previous sequence and adding a new bit to the left of the reflected sequence. This process generates all possible Gray codes of length n without storing all of them explicitly in memory.

Here is the implementation of the iterative algorithm in Python:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def gray_code(n):
    if n == 0:
        return ['']
    
    prev_code = ['0', '1']
    for i in range(2, n+1):
        reflected_code = prev_code[::-1]
        zero_prefixed = [f'0{c}' for c in reflected_code]
        one_prefixed = [f'1{c}' for c in prev_code]
        prev_code = zero_prefixed + one_prefixed
    
    return prev_code

This algorithm starts by initializing the prev_code variable with the Gray code of length 1 (['0', '1']), and then generates the Gray code of length n by iteratively applying the steps described above.

To use this function, you can simply call gray_code(n) where n is the length of the Gray code you want to generate. For example:

1
2
result = gray_code(3)
print(result)

This will output:

1
['000', '001', '011', '010', '110', '111', '101', '100']

which are all the possible Gray codes of length 3.

Note that this algorithm has a time complexity of O(2^n) since it generates all possible Gray codes of length n. However, it only uses O(1) extra space, which makes it more memory-efficient than the recursive approach.

Person:

ChatGPT:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def gray_code(n):
    if n == 0:
        return ['']
    
    prev_code = ['0', '1']
    for i in range(2, n+1):
        reflected_code = prev_code[::-1]
        zero_prefixed = [f'0{c}' for c in prev_code]
        one_prefixed = [f'1{c}' for c in reflected_code]
        prev_code = zero_prefixed + one_prefixed
    
    return prev_code

Now, calling gray_code(3) will produce the expected output:

1
['000', '001', '011', '010', '110', '111', '101', '100']

Person:

ChatGPT:

Person:

ChatGPT:

Person:

ChatGPT:

I hope this summary is helpful! Let me know if you have any further questions.