Download (n!)+

Document related concepts

Abuse of notation wikipedia , lookup

Big O notation wikipedia , lookup

Brouwer–Hilbert controversy wikipedia , lookup

Georg Cantor's first set theory article wikipedia , lookup

Quadratic reciprocity wikipedia , lookup

Four color theorem wikipedia , lookup

Halting problem wikipedia , lookup

Addition wikipedia , lookup

Wiles's proof of Fermat's Last Theorem wikipedia , lookup

Collatz conjecture wikipedia , lookup

Fundamental theorem of algebra wikipedia , lookup

Fermat's Last Theorem wikipedia , lookup

Theorem wikipedia , lookup

Factorial wikipedia , lookup

Elementary mathematics wikipedia , lookup

Mathematical proof wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Transcript
461191 Discrete Mathematics
Lecture 4: Induction and Recursion
San Ratanasanya
CS, KMUTNB
Today’s Topics







Last week review
Administrivia
Proof Review
Mathematical Induction
Recursive Definitions
Recursive Algorithms
Program Correctness
Last week review
Algorithms,
The Integers, and
Matrices
3
Algorithm
is a finite set of precise instructions for
performing a computation or for solving a
problem.
Pseudocode
Algorithm 1
Finding the maximum element in a Finite Sequence.
procedure max (a1, a2,…, an : integers)
max := a1
for i := 2 to n
if max < ai then max := ai
{max is the largest element}
Algorithm 2
The Linear Search Algorithm
procedure linear search (x : integer; a1, a2,…, an
:distinct integers)
i := 1
while (i ≤ n and x ≠ ai)
i := i + 1
if i ≤n then location := i
else location := 0
{location is the subscript of term that equals x, or is 0 if
not found}
Algorithm 3
Binary Search Algorithm
procedure binary search (x : integer; a1, a2,…, an
: increasing integers)
i := 1 {i is left endpoint of search interval}
j := n {j is right endpoint of search interval}
while i < j
begin
m := (i + j)/2
if x > am then i := m + 1
else j := m
end
if x = ai then location := i
else location := 0
{location is the subscript of term equal to x, or 0 if x is not found}
Pseudocodes
Procedure bubblesort(a1,…,an: real numbers with n  2)
for i := 1 to n-1
for j := 1 to n-1
if aj > aj+1 then interchange aj and aj+1
{a1,…,an is in increasing order}
The growth of functions
DEFINITION 1 :
Let f and g be functions from the set
of integers or the set of real numbers
to the set of real numbers. We say that
f(x) is O(g(x)) if there are constants C
and k such that,
|f(x)| ≤ C |g(x)| when x > k
Example 1 : show that f(x) =
x2+2x+1 is O(x2)
case 1
0 ≤ x2+2x+1 ≤ x2+2x2+x2 = 4x2, when x >1
So f(x) = O(x2) , when C = 4 and k = 1
Ans
case 2
when x >2 , 2x < x2
0 ≤ x2+2x+1 ≤ x2+x2+x2 = 3x2,when x >2
so f(x) = O(x2) when C = 3 and k = 2
Ans
In this example, g(x) = O(f(x)) since x2 ≤ x2+2x+1
so, we say that f(x) and g(x) are of the same order
THEOREM 1 : Let f(x) = anxn+an-1xn-1+…+a1x+a0
where a0,a1,…an are real numbers.
Then f(x) is O(xn)
Proof :
|f(x)| = | anxn+an-1xn-1+…+a1x+a0 |
≤ |an|xn+|an-1|xn-1+…+|a1|x+|a0|
= xn (|an|+|an-1|x-1+…+ |a1|x(n-1)+ |a0|x-n-1)
≤ xn (|an|+|an-1|+…+ |a1|+ |a0|)
This shows that |f(x)| ≤ C xn
when C = (|an|+|an-1|+…+ |a1|+ |a0| and x > 1.
Example 2 : 1+2+3+…+n = O(?)
≤ n +n +…+n = n × n = n2
= O(n2) , C = 1 , k = 1
Example 3 : f(n) = n! = O(?)
= 1 * 2 * 3 * …* n
≤ n * n * n * …* n
= nn ≤ C |g(n)|
= O(nn), C = k = 1
Since…
…n! ≤ nn
log n! ≤ log nn = n log n
log n! = O(n log n)
...n < 2n
n = O(2n) , k = C = 1
...log n < n
log n = O(n) , k = C = 1
...logbn = log n/log b < n/log b
logbn = O(n) , k = 1 , C = 1/log b
THEOREM 2 : If f1(x) = O(g1(x)) and f2(x) = O(g2(x))
then (f1+f2)(x) = O(max(g1(x), g2(x)))
since |f1(x)| ≤ C1 |g1(x)| , when x > k1 and |f2(x)| ≤ C2
|g2(x)| , when x > k2
|(f1+f2)(x)|
|f1(x)| + |f2(x)|
= |f1(x) + f2(x)| ≤ |f1(x)| + |f2(x)|
< C1|g1(x)| + C2|g2(x)|
≤ C1|g(x)| + C2|g(x)|
= (C1+C2) |g(x)| = C |g(x)|
when C = C1+C2 , and g(x) = max|(g1(x),g2(x)| ,
k = max(k1,k2)
THEOREM 3 : Let f1(x) = O(g1(x)) and f2(x) = O(g2(x))
(f1 f2)(x) = O( g1(x) g2(x) )
Example 4 : f(n) = 3n log (n!)+(n2+3) log n = O(?), where n = positive integer
≤ O(n) O(n log n) + O(2n2) O(log n)
≤ O(n2 log n) + O((2n2) log n) = O(3n2 log n)
= O(n2 log n)
Example 5 : f(x) = (x+1) log (x2+1) + 3x2 = O(?)
====================================================
Commonly Estimated Functions : 1, log n , n , n log n , n2 , 2n , n!
Complexity Analysis of
Algorithms
Computational Complexity:
1) Time Complexity: An analysis of the time required to solve a
problem.
Time complexity is described in terms of the number of operations
required instead of actual computer time, such as comparison,
addition, subtraction, multiplication, division, etc.
There are 3 possible cases in time complexity
best case, average case, and worst case
2) Space Complexity: An analysis of the computer
memory required to solve a problem.
space complexity are tied in with the particular
data structures used to implement the algorithm.
Example 1
Describe the time complexity of Algorithm 1 of Section 2.1
for finding the maximum element in a set.
Solution: “The number of comparisons will be used as the
measurement of the time complexity of the algorithm,
since comparisons are the basic operation used.”

