--------- Induction --------- - Induction can be used to define things, such as functions, sets, etc. - Inductive definitions of functions yield easy recursive programs. Example 1: Let f:N --> N be the following function: / 0 if n = 0 | f(n) = < | \ f(n - 1) + 2n - 1 if n > 0 . This defines the value of f(0) explicitly (as the base case). The other first few values of f(n) can be computed by first computing f(1), then f(2), then f(3), etc.: f(1) = f(0) + 2*1 - 1 = 1, f(2) = f(1) + 2*2 - 1 = 4, f(3) = f(2) + 2*3 - 1 = 9, f(4) = f(3) + 2*4 - 1 = 16 .... . By looking at these values, we guess: For any n >= 0, f(n) = n^2. . Define "S(n): f(n) = n^2". Prove by induction that S(n) holds for n >= 0. . Base: S(0) holds since by definition of f: f(0)= 0 = 0^2 . Ind. Hyp.: Assume that for k >= 0, S(k) holds. . Ind. Step: To prove S(k + 1): f(k + 1) = f(k) + 2(k + 1) - 1 by definition of f and since k + 1 > 0 = k^2 + 2k + 2 - 1 by Ind. Hyp. = k^2 + 2k + 1 = (k + 1)^2. . We could prove the same statement by complete induction. Note: Like in proofs by induction: . The base case in defining a function recursively is very important. For example: "g(n) = g(n - 1) + 3n , for all n > 0" does *NOT* define a function, since we cannot compute g(1) or any other value of g. . There should be no gap between the induction step and the base: Example: / 0 if n = 0 | g(n) = < | \ g(n - 2) + 2 if n > 0 does *NOT* define a function, since we cannot compute g(1). . Sometimes we need to have more than 1 base cases (as in the next example). Example 2: The Fibonacci function f: N ---> N is defined as: / 0, if n = 0 | f(n) = < 1, if n = 1 | \ f(n - 1) + f(n - 2) if n > 1. . We want to prove using induction on n, that for all n >= 0: n sum f(n)^2 = f(n)*f(n+1). i=1 . Define S(n) to be the above equality. We prove that S(n) holds for all n >= 0. 0 . Base: sum f(i)^2 = f(0)^2 = 0^2 = 0 = 0*1 = f(0)*f(1). i=0 . Ind. Hyp.: Let k >= 0 be an arbitrary integer and assume that S(k) is true. . Ind. Step: k+1 k sum f(i)^2 = f(k + 1)^2 + sum f(i)^2 since k >= 0, i=1 i=1 k + 1 >= 1, = f(k + 1)^2 + f(k)*f(k+1) by Ind. Hyp. = f(k + 1) * [f(k) + f(k + 1)] = f(k + 1) * f(k + 2) by def. of f(k + 2) and since k + 2 >= 2. . We can find a closed form for f(n) using induction (see Theorem 1.17 in the notes, page 47). We can define sets of objects using recursion, by: . First defining what are the smalles or simplest objects in the set. . Define a finite number of well-defined ways to combine the smaller objects. The frist step is the base, and the second step is called the induction step. Example 3: We want to define the E set of well-formed, fully parenthesised algebraic expressions involving variables x, y, and z, and operations +, -, *, and : (As an example, ((x + y)* z) should be in the set while y(:z)+ should not) . Here is the definition: - E is the smallest set, such that: - Base: x,y,z in E - Ind. Step: if e1, e2 are in E then the following four expressions are also in E: (e1 + e2), (e1 - e2), (e1 * e2), (e1 : e2). . It is important to say in the first part that "E is the smallest set...". Otherwise, we are not defining a unique set. To prove properties for sets with recursive definitions, we can use a kind of induction called "structural induction". Similar to simple (and complete) induction, to use structural induction to prove property P for set E, we follow two main steps: . Base: Prove that every "smallest" element of E has property P. . Ind. Step: Prove that each of the (finitely many) ways of constructing the "larger" elements of E preserves property P. Example 4: Consider set E defined in example 3. We want to prove that for all e in E, number of variables in e is one plus the number of operators. For any e in E, let VR(e) and OP(e) denote the number of variables and operators in e, respectively. . Let P(e) be the following predicate: VR(e) = OP(e) + 1. . We prove by structural induction that P(e) holds for all e in E. . Base: if e = x, e = y, or e = z then VR(e) = 1, OP(e) = 0 and P(e) holds. . Ind. Hyp.: Let e1 and e2 be arbitrary elements of E s.t. P(e1) and P(e2) hold. . Ind. Step: We prove that for any e that can be constructed from e1 and e2, P(e) holds too. There are four ways to construct e from e1 and e2: e = (e1 + e2), or e = (e1 - e2), or e = (e1 * e2), or e = (e1 : e2). In each case: VR(e) = VR(e1) + VR(e2) and OP(e) = OP(e1) + OP(e2) + 1 Hence, VR(e) = VR(e1) + VR(e2) = (OP(e1) + 1) + (OP(e2) + 1) by Ind. Hyp. = (OP(e1) + OP(e2)) + 2 = (OP(e) - 1) + 2 = OP(e) + 1. Example 5: We want to give a formal recursive definition for the set B of full binary trees (binary trees in which every node has either two or no children). . B is the smallest set such that: - Base: A single node is a binary tree, whose root is itself. - Ind. Step: If T1 and T2 are two arbitrary elements of B whose roots are r1 and r2, respectively, then T which is obtained from creating a new node r and connecting it to r1 and r2 is also in B. . For any tree in B let L(T) be the number of nodes in T that have no children and I(T) be the set of nodes in T that have exactly two children. . Define "S(T): L(T) = I(T) + 1". We prove by structural induction that S(T) holds for all trees in B. . Base: if T is a smallest object in B it is a single vertex. In this case, L(T) = 1 and I(T) = 0 and so S(T) holds. . Ind. Step: Let T1 and T2 be two arbitrary trees in B, whose roots are r1 and r2, such that S(T!) and S(T2) are true. There is only one way to construct a new tree from T1 and T2: creat a new node r (which will be the root) and connect it to r1 and r2. Call this new tree T. Every node in T1 and T2 has the same number of children in T. The new node r has two children. Therefore, L(T) = L(T1) + L(T2) and I(T) = I(T1) + I(T2) + 1 By Ind. Hyp. L(T1) = I(T1) + 1 and L(T2) = I(T2) + 1. Therefore: L(T) = (I(T1) + 1) + (I(T2) + 1) = I(T1) + I(T2) + 1 + 1 = I(T) + 1, as wanted.