Lecture 2
Lecture 2
Mwangi H. (Ph.D.)
CS Yr 3.1
Department of Computing
J.K.U.A.T.
2 Recursion
Recursive Functions and Algorithms
Proving Recursive Algorithms
Exercises
Recall existence proofs were quite easy. We just find one example
which satisfies the statement.
Proving that a statement holds for every possible choice is much more
challenging.
Mathematical Induction is a mathematical technique to reason about,
and prove, that a statement holds for every choice of natural number.
We look to prove that P(n) is true for all natural numbers n that are
greater than some natural number n0
1 Basis Step. Show directly that P(n0 ) is true
2 Inductive Step. Show that the implication P(k) → P(k + 1)
is true for all k ∈ N, k ≥ n0
If both 1 and 2 can be shown to be true, then by induction P(n) is
true for all natural numbers greater than or equal to n0
Pn n(n+1)
Recall the summation fromula i=1 i = 2
. Prove this formula is correct for all
n≥1
Proof.
P1 1(2)
1 Basic step: i=1 i =1= Therefore, the formula is correct for n = 1
2
k(k+1)
Inductive step. Assume the inductive hypothesis, that ki=1 i =
P
2
2
We must show that
Pk+1 (k+1)(k+2)
i=1 i = 2
k+1
X
i = 1 + 2 + 3 + · · · + k + (k + 1)
i=1
k
X
= i + (k + 1)
i=1
k(k + 1)
= + (k + 1) by inductive hypothesis
2
Proof Contd.
k(k + 1) + 2(k + 1)
=
2
k 2 + 3k + 2
=
2
(k + 1)(k + 2)
=
2
Example 1.3
Show that the sum of the first n odd positive integers is equal to n2 . That is 1 + 3 +
5 + · · · + (2n − 1) = n2 . e.g.
1, 3, 5, 7, 9, 11 is the first 6 odd integers and we have that
1 + 3 + 5 + 7 + 9 + 11 = 36 = 62
Proof.
We proceed by induction.
The sum of the first 1 positive integers is 12
Assume the inductive hypothesis, that
1 + 3 + 5 + 7 + . . . + (2k − 1) = k 2
Then we have,
Therefore, by induction the sum of the first n positive odd integers is equal to n2 for all
positive integers n ≥ 1
Use mathematical induction to prove that n < 2n for all positive integers n
Proof.
Let P(n) be ”n < 2n ”
For the basic step, P(1) is true since 1 < 21 = 2
For the inductive step; Assume P(k) is true. That is k < 2k , for some positive integer
k.
Show that k + 1 < 2k+1 is also true
k + 1 < 2k + 1 ≤ 2 · 2k
k + 1 < 2k + 1 ≤ 2k+1
k + 1 < 2k+1
Strong Induction
Theorem 1.1
Proof.
Let P(n) be ”n can be written as a product of primes”
Basis step: P(2) is true since 2 is itself a prime.
Inductive step: Let the inductive hypothesis be that P(i) is true for all i, 2 ≤ i ≤ k.
We show P(k + 1) must be true.
If k + 1 is prime, then it is already written as a product of primes (itself) and thus
P(k + 1) is true. Otherwise k + 1 is a composite number and can be written as the
product of two integers greater than 1 say a and b.
That is k + 1 = ab with 2 ≤ a < k + 1 and 2 ≤ b < k + 1. This is obvious since k is
greater than 2 and thus both a and b must be greater that 2
By the inductive hypothesis, P(a) is true and P(b) is true. Since k + 1 = a · b, then
P(k + 1) is also a product of primes by replacing a and b by their respective product of
primes.
Therefore, by strong induction, P(n) is true for all n ≥ 2
Definition 1.1
Proof by well-ordering
Proposition 1.1: Proof by well-ordering
Proof.
Let a, b be integers satisfying the hypothesis of Euclidean division. Let S be the
set of non-negative integers of the form a − bq for some integer q. The set is
non-empty because we can choose q arbitrarily.
By the well-ordering principle, S has a least element. Call it r . Since r ∈ S and r
is non-negative. Moreover, r = a − bq0 for some integer q0
It must also be that r < b Proceed by contradiction. Assume that r ≥ b. Then,
another element of S is a − b(q0 + 1) where a − b(q0 + 1) = a − bq0 − b = r − b.
Where r − b ≥ 0 since r ≥ b. But r was the smallest element of S, so it cannot
be that r ≥ b. Hence r < b
By the well-ordering principle integers q, r exist satisfying a = bq + r and
0≤r <b
[email protected] (SCIT) Computer Science March 11, 2024 13 / 28
Induction Exercises
Exercises
Solution
Basis:
P0
k=0 a0 = a0
Recursive Step/Production Step:
Pn+1 Pn
i=0 ai = i=0 ai + an+1
Recursive definitions can lead to some very abstract, yet very useful definitions.
We can also define the Fibonacci function as the function which takes n as
argument and returns the nth Fibonacci number:
n, n = 0, 1
Fib(n) =
Fib(n − 1) + Fib(n − 2), otherwise
Recursive Exercises
Proof.
Proof of 2.1 Here we need to proof the function properly computed x n
Proceed by induction.
For the basis step, power(x,0) clearly returns clearly returns x 0 = 1 from
the first if condition.
For the inductive step, assume that power(x,k) returns the correct result
x k for some k ≥ 0
We will show that power(x,k+1) returns the correct result.
By the definition of power we see that power(x,k+1) returns x * power(x,
k). By the inductive hypothesis, power(x,k)=x k .
Thus we have power(x,k+1) = x · x k = x k+1
Therefore, by mathematical induction, power(x,n) correctly computed x n
for all n ≥ 0
Exercises
For some alphabet Σ the set of all possible strings from characters in
Σ is denoted by Σ∗
Loop Invariant
Lets begin with simple example: Consider the following Python program
which computes the maximum element in a list of integers.
Example 3.1
d e f max ( L ) :
m = L[0];
i = 1;
while ( i < len (L)) :
i f ( L [ i ] > m) :
m = L[ i ]
i = i + 1
return m
p r i n t ( max ( [ 4 , 1 2 , 5 , 1 , 5 6 , 1 , 4 , 7 ] ) ) − o u t p u t 56
Loop Invariant
Loop Invariant
Proving that a loop invariant holds for every loop in a program is one
step toward proving or verifying that the entire program is correct.
Generally, formal program verification is a very difficult task. For
more information, consider reading the articles Loop Invariant and
Formal Verification.
Later in your studies in computer science, you will see loop invariants
a program verification again. When that time comes, remember
induction!