when i = n + 1, there are exactly 2(n - 1) + 1 = 2n - 1
comparisons
Example 2
Describe the time complexity of linear search algorithm.
n = 2k or 2k < n < 2k+1
2k elements in the first loop
2k-1 elements in the second loop
:
:
21 elements in the k-1th loop
20 elements in the kth loop
k loops
At most 2k+2 comparisons
= (2 log n ) + 2 = O(log n)
Example 3
Describe the time complexity of the binary search algorithm.
Solution:
For simplicity, assume there are n = 2k elements in the list
a1, a2,…,an, where k is a non-negative integer. Note that k = log n
(If n, the number of elements in the list, is not a power of 2, the list
can be considered as part of a larger list with 2k + 1 elements, where
2k < n <2k + 1. Here 2k + 1 is the smallest power of 2 larger than n).
Example 4
Average case performance of Linear Search Algorithm
2i + 1 comparisons
i=1:3
i=2:5
:
i = n : 2n + 1
Average = (3 + 5 + 7 +n…+ 2n + 1)/2
= (2(1+2+n…+ n) + n)/n
Since 1 + 2 + 3 +…+ n = n(n + 1)/2
So, the average = 2[n(n + 1)/2] + n = n + 1 + 1 = n + 2 = O(n)
Polynomial worst-case complexity = tractable
O(nb), b is an integer ≥ 1
But when b is large or C is very large  problem
(takes too long time to solve)
“Intractable” : problem that cannot be solved within Polynomial time.




Big O estimates of time complexity cannot directly tell
the actual amount of computer time used.
Knowing the constants C and k makes
| f(n)| ≤ C |(g(n))| when n > k
The time each type of operations used is not
equivalent.
Type of computers can also be different in terms of
specification, and generation.
Asymptotic Notations

Big O (Upper bound)



Big  (Lower bound)



f(n) grow slower than g(n)
|f(n)|  C|g(n)| when n > k  f(n) = (g(n))
Big  (Tight bound)



f(n) grow faster than g(n)
|f(n)|  C|g(n)| when n > k  f(n) = O(g(n))
f(n) and g(n) have the same rate of growth
C1|g(n)|  |f(n)|  C2|g(n)| when n > k
 f(n) = (g(n))
