This blog is still alive, just in semi-hibernation.
When I want to write something longer than a tweet about something other than math or sci-fi, here is where I'll write it.

Wednesday, August 12, 2009

Wednesday Math, Vol. 83: Gray codes

If you have two on-off switches, there are four possible configurations.

on on
on off
off on
off off

To save on typing, mathematicians will often use two symbols to represent on and off, usually 1 for on and 0 for off, like so.

11
10
01
00

Is there a 'natural' order for the list of configurations? It depends on what the configurations represent. If we consider the two symbols an alphabet, we could put the configurations in 'alphabetical' order. Since the 'alphabets' used often don't resemble what most people consider to be the alphabet, the phrase used is lexicological order. If we say 0 comes before 1, the lexicological order would be as follows.

00
01
10
11

Another way to think of the patterns is to go back to the idea of switches. Let's see if we can make every pattern with the rule that successive patterns in our order only differ by a single switch, either a 1 becoming a 0 or vice versa. The changed switches in each pattern will be in bold and red, and we will start with 00, which means both switches off.

00
01
11
10

This order for the patterns is called a Gray code, named for the Bell Labs engineer Frank Gray who got the patent for the concept back in 1947. He is not the very first person to come up with the idea. The Frenchman Émile Baudot used this idea in regards to telegraphy and received the Medal of the Legion of Honor for it in 1878.

If this just worked for patterns with two switches, the idea wouldn't be very valuable, but no matter how many on/off switches you have, you can make a Gray code that will go through every possible configuration. Instead of solving the puzzle over and over again, we can build a Gray code for three switches by using the Gray code for two switches.

Step 1: Take the Gray code for n switches (in this case n = 2) and write it down forwards, then make a second copy in reverse order. (Note: a Gray code in reverse order is still a Gray code, as just one switch will be changed.) The second half will be in red.

00
01
11
10
10
11
01
00

Step 2: Put a 0 behind the first half patterns and a 1 behind the second half patterns. The places where the switch has been made will be marked in bold and red.

000
010
110
100
101
111
011
001

Notice that the last pattern in the list, 001, is just one switch away from 000, the first pattern in the list. Gray codes "wrap around", which means that you can start from any pattern of switches and use the methods of Gray code construction to cycle through all the patterns.

While the uses in telegraphy are now somewhat obsolete, Gray codes are used in computer science as the way to orient switches when constructing Karnaugh maps, a fun little puzzle that helps to optimize the construction of computer circuits. Both Gray codes and Karnaugh maps are topics in a class called discrete math, a survey course of topics useful in computer science that don't fall into the curriculum that culminates with calculus, linear algebra and differential equations, which was the math that all the "hard sciences" used before computer science came along.

1 comment:

Margaret Benbow said...

(...Sweet Mother of God, please explain this mystery: all these numbers and codes and charts are obviously very edifying, and yet I feel as if I've been hit on the head with a brick! Go figure!...)