Download presentation
Presentation is loading. Please wait.
Published byFarida Budiman Modified over 5 years ago
1
Three. The Induction Principle and Properties of Integers
3.1. Three Principles Most (if not all) discrete structures studied in computer science have infinitely many objects, but fortunately, there are usually systematic techniques to investigate the properties about these structures. The single most important such technique is based on an important property of natural numbers known as the induction principle. The set of natural numbers, N = {0, 1, 2, …}, consists of all non-negative integers. There are two operations, + (add) and • (multiply), defined on N which satisfy the usual algebra laws (closure, commutative, associative, distributive, and identity elements). In addition, and at a more fundamental level, mathematicians have sought after the definition of natural numbers, e.g., what is zero, one, etc. One important outcome of this investigation is the following principle, characterizing the infinite yet simple nature of N: The Induction Principle: Let A N denote a subset that satisfies the following two properties: (1) 0 A; and (2) if k A, then k + 1 A. Then A = N. Since this is a principle, it is intended to state an obvious fact for which no proof is required. The principle effectively says that starting with the number 0, every natural number can be reached by repeatedly adding 1 for enough times. 3-1, © Dr. S. Lang
2
A similar and equivalent principle is the following:
The Strong Induction Principle: Let A N denote a subset that satisfies the following two properties: (1) 0 A; and (2) if 0, 1, …, k A, then k + 1 A. Then A = N. Again, this principle should be obviously true. For example, if set A N satisfies the two properties, Property (1) implies 0 A. Applying (2) with k = 0, we conclude 1 A. Then since both 0, 1 A, applying (2) again with k = 1 implies 2 A, etc. Thus, we can conclude A = N. (Of course, this argument is not considered a proof.) There is another (lesser-known) principle of natural numbers, stated as follows: The Well-Ordering Principle: Let B N be a subset of natural numbers, and B . Then B has a smallest element (that is, there is a number s B such that s b for every b B). The reason that set B in the Principle has a smallest element is that the set of natural numbers N has 0 as its lower bound, preventing its subset B from finding smaller and smaller elements forever. It turns out that any of the preceding principles can be used to prove the others. 3-2, © Dr. S. Lang
3
Theorem. The following three principles are equivalent: (a) the Induction Principle; (b) the Strong Induction Principle; and (c) the Well-Ordering Principle. Proof: It suffices to prove (a) (b), (b) (c), and (c) (a) Proof of (a) (b): Let A N and A satisfies the two properties: (1) 0 A; and (2) if 0, 1, …, k A, then k + 1 A. We need to prove A = N. Define C = {m| m N and 0, 1, …, m A}. Thus, C A N ---- (3). Since 0 A by (1), 0 C by its definition. Also, if k C, that is, 0, 1, …, k A by the definition of C, then k + 1 A by (2), which implies k + 1 C because now, 0, 1, …, k + 1 A. Thus, set C satisfies the two properties of the Induction Principle, so C = N. Therefore, A = N because of (3). Proof of (b) (c): Assume B N and B (4). Suppose the Well-Ordering Principle is false, that is, suppose B doesn’t have a smallest element (5). Then we want to show this leads to a contradiction. Let A = N – B (6). By (5), 0 B because B doesn’t have a smallest element; thus, 0 A because of (6). If the numbers 0, 1, …, k A, then these numbers don’t belong to B, so k + 1 B, because if it were, k + 1 would be a smallest number in B, contradicting to (5). Thus, k + 1 A. Thus, by the Strong Induction Principle, A = N, which implies B = by (6), contradicting to (4). So (c) is proved. Proof of (c) (a): Let A N and A satisfies the two properties: (7) 0 A; and (8) if k A, then k + 1 A. We need to prove A = N. Let B = N – A; it suffices to prove B = ---- (9). Suppose B . The Well-ordering Principle (c) implies B has a smallest element, call it s (10). Note that s 0 because (7). So s > 0, and s – 1 N (being 0). Note that s – 1 A, because s is the smallest element of B and s – 1 < s. But then (8) implies (s – 1) + 1 = s A, contradicting to (10). This contradiction proves (9). 3-3, © Dr. S. Lang
4
Theorem. The expression n(n + 1) is an even number for all n N.
Typically, the two induction principles are used in proving that a statement P(n) holds true for all natural numbers n. Let us consider a few examples. Theorem. The expression n(n + 1) is an even number for all n N. Proof: We will first prove this fact by setting it up for using the Induction Principle. We then will show a simplified proof which is what people usually do, but which is still based on the Induction Principle. (Longer Version) Define a set A = {n| n N and n(n + 1) is even}. Thus, proving the theorem is equivalent to proving A = N, which consists of proving (1) 0 A; and (2) if k A, then k + 1 A, according to the Induction Principle. To prove (1), which is known as the Induction Basis Step, we verify that when n = 0, the expression 0(0 + 1) is even. This is true because 0(0 + 1) = 0, an even number. To prove (2), we assume k A, that is, assume k(k + 1) = 2m, an even number (this is known as the Induction Hypothesis), we then need to prove k + 1 A, that is, prove (k + 1)(k ) = (k + 1)(k + 2) is even (known as the Induction Step). Note that (k + 1)(k + 2) = (k + 1)k + (k + 1)2 = 2m + (k + 1)2 = 2(m + k + 1), by applying the Induction Hypothesis (and other algebra laws). Thus, since (m + k + 1) is an integer by the closure property of addition, we have proved (k + 1)(k + 2) is even, i.e., k + 1 A. By the Induction Principle, we have proved A = N, i.e., n(n + 1) is even for all n N. 3-4, © Dr. S. Lang
5
(k + 1)(k + 2) = (k + 1)k + (k + 1)2, by distributive law
(Proof cont’d) (Shorter Version) To prove n(n + 1) is even for all n 0, we use induction on n. (Basis Step) Consider n = 0. In this case, the expression n(n + 1) = 0(0 + 1) = 0, is an even number. So the Basis Step is proved. (Induction Hypothesis) Consider n = k. Assume k(k + 1) is an even number, where k 0 is a natural number. Thus, k(k + 1) = 2m for some integer m, by the definition of even. (Induction Step) Consider n = k We want to prove n(n + 1) = (k + 1)(k ) = (k + 1)(k + 2) is even. Note that (k + 1)(k + 2) = (k + 1)k + (k + 1)2, by distributive law = 2m + (k + 1)2, by Induction Hypothesis = 2(m + k + 1), by distributive law. Thus, since (m + k + 1) is an integer by the closure property of integer addition, the expression (k + 1)(k + 2) is even, and we have proved n(n + 1) is even for n = k + 1. By (mathematical) induction, we have proved n(n + 1) is even for all n 0. Thus, the shorter version of an induction proof can be applied to proving the truth of a statement P(n), for n 0. The proof consists of three steps: (Basis Step) Prove P(0) is true; (Induction Hypothesis) Assume P(k) is true, for some k N; and (Induction Step) Prove P(k + 1) is true. As a result, we conclude that P(n) is true, for all n 0. 3-5, (c0 Dr. S. lang
6
Proof: We use strong induction on n 2.
Notice that an induction problem may not start with n = 0, but the Induction Principle still applies with a slight modification. Sometimes, the Strong Induction Principle needs to be used, because proving the case of the Induction Step (i.e., when n = k + 1) requires the truth for all the preceding values (i.e., when n = 0, 1, …, k). Theorem (Fundamental Theorem of Arithmetic, Part I). Let n 2 denote an integer. Then there exists prime numbers p1, p2, …, pk, not necessarily distinct, such that n = p1p2 … pk; that is, any integer n 2 can be factored as a product of prime numbers. Proof: We use strong induction on n 2. (Basis Step) Consider n = 2. In this case, since 2 is a prime, n = 2 is a prime factorization. (Induction Hypothesis) Assume the theorem is true for all values of n = 2, 3, …, k, i.e., assume each number n in the range 2 n k, for some k 2, has a prime factorization. (Induction Step) Consider n = k There are two cases, either n is itself a prime, in which case the theorem is immediately proven because n = n is a prime factorization; or, if n is not a prime. In the latter case, n = ab, where 1 < a, b < n, by the definition of non-prime. By the Induction Hypothesis, both a and b have their prime factorizations, a = p1p2 … pk and b = q1q2 … qh , where k, h 1, and all pi and qj are primes. Thus, n = ab = p1p2 … pk q1q2 … qh , which is a prime factorization for n. Thus, we proved the Induction Step. By strong induction, the theorem is true (i.e., n has a prime factorization) for all n 2. 3-6, © Dr. S. Lang
7
(For example, 3 is a prime and 3 | 24 = 12 • 2. Note 3 | 12.)
We now illustrate how the Well-Ordering Principle can be applied. We first state a simple fact about prime numbers without proving it (it is proved on page 3-27). Theorem. Let p denote a prime. If p | ab, where a, b are two integers, then p|a or p|b. (For example, 3 is a prime and 3 | 24 = 12 • 2. Note 3 | 12.) Theorem (Fundamental Theorem of Arithmetic, Part II). Let n 2 denote an integer. Then the prime factorization of n, n = p1p2 … pk, is unique except for possible rearrangement of the prime factors. Proof: We use the method of proof by contradiction. Assume the theorem is false, that is, there exists some integer which has non-unique prime factorization. By the Well-Ordering Principle, there is a smallest such integer, which we call it m. That is, m 2 has two distinct prime factorizations, and m is the smallest such integer. Let m = p1p2 … pk = q1q2 … qh (1), be the two factorizations. Since p1 is a prime, and p1 | m = q1q2 … qh , then p1| qi for some qi , by the theorem stated above. Without loss of generality, we assume p1| q1. Since both p1 and q1 are primes, we must have p1 = q1. Therefore, canceling p1 and q1 from (1) yields m’ = m/p1 = p2 … pk = q2 … qh , which gives two distinct factorizations of a number m’ < m, a contradiction to the fact m is the smallest such integer. Thus, we proved the truth of the theorem. 3-7, © Dr. S. lang
8
and the sequence of positive odd integers are 1, 3, 5, 7, …
3.2. Sequences and Series A sequence refers to a list of objects, for example, the sequence of prime numbers are 2, 3, 5, 7, 11, … and the sequence of positive odd integers are 1, 3, 5, 7, … More generally, a sequence of n objects is denoted by the following notations: a1, a2, …, an. A sequence such as a1, a2, …, an can be formally defined as a function whose domain is the set of natural numbers, or a subset of it possibly missing some initial integers. That is, the sequence a1, a2, …, an is basically a function: f: {1, 2, …, n} A, for some co-domain A, and we simply use the notations a1, a2, …, an to represent, respectively, f(1), f(2), …, f(n). Typically, a sequence’s index (subscript) starts at 1 or 0. Very often, a sequence consists of numbers which are given by a formula, for example, ak = 2k – 1, for 1 k n, defines the sequence of the first n odd integers; bk = k2, for 1 k n, defines the sequence of the first n squares. 3-8, © Dr. S. Lang
9
A series refers to a sum of the numbers in a sequence
A series refers to a sum of the numbers in a sequence. Thus, the following is a series which consists of the terms in the sequence a1, a2, …, an: a1 + a2 + … + an. The above series can be abbreviated using the summation notation as follows: Thus, the summation term ak is used to represent each of the terms in the series, and the range of the summation index k is from k = 1 to n. Examples. The following are summation notations for the corresponding series: Note that the name of the summation index is only a dummy variable; thus, 3-9, © Dr. S. Lang
10
First, we review some basic rules and laws of Algebra.
The induction method is often used to prove properties about sequences and series. First, we review some basic rules and laws of Algebra. Properties of +, –, •, and /: (Commutative) a + b = b + a; a • b = b • a. (Associative) (a + b) + c = a + (b + c); (a • b) • c = a • (b • c). (Distributive) a • (b + c) = a • b + a • c. (Additive inverse) a – b = a + (–b); a + (–a) = 0. (Multiplicative inverse) a / b = a • (1/b) if b 0; a • (1/a) = 1, if a 0. (Identity elements) a + 0 = a; a • 1 = a. Laws of exponent and Logarithm: If a > 0, ax • ay = ax+y; (ax)y = axy ; ax / ay = ax–y; a–x = 1/(ax); If b > 0 and b 1, logb(xy) = logb x + logb y ; logb(x/y) = logb x – logb y ; logb(xp) = p logb x. Rules of inequalities: a > b a + c > b + c; if c > 0, then a > b a • c > b • c; if a > b and b > c, then a > c; if a > b and c > d, then a + c > b + d. Useful algebra rules: ab = 0 a = 0 or b = 0; if bd 0, then a/b = c/d ad = bc; (a + b)2 = a2 + 2ab + b2; (a + b)(a – b) = a2 – b2. 3-10, © Dr. S. Lang
11
Proof: We use induction on n 1.
Example. Prove the following summation formula using the induction method. Proof: We use induction on n 1. (Basis Step) Consider n = 1. In this case, the summation = The formula on the RHS (right-hand side) of the identity = 1(1 + 1)/2 = 2/2 = 1. So, LHS = RHS, the Basis Step is proved. (Induction Hypothesis) Consider n = k. Assume the identity holds for n = k, that is, (Induction Step) Consider n = k We need to prove the following identity: Notice that the LHS = Thus, identity (1) of the Induction Step is proved. Therefore, by mathematical induction, we have proved the summation formula of the theorem for all n 1. 3-11, © Dr. S. Lang
12
Proof: We use induction on n 1.
Example. Prove the following summation formula using the induction method. Proof: We use induction on n 1. (Basis Step) Consider n = 1. In this case, the summation = The formula on the RHS = 12 = 1 = LHS. Thus, the Basis Step is proved. (Induction Hypothesis) Consider n = k. Assume the identity holds for n = k, that is, (Induction Step) Consider n = k We need to prove the following identity: Note that the summation on the LHS of (1) is = k2 + (2k + 1), by the Induction Hypothesis = (k + 1)2, by the algebra rule (a + b)2 = a2 + 2ab + b2. Thus, the identity (1) of the Induction Step is proved. Therefore, by mathematical induction, we have proved the summation formula for all n 1. 3-12, © Dr. S. Lang
13
Proof: We use induction on n 2.
Example. Prove the following summation inequality using the induction method. Proof: We use induction on n 2. (Basis Step) Consider n = 2. The summation on the LHS = (Induction Hypothesis) Consider n = k. Suppose the summation inequality holds, i.e., suppose (Induction Step) Consider n = k We need to prove Note that the summation on the LHS of (1) = Since the summation in (2) is the LHS of the Induction Hypothesis, and since the second expression in (2) is, substituting this into (2) implies the expression of (2) > 13/24, by the Induction Hypothesis. So the Induction Step is proved. By induction, we have proved the theorem for all n 2. 3-13, © Dr. S. Lang
14
Proof: We use induction on n 4.
Example. Prove by induction that n! > 2n for all n 4. (Note: n! = n(n – 1)···2·1, for n 1; 0! = 1 by convention.) Proof: We use induction on n 4. (Basis Step) Consider n = 4. In this case, n! = 4! = 4·3·2·1 = 24, and 2n = 24 = 16 < 24. So the Basis Step is proved. (Induction Hypothesis) Consider n = k. Suppose k! > 2k for some k 4. (Induction Step) Consider n = k We need to prove (k + 1)! > 2k (1). Note that the LHS of (1) = (k + 1)! = (k + 1) ·k ···2·1, by the definition of (k + 1)! = (k + 1) ·k!, by the definition of k! > (k + 1) ·2k , by the Induction Hypothesis > 2 ·2k , because k + 1 5 > 2 = 2k+1 . Thus, we have proved Inequality (1) of the Induction Step. By induction, we have proved the inequality n! > 2n for all n 4. 3-14, © Dr. S. Lang
15
Proof: We use induction on n 0.
Example. Prove by induction that the expression 11n n+1 is divisible by 133 for all n 0. Proof: We use induction on n 0. (Basis Step) Consider n = 0. In this case, the expression 11n n+1 = = = 133, which is divisible by So the Basis Step is proved. (Induction Hypothesis) Consider n = k. Suppose 11k k+1 is divisible by 133 for some k 0, that is, 11k k+1 = 133m, for some integer m. (Induction Step) Consider n = k We need to prove the expression 11n n+1 = 11(k+1) (k+1)+1 = 11k k+3 is divisible by 133. Note that this expression 11k k+3 = 11·11k ·122k+1 = 11 ·(11k k+1) + (122 – 11) ·122k+1 = 11·133m + (144 – 11) ·122k+1, by the Induction Hypothesis = 133 ·(11m + 122k+1), which is divisible by Thus, we proved the Induction Step. By induction, we have proved that 11n n+1 is divisible by 133 for all n 0. 3-15, © S. Lang
16
3.3. Inductively (Recursively) Defined Objects
The induction principles can be used to define structures in terms of themselves, usually “smaller” in size. We first consider recursively defined functions. For example, to define the Factorial function n!, where n is a natural number, we first define the case with n = 0, as 0! = 1; and we then define n! for n > 0, in terms of (n – 1)!, by a recursive (inductive) formula: n! = n · (n – 1)!. Thus, to compute 1!, we use the recurrence relation (by letting n = 1) which says 1! = 1 · (1 – 1)! = 1 · 0! = 1 · 1 = 1. Similarly, 2! = 2 · (2 – 1)! = 2 · 1! = 2 · 1 = 2; and 3! = 3 · (3 – 1)! = 3 · 2! = 6, etc. It can be seen that by applying the induction principle, n! is defined for all n N. That is, we could apply the recurrence relation repeatedly which eventually leads to the case of 0!, in order to compute the value of n! for any n. More generally, a recursively (or inductively) defined function f(n) for n 0 consists of the initial value f(0), and a recurrence relation which relates the value of f(n) in terms of the preceding value f(n – 1) for n 1. Minor variations of this recursive definition include a starting value different than 0, several starting values and a recurrence relating the function value to several of the preceding values. 3-16, © Dr. S. Lang
17
f(0) = 0; and f(n) = 2 f(n – 1) + 1 for n 1.
When a function is defined by a recurrence, the most natural way to prove the properties about the function is by using induction. Example. Consider a sequence f(n), n 0, defined by the following recurrence: f(0) = 0; and f(n) = 2 f(n – 1) + 1 for n 1. Prove that f(n) = 2n – 1, for n 0. (Thus, we are proving a closed-form formula or closed-form solution for the function f(n).) Proof: We prove that f(n) = 2n – 1 using induction on n 0. (Basis Step) Consider n = 0. In this case, the formula 2n – 1 = 20 – 1 = 1 – 1 = 0, which is equal to the initial value f(0) = 0. So the Basis Step is proved. (Induction Hypothesis) Consider n = k. Suppose f(k) = 2k – 1 for some k 0. (Induction Step) Consider n = k We need to prove f(k + 1) = 2k+1 – (1). Note that f(k + 1) = 2 f((k + 1) – 1) + 1, by applying the recurrence since k + 1 1 = 2 f(k) + 1 = 2(2k – 1) + 1, by the Induction Hypothesis = 2k+1 – = 2k+1 – 1 = RHS of (1). Thus, we have proved (1) of the Induction Step By mathematical induction, we have proved f(n) = 2n – 1, for all n 0. Note: The recurrence given in this example arises from the Tower of Hanoi puzzle, see for a detailed description. 3-17, © Dr. S. Lang
18
Example. Given the following recurrence for a sequence an , n 1:
Sometimes, there are more than one initial value in defining a recurrence for a function. In such cases, proving properties about the function using induction requires verification of all the initial values in the basis step, and requires a strong induction proof. Example. Given the following recurrence for a sequence an , n 1: a1 = 4, a2 = 10, and an = 3an–1 – 2 an–2 when n 3. Prove that an = 3 · 2n – 2 for n 1. Proof: We use strong induction on n 1 to prove the formula an = 3 · 2n – 2. (Basis Step) Consider n = 1. By the formula, a1 = 3 · 21 – 2 = 6 – 2 = 4, which is equal to the given initial value of a1. Also, consider n = 2. By the formula, a2 = 3 · 22 – 2 = 12 – 2 = 10, which is also equal to the given initial value of a2. Thus, the Basis Step is proved. (Induction Hypothesis) Suppose the formula an = 3 · 2n – 2 holds for all n in the range 1 n k, for some k 2. (Induction Step) Consider n = k We need to prove ak+1 = 3 · 2k+1 – (1). Note that ak+1 = 3 a(k+1)–1 – 2 a(k+1) –2 by applying the recurrence since k + 1 3 = 3 ak – 2 ak–1 = 3 (3 · 2k – 2) – 2 (3 · 2k–1 – 2) by applying the Induction Hypothesis to both ak and ak–1 = 3(3 · 2k) – 6 – 3 · 2k + 4 = 2(3 · 2k) – 2 = 3 · 2k+1 – 2 = RHS of (1). Thus, the Induction Step is proved. By induction, we have proved the formula for all n 1. 3-18, © Dr. S. Lang
19
The induction principle can be used in defining and proving properties about many discrete structures used in computer science. Example. A graph G = (V, E) is called a tree if there exists a unique (simple) path between every pair of distinct vertices in V. (See figure below for a tree.) Notice that a tree is connected and there are no simple circuits in the graph. A special type of the tree, known as a binary tree, arranges the tree nodes (vertices) in levels such that there is one node called the root at the top level, which is connected to up to two nodes at the next level (called the children or child nodes of the root), and each child node may be connected to up to two children of the next level, etc. Thus, each node of a binary tree has zero, one, or two child nodes at the next level, and the two sides are distinguished as the left child versus the right child. (See figure below.) By convention, the empty tree is a binary tree. root A tree A binary tree 3-19, © Dr. S. Lang
20
Proof: We prove (a) by using induction on q, the level number.
Note that a binary tree is a recursive structure, in the sense that starting at any node v of the tree, the node v can be considered the root for a binary tree consisting of v and all the nodes at a lower level that are are connected to node v via some path (that is, the children of v, the grand-children of v, the great-grandchildren, etc.). A clean and concise way to define such recursive structure is to use a recursive definition as follows. Definition. A binary tree T is either empty (the empty tree), or it consists of a root node which has two children (left and right), both are binary trees. Definition. The level of a node of a binary tree is the number of nodes on the (unique) path that connects the node to the root. Thus, the root is at level1, its children at level 2, etc. The maximum level of any node is called the height (or depth) of the binary tree. We now use the recursive definition of binary trees to prove several properties. Theorem. Suppose T is a binary tree of n nodes, n 1, and height h 1. Let nq denote the total number of nodes at level q, for 1 q h. The following properties hold: (a) nq 2q–1, for 1 q h; (b) Proof: We prove (a) by using induction on q, the level number. (Basis Step) When q = 1, that is, at the root level, there is only one node (the root). Thus, n0 = 1 = 20 2q–1. So the Basis Step is proved. 3-20, © Dr. S. lang
21
(Induction Hypothesis) Consider q = k
(Induction Hypothesis) Consider q = k. Suppose Property (a) holds for level k, that is suppose nk = the total number of nodes of tree T at level k 2k–1 for some k 1. (Induction Step) Consider q = k We need to prove nk+1 2(k+1)–1 = 2k (1). Since each node at level (k + 1) must be a child of a node at level k, by the definition of level, and since each node has at most two child nodes, by the definition of binary tree, so the total number of nodes at level (k + 1) = nk+1 2 (nk) 2 (2k–1) by the Induction Hypothesis = 2k = RHS of (1). Thus, the Induction Step is proved. By induction, we proved nq 2q–1, for 1 q h. (b) Note that the sum of all the nodes of all levels is equal to n; therefore, Applying the following summation formula (which can be proved using induction) substituting x = 2 and m = h – 1 yields which can be substituted into (2) to prove (b). 3-21, © Dr. S. Lang
22
f(0) = 1; and f(n) = n · f(n – 1) for n 1.
Another application of the induction principle is in the area of developing computer programs using recursion. First, we will demonstrate that recursively (or inductively) defined functions lead to recursive programs. Example. Write a recursive function (in a pseudo-code) that computes the following recursively defined function (the factorial function n!): f(0) = 1; and f(n) = n · f(n – 1) for n 1. A Pascal solution: function factorial(n: integer): integer; begin if n = 0 then factorial := 0 else factorial := n * factorial(n – 1) end; We can use induction to show that the Pascal code computes exactly what the function defines, in the sense that the computer code returns exactly the same value for f(n) as defined by the recurrence. Applying induction, we first check the code (the program) with n = 0. Line 1 of the program body returns a value of 1, which is equal to f(0). So the Basis Step is proved. Suppose the code returns the correct value for f(n) when n = k, for some integer k 0 (Induction Hypothesis), we now call the program passing n = k Since k + 1 0, the program executes line 2 of its body, by first calling itself, factorial(n – 1), then multiplying the return value by n. The call to itself returns the correct value for (n – 1)! = k! by the Induction Hypothesis, so line 2 computes n * (n – 1)! = (k + 1)k!, as defined by the recurrence. Thus, the Induction Step is proved, which implies the program computes n! correctly for all n 0. 3-22, © Dr. S. Lang
23
function height(T: tree): integer: { assume a pre-defined type tree }
We now use the induction principle to develop an algorithm (computer program) for solving a problem recursively. Example. Write a computer program that computes the height (maximum level, or depth) of a binary tree T, assuming the function empty(T) returns true if T is empty, and functions Left(T) and right(T) return, respectively, the left child and right child of tree T. Since binary trees are defined recursively, the most natural way of solving this problem is by using recursion. Here is a Pascal solution for computing the height of T: function height(T: tree): integer: { assume a pre-defined type tree } begin if empty(T) then height := 0 else height := 1 + Max(height(Left(T)), height(right(T))) end; We can use induction to prove that this code computes the tree height correctly, by using induction on the number of levels (i.e. the height) of the tree. In the Basis Step, if the tree is empty (height = 0 by convention), the code returns the proper value from line 1 of its body. If the tree T is not empty when the code is called, it must have a root node, and the height of Left(T) (and of right(T)) is less than that of T. Thus, the Induction Hypothesis implies that the return values of height(Left(T)) and height(right(T)) in line 2 of the program body are correct. Further, the “longer” side of the child trees plus one determines the height of T, as is done in line 2. Thus, the Induction Step is proved. By induction, the code correctly computes the tree height for trees of any height 0. 3-23, © Dr. S. Lang
24
Recall the following definition of divisibility of integers:
3.4. Properties of Integers: Divisibility, Primes, Euclid’s Division Algorithm, Greatest Common Divisor (GCD), and Least Common Multiple (LCM) Recall the following definition of divisibility of integers: Definition: An integer a is divisible by integer b, where b 0, if a = bc for some integer c. In this case, b is called a divisor of a; the notation is b | a. We often restricted the divisors to positive integers since if b | a, where b 0, then (–b) | a. For example, since 12 = 3 · 4, we can say 3 | 12 (and 4 | 12). Also, 1 | n and n | n for any non-zero integer n. Definition: An integer p is called a prime if p 2, and the only (positive) divisors of p are 1 and p itself; that is, if p is a prime and a | p, where a > 0, then a = 1 or a = p. Thus, some primes are 2, 3, 5, 7, 11, 13, … It is well known that there exist infinitely many primes (proved by Euclid). However, it is still unknown whether there are infinitely many prime pairs such as 3 and 5, 5 and 7, 11 and 13, etc., where two primes of the form p and p+2 (known as twin primes). Any integer n > 1 can be factored into a product of primes in essentially a unique way, which is known as the fundamental theorem of arithmetic. It turns out that factorization is not a trivial problem; that is, given a (large) integer finding its factors may take many steps given the state-of-art algorithms. However, finding common factors between two integers can be done very efficiently, something known to Euclid. Definition: Given two positive integers a and b. An integer m is a common divisor of a and b if m | a and m | b. The greatest common divisor of a and b, denoted GCD(a, b), is the largest of the common divisors of a and b. Note that the GCD always exists since 1 is always a common divisor, so GCD(a, b) 1. Two integers a and b are relatively prime if GCD(a, b) = 1. 3-24, © Dr. S. Lang
25
Theorem: If a = bq + r, where b 0, then GCD(a, b) = GCD(b, r).
We now develop Euclid’s algorithm which computes the GCD of two positive integers. The basic idea is based on the following theorem. Theorem: If a = bq + r, where b 0, then GCD(a, b) = GCD(b, r). Proof: We first note that if m | a and m | b (that is, if m is a common divisor of a and b), then m | r. This is true because m | a implies a = mc; m | b implies b = md, for some integers c and d. Thus, r = a – bq = mc – mdq = m(c – dq), which proves m | r. Thus, m is a common divisor of b and r. Therefore, m GCD(b, r) by the definition of GCD(b, r). In particular, GCD(a, b) GCD(b, r). Similarly, we can prove that any common divisor of b and r divides a.. Thus, GCD(b, r) GCD(a, b). Combining the two inequalities proves the theorem. Example: We now illustrate Euclid’s GCD algorithm by computing GCD(228, 95). 228 = 95 , so GCD(228, 95) = GCD(95, 38). Repeating the division process, using the previous divisor and remainder as the dividend and divisor, respectively, of the next step, we find 95 = 38 , so GCD(95, 38) = GCD(38,19); and since 38 = 19 2 + 0, so GCD(38,19) = GCD(19, 0) = 19. Combining, we find GCD(228, 95) = 19, the divisor of the last division step. Note that the above process always terminates since each remainder is strictly smaller than the divisor of that step, thus strictly smaller than the remainder of the previous step, so the remainder eventually becomes zero. When that occurs, the last GCD between the divisor and zero is the divisor itself, which gives the GCD of the original pair of integers. 3-25, © Dr. S. Lang
26
Euclid’s algorithm written in C that computes the GCD is given below:
int gcd (int a, int b) // assume a, b > { int r; while (1) {// repeat until remainder = 0 r = a % b; // r is the remainder if (r == 0) return b; // otherwise a = b; b = r; } } Theorem: Suppose a and b are two positive integers. There exist integers t and u (may not be positive) such that at + bu = GCD(a, b). That is, the GCD can be written as a linear combination of the two integers. We will “demonstrate” this theorem by the extended Euclid’s GCD algorithm. Basically, the GCD is equal to the remainder of the second-to-the-last division step of Euclid’s algorithm. Thus, the GCD can be written as a linear combination of the dividend and the divisor of that division step. We can then use the division step above it to substitute out its remainder, resulting in a linear combination of the dividend and divisor from this previous step. Repeating the substitutions using the division steps toward the beginning of Euclid’s algorithm eventually leads to a linear combination in terms of the original pairs of the integers. 3-26, © Dr. S. Lang
27
Example. Find integers t and u such that GCD(228, 95) = 228 t + 95 u, that is, write GCD(228, 95) as a linear combination of 228 and 95. Recall the following division steps in computing GCD(228, 95) using Euclid’s algorithm: 228 = 95 (1), 95 = 38 (2), and 38 = 19 (3) Thus, GCD(228, 95) = 19, and 19 = 95 – 38 (4), by rewriting the remainder in Step (2). We now substitute out the remainder 38 from Step (1), thus, rewriting (4) as 19 = 95 – (228 – 95 2) 2 = 228 (–2) + 95 (4 + 1), by combining the like terms, writing as a linear combination of the previous dividend and divisor = 228 (–2) + 95 5. Thus, t = –2 and u = 5. We can now use this result to prove a fundamental property about primes. Theorem: If p is a prime and p | ab, then p | a or p | b, where a and b are two positive integers. Proof: Consider the value of GCD(p, a). Since this is a divisor of p, but p is a prime by assumption, so GCD(p, a) = 1 or p (by the definition primes). We now have two cases: (Case one) Suppose GCD(p, a) = 1. Write 1 = pt + au using the Extended Euclid’s algorithm. Multiplying both sides by b yields b = ptb + abu = p(tb + mu) assuming ab = pm since p | ab by assumption. Thus, we proved p | b in this case, (Case two) Suppose GCD(p, a) = p. In this case, p is the GCD of p and a, so p | a. 3-27, © Dr. S. Lang
28
Example. Consider the following prime factorizations:
We can now state the following theorem which says any integer greater than 1 can be factored into a product of primes, in essentially a unique way. The proof is given on pages 3-6 and 3-7. Theorem (Fundamental Theorem of Arithmetic). Let n 2 denote an integer. Then there exists prime numbers p1, p2, …, pk, not necessarily distinct, such that n = p1p2 … pk; that is, any integer n 2 can be factored as a product of prime numbers. Furthermore, the product is unique except for possible rearrangement of the prime factors. Example. Consider the following prime factorizations: 10296 = 23 32 11 13; = 3 52 132 ; and = 24 112 13. Note that the GCD can be calculated quickly once the prime factorizations are given. That is, if integers a and b have a common prime factor p, e.g., p | a and p | b, then pmin(, ) is a prime-power factor in GCD(a, b). Thus, GCD(10296, 12675) = 3 13 = 39; and GCD(10296, 25168) = 23 11 13 = 1144. Definition: The least common multiple of two integers a and b, denoted LCM(a, b), is the smallest positive multiple of a and b. For example, LCM(10296, 12675) = 23 32 52 11 132 and LCM(10296, 25168) = 24 32 112 13. Theorem: GCD(a, b) LCM(a, b) = ab, for any two positive integers a and b. Proof: It can be seen that if p | a and p | b, then pmax(, ) is a prime-power factor in LCM(a, b). Thus, the prime-power factor using prime p in GCD(a, b) LCM(a, b) is pmin(, ) pmax(, ) which equals p + , exactly the same as the prime-power factor using prime p in ab. Since this is true for all prime-power factors of a and b, the theorem is proved. 3-28, © Dr. S. Lang
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.