C and k called witness pair.
The Integers & Division
Definition 1: If a and b are integers with a ≠ 0, we say that
a divides b if there is an integer c such that b = ac.
When a divides b we say that a is a factor of b and that
b is multiple of a. The notation a | b denotes that a
divides b. We write a | b when a does not divide b.
THEOREM 1: Let a, b and c be integers. Then
1. if a | b and a | c, then a | (b + c);
2. if a | b, then a | bc for all integers c;
3. if a | b and b | c, then a | c.
Definition 2: A positive integer p greater than 1 is called
prime if the only positive factors of p are 1 and p. A
positive integer that is greater than 1 and is not prime is
called composite.
THEOREM 2: THE FUNDAMENTAL THEOREM OF ARITHMETIC
Every positive integer greater than 1 can be written uniquely as a prime
or as the product of two or more primes where the prime factors are
written in order of non-decreasing size.
Example 1
The prime factorizations of 100, 641, 999, and 1024 are given by
100 = 2255 = 2252,
641 = 641,
999 = 33337 = 33 37,
1024 = 2222222222 = 210
THEOREM 3: If n is a composite integer, then n
has a prime divisor less than or equal to n
THE DIVISION ALGORITHM
THEOREM 6: THE DIVISION ALGORITHM
Let a be an integer and d a positive integer. Then there are
unique integers q and r, with 0 ≤ r < d, such that
a = dq + r
Definition 3: In the equality given in the division algorithm, d is
called the divisor, a is called the dividend, q is called the quotient,
and r is called the remainder.
Definition 4: Let a and b be integers, not both zero. The largest
integer d such that d|a and d|b is called the greatest common
divisor of a and b. The greatest common divisor of a and b is
denoted by gcd(a, b).
Definition 5: The integer a and b are relatively prime if their
greatest common divisor is 1.
Example: gcd (17, 22) = 1
17, 22 = relatively prime
Definition 6: The integer a1, a2,…, an are pairwise relatively prime if
gcd(ai, aj) = 1 whenever 1 ≤ i < j ≤ n.
Example:
10, 17, 21 are pairwise relatively prime?
10, 19, 24 are pairwise relatively prime?
Definition 7: The least common multiple of the positive integers a
and b is the smallest positive that is divisible by both a and b. The
least common multiple of a and b is denoted lcm(a, b).
Example: What is the least common multiple of 233572 and 2433?
Solution: We have
lcm(233572, 2433) = 2max(3, 4)3max(5, 3)7max(2, 0) = 243572
THEOREM 7:
Let a and b be positive integers.
Then ab = gcd(a, b)  lcm(a, b)
MODULAR ARITHMATIC
Let a be an integer and m be a positive integer. We denote “a mod m”
the remainder when a is divided by m.
Definition 8: If a and b are integers and m is a positive integer, then
a is congruent to b modulo m, if m divides a - b.
The notation a ≡ b (mod m) is used to indicate that a is congruent to b
modulo m. If a and b are not congruent modulo m, a ≡ b (mod m).
Theorem 8: Let a and b be integers, and let m be a positive integer.
Then a ≡ b (mod m) if and only if a mod m = b mod m.
Example: Determine whether 17 is congruent to 5 modulo 6 and
whether 24 and 14 are congruent modulo 6.
Solution:
Since 6 divides 17 - 5 = 12, we see that 17 ≡ 5 (mod 6).
However, since 24 - 14 = 10 is not divisible by 6, we see that
24 ≡ 14 (mod 6).
Applications of Number
Theory


Puzzle
Cryptography







Caesar’s Ciphering
RSA
Computer Graphics
Signal processing
Thermodynamics
Quantum Physics
etc.
Chinese Remainder Theorem
Modular Arithmetic
Matrices

Matrix equality


Matrix Arithmetic



Addition, multiplication
Identity matrix


Equals in number of rows and columns, and
elements
AIn = ImA = A
Transpose and Inverse matrix
Boolean product
Administrivia

Homework due today



Homework 3 is out today
Study by yourself


1.6-1.8
Prolog and Python
Programming Assignments will be
coming soon!!
Proof Review:
Basic Proof Methods
41
Nature & Importance of Proofs

In mathematics, a proof is:



A sequence of statements that form an argument.
Must be correct (well-reasoned, logically valid) and
complete (clear, detailed) that rigorously & undeniably
establishes the truth of a mathematical statement.
Why must the argument be correct & complete?


Correctness prevents us from fooling ourselves.
Completeness allows anyone to verify the result.
42
Rules of Inference


Rules of inference are patterns of logically
valid deductions from hypotheses to
conclusions.
We will review “inference rules” (i.e.,
correct & fallacious), and “proof methods”.
43
Visualization of Proofs
Rules
Of
Inference
A Particular Theory
A proof
The Axioms
of the Theory
…
Various Theorems
44
Inference Rules - General Form

Inference Rule –

Pattern establishing that if we know that a set
of hypotheses are all true, then a certain
related conclusion statement is true.
Hypothesis 1
Hypothesis 2 …
 conclusion
“” means “therefore”
45
Inference Rules & Implications
Each logical inference rule corresponds to an
implication that is a tautology.
Hypothesis 1
Inference rule
Hypothesis 2 …
 conclusion
Corresponding tautology:
((Hypoth. 1)  (Hypoth. 2)  …)  conclusion
46
Formal Proofs


A formal proof of a conclusion C, given
premises p1, p2,…,pn consists of a sequence of
steps, each of which applies some inference
rule to premises or to previously-proven
statements (as hypotheses) to yield a new
true statement (the conclusion).
A proof demonstrates that if the premises are
true, then the conclusion is true (i.e., valid
argument).
47
Common Fallacies

A fallacy is an inference rule or other proof
method that is not logically valid.


Fallacy of affirming the conclusion:


May yield a false conclusion!
“pq is true, and q is true, so p must be true.”
(No, because FT is true.)
Fallacy of denying the hypothesis:

“pq is true, and p is false, so q must be false.”
(No, again because FT is true.)
48
Common Fallacies - Examples
“If you do every problem in this book, then you
will learn discrete mathematics. You learned
discrete mathematics.”
p: “You did every problem in this book”
q: “You learned discrete mathematics”

Fallacy of affirming the conclusion:
pq and q does not imply p

