34.0 KB · commit a9602da
Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object in terms of itself. This process is called recursion. For instance, the picture shown in Figure 1 is produced recursively. First, an original picture is given. Then a process of successively superimposing centered smaller pictures on top of the previous pictures is carried out.
Figure 1 — A Recursively Defined Picture.
We can use recursion to define sequences, functions, and sets. In Section 2.4, and in most beginning mathematics courses, the terms of a sequence are specified using an explicit formula. For instance, the sequence of powers of 2 is given by an=2n for n=0,1,2,…. Recall from Section 2.4 that we can also define a sequence recursively by specifying how terms of the sequence are found from previous terms. The sequence of powers of 2 can also be defined by giving the first term of the sequence, namely, a0=1, and a rule for finding a term of the sequence from the previous one, namely, an+1=2an for n=0,1,2,…. When we define a sequence recursively by specifying how terms of the sequence are found from previous terms, we can use induction to prove results about the sequence.
When we define a set recursively, we specify some initial elements in a basis step and provide a rule for constructing new elements from those we already have in the recursive step. To prove results about recursively defined sets we use a method called structural induction.
We use two steps to define a function with the set of nonnegative integers as its domain:
Such a definition is called a recursive or inductive definition. Note that a function f(n) from the set of nonnegative integers to the set of real numbers is the same as a sequence a0,a1,… where ai is a real number for every nonnegative integer i. So, defining a real-valued sequence a0,a1,… using a recurrence relation, as was done in Section 2.4, is the same as defining a function from the set of nonnegative integers to the set of real numbers.
Suppose that f is defined recursively by
f(0)f(n+1)=3,=2f(n)+3.Find f(1),f(2),f(3), and f(4).
Solution: From the recursive definition it follows that
f(1)f(2)f(3)f(4)=2f(0)+3=2⋅3+3=9,=2f(1)+3=2⋅9+3=21,=2f(2)+3=2⋅21+3=45,=2f(3)+3=2⋅45+3=93.◀
Recursively defined functions are well defined. That is, for every positive integer, the value of the function at this integer is determined in an unambiguous way. This means that given any positive integer, we can use the two parts of the definition to find the value of the function at that integer, and that we obtain the same value no matter how we apply the two parts of the definition. This is a consequence of the principle of mathematical induction. Additional examples of recursive definitions are given in Examples 2 and 3.
Give a recursive definition of an, where a is a nonzero real number and n is a nonnegative integer.
Solution: The recursive definition contains two parts. First a0 is specified, namely, a0=1. Then the rule for finding an+1 from an, namely, an+1=a⋅an, for n=0,1,2,3,…, is given. These two equations uniquely define an for all nonnegative integers n. ◀
Give a recursive definition of ∑k=0nak.
Solution: The first part of the recursive definition is k=0∑0ak=a0. The second part is k=0∑n+1ak=(k=0∑nak)+an+1. ◀
In some recursive definitions of functions, the values of the function at the first k positive integers are specified, and a rule is given for determining the value of the function at larger integers from its values at some or all of the preceding k integers. That recursive definitions defined in this way produce well-defined functions follows from strong induction.
Recall from Section 2.4 that the Fibonacci numbers, f0,f1,f2,…, are defined by the equations f0=0, f1=1, and fn=fn−1+fn−2 for n=2,3,4,…. [We can think of the Fibonacci number fn either as the nth term of the sequence of Fibonacci numbers f0,f1,… or as the value at the integer n of a function f(n).]
We can use the recursive definition of the Fibonacci numbers to prove many properties of these numbers. We give one such property in Example 4.
Show that whenever n≥3, fn>αn−2, where α=(1+5)/2.
Solution: We can use strong induction to prove this inequality. Let P(n) be the statement fn>αn−2. We want to show that P(n) is true whenever n is an integer greater than or equal to 3.
BASIS STEP: First, note that
αα2<2=f3,=(3+5)/2<3=f4,so P(3) and P(4) are true.
INDUCTIVE STEP: Assume that P(j) is true, namely, that fj>αj−2, for all integers j with 3≤j≤k, where k≥4. We must show that P(k+1) is true, that is, that fk+1>αk−1. Because α is a solution of x2−x−1=0 (as the quadratic formula shows), it follows that α2=α+1. Therefore, αk−1=α2⋅αk−3=(α+1)αk−3=α⋅αk−3+1⋅αk−3=αk−2+αk−3.
By the inductive hypothesis, because k≥4, we have
fk−1fk>αk−3,>αk−2.Therefore, it follows that fk+1=fk+fk−1>αk−2+αk−3=αk−1. Hence, P(k+1) is true. This completes the proof. ◀
We can now show that the Euclidean algorithm, introduced in Section 4.3, uses O(logb) divisions to find the greatest common divisor of the positive integers a and b, where a≥b.
Proof: Recall that when the Euclidean algorithm is applied to find gcd(a,b) with a≥b, this sequence of equations (where a=r0 and b=r1) is obtained:
r0r1rn−2rn−1=r1q1+r2=r2q2+r3⋮=rn−1qn−1+rn=rnqn.0≤r2<r1,0≤r3<r2,0≤rn<rn−1,Here n divisions have been used to find rn=gcd(a,b). Note that the quotients q1,q2,…,qn−1 are all at least 1. Moreover, qn≥2, because rn<rn−1. This implies that
rnrn−1rn−2r2b=r1≥1=f2,≥2rn≥2f2=f3,≥rn−1+rn≥f3+f2=f4,⋮≥r3+r4≥fn−1+fn−2=fn,≥r2+r3≥fn+fn−1=fn+1.It follows that if n divisions are used by the Euclidean algorithm to find gcd(a,b) with a≥b, then b≥fn+1. By Example 4 we know that fn+1>αn−1 for n>2, where α=(1+5)/2. Therefore, it follows that b>αn−1. Furthermore, because log10α≈0.208>1/5, we see that log10b>(n−1)log10α>(n−1)/5.
Hence, n−1<5⋅log10b. Now suppose that b has k decimal digits. Then b<10k and log10b<k. It follows that n−1<5k, and because k is an integer, it follows that n≤5k. This finishes the proof. ■
Because the number of decimal digits in b, which equals ⌊log10b⌋+1, is less than or equal to log10b+1, Theorem 1 tells us that the number of divisions required to find gcd(a,b) with
a>b is less than or equal to 5(log10b+1). Because 5(log10b+1) is O(logb), we see that O(logb) divisions are used by the Euclidean algorithm to find gcd(a,b) whenever a>b.
We have explored how functions can be defined recursively. We now turn our attention to how sets can be defined recursively. Just as in the recursive definition of functions, recursive definitions of sets have two parts, a basis step and a recursive step. In the basis step, an initial collection of elements is specified. In the recursive step, rules for forming new elements in the set from those already known to be in the set are provided. Recursive definitions may also include an exclusion rule, which specifies that a recursively defined set contains nothing other than those elements specified in the basis step or generated by applications of the recursive step. In our discussions, we will always tacitly assume that the exclusion rule holds and no element belongs to a recursively defined set unless it is in the initial collection specified in the basis step or can be generated using the recursive step one or more times. Later we will see how we can use a technique known as structural induction to prove results about recursively defined sets.
Examples 5, 6, 8, and 9 illustrate the recursive definition of sets. In each example, we show those elements generated by the first few applications of the recursive step.
Consider the subset S of the set of integers recursively defined by
The new elements found to be in S are 3 by the basis step, 3+3=6 at the first application of the recursive step, 3+6=6+3=9 and 6+6=12 at the second application of the recursive step, and so on. We will show in Example 10 that S is the set of all positive multiples of 3. ◀
Recursive definitions play an important role in the study of strings. (See Chapter 13 for an introduction to the theory of formal languages, for example.) Recall from Section 2.4 that a string over an alphabet Σ is a finite sequence of symbols from Σ. We can define Σ∗, the set of strings over Σ, recursively, as Definition 1 shows.
The basis step of the recursive definition of strings says that the empty string belongs to Σ∗. The recursive step states that new strings are produced by adding a symbol from Σ to the end of strings in Σ∗. At each application of the recursive step, strings containing one additional symbol are generated.
If Σ={0,1}, the strings found to be in Σ∗, the set of all bit strings, are λ, specified to be in Σ∗ in the basis step, 0 and 1 formed during the first application of the recursive step, 00,01,10, and 11 formed during the second application of the recursive step, and so on. ◀
Recursive definitions can be used to define operations or functions on the elements of recursively defined sets. This is illustrated in Definition 2 of the concatenation of two strings and Example 7 concerning the length of a string.
The concatenation of the strings w1 and w2 is often written as w1w2 rather than w1⋅w2. By repeated application of the recursive definition, it follows that the concatenation of two strings w1 and w2 consists of the symbols in w1 followed by the symbols in w2. For instance, the concatenation of w1=abra and w2=cadabra is w1w2=abracadabra.
Give a recursive definition of l(w), the length of the string w.
Solution: The length of a string can be recursively defined by
l(λ)l(wx)=0;=l(w)+1if w∈Σ∗ and x∈Σ.◀
Another important use of recursive definitions is to define well-formed formulae of various types. This is illustrated in Examples 8 and 9.
We can define the set of well-formed formulae in propositional logic involving T,F, propositional variables, and operators from the set {¬,∧,∨,→,↔}.
For example, by the basis step we know that T,F,p, and q are well-formed formulae, where p and q are propositional variables. From an initial application of the recursive step, we know that (p∨q),(p→F),(F→q), and (q∧F) are well-formed formulae. A second application of the recursive step shows that ((p∨q)→(q∧F)),(q∨(p∨q)), and ((p→F)→T) are well-formed formulae. We leave it to the reader to show that p¬∧q,pq∧, and ¬∧pq are not well-formed formulae, by showing that none can be obtained using the basis step and one or more applications of the recursive step. ◀
We can define the set of well-formed formulae consisting of variables, numerals, and operators from the set {+,−,∗,/,↑} (where ∗ denotes multiplication and ↑ denotes exponentiation) recursively:
For example, by the basis step we see that x,y,0, and 3 are well-formed formulae (as is any variable or numeral). Well-formed formulae generated by applying the recursive step once include (x+3),(3+y),(x−y),(3−0),(x∗3),(3∗y),(3/0),(x/y),(3↑x), and (0↑3). Applying the recursive step twice shows that formulae such as ((x+3)+3) and (x−(3∗y)) are well-formed formulae. [Note that (3/0) is a well-formed formula because we are concerned only with syntax matters here.] We leave it to the reader to show that each of the formulae x3+,y∗+x, and ∗x/y is not a well-formed formula by showing that none of them can be obtained from the basis step and one or more applications of the recursive step. ◀
We will study trees extensively in Chapter 11. A tree is a special type of a graph; a graph is made up of vertices and edges connecting some pairs of vertices. We will study graphs in Chapter 10. We will briefly introduce them here to illustrate how they can be defined recursively.
In Figure 2 we illustrate some of the rooted trees formed starting with the basis step and applying the recursive step one time and two times. Note that infinitely many rooted trees are formed at each application of the recursive definition.
Figure 2 — Building Up Rooted Trees.
Figure 3 — Building Up Extended Binary Trees.
Binary trees are a special type of rooted trees. We will provide recursive definitions of two types of binary trees—full binary trees and extended binary trees. In the recursive step of the definition of each type of binary tree, two binary trees are combined to form a new tree with one of these trees designated the left subtree and the other the right subtree. In extended binary trees, the left subtree or the right subtree can be empty, but in full binary trees this is not possible. Binary trees are one of the most important types of structures in computer science. In Chapter 11 we will see how they can be used in searching and sorting algorithms, in algorithms for compressing data, and in many other applications. We first define extended binary trees.
Figure 3 shows how extended binary trees are built up by applying the recursive step from one to three times.
We now show how to define the set of full binary trees. Note that the difference between this recursive definition and that of extended binary trees lies entirely in the basis step.
Figure 4 — Building Up Full Binary Trees.
Figure 4 shows how full binary trees are built up by applying the recursive step one and two times.
To prove results about recursively defined sets, we generally use some form of mathematical induction. Example 10 illustrates the connection between recursively defined sets and mathematical induction.
Show that the set S defined in Example 5 by specifying that 3∈S and that if x∈S and y∈S, then x+y∈S, is the set of all positive integers that are multiples of 3.
Solution: Let A be the set of all positive integers divisible by 3. To prove that A=S, we must show that A is a subset of S and that S is a subset of A. To prove that A is a subset of S, we must show that every positive integer divisible by 3 is in S. We will use mathematical induction to prove this.
Let P(n) be the statement that 3n belongs to S. The basis step holds because by the first part of the recursive definition of S, 3⋅1=3 is in S. To establish the inductive step, assume that P(k) is true, namely, that 3k is in S. Because 3k is in S and because 3 is in S, it follows from the second part of the recursive definition of S that 3k+3=3(k+1) is also in S.
To prove that S is a subset of A, we use the recursive definition of S. First, the basis step of the definition specifies that 3 is in S. Because 3=3⋅1, all elements specified to be in S in this step are divisible by 3 and are therefore in A. To finish the proof, we must show that all integers in S generated using the second part of the recursive definition are in A. This consists of showing that x+y is in A whenever x and y are elements of S also assumed to be in A. Now if x and y are both in A, it follows that 3∣x and 3∣y. By part (i) of Theorem 1 of Section 4.1, it follows that 3∣x+y, completing the proof. ◀
In Example 10 we used mathematical induction over the set of positive integers and a recursive definition to prove a result about a recursively defined set. However, instead of using mathematical induction directly to prove results about recursively defined sets, we can use a more convenient form of induction known as structural induction. A proof by structural induction consists of two parts. These parts are:
The validity of structural induction follows from the principle of mathematical induction for the nonnegative integers. To see this, let P(n) state that the claim is true for all elements of the set that are generated by n or fewer applications of the rules in the recursive step of a recursive definition. We will have established that the principle of mathematical induction implies the principle of structural induction if we can show that P(n) is true whenever n is a positive integer. In the basis step of a proof by structural induction we show that P(0) is true. That is, we show that the result is true of all elements specified to be in the set in the basis step of the definition. A consequence of the recursive step is that if we assume P(k) is true, it follows that P(k+1) is true. When we have completed a proof using structural induction, we have shown that P(0) is true and that P(k) implies P(k+1). By mathematical induction it follows that P(n) is true for all nonnegative integers n. This also shows that the result is true for all elements generated by the recursive definition, and shows that structural induction is a valid proof technique.
Structural induction can be used to prove that all members of a set constructed recursively have a particular property. We will illustrate this idea by using structural induction to prove results about well-formed formulae, strings, and binary trees. For each proof, we have to carry out the appropriate basis step and the appropriate recursive step. For example, to use structural induction to prove a result about the set of well-formed formulae defined in Example 8, where we specify that T,F, and every propositional variable s are well-formed formulae and where we specify that if E and F are well-formed formulae, then (¬E),(E∧F),(E∨F),(E→F), and (E↔F) are well-formed formulae, we need to complete this basis step and this recursive step:
Example 11 illustrates how we can prove results about well-formed formulae using structural induction.
Show that every well-formed formula for compound propositions, as defined in Example 8, contains an equal number of left and right parentheses.
Solution: BASIS STEP: Each of the formula T,F, and s contains no parentheses, so clearly they contain an equal number of left and right parentheses.
RECURSIVE STEP: Assume p and q are well-formed formulae each containing an equal number of left and right parentheses. That is, if lp and lq are the number of left parentheses in p and q, respectively, and rp and rq are the number of right parentheses in p and q, respectively, then lp=rp and lq=rq. To complete the inductive step, we need to show that each of
(¬p),(p∨q),(p∧q),(p→q), and (p↔q) also contains an equal number of left and right parentheses. The number of left parentheses in the first of these compound propositions equals lp+1 and in each of the other compound propositions equals lp+lq+1. Similarly, the number of right parentheses in the first of these compound propositions equals rp+1 and in each of the other compound propositions equals rp+rq+1. Because lp=rp and lq=rq, it follows that each of these compound expressions contains the same number of left and right parentheses. This completes the proof by structural induction. ◀
Suppose that P(w) is a propositional function over the set of strings w∈Σ∗. To use structural induction to prove that P(w) holds for all strings w∈Σ∗, we need to complete both a basis step and a recursive step. These steps are:
Example 12 illustrates how structural induction can be used in proofs about strings.
Use structural induction to prove that l(xy)=l(x)+l(y), where x and y belong to Σ∗, the set of strings over the alphabet Σ.
Solution: We will base our proof on the recursive definition of the set Σ∗ given in Definition 1 and the definition of the length of a string in Example 7, which specifies that l(λ)=0 and l(wx)=l(w)+1 when w∈Σ∗ and x∈Σ. Let P(y) be the statement that l(xy)=l(x)+l(y) whenever x belongs to Σ∗.
BASIS STEP: To complete the basis step, we must show that P(λ) is true. That is, we must show that l(xλ)=l(x)+l(λ) for all x∈Σ∗. Because l(xλ)=l(x)=l(x)+0=l(x)+l(λ) for every string x, it follows that P(λ) is true.
RECURSIVE STEP: To complete the inductive step, we assume that P(y) is true and show that this implies that P(ya) is true whenever a∈Σ. What we need to show is that l(xya)=l(x)+l(ya) for every a∈Σ. To show this, note that by the recursive definition of l(w) (given in Example 7), we have l(xya)=l(xy)+1 and l(ya)=l(y)+1. And, by the inductive hypothesis, l(xy)=l(x)+l(y). We conclude that l(xya)=l(x)+l(y)+1=l(x)+l(ya). ◀
We can prove results about trees or special classes of trees using structural induction. For example, to prove a result about full binary trees using structural induction we need to complete this basis step and this recursive step:
Before we provide an example showing how structural induction can be used to prove a result about full binary trees, we need some definitions. We will recursively define the height h(T) and the number of vertices n(T) of a full binary tree T. We begin by defining the height of a full binary tree.
If we let n(T) denote the number of vertices in a full binary tree, we observe that n(T) satisfies the following recursive formula:
We now show how structural induction can be used to prove a result about full binary trees.
Proof: We prove this inequality using structural induction.
BASIS STEP: For the full binary tree consisting of just the root r the result is true because n(T)=1 and h(T)=0, so that n(T)=1≤20+1−1=1.
RECURSIVE STEP: For the inductive hypothesis we assume that n(T1)≤2h(T1)+1−1 and n(T2)≤2h(T2)+1−1 whenever T1 and T2 are full binary trees. By the recursive formulae for n(T) and h(T) we have n(T)=1+n(T1)+n(T2) and h(T)=1+max(h(T1),h(T2)). We find that
n(T)=1+n(T1)+n(T2)≤1+(2h(T1)+1−1)+(2h(T2)+1−1)≤2⋅max(2h(T1)+1,2h(T2)+1)−1=2⋅2max(h(T1),h(T2))+1−1=2⋅2h(T)−1=2h(T)+1−1.by the recursive formula for n(T)by the inductive hypothesisbecause the sum of two terms is at most 2 times the largerbecause max(2x,2y)=2max(x,y)by the recursive definition of h(T)This completes the recursive step. ■
We can extend mathematical induction to prove results about other sets that have the well-ordering property besides the set of integers. Although we will discuss this concept in detail in Section 9.6, we provide an example here to illustrate the usefulness of such an approach.
As an example, note that we can define an ordering on N×N, the ordered pairs of nonnegative integers, by specifying that (x1,y1) is less than or equal to (x2,y2) if either x1<x2, or x1=x2 and y1<y2; this is called the lexicographic ordering. The set N×N with this ordering has the property that every subset of N×N has a least element (see Exercise 53 in Section 9.6). This implies that we can recursively define the terms am,n, with m∈N and n∈N, and prove results about them using a variant of mathematical induction, as illustrated in Example 13.
Suppose that am,n is defined recursively for (m,n)∈N×N by a0,0=0 and
am,n={am−1,n+1am,n−1+nif n=0 and m>0if n>0.Show that am,n=m+n(n+1)/2 for all (m,n)∈N×N, that is, for all pairs of nonnegative integers.
Solution: We can prove that am,n=m+n(n+1)/2 using a generalized version of mathematical induction. The basis step requires that we show that this formula is valid when (m,n)=(0,0). The induction step requires that we show that if the formula holds for all pairs smaller than (m,n) in the lexicographic ordering of N×N, then it also holds for (m,n).
BASIS STEP: Let (m,n)=(0,0). Then by the basis case of the recursive definition of am,n we have a0,0=0. Furthermore, when m=n=0, m+n(n+1)/2=0+(0⋅1)/2=0. This completes the basis step.
INDUCTIVE STEP: Suppose that am′,n′=m′+n′(n′+1)/2 whenever (m′,n′) is less than (m,n) in the lexicographic ordering of N×N. By the recursive definition, if n=0, then am,n=am−1,n+1. Because (m−1,n) is smaller than (m,n), the inductive hypothesis tells us that am−1,n=m−1+n(n+1)/2, so that am,n=m−1+n(n+1)/2+1=m+n(n+1)/2, giving us the desired equality. Now suppose that n>0, so am,n=am,n−1+n. Because (m,n−1) is smaller than (m,n), the inductive hypothesis tells us that am,n−1=m+(n−1)n/2, so am,n=m+(n−1)n/2+n=m+(n2−n+2n)/2=m+n(n+1)/2. This finishes the inductive step. ◀