14.1 KB · commit a9602da
In this section we will discuss some important applications of congruences to computer science. First, we will show how congruences can be used to assign memory locations to computer files. When we assign memory locations to files, we want to be able to find where a particular file has been stored quickly. We also want to assign memory locations so that the available space is used efficiently. Assigning memory locations to files in this way is called hashing. We will show how we can use modular arithmetic to do hashing.
Constructing sequences of random numbers is important for randomized algorithms, for simulations, and for many other purposes. Constructing a sequence of truly random numbers is extremely difficult, or perhaps impossible, because any method for generating what are supposed to be random numbers may generate numbers with hidden patterns. As a consequence, methods have been developed for finding sequences of numbers that have many desirable properties of random numbers, and which can be used for various purposes in place of random numbers. In this section we will show how to use congruences to generate sequences of pseudorandom numbers. The advantage is that the pseudorandom numbers so generated are constructed quickly; the disadvantage is that they have too much predictability to be used for many tasks.
Congruences also can be used to produce check digits for identification numbers of various kinds, such as code numbers used to identify retail products, numbers used to identify books, airline ticket numbers, and so on. We will explain how to construct check digits using congruences for a variety of types of identification numbers. We will show that these check digits can be used to detect certain kinds of common errors made when identification numbers are printed.
The central computer at an insurance company maintains records for each of its customers. How can memory locations be assigned so that customer records can be retrieved quickly? The solution to this problem is to use a suitably chosen hashing function. Records are identified using a key, which uniquely identifies each customer's records. For instance, customer records are often identified using the Social Security number of the customer as the key. A hashing function h assigns memory location h(k) to the record that has k as its key.
In practice, many different hashing functions are used. One of the most common is the function h(k)=kmodm where m is the number of available memory locations.
Hashing functions should be easily evaluated so that files can be quickly located. The hashing function h(k)=kmodm meets this requirement; to find h(k), we need only compute the remainder when k is divided by m. Furthermore, the hashing function should be onto, so that all memory locations are possible. The function h(k)=kmodm also satisfies this property.
Find the memory locations assigned by the hashing function h(k)=kmod111 to the records of customers with Social Security numbers 064212848 and 037149212.
Solution: The record of the customer with Social Security number 064212848 is assigned to memory location 14, because h(064212848)=064212848mod111=14. Similarly, because h(037149212)=037149212mod111=65, the record of the customer with Social Security number 037149212 is assigned to memory location 65. ◀
Because a hashing function is not one-to-one (because there are more possible keys than memory locations), more than one file may be assigned to a memory location. When this happens, we say that a collision occurs. One way to resolve a collision is to assign the first free location following the occupied memory location assigned by the hashing function.
After making the assignments of records to memory locations in Example 1, assign a memory location to the record of the customer with Social Security number 107405723.
Solution: First note that the hashing function h(k)=kmod111 maps the Social Security number 107405723 to location 14, because h(107405723)=107405723mod111=14. However, this location is already occupied (by the file of the customer with Social Security number 064212848). But, because memory location 15, the first location following memory location 14, is free, we assign the record of the customer with Social Security number 107405723 to this location. ◀
In Example 2 we used a linear probing function, namely h(k,i)=(h(k)+i)modm, to look for the first free memory location, where i runs from 0 to m−1.
Randomly chosen numbers are often needed for computer simulations. Because numbers generated by systematic methods are not truly random, they are called pseudorandom numbers.
The most commonly used procedure for generating pseudorandom numbers is the linear congruential method. We choose four integers: the modulus m, multiplier a, increment c, and seed x0, with 2≤a<m, 0≤c<m, and 0≤x0<m. We generate a sequence of pseudorandom numbers {xn}, with 0≤xn<m for all n, by successively using the recursively defined function xn+1=(axn+c)modm.
Many computer experiments require the generation of pseudorandom numbers between 0 and 1. To generate such numbers, we divide numbers generated with a linear congruential generator by the modulus: that is, we use the numbers xn/m.
Find the sequence of pseudorandom numbers generated by the linear congruential method with modulus m=9, multiplier a=7, increment c=4, and seed x0=3.
Solution: We compute the terms of this sequence by successively using the recursively defined function xn+1=(7xn+4)mod9, beginning by inserting the seed x0=3 to find x1. We find that
x1x2x3x4x5x6x7x8x9=(7x0+4)mod9=(7⋅3+4)mod9=25mod9=7,=(7x1+4)mod9=(7⋅7+4)mod9=53mod9=8,=(7x2+4)mod9=(7⋅8+4)mod9=60mod9=6,=(7x3+4)mod9=(7⋅6+4)mod9=46mod9=1,=(7x4+4)mod9=(7⋅1+4)mod9=11mod9=2,=(7x5+4)mod9=(7⋅2+4)mod9=18mod9=0,=(7x6+4)mod9=(7⋅0+4)mod9=4mod9=4,=(7x7+4)mod9=(7⋅4+4)mod9=32mod9=5,=(7x8+4)mod9=(7⋅5+4)mod9=39mod9=3.Because x9=x0 and because each term depends only on the previous term, we see that the sequence 3,7,8,6,1,2,0,4,5,3,7,8,6,1,2,0,4,5,3,… is generated. This sequence contains nine different numbers before repeating. ◀
Most computers do use linear congruential generators to generate pseudorandom numbers. Often, a linear congruential generator with increment c=0 is used. Such a generator is called a pure multiplicative generator. For example, the pure multiplicative generator with modulus 231−1 and multiplier 75=16,807 is widely used. With these values, it can be shown that 231−2 numbers are generated before repetition begins.
Congruences are used to check for errors in digit strings. A common technique for detecting errors in such strings is to add an extra digit at the end of the string. This final digit, or check digit, is calculated using a particular function. Then, to determine whether a digit string is correct, a check is made to see whether this final digit has the correct value.
Digital information is represented by bit strings, split into blocks of a specified size. Before each block is stored or transmitted, an extra bit, called a parity check bit, can be appended to each block. The parity check bit xn+1 for the bit string x1x2…xn is defined by xn+1=(x1+x2+⋯+xn)mod2. It follows that xn+1 is 0 if there are an even number of 1 bits in the block of n bits and it is 1 if there are an odd number of 1 bits in the block of n bits. A parity check can detect an odd number of errors in the previous bits, but not an even number of errors.
Suppose we receive in a transmission the bit strings 01100101 and 11010110, each ending with a parity check bit. Should we accept these bit strings as correct?
Solution: Before accepting these strings as correct, we examine their parity check bits. The parity check bit of the first string is 1. Because 0+1+1+0+0+1+0≡1(mod2), the parity check bit is correct. The parity check bit of the second string is 0. We find that 1+1+0+1+0+1+1≡1(mod2), so the parity check is incorrect. We conclude that the first string may have been transmitted correctly and we know for certain that the second string was transmitted incorrectly. We accept the first string as correct (even though it still may contain an even number of errors), but we reject the second string. ◀
Check bits computed using congruences are used extensively to verify the correctness of various kinds of identification numbers.
Retail products are identified by their Universal Product Codes (UPCs). The most common form of a UPC has 12 decimal digits: the first digit identifies the product category, the next five digits identify the manufacturer, the following five identify the particular product, and the last digit is a check digit. The check digit is determined by the congruence 3x1+x2+3x3+x4+3x5+x6+3x7+x8+3x9+x10+3x11+x12≡0(mod10).
Answer these questions:
Solution:
Hence, 041331021641 is not a valid UPC. ◀
All books are identified by an International Standard Book Number (ISBN-10), a 10-digit code x1x2…x10, assigned by the publisher. An ISBN-10 consists of blocks identifying the language, the publisher, the number assigned to the book by its publishing company, and finally, a check digit that is either a digit or the letter X (used to represent 10). This check digit is selected so that x10≡i=1∑9ixi(mod11), or equivalently, so that i=1∑10ixi≡0(mod11).
Answer these questions about ISBN-10s:
Solution:
Hence, x10=2. 2. To see whether 084930149X is a valid ISBN-10, we see if ∑i=110ixi≡0(mod11):
1⋅0+2⋅8+3⋅4+4⋅9+5⋅3+6⋅0+7⋅1+8⋅4+9⋅9+10⋅10=0+16+12+36+15+0+7+32+81+100=299≡2≡0(mod11).Hence, 084930149X is not a valid ISBN-10. ◀
Several kinds of errors often arise in identification numbers. A single error, an error in one digit of an identification number, is perhaps the most common type of error. Another common kind of error is a transposition error, which occurs when two digits are accidentally interchanged.
Suppose that x1x2…x10 is a valid ISBN (so that ∑i=110ixi≡0(mod11)). We will show that we can detect a single error and a transposition of two digits (where we include the possibility that one of the two digits is the check digit X, representing 10).
Suppose that this ISBN has been printed with a single error as y1y2…y10. If there is a single error, then, for some integer j, yi=xi for i=j and yj=xj+a where −10≤a≤10 and a=0. Note that a=yj−xj is the error in the jth place. It then follows that i=1∑10iyi=(i=1∑10ixi)+ja≡ja≡0(mod11).
These last two congruences hold because ∑i=110ixi≡0(mod11) and 11∤ja, because 11∤j and 11∤a. We conclude that y1y2…y10 is not a valid ISBN. So, we have detected the single error.
Now suppose that two unequal digits have been transposed. It follows that there are distinct integers j and k such that yj=xk and yk=xj, and yi=xi for i=j and i=k. Hence, i=1∑10iyi=(i=1∑10ixi)+(jxk−jxj)+(kxj−kxk)≡(j−k)(xk−xj)≡0(mod11), because ∑i=110ixi≡0(mod11) and 11∤(j−k) and 11∤(xk−xj). We see that y1y2…y10 is not a valid ISBN. Thus, we can detect the interchange of two unequal digits.