Fallacy of denying the hypothesis:
pq and  p does not imply  q
49
Inference Rules for Quantifiers




x P(x)
P(o)
P(g)
x P(x)
x P(x)
P(c)
P(o)
x P(x)
(substitute any object o)
(for g a general element of u.d.)
(substitute a new constant c)
(substitute any extant object o)
50
Proof Methods

Proving pq





Direct proof: Assume p is true, and prove q.
Indirect proof: Assume q, and prove p.
Trivial proof: Prove q true.
Vacuous proof: Prove p is true.
Proving p
Proof by contradiction: Prove p (r  r)
(r  r is a contradiction); therefore p must be false.


Prove (a  b)  p


Proof by cases: prove (a p) and (b p).
More …
51
What you might write
Theorem:
x,y: Rational(x)  Irrational(y) → Irrational(x+y)
Proof: Let x, y be any rational and irrational numbers,
respectively. … (universal generalization)
Now, just from this, what do we know about x and y? You should
think back to the definition of rational:
“… Since x is rational, we know (from the very definition of
rational) that there must be some integers i and j such that x =
i/j. So, let ix,jx be such integers …”
We give them unique names so we can refer to them later.
52
What next?
What do we know about y? Only that y is irrational: ¬ integers i,j:
y = i/j.
But, it’s difficult to see how to use a direct proof in this case. We
could try indirect proof also, but in this case, it is a little simpler
to just use proof by contradiction (very similar to indirect).
So, what are we trying to show? Just that x+y is irrational. That is,
¬i,j: (x + y) = i/j.
What happens if we hypothesize the negation of this statement?
53
More writing…
Suppose that x+y were not irrational. Then x+y would be rational,
so  integers i,j: x+y = i/j. So, let is and js be any such integers
where x+y = is/ js.
Now, with all these things named, we can start seeing what
happens when we put them together.
So, we have that (ix/jx) + y = (is/js).
Observe! We have enough information now that we can conclude
something useful about y, by solving this equation for it.
54
Finishing the proof.
Solving that equation for y, we have:
y = (is/js) – (ix/jx)
= (isjx – ixjs)/(jsjx)
Now, since the numerator and denominator of this
expression are both integers, y is (by definition)
rational. This contradicts the assumption that y
was irrational. Therefore, our hypothesis that x+y
is rational must be false, and so the theorem is
proved.
55
Proof by Cases
To prove
( p1  p2  ...  pn )  q
we need to prove
( p1  q)  ( p2  q)  ...  ( pn  q)
Example: Show that |xy|=|x| |y|, where x,y are
real numbers.
56
Equivalence of a group of
propositions
To prove
[ p1  p2  ...  pn ]
we need to prove
[( p1  p2 )  ( p2  p3 )  ...( pn  p1 )]
57
Proof of Equivalences
To prove
pq
we need to prove
( p  q)
( p  q)  ( q  p)
Example: Prove that n is odd iff n2 is odd.
58
Example

Show that the statements below are
equivalent:
p1: n is even
p2: n-1 is odd
p3: n2 is even
59
Counterexamples

When we are presented with a statement
of the form xP(x) and we believe that it is
false, then we look for a counterexample.

Example

Is it true that “every positive integer is the sum
of the squares of three integers?”
60
Proving Existentials



A proof of a statement of the form x P(x)
is called an existence proof.
If the proof demonstrates how to actually
find or construct a specific element a such
that P(a) is true, then it is called a
constructive proof.
Otherwise, it is called a non-constructive
proof.
61
Constructive Existence Proof

Theorem: There exists a positive integer n
that is the sum of two perfect cubes in two
different ways:


equal to j3 + k3 and l3 + m3 where j, k, l, m are
positive integers, and {j,k} ≠ {l,m}
Proof: Consider n = 1729, j = 9, k = 10,
l = 1, m = 12. Now just check that the
equalities hold.
62
Another Constructive
Existence Proof



Definition: A composite is an integer which
is not prime.
Theorem: For any integer n>0, there exists
a sequence of n consecutive composite
integers.
Same statement in predicate logic:
n>0 x i (1in)(x+i is composite)
63
The proof...







Given n>0, let x = (n + 1)! + 1.
Let i  1 and i  n, and consider x+i.
Note x+i = (n + 1)! + (i + 1).
Note (i+1)|(n+1)!, since 2  i+1  n+1.
Also (i+1)|(i+1). So, (i+1)|(x+i).
 x+i is composite.
 n x 1in : x+i is composite. Q.E.D.
64
Non-constructive Existence Proof





Theorem:
“There are infinitely many prime numbers.”
Any finite set of numbers must contain a maximal
element, so we can prove the theorem if we can just
show that there is no largest prime number.
i.e., show that for any prime number, there is a larger
number that is also prime.
More generally: For any number,  a larger prime.
Formally: Show n p>n : p is prime.
65
The proof, using proof by cases...




Given n>0, prove there is a prime p>n.
Consider x = n!+1. Since x>1, we know
(x is prime)(x is composite).
Case 1: x is prime. Obviously x>n, so let p=x
and we’re done.
Case 2: x has a prime factor p. But if pn,
then p mod x = 1. So p>n, and we’re done.
66
Limits on Proofs

