
























































Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!

























































The power of computers comes from their ability to execute the same task, or different versions of the same task, repeatedly. In computing, the theme of iteration is met in a number of guises. Many concepts in data models, such as lists, are forms of repetition, as “A list either is empty or is one element followed by another, then another, and so on.” Programs and algorithms use iteration to perform repetitive jobs without requiring a large number of similar steps to be specified individually, as “Do the next step 1000 times.” Programming languages use looping constructs, like the while- and for-statements of C, to implement iterative algorithms. Closely related to repetition is recursion, a technique in which a concept is defined, directly or indirectly, in terms of itself. For example, we could have defined a list by saying “A list either is empty or is an element followed by a list.” Recursion is supported by many programming languages. In C, a function F can call itself, either directly from within the body of F itself, or indirectly by calling some other function, which calls another, and another, and so on, until finally some function in the sequence calls F. Another important idea, induction, is closely related to “recursion” and is used in many mathematical proofs. Iteration, induction, and recursion are fundamental concepts that appear in many forms in data models, data structures, and algorithms. The following list gives some examples of uses of these concepts; each will be covered in some detail in this book.
An oversized Greek capital letter sigma is often used to denote a summation, as in
∑n i=1 i. This particular expression represents the sum of the integers from 1 to n; that is, it stands for the sum 1 + 2 + 3 + · · · + n. More generally, we can sum any function f (i) of the summation index i. (Of course, the index could be some symbol other than i.) The expression
∑b i=a f^ (i) stands for f (a) + f (a + 1) + f (a + 2) + · · · + f (b) For example,
∑m j=2 j (^2) stands for the sum 4 + 9 + 16 + · · · + m (^2). Here, the function f is “squaring,” and we used index j instead of i. As a special case, if b < a, then there are no terms in the sum
∑b i=a f^ (i), and the value of the expression, by convention, is taken to be 0. If b = a, then there is exactly one term, that for i = a. Thus, the value of the sum
∑a i=a f^ (i) is just^ f^ (a). The analogous notation for products uses an oversized capital pi. The expres- sion
∏b i=a f^ (i) stands for the product^ f^ (a)^ ×^ f^ (a^ + 1)^ ×^ f^ (a^ + 2)^ × · · · ×^ f^ (b); if b < a, the product is taken to be 1.
i=
i = n(n + 1)/ 2 (2.1)
which says that the sum of the integers from 1 to n equals n(n+1)/2. The basis could be S(1) — that is, Equation (2.1) with 1 in place of n — which is just the equality 1 = 1 × 2 /2. The inductive step is to show that
∑n i=1 i^ =^ n(n^ + 1)/^2 implies that
∑n+ i=1 i^ = (n^ + 1)(n^ + 2)/2; the former is^ S(n), which is Equation (2.1) itself, while the latter is S(n + 1), which is Equation (2.1) with n + 1 replacing n everywhere n appears. Section 2.3 will show us how to construct proofs such as this.
As you study this chapter, you should be alert to two themes that run through the various concepts. The first is self-definition, in which a concept is defined, or built, in terms of itself. For example, we mentioned that a list can be defined as being empty or as being an element followed by a list. The second theme is basis-induction. Recursive functions usually have some sort of test for a “basis” case where no recursive calls are made and an “inductive” case where one or more recursive calls are made. Inductive proofs are well known to consist of a basis and an inductive step, as do inductive definitions. This basis- induction pairing is so important that these words are highlighted in the text to introduce each occurrence of a basis case or an inductive step. There is no paradox or circularity involved in properly used self-definition, because the self-defined subparts are always “smaller” than the object being defined. Further, after a finite number of steps to smaller parts, we arrive at the basis case, at which the self-definition ends. For example, a list L is built from an element and a list that is one element shorter than L. When we reach a list with zero elements, we have the basis case of the definition of a list: “The empty list is a list.” As another example, if a recursive function works, the arguments of the call must, in some sense, be “smaller” than the arguments of the calling copy of the function. Moreover, after some number of recursive calls, we must get to arguments that are so “small” that the function does not make any more recursive calls.
To sort a list of n elements we need to permute the elements of the list so that they appear in nondecreasing order.
We sort this list by permuting it into the sequence (1, 1 , 2 , 3 , 4 , 5 , 5 , 6 , 9). Note that sorting not only orders the values so that each is either less than or equal to the one that follows, but it also preserves the number of occurrences of each value. Thus, the sorted list has two 1’s, two 5’s, and one each of the numbers that appear once in the original list. F
We can sort a list of elements of any type as long as the elements have a “less- than” order defined on them, which we usually represent by the symbol <. For example, if the values are real numbers or integers, then the symbol < stands for the usual less-than relation on reals or integers, and if the values are character strings, we would use the lexicographic order on strings. (See the box on “Lexicographic Order.”) Sometimes when the elements are complex, such as structures, we might use only a part of each element, such as one particular field, for the comparison. The comparison a ≤ b means, as always, that either a < b or a and b are the Sorted list same value. A list (a 1 , a 2 ,... , an) is said to be^ sorted^ if^ a 1 ≤^ a 2 ≤ · · · ≤^ an; that is, if the values are in nondecreasing order. Sorting is the operation of taking an arbitrary list (a 1 , a 2 ,... , an) and producing a list (b 1 , b 2 ,... , bn) such that
The usual way in which two character strings are compared is according to their lexicographic order. Let c 1 c 2 · · · ck and d 1 d 2 · · · dm be two strings, where each of the c’s and d’s represents a single character. The lengths of the strings, k and m, need not be the same. We assume that there is a < ordering on characters; for example, in C characters are small integers, so character constants and variables can be used as integers in arithmetic expressions. Thus we can use the conventional < relation on integers to tell which of two characters is “less than” the other. This ordering includes the natural notion that lower-case letters appearing earlier in the alphabet are “less than” lower-case letters appearing later in the alphabet, and the same holds for upper-case letters. We may then define the ordering on character strings called the lexicographic, dictionary, or alphabetic ordering, as follows. We say c 1 c 2 · · · ck < d 1 d 2 · · · dm if either of the following holds:
Proper prefix 1. The first string is a proper prefix of the second, which means that k < m and for i = 1, 2 ,... , k we have ci = di. According to this rule, bat < batter. As a special case of this rule, we could have k = 0, in which case the first string has no characters in it. We shall use ǫ, the Greek letter epsilon, to denote the Empty string empty string, the string with zero characters. When k = 0, rule (1) says that ǫ < s for any nonempty string s.
Permutation 2.^ List (b 1 , b 2 ,... , bn) is a^ permutation^ of the original list.^ That is, each value appears in list (a 1 , a 2 ,... , an) exactly as many times as that value appears in list (b 1 , b 2 ,... , bn).
A sorting algorithm takes as input an arbitrary list and produces as output a sorted list that is a permutation of the input.
base, ball, mound, bat, glove, batter
Given this input, and using lexicographic order, a sorting algorithm would produce this output: ball, base, bat, batter, glove, mound. F
Suppose we have an array A of n integers that we wish to sort into nondecreasing
void SelectionSort(int A[], int n) { int i, j, small, temp; (1) for (i = 0; i < n-1; i++) { /* set small to the index of the first occur- / / rence of the smallest element remaining / (2) small = i; (3) for (j = i+1; j < n; j++) (4) if (A[j] < A[small]) (5) small = j; / when we reach here, small is the index of / / the first smallest element in A[i..n-1]; / / we now exchange A[small] with A[i] */ (6) temp = A[small]; (7) A[small] = A[i]; (8) A[i] = temp; } }
Fig. 2.2. Iterative selection sort.
in A[small] to temp at line (6), move the value in A[i] to A[small] at line (7), and finally move the value originally in A[small] from temp to A[i] at line (8).
First, let us look at what happens when we run SelectionSort on an array with no elements. When n = 0, the body of the for-loop of line (1) is not executed, so SelectionSort does “nothing” gracefully. Now let us consider the case in which the array has only one element. Again, the body of the for-loop of line (1) is not executed. That response is satisfactory, because an array consisting of a single element is always sorted. The cases in which n is 0 or 1 are important boundary conditions, on which it is important to check the performance of any algorithm or program. Finally, let us run SelectionSort on a small array with four elements, where A[0] through A[3] are 0 1 2 3 A 40 30 20 10
We begin the outer loop with i = 0, and at line (2) we set small to 0. Lines (3) to (5) form an inner loop, in which j is set to 1, 2, and 3, in turn. With j = 1, the test of line (4) succeeds, since A[1], which is 30, is less than A[small], which is A[0], or 40. Thus, we set small to 1 at line (5). At the second iteration of lines (3) to (5), with j = 2, the test of line (4) again succeeds, since A[2] < A[1], and so we set small to 2 at line (5). At the last iteration of lines (3) to (5), with j = 3, the test of line (4) succeeds, since A[3] < A[2], and we set small to 3 at line (5). We now fall out of the inner loop to line (6). We set temp to 10, which is A[small], then A[3] to A[0], or 40, at line (7), and A[0] to 10 at line (8). Now, the
When we sort, we apply a comparison operation to the values being sorted. Often the comparison is made only on specific parts of the values and the part used in the comparison is called the key. For example, a course roster might be an array A of C structures of the form struct STUDENT { int studentID; char *name; char grade; } A[MAX];
We might want to sort by student ID, or name, or grade; each in turn would be the key. For example, if we wish to sort structures by student ID, we would use the comparison
A[j].studentID < A[small].studentID
at line (4) of SelectionSort. The type of array A and temporary temp used in the swap would be struct STUDENT, rather than integer. Note that entire structures are swapped, not just the key fields. Since it is time-consuming to swap whole structures, a more efficient approach is to use a second array of pointers to STUDENT structures and sort only the pointers in the second array. The structures themselves remain stationary in the first array. We leave this version of selection sort as an exercise.
first iteration of the outer loop is complete, and array A appears as
0 1 2 3 A 10 30 20 40
The second iteration of the outer loop, with i = 1, sets small to 1 at line (2). The inner loop sets j to 2 initially, and since A[2] < A[1], line (5) sets small to 2. With j = 3, the test of line (4) fails, since A[3] ≥ A[2]. Hence, small = 2 when we reach line (6). Lines (6) to (8) swap A[1] with A[2], leaving the array
0 1 2 3 A 10 20 30 40
Although the array now happens to be sorted, we still iterate the outer loop once more, with i = 2. We set small to 2 at line (2), and the inner loop is executed only with j = 3. Since the test of line (4) fails, small remains 2, and at lines (6) through (8), we “swap” A[2] with itself. The reader should check that the swapping has no effect when small = i. F
Figure 2.3 shows how the function SelectionSort can be used in a complete program to sort a sequence of n integers, provided that n ≤ 100. Line (1) reads and stores n integers in an array A. If the number of inputs exceeds MAX, only the first MAX integers are put into A. A message warning the user that the number of inputs is too large would be useful here, but we omit it.
2.2.3: Write a C function that takes two linked lists of characters as arguments and returns TRUE if the first string precedes the second in lexicographic order. Hint: Implement the algorithm for comparing character strings that was described in this section. Use recursion by having the function call itself on the tails of the character strings when it finds that the first characters of both strings are the same. Alternatively, one can develop an iterative algorithm to do the same. 2.2.4: Modify your program from Exercise 2.2.3 to ignore the case of letters in comparisons. 2.2.5: What does selection sort do if all elements are the same? 2.2.6: Modify Fig. 2.3 to perform selection sort when array elements are not inte- gers, but rather structures of type struct STUDENT, as defined in the box “Sorting on Keys.” Suppose that the key field is studentID. 2.2.7: Further modify Fig. 2.3 so that it sorts elements of an arbitrary type T. You may assume, however, that there is a function key that takes an element of type T as argument and returns the key for that element, of some arbitrary type K. Also assume that there is a function lt that takes two elements of type K as arguments and returns TRUE if the first is “less than” the second, and FALSE otherwise. 2.2.8: Instead of using integer indexes into the array A, we could use pointers to integers to indicate positions in the array. Rewrite the selection sort algorithm of Fig. 2.3 using pointers. 2.2.9*: As mentioned in the box on “Sorting on Keys,” if the elements to be sorted are large structures such as type STUDENT, it makes sense to leave them stationary in an array and sort pointers to these structures, found in a second array. Write this variation of selection sort. 2.2.10: Write an iterative program to print the distinct elements of an integer array. 2.2.11: Use the
and
notations described at the beginning of this chapter to express the following. a) The sum of the odd integers from 1 to 377 b) The sum of the squares of the even integers from 2 to n (assume that n is even) c) The product of the powers of 2 from 8 to 2k 2.2.12: Show that when small = i, lines (6) through (8) of Fig. 2.2 (the swapping steps) do not have any effect on array A.
2.3 Inductive Proofs
Mathematical induction is a useful technique for proving that a statement S(n) is true for all nonnegative integers n, or, more generally, for all integers at or above some lower limit. For example, in the introduction to this chapter we suggested that the statement
∑n i=1 i^ =^ n(n^ + 1)/2 can be proved true for all^ n^ ≥^ 1 by an induction on n. Now, let S(n) be some arbitrary statement about an integer n. In the simplest form of an inductive proof of the statement S(n), we prove two facts:
It is often useful to explain an induction by giving the intuitive meaning of the variable n in the statement S(n) that we are proving. If n has no special meaning, as in Example 2.4, we simply say “The proof is by induction on n.” In other cases, n may have a physical meaning, as in Example 2.6, where n is the number of bits in the code words. There we can say, “The proof is by induction on the number of bits in the code words.”
S(n) · · · S(2) S(1) S(0)
Fig. 2.4. In an inductive proof, each instance of the statement S(n) is proved using the statement for the next lower value of n.
Figure 2.4 illustrates an induction starting at 0. For each integer n, there is a statement S(n) to prove. The proof for S(1) uses S(0), the proof for S(2) uses S(1), and so on, as represented by the arrows. The way each statement depends on the previous one is uniform. That is, by one proof of the inductive step, we prove each of the steps implied by the arrows in Fig. 2.4.
STATEMENT S(n):
∑^ n
i=
2 i^ = 2n+1^ − 1 for any n ≥ 0.
That is, the sum of the powers of 2, from the 0th power to the nth power, is 1 less than the (n + 1)st power of 2.^3 For example, 1 + 2 + 4 + 8 = 16 − 1. The proof proceeds as follows.
BASIS. To prove the basis, we substitute 0 for n in the equation S(n). Then S(n) becomes
(^3) S(n) can be proved without induction, using the formula for the sum of a geometric series. However, it will serve as a simple example of the technique of mathematical induction. Further, the proofs of the formulas for the sum of a geometric or arithmetic series that you have probably seen in high school are rather informal, and strictly speaking, mathematical induction should be used to prove those formulas.
People are often confused when they have to substitute for a variable such as n in S(n), an expression involving the same variable. For example, we substituted n + 1 for n in S(n) to get Equation (2.3). To make the substitution, we must first mark every occurrence of n in S. One useful way to do so is to replace n by some new variable — say m — that does not otherwise appear in S. For example, S(n) would become
∑^ m
i=
2 i^ = 2m+1^ − 1
We then literally substitute the desired expression, n + 1 in this case, for each occurrence of m. That gives us
n∑+
i=
2 i^ = 2(n+1)+1^ − 1
When we simplify (n + 1) + 1 to n + 2, we have (2.3). Note that we should put parentheses around the expression substituted, to avoid accidentally changing the order of operations. For example, had we substi- tuted n + 1 for m in the expression 2 × m, and not placed the parentheses around n + 1, we would have gotten 2 × n + 1, rather than the correct expression 2 × (n + 1), which equals 2 × n + 2.
each of our “proofs” that induction works requires an inductive proof itself, and therefore is no proof at all. Technically, induction must be accepted as axiomatic. Nevertheless, many people find the following intuition useful. In what follows, we assume that the basis value is n = 0. That is, we know that S(0) is true and that for all n greater than 0, if S(n) is true, then S(n + 1) is true. Similar arguments work if the basis value is any other integer.
First “proof”: Iteration of the inductive step. Suppose we want to show that S(a) is true for a particular nonnegative integer a. If a = 0, we just invoke the truth of the basis, S(0). If a > 0, then we argue as follows. We know that S(0) is true, from the basis. The statement “S(n) implies S(n + 1),” with 0 in place of n, says “S(0) implies S(1).” Since we know that S(0) is true, we now know that S(1) is true. Similarly, if we substitute 1 for n, we get “S(1) implies S(2),” and so we also know that S(2) is true. Substituting 2 for n, we have “S(2) implies S(3),” so that S(3) is true, and so on. No matter what the value of a is, we eventually get to S(a), and we are done.
Second “proof”: Least counterexample. Suppose S(n) were not true for at least one value of n. Let a be the least nonnegative integer for which S(a) is false. If a = 0, then we contradict the basis, S(0), and so a must be greater than 0. But if a > 0, and a is the least nonnegative integer for which S(a) is false, then S(a − 1) must be true. Now, the inductive step, with n replaced by a−1, tells us that S(a−1) implies S(a). Since S(a−1) is true, S(a) must be true, another contradiction. Since we assumed there were nonnegative values of n for which S(n) is false and derived a contradiction, S(n) must therefore be true for any n ≥ 0.
We shall now begin an extended example of “error-detecting codes,” a concept that is interesting in its own right and also leads to an interesting inductive proof. When we transmit information over a data network, we code characters (letters, digits, punctuation, and so on) into strings of bits, that is, 0’s and 1’s. For the moment let us assume that characters are represented by seven bits. However, it is normal to transmit more than seven bits per character, and an eighth bit can be used to help detect some simple errors in transmission. That is, occasionally, one of the 0’s or 1’s gets changed because of noise during transmission, and is received as the opposite bit; a 0 entering the transmission line emerges as a 1, or vice versa. It is useful if the communication system can tell when one of the eight bits has been changed, so that it can signal for a retransmission. To detect changes in a single bit, we must be sure that no two characters are represented by sequences of bits that differ in only one position. For then, if that position were changed, the result would be the code for the other character, and we could not detect that an error had occurred. For example, if the code for one character is the sequence of bits 01010101, and the code for another is 01000101, then a change in the fourth position from the left turns the former into the latter. One way to be sure that no characters have codes that differ in only one position Parity bit is to precede the conventional 7-bit code for the character by a parity bit. If the total number of 1’s in a group of bits is odd, the group is said to have odd parity. If the number of 1’s in the group is even, then the group has even parity. The coding scheme we select is to represent each character by an 8-bit code with even parity; we could as well have chosen to use only the codes with odd parity. We force the parity to be even by selecting the parity bit judiciously.
“American Standard Code for Information Interchange”) 7-bit code for the charac- ter A is 1000001. That sequence of seven bits already has an even number of 1’s, and so we prefix it by 0 to get 01000001. The conventional code for C is 1000011, which differs from the 7-bit code for A only in the sixth position. However, this code has odd parity, and so we prefix a 1 to it, yielding the 8-bit code 11000011 with even parity. Note that after prefixing the parity bits to the codes for A and C, we have 01000001 and 11000011, which differ in two positions, namely the first and seventh, as seen in Fig. 2.5. F
Fig. 2.5. We can choose the initial parity bit so the 8-bit code always has even parity.
We can always pick a parity bit to attach to a 7-bit code so that the number of 1’s in the 8-bit code is even. We pick parity bit 0 if the 7-bit code for the character at hand has even parity, and we pick parity bit 1 if the 7-bit code has odd parity. In either case, the number of 1’s in the 8-bit code is even.
show, using this assumption, that any error-detecting set C of strings with length n + 1 has at most 2n^ strings. Thus, divide C into two sets, C 0 , the set of strings in C that begin with 0, and C 1 , the set of strings in C that begin with 1. For instance, suppose n = 2 and C is the code with strings of length n + 1 = 3 constructed using a parity bit. Then, as shown in Fig. 2.6, C consists of the strings 000, 101, 110, and 011; C 0 consists of the strings 000 and 011, and C 1 has the other two strings, 101 and 110.
Fig. 2.6. The set C is split into C 0 , the strings beginning with 0, and C 1 , the strings beginning with 1. D 0 and D 1 are formed by deleting the leading 0’s and 1’s, respectively.
Consider the set D 0 consisting of those strings in C 0 with the leading 0 removed. In our example above, D 0 contains the strings 00 and 11. We claim that D 0 cannot have two strings differing in only one bit. The reason is that if there are two such strings — say a 1 a 2 · · · an and b 1 b 2 · · · bn — then restoring their leading 0’s gives us two strings in C 0 , 0a 1 a 2 · · · an and 0b 1 b 2 · · · bn, and these strings would differ in only one position as well. But strings in C 0 are also in C, and we know that C does not have two strings that differ in only one position. Thus, neither does D 0 , and so D 0 is an error detecting set. Now we can apply the inductive hypothesis to conclude that D 0 , being an error-detecting set with strings of length n, has at most 2n−^1 strings. Thus, C 0 has at most 2n−^1 strings. We can reason similarly about the set C 1. Let D 1 be the set of strings in C 1 , with their leading 1’s deleted. D 1 is an error-detecting set with strings of length n, and by the inductive hypothesis, D 1 has at most 2n−^1 strings. Thus, C 1 has at most 2n−^1 strings. However, every string in C is in either C 0 or C 1. Therefore, C has at most 2n−^1 + 2n−^1 , or 2n^ strings.
We have proved that S(n) implies S(n + 1), and so we may conclude that S(n) is true for all n ≥ 1. We exclude n = 0 from the claim, because the basis is n = 1, not n = 0. We now see that the error-detecting sets constructed by parity check are as large as possible, since they have exactly 2n−^1 strings when strings of n bits are used. F
There is no “crank to turn” that is guaranteed to give you an inductive proof of any (true) statement S(n). Finding inductive proofs, like finding proofs of any kind, or like writing programs that work, is a task with intellectual challenge, and we can only offer a few words of advice. If you examine the inductive steps in Examples 2. and 2.6, you will notice that in each case we had to rework the statement S(n + 1) that we were trying to prove so that it incorporated the inductive hypothesis, S(n), plus something extra. In Example 2.4, we expressed the sum 1 + 2 + 4 + · · · + 2n^ + 2n+ as the sum 1 + 2 + 4 + · · · + 2n which the inductive hypothesis tells us something about, plus the extra term, 2n+1. In Example 2.6, we expressed the set C, with strings of length n + 1, in terms of two sets of strings (which we called D 0 and D 1 ) of length n, so that we could apply the inductive hypothesis to these sets and conclude that both of these sets were of limited size. Of course, working with the statement S(n + 1) so that we can apply the inductive hypothesis is just a special case of the more universal problem-solving adage “Use what is given.” The hard part always comes when we must deal with the “extra” part of S(n+1) and complete the proof of S(n+1) from S(n). However, the following is a universal rule: F An inductive proof must at some point say “· · ·and by the inductive hypothesis we know that· · · .” If it doesn’t, then it isn’t an inductive proof.
2.3.1: Show the following formulas by induction on n starting at n = 1.
a)
∑n i=1 i^ =^ n(n^ + 1)/2. b)
∑n i=1 i
(^2) = n(n + 1)(2n + 1)/6.
c)
∑n i=1 i (^3) = n (^2) (n + 1) (^2) /4.
d)
∑n i=1 1 /i(i^ + 1) =^ n/(n^ + 1).
Triangular 2.3.2: Numbers of the form tn = n(n + 1)/2 are called triangular numbers, because number marbles arranged in an equilateral triangle, n on a side, will total
∑n i=1 i^ marbles, which we saw in Exercise 2.3.1(a) is tn marbles. For example, bowling pins are arranged in a triangle 4 on a side and there are t 4 = 4 × 5 /2 = 10 pins. Show by induction on n that
∑n j=1 tj^ =^ n(n^ + 1)(n^ + 2)/6. 2.3.3: Identify the parity of each of the following bit sequences as even or odd:
a) 01101 b) 111000111
There are two formulas from high-school algebra that we shall use frequently. They each have interesting inductive proofs, which we ask the reader to provide in Exer- cises 2.3.9 and 2.3.10.
Arithmetic An arithmetic series is a sequence of n numbers of the form series a, (a + b), (a + 2b),... ,
a + (n − 1)b
The first term is a, and each term is b larger than the one before. The sum of these n numbers is n times the average of the first and last terms; that is: n∑− 1
i=
a + bi = n
2 a + (n − 1)b
For example, consider the sum of 3 + 5 + 7 + 9 + 11. There are n = 5 terms, the first is 3 and the last 11. Thus, the sum is 5 × (3 + 11)/2 = 5 × 7 = 35. You can check that this sum is correct by adding the five integers.
Geometric A geometric series is a sequence of n numbers of the form series a, ar, ar^2 , ar^3 ,... , arn−^1 That is, the first term is a, and each successive term is r times the previous term. The formula for the sum of n terms of a geometric series is n∑− 1
i=
ari^ =
(arn^ − a) (r − 1)
Here, r can be greater or less than 1. If r = 1, the above formula does not work, but all terms are a so the sum is obviously an. As an example of a geometric series sum, consider 1 + 2 + 4 + 8 + 16. Here, n = 5, the first term a is 1, and the ratio r is 2. Thus, the sum is (1 × 25 − 1)/(2 − 1) = (32 − 1)/1 = 31 as you may check. For another example, consider 1 + 1/2 + 1/4 + 1/8 + 1/16. Again n = 5 and a = 1, but r = 1/2. The sum is ( 1 × ( 12 )^5 − 1
Error-correcting 2.3.13**: If no two strings in a code differ in fewer than three positions, then we code can actually correct a single error, by finding the unique string in the code that differs from the received string in only one position. It turns out that there is a code of 7-bit strings that corrects single errors and contains 16 strings. Find such a code. Hint: Reasoning it out is probably best, but if you get stuck, write a program that searches for such a code.
2.3.14*: Does the even parity code detect any “double errors,” that is, changes in two different bits? Can it correct any single errors?
Let us summarize Section 2.3 by giving a template into which the simple inductions of that section fit. Section 2.4 will cover a more general template.
2.4 Complete Induction
In the examples seen so far, we have proved that S(n + 1) is true using only S(n) as an inductive hypothesis. However, since we prove our statement S for values of its parameter starting at the basis value and proceeding upward, we are entitled to use S(i) for all values of i, from the basis value up to n. This form of induction is Strong and called complete (or sometimes perfect or strong) induction, while the simple form of weak induction induction of Section 2.3, where we used only S(n) to prove S(n + 1) is sometimes called weak induction. Let us begin by considering how to perform a complete induction starting with basis n = 0. We prove that S(n) is true for all n ≥ 0 in two steps:
As for weak induction described in the previous section, we can also pick some value a other than 0 as the basis. Then, for the basis we prove S(a), and in the inductive step we are entitled to assume only S(a), S(a + 1),... , S(n). Note that weak induction is a special case of complete induction in which we elect not to use any of the previous statements except S(n) to prove S(n + 1). Figure 2.7 suggests how complete induction works. Each instance of the state- ment S(n) can (optionally) use any of the lower-indexed instances to its right in its proof.