/ Cookbook.GrayCodes

A Binary Gray Code of k bits is a rearrangement of the list of the numbers 0 up to but not including 2^{k} such that every element differs in presicely one bit from the preceeding element. For example (#b000 #b001 #b011 #b010 #b110 #b111 #b101 #b100). The leading zero's must not be omitted. A Decimal Gray Code of k decimal figures is a rearrangement of the list of the numbers 0 up to but not including 10^{k} such that every next element differs in only one decimal figure from the preceeding element. In general an (n,k) Gray code is a rearrangement of the list of the numbers 0 up to but not including n^{k} such that the base n representation of each element differs in exactly one position from the base n representation of the next element.

How to make a Gray Code Table?
How to find the m^{th} element of a Gray code?
How to find m given the m^{th} element of a Gray Code?

For every n>1 and k>0 more than one Gray Code exists. The Gray Codes produced by procedures shown below have the property that going from left to right through the list, each digit switches more frequently than all more significant digits and that each digit passes the values 0, 1, ... n-1, n-1, n-2, ... 0, except for the most significant digit, which runs from 0 to n-1.

(define-syntax while (syntax-rules () ((while test body ...) (let loop () (if test (begin body ... (loop))))))) (define (gray-code n k) ;--> (n k) gray code (define g (make-vector k 0)) (define u (make-vector k 1)) (cons (vector->list g) (let loop () (let/ec ec (let ((i 0) (j (+ (vector-ref g 0) (vector-ref u 0)))) (while (or (>= j n) (< j 0)) (vector-set! u i (- (vector-ref u i))) (set! i (add1 i)) (if (>= i k) (ec ())) (set! j (+ (vector-ref g i) (vector-ref u i)))) (vector-set! g i j) (cons (reverse (vector->list g)) (loop))))))) ; For example: (write (gray-code 4 3)) ;--> ((0 0 0) (0 0 1) (0 0 2) (0 0 3) (0 1 3) (0 1 2) (0 1 1) (0 1 0) (0 2 0) (0 2 1) (0 2 2) (0 2 3) (0 3 3) (0 3 2) (0 3 1) (0 3 0) (1 3 0) (1 3 1) (1 3 2) (1 3 3) (1 2 3) (1 2 2) (1 2 1) (1 2 0) (1 1 0) (1 1 1) (1 1 2) (1 1 3) (1 0 3) (1 0 2) (1 0 1) (1 0 0) (2 0 0) (2 0 1) (2 0 2) (2 0 3) (2 1 3) (2 1 2) (2 1 1) (2 1 0) (2 2 0) (2 2 1) (2 2 2) (2 2 3) (2 3 3) (2 3 2) (2 3 1) (2 3 0) (3 3 0) (3 3 1) (3 3 2) (3 3 3) (3 2 3) (3 2 2) (3 2 1) (3 2 0) (3 1 0) (3 1 1) (3 1 2) (3 1 3) (3 0 3) (3 0 2) (3 0 1) (3 0 0)) (define (number->gray-code m n k) ; m n k --> m-th element of (n,k) gray code (let ((2n (* 2 n))) (let loop ((m m) (k k) (gc ())) (if (zero? k) gc (let ((q (quotient m n)) (r (modulo m 2n))) (loop q (sub1 k) (cons (if (>= r n) (- 2n r 1) r) gc))))))) ; With its inverse: (define (gray-code->number gc n) ; m-th (n,k) gray-code --> m (let loop ((gc gc) (significance (expt n (sub1 (length gc))))) (if (null? gc) 0 (let ((digit (car gc)) (gc (cdr gc))) (let ((m (loop gc (quotient significance n)))) (+ (* digit significance) (if (odd? digit) (- significance m 1) m))))))) (number->gray-code 10 4 3) ;--> (0 2 2) (gray-code->number '(0 2 2) 4) ;--> 10

For readers familiar with the Tower of Hanoi: `(gray-code 3 h)`

shows how to move a Tower of h disks from peg 0 to peg 2 passing every other feasible distribition of disks exactly once (Hamilton path)

-- JosKoot - 24 Nov 2006

CookbookForm | |
---|---|

TopicType: | Recipe |

ParentTopic: | NumberRecipes |

TopicOrder: | 999 |

Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.

The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.

Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.

Ideas, requests, problems regarding Schematics Cookbook? Send feedback. / You are Main.guest

The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.

Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.

Ideas, requests, problems regarding Schematics Cookbook? Send feedback. / You are Main.guest