Notes+11.22

Recursion
Forehead slap and "Doh!": a classic problem which works really well with recursion, but is clumsy to do with an iteration is "Towers of Hanoi". Much of this sample program is the "fluff" that makes it print nicely: the workhorse recursive algorithm could otherwise have a 3-statement body. code format="c" $ cat hanoi.c
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 

void hanoi (int num, char *from, char *to, char *via) { int ix; if (num > 1) hanoi(num-1, from, via, to); printf(" %i ", num);  for (ix=0; ix %s\n", from, to); if (num > 1) hanoi(num-1, via, to, from); }

main { int numDisks; printf("The legend of the Towers of Hanoi:\n\n"); printf("The monks at Hanoi have been given the task of transferring 64 heavy metal disks\n\tfrom the post (spindle) they started on\n\tto their final resting place.\nThe lowest disk is the largest, and each disk going up is a little smaller.\nThe catch is, they can only move one disk at a time,\nand they may never place a disk on top of a smaller disk. \nThey have one extra post which can be used to store disks while larger, lower disks are being moved.\n\n"); printf("When the monks finish this task, the universe will end.\n\n");

do { printf("How many disks would you like to move? "); scanf("%i", &numDisks); if (numDisks >= 64) printf("\nPerhaps you did not understand: if you move 64 disks, the universe will end!\n\n"); } while (numDisks >= 64);

printf("\nAll disks begin on peg %s\n", "A"); hanoi(numDisks, "A", "B", "C"); printf("All disks have been moved to peg %s\n", "B"); } code A sample run: code $ hanoi The legend of the Towers of Hanoi:

The monks at Hanoi have been given the task of transferring 64 heavy metal disks from the post (spindle) they started on   to their final resting place. The lowest disk is the largest, and each disk going up is a little smaller. The catch is, they can only move one disk at a time, and they may never place a disk on top of a smaller disk. They have one extra post which can be used to store disks while larger, lower disks are being moved.

When the monks finish this task, the universe will end.

How many disks would you like to move? 4

All disks begin on peg A 1 =     A => C 2 ==      A => B 1 =      C => B 3 ===      A => C 1 =      B => A 2 ==      B => C 1 =      A => C 4 ====      A => B 1 =      C => B 2 ==      C => A 1 =      B => A 3 ===      C => B 1 =      A => C 2 ==      A => B 1 =      C => B All disks have been moved to peg B code Note how many moves it takes to move N disks.

Huffman Codes:
The encoding used by Morse is a Huffman code. Codes are assigned to letters based on their frequency in typical English text. Not all bit patterns are the same length. You may look in the textbook (pg 430..) or just sit down with an example and develop optimal codes. Here's one example problem: code A       1 power       2 of       4 two       8 sequence   16 a       32 lop       64 sided       128 decoding   256 tree       512

code where can see that weights which increase as powers of two produce a spindly optimal tree/code: code 0 == tree $512 10 == decoding $256 1*     + =$511 110 == sided  $128 11*     + =$255 1110 == lop  $64 111*     + =$127 11110 == a  $32 1111*     + =$63 111110 == sequence  $16 11111*     + =$31 1111110 == two  $8 111111*     + =$15 11111110 == of  $4 1111111*     + =$7 111111110 == power  $2 11111111*     + =$3 111111111 == A  $1 code Here's an example optimized code with code words A..M and randomly generated values: code 000000 == E $13 000001 == G $13 00001 == I $23 000100 == A $10 0001010 == L $5 0001011 == M $4 00011 == D $16 001 == B $83 010 == F $79 011 == C $73 100 == J $67 101 == K $66 11 == H $86 code
 * + =$1023

Parity bits, Check-sums
When there's any chance of garbled transmission, a message can include some extra bits which allow error detection and correction. If each row of bits gets one extra bit to bring the parity to even, we could detect any one-bit error in a row. Can you tell which of the following rows have errors? code 000000 010111 111111 100101 010011 111001 101011 111101 code If we also put an extra row at the bottom which brings the parity of each column to 0, then we could detect and correct any one-bit error. Can you find and correct the errors in the following examples? code 00111111 00101100 00100111 10100000 00110101 10000001

111001000 011110000 001000010 100110001 001001010 code Realistically, if error correcting codes are used, we would slice the bits in several independent moduli and include enough extra checksum bits to correct several errors and almost certainly detect data corruption.