Some very simple statements of number
theory haven’t been proved or disproved!



E.g. Goldbach’s conjecture: Every integer n≥2 is
exactly the average of some two primes.
n≥2  primes p,q: n=(p+q)/2.
There are true statements of number
theory (or any sufficiently powerful system)
that can never be proved (or disproved)
(Gödel).
67
Circular Reasoning
The fallacy of (explicitly or implicitly) assuming the
very statement you are trying to prove in the course
of its proof.
Example: Prove that an integer n is even, if n2 is even.
Attempted proof: “Assume n2 is even. Then n2=2k for
some integer k. Dividing both sides by n gives n =
(2k)/n = 2(k/n). So there is an integer j (namely k/n)
such that n=2j. Therefore n is even.”

Begs the question: How do
you show that j=k/n=n/2 is an integer, without first
assuming n is even?
68
Mathematical Induction
69
Mathematical Induction

is widely used for Discrete Objects:




Prove results ~ complexity of algorithms
Prove correctness of some kinds of computer
programs
Prove theory about graph and tree
Prove identities and inequalities
70
Mathematical Induction



A powerful, rigorous technique for proving
that a predicate P(n) is true for every
natural number n, no matter how large.
Based on a predicate-logic inference rule:
P(0)
n0 (P(n)P(n+1))
n0 P(n)
71
Infinite Ladder

We know two things:
(1) We can reach the first rung
of the ladder
(2) If we can reach a particular
rung of the ladder, then we
can reach the next rung.

Can we conclude that we
can reach every rung?
72
Infinite Ladder

By (1), we can reach the 1st rung.

By (2), we can reach the 2nd rung, because it is next to the 1st rung.

By (2), because it is next to the 2nd rung, we can reach the 3rd rung.

…

By (2), because we can reach the 100th rung, we can reach the 101st rung.

…
73
Example
74
Example
75
Mathematical Induction

Two parts:



Show that the statement holds for the positive
integers 1
Show that if the statement holds for a positive integer
then it must also hold for the next larger integer
Based on the rule of inference for positive
integers:
{P(1)  k ( P(k )  P(k  1))}  P(n)
76
Principle of Mathematical
Induction

To prove that P(n) is true for all positive
integers n, where P(n) is a propositional
function, we complete two steps:

Basis step: We verify that P(1) is true.

Inductive step: We show that the conditional
statement P(k)  P(k+1) is true for all positive
integers k.
[ P(1)  k ( P(k )  P(k  1))]  P(n)
77
Proof by Mathematical Induction
P(n) is true for every positive integer n
1) Basic step
P(1) = TRUE
2) Inductive step
P(k) →P(k+1) , for n
[p(1)  k[P(k) →P(k+1)]] →n P(n)
if P(k) is assumed to be True, then P(k+1)is also True
78
Example: Prove that if n is a positive
n(n  1)
integer, then 1  2  ...  n 
2


กำหนด P(n): sum of the first n positive
integers is n(n+1)/2
กำรพิสูจน์ P(n) มี 2 ขั้นตอน

Basis step: หำค่ำของ P(1)
(1)(1  1) 2
P(1) 
 1
2
2
ดังนั้น P(1) เป็ นจริ ง
79
Example: Prove that if n is a positive
n(n  1)
integer, then 1  2  ...  n 
2
Inductive step: สมมติ P(k) เป็ นจริ ง แล้วหำ P(k+1)
สำหรับ P(k) จะได้

k (k  1)
1  2  ...  k 
2
ดังนั้น สำหรับ P(k+1) จะต้องแสดงว่ำ P(k+1) เป็ นจริ ง นัน่ คือต้องแสดงว่ำ
(k  1)( k  1  1)
1  2  ...  k  (k  1) 
2
(k  1)( k  2)

2
80
Example: Prove that if n is a positive
n(n  1)
integer, then 1  2  ...  n 
2
สำหรับ P(k+1) เรำนำสมกำรของ P(k) มำใช้ โดยกำรบวก k+1 กับทั้งสองข้ำง
ของสมกำร จะได้
1  2  ...  k  (k  1) 




ซึ่ งแสดงให้เห็นว่ำ P(k+1) เป็ นจริ ง
k (k  1)
 (k  1)
2
k (k  1)  2(k  1)
2
k 2  k  2k  2
2
k 2  3k  2
2
(k  1)( k  2)
2
81
Example: Prove that if n is a positive
n(n  1)
integer, then 1  2  ...  n 
2

We have completed the basis step and the inductive
step, so by mathematical induction we know that P(n)
is true for all positive integers n. That is, we have
proven that
n(n  1)
1  2  ...  n 
2
for all positive integers n.
82
Example2: Prove that the sum of the
first n positive odd integers is n2



nth positive odd integer is 2n-1
P(n): 1  3  ...  (2n  1)  n 2
Prove

Basis step: P(1) เป็ นจริ ง เนื่องจำก
1 = 12
83
Example2: Prove that the sum of the
first n positive odd integers is n2


Inductive step: P(k)P(k+1)
สมมติ P(k) เป็ นจริ ง นัน่ คือ 1+3+…+(2k-1) = k2
หำค่ำของ P(k+1)
เนื่องจำกเลขคี่ตวั ที่ k+1 คือ 2(k+1)-1 = 2k+1 ดังนั้น
1+3+…+(2k-1)+(2k+1) = k2+2k+1
= (k+1)2
แสดงว่ำ P(k+1) เป็ นจริ ง
ดังนั้นจึงสรุ ปได้วำ่ P(n) เป็ นจริ ง
84
Example3
Prove that n3-n is divisible by 3, whenever n is a positive integer.
Let P(k) : k3-k is divisible by 3.
Basic step : P(1) : 13-1 = 0 is divisible by 3
Inductive step :
if we assume P(k) = True,
P(k) = k3-k -------(1)
then P(k + 1) must be True,
P(k + 1) = (k + 1)3 - (k + 1) -------(2)
Proof : from (2),
(k + 1)3 - (k + 1) = (k3 + 3k2 + 3k + 1) - (k+1) = (k3-k) + 3(k2 + k)
85
Example 4 : Prove that 1+2+22+…+2n = 2n+1-1
1
 Step1 : P(0) = 1 = 2 -1 = 1 is true
 Step2 :
Example 5 : Prove (sum of Geometric Progressions)
86
Example 6: An Inequality for Harmonic Numbers
Show that
Example 7: Prove that 2n < n! For n = positive
integer and n ≥ 4
87
Another Induction Example

Prove that n  0 , n<2n. Let P(n)=(n<2n)


Base case: P(0)=(0<20)=(0<1)=T.
Inductive step: For n  0prove P(n)P(n+1).



Assuming n<2n, prove n+1 < 2n+1.
Note n + 1 < 2n + 1 (by inductive hypothesis)
< 2n + 2n (because 1<2=22022n-1= 2n)
= 2n+1
So n + 1 < 2n+1, and we’re done.
88
Outline of an Inductive Proof



Want to prove n P(n) …
Base case (or basis step): Prove P(0).
Inductive step: Prove n P(n)P(n+1).

e.g. use a direct proof:



Let nN, assume P(n). (inductive hypothesis)
Under this assumption, prove P(n+1).
Inductive inference rule then gives n P(n).
89
Validity of Induction
Prove: if n0 (P(n)P(n+1)), then k0 P(k)
(a) Given any k0, n0 (P(n)P(n+1)) implies
(P(0)P(1))  (P(1)P(2))  …  (P(k1)P(k))
(b) Using hypothetical syllogism k-1 times we have
P(0)P(k)
(c) P(0) and modus ponens gives P(k).
Thus k0 P(k).
90
The Well-Ordering Property

The validity of the inductive inference rule
can also be proved using the well-ordering
property, which says:


Every non-empty set of non-negative integers
has a minimum (smallest) element.
 SN : mS : nS : mn
91
Use Well-Ordering Property and
Proof by Contradiction




Suppose P(0) is true and that P(k)P(k+1)
is true for all positive k.
Assume P(n) is false for some positive n.
Implies S={n|P(n)} is non-empty and has a
minimum element m (i.e., P(m)=false)
But then, P(m-1)P((m-1)+1)=P(m) which
is a contradiction!
92
Generalizing Induction

Can also be used to prove nc P(n) for a
given constant cZ, where maybe c0, then:


Base case: prove P(c) rather than P(0)
The inductive step is to prove:
nc (P(n)P(n+1)).
93
Induction Example

Prove that the sum of the first n odd positive
integers is n2. That is, prove:
n
n  1 :  (2i  1)  n
2
i 1

Proof by induction.

P(n)
Base case: Let n=1. The sum of the first 1 odd positive
integer is 1 which equals 12.
(Cont…)
94
Example cont.

Inductive step: Prove n  1: P(n)P(n+1).

Let n  1, assume P(n), and prove P(n+1).
 n

(2i  1)    (2i  1)   (2(n  1)  1)

i 1
 i 1

By inductive
2
 n  2n  1
hypothesis P(n)
n 1
 (n  1) 2
95
Strong Induction


Characterized by another inference rule:
P is true in all previous cases
P(0)
n0: (0kn P(k))  P(n+1)
n0: P(n)
Difference with previous version is that the
inductive step uses the fact that P(k) is true
for all smaller k  n , not just for k=n.
P(1)  P(2)  P(3)  ...  P(n)  P(n 1)
96
Example 1
Show that every n>1 can be written as a product
p1p2…ps of some series of s prime numbers.
Let P(n)=“n has that property”
Base case: n=2, let s=1, p1=2.
Inductive step: Let n2. Assume 2kn: P(k).
Consider n+1. If prime, let s=1, p1=n+1.
Else n+1=ab, where 1<an and 1<bn.
Then a=p1p2…pt and b=q1q2…qu. Then n+1= p1p2…pt
q1q2…qu, a product of s=t+u primes.
97
Example 2



Prove that every amount of postage of 12
cents or more can be formed using just 4cent and 5-cent stamps.
Base case: 12=3(4), 13=2(4)+1(5),
14=1(4)+2(5), 15=3(5), so 12n15, P(n).
Inductive step: Let n15, assume 12kn
P(k). Note 12n3n, so P(n3), so add a
4-cent stamp to get postage for n+1.
98
Example 3
Suppose a0, a1, a2, … is defined as follows:
a0=1, a1=2, a2=3,
ak = ak-1+ak-2+ak-3 for all integers k≥3.
Then an ≤ 2n for all integers n≥0.

P(n)
Proof (by strong induction):
1) Basis step:
The statement is true for n=0: a0=1 ≤1=20
for n=1: a1=2 ≤2=21
for n=2: a2=3 ≤4=22
P(0)
P(1)
P(2)
99
Example 3 (cont’d)
2) Inductive hypothesis: For any k>2,
Assume P(i) is true for all i with 0≤i<k:
ai ≤ 2i for all 0≤i<k .
3) Inductive step:
Show that P(k) is true: ak ≤ 2k
ak= ak-1+ak-2+ak-3
≤ 2k-1+2k-2+2k-3
(using inductive hypothesis)
≤ 20+21+…+2k-3+2k-2+2k-1
= 2k-1
(as a sum of geometric sequence)
≤ 2k
Thus, P(n) is true by strong induction.
100
Recursive
101
Recursive definition
When it is difficult to define an object explicitly.
It may be easier to define the object in terms of
itself. This process is call “Recursive Process”
Ex : an = 3n for n = 0,1,2,…
can be defined as a0 = 1 , and an+1 = 3an
102
Example
Discrete Mathematics for Computer
Science
103
Recursively Defined Function


Define the function in terms of itself.
Two steps



Basis step: Specify the value of the function at
zero
Recursive step: Give a rule for finding its value at
an integer from its values at smaller integers.
Also called recursive/inductive definition
104
Recursively defined functions
To define a function f(n) where n = 0, 1, 2,…
Basic Step:
Specify the value of the function f(0)
Recursive Step:
Give the rule for finding f(n+1) in terms of f(n)
105
Example
106
Example


Suppose that f is defined recursively by

f(0) = 3

f(n+1) = 2f(n) + 3
Find f(1), f(2), f(3) and f(4)

f(1) = 2f(0) + 3 = 2(3) + 3 = 6 + 3 = 9

f(2) = 2f(1) + 3 = 2(9) + 3 = 18 + 3 = 21

f(3) = 2f(2) + 3 = 2(21) + 3 = 42 + 3 = 45

f(4) = 2f(3) + 3 = 2(45) + 3 = 90 + 3 = 93
107
Example: Factorial

Factorial function F(n) = n! where



F(0) = 1
F(n+1) = (n+1)F(n)
Find F(5)
F(5) = 5 · F(4) = 5 · 4 · F(3) = 5 · 4 · 3 · F(2)
= 5 · 4 · 3 · 2 · F(1)
= 5 · 4 · 3 · 2 · 1 · F(0)
=5·4·3·2·1·1
= 120
108
Example: Fibonacci Number

Definition:

The Fibonacci numbers,
are defined by the equation
and

f 0 , f1 , f, 2 ,...
f 0  0, f1  1
f n  f n1  f n2 for n = 2,3,4,…
Find
f 2 , f3 , f 4 , f5 , f6
109
Example: Fibonacci Numbers (cont.)
f 2  f1  f 0  1  0  1
f 3  f 2  f1  1  1  2
f 4  f3  f 2  2  1  3
f5  f 4  f3  3  2  5
f6  f5  f 4  5  3  8
110
Recursively Defined Sets

Two steps


Basis step: an initial collection of elements is
specified
Recursive step: rules for forming new elements
in the set from those already known to be in
the set are provided
111
Example

The subset S of the set of integers defined
by


Basis step: 3 Є S
Recursive step:
If x Є S and y Є S, then x + y Є S.
S = { 3, 6, 9, 12, … }
112
Recursive Algorithms
113
Recursive Algorithms

An algorithm is said to be recursive if it
solves a problem by reducing it to an
instance of the same problem with smaller
input
Examples

See example 1-6 of Section 4.4 on pages
311-314
procedure factorial (n: nonnegative integer)
if n = 0 then factorial (n) := 1
else factorial (n) := nfactorial (n-1)
procedure gcd (a, b: nonnegative integers with a<b)
if a = 0 then gcd (a, b) := b
else gcd (a, b) := gcd (b mod a, a)
Proving Recursive Algorithms

Applies proof by induction


either mathematical or strong induction
Two steps


Basis step: prove that P(1) is true
Inductive step: assume inductive
hypothesis P(k) is ture and prove that
P(k+1) is also true
Example
Algorithm 2
procedure power (a: nonzero real numbers,
n: nonnegative integers with a<b)
if n = 0 then power (a, n) := 1
else power (a, n) := apower (a, n-1)
Prove that Algorithm 2, which computes powers of real
numbers, is correct
Basis Step: if n = 0, power(a, 0) = 1. This is correct.
Inductive Step: the inductive hypothesis is power(a,k) = ak for a  0
for nonnegative integer k. We have to show that power(a,k+1) = ak+1.
Since power (a, k+1) = apower (a, k) = a ak a= ak+1. This complete
the inductive step.
Iterative Approach


Iterative approach is an approach to
evaluate a recursively defined sequence
that often requires much less
computation than recursive.
By setting the value at the base case of
the function and successively apply the
recursive definition
Example
procedure fibonacci (n: nonnegative integer)
if n = 0 then fibonacci (0) := 0
else if n = 1 then factorial (1) := 1
else fibonacci (n) := fibonacci (n-1) + fibonacci (n-2)
procedure iterative fibonacci (n: nonnegative integer)
if n = 0 then y := 0
else
begin
x := 0
y := 0
for i := to n-1
begin
z := x+y
x := y
y := z
end
end
{y is the nth fibonacci number}
Merge Sort
procedure mergesort (L: a1, a2, …, an)
if n > 1 then
begin
m := n/2
L1 := a1, a2, …, am
L2 := am+1, am+2, …, an
L := merge ( mergesort (L1), mergesort (L2) )
end
{L is now sorted into elements in nondecreasing order}
Program Correctness
121
Program Correctness

How to ensure that a program is correct?



Tests with data are subjective to data


Test with some data
Program verification
What about other untested data?
Program verification = proof of correctness of
programs


Give strong assurance
Independent to data
Program Correctness

A program is correct if it produces the
correct output for every required input
Initial State
Final State
• x is greater
than 0
•x>0
• y is the square
root of x
• y = x
program
Program Correctness

Program correctness =



Program specification +
Proof methods (verification)
Program specification



Describe program effects
Describe Input and output
Use mathematical precise notation
Program Verification
Definition 1:
A program, or program segment, S is said to be partially correct with
respect to the initial assertion p and the final assertion q if
whenever p is true for the input values of S and S terminates, then q
is true for the output values of S. The notation p{S}q indicates that
the program, or program segment, S is partially correct with respect
to the initial assertion p and final assertion q.
Initial and Final Assertions

Initial Assertion


Final Assertion


The properties that the input values must
have
The properties that the output values of
the program should have
Hoare triple notation

p{S}q
Program Specification

Description about input and output = assertion

Logical formula – two propositions


Program specification focuses on properties


Initial assertion and final assertion
Not describe procedures to compute output from input
Program specification is more precise than informal
specification eg. English language

Flow charts, Diagrams
Proof Methods

Proof methods = inference rules about input
and output of each program statement

To prove that a program will produce desired
output by reading some specified input
Program Correctness Logic

We focus on imperative programming languages





allows destructive assignment, eg. x := y
Like C, C++, Java, etc.
Program specification is due to Hoare triples
Proof method is due to Floyd-Hoare logic
We normally split program into series of subprograms
and prove that each section is true then we show that
the program is correct using rules of inference.
Example
Show that this program segment is correct
with respect to the initial assertion p: x = 1 and
the final assertion q: z = 3
y := 2
z := x +y
Solution
Suppose that p is true then z equals to 3. Hence, p{S}q is
true which means this program is correct.
Rules of Inference

Composition Rule



we write S = S1;S2 to show that a program S is made
up of subprogram S1 followed by S2.
Suppose that p{S1}q is ture and q{S2}r is true
if p is true and S = {S1;S2} is executed and
terminates, then r is true.
p{S1}q
q{S2}r
p{S1;S2}r
Rules of inference

Conditional Statements
If condition then
S
If condition then
S1
else
S2
(p  condition){S}q
(p  condition)  q
p{if condition then S}q
(p  condition){S1}q
(p  condition){S2}q
p{if condition then S1 else S2}q
Rules of inference

Loop Invariants

An assertion that remains true each time S is
executed must be chosen and such assertion is
called loop invariant.
while condition
S
(p  condition){S}p
p{while condition S}( condition  p)
Example
Verify that the program segment is correct with respect to the initial
assertion T and the final assertion abs = |x| If x < 0 then
abs := -x
else
abs := x
Solution
We have to show two things
- If the initial assertion is true and x < 0, then abs = |x|
- If the initial assertion is true and x >0, then abs = x
These are true, thus this program segment is correct
Excercise
procedure multiply (m, n: integers)
if n < 0 then a := -n
else a := n
k := 0
x := 0
S1
S2
while k < a
begin
x := x + m
k := k + 1
end
if n < 0 then product := -x
else product := x
Prove that this program is correct.
S4
S3
Homework 4

Section 4.1


Section 4.2


4, 19, 34, 42, 44
Section 4.5


4, 6, 8, 24, 25, 58
Section 4.4


4, 9, 16, 29, 39
Section 4.3


3, 6, 10, 16, 23, 32, 48, 49, 51, 78
1, 4, 7
Supplementary

4, 14, 27, 37, 47, 51
136