Lecture 1 CP Iterative-RMB
Lecture 1 CP Iterative-RMB
2
Grading policy
%
No. Assessment Warning CLO1 CLO2 CLO3
each
1 Tugas Besar 20 0 8 12 20
2 Kuis 10 5 2 3 10
3 UTS 35 5 30 0 35
4 UAS 35 0 5 30 35
Overall Total 100 10 45 45 100
Total
POs 100 100
3
Recall on mathematical induction
Claim: for all ∈ ℕ,
+1
1 + 2 + ⋯+ = .
2
When using induction to prove that some statement concerning the positive integer is true, the following
terminology is used:
The induction base is the proof that the statement is true for = 1 (or another initial value).
The induction hypothesis is the assumption that the statement is true for an arbitrarty ≥ 1 (or another
initial value).
The induction step is the proof that if the statement is true for , it must be also true for + 1.
4
Recall on mathematical induction
Suppose we intend to prove the previous example. Consider the following induction proof step.
1 + 2 + ⋯+ + +1 =
To that end,
= + +1
= + +1
=
= (proved.)
5
Algorithm correctness
Logical methods of checking correctness:
Testing : try the algorithm on sample inputs
Correctness proof: prove mathematically
6
Notations
The value in identifier immediately after the th iteration of the loop is denoted
( = 0 means immediately before entering for the first time).
For example, denotes the value of identifier after the 6th time around the loop.
7
Example #1: Fibonacci numbers
What is the loop invariant inside the while-loop in the following program ? Prove it that it correctly computes
the Fibonacci number !
8
Solution
Claim: fib returns
Facts:
"= 2; $ = $ + 1
%" = 0; %$ = &$
&" = 1; &$ = '$ + 1
'$ = %$ + &$
Loop invariant
∀ natural numbers , ≥ 0, $ = , + 2, %$ = $ , &$ = $ .
Proof by induction on ,.
Induction base. For , = 0, " = 2, %" = 0 = ", and &" = 1 = (trivial).
Induction hypothesis. Suppose that ∀, ≥ 0, $ = , + 2, %$ = $ , &$ = $ .
Induction step.
RTP (required to prove)
1. $ = , + 3
2. %$ = $
3. &$ = $ 9
Solution (cont’d)
$ = $ + 1 (by fact) Correctness proof
= , + 2 + 1 (by ind. hyp) Claim: the algorithm terminates with & containing .
=,+3
The claim is certainly true if = 0. If > 0, we enter the while-loop.
%$ = &$ (by fact)
= $ (by ind. hyp) Termination.
When loop terminates, will equal + 1.
&$ = '$ (by fact) Suppose this happens after / iterations, we have 0 = / + 2 and 0 =
= %$ + &$ (by fact) + 1. We can conclude that / = − 1.
= $ + $ (by ind. hyp)
By the loop invariant, &0 = 0 = (proved as claimed.)
= $ .
10
Example #2: Maximum values
What is the loop invariant inside the while-loop in the following program ? Prove it that it correctly finds the
maximum value in A[1:n] !
11
Solution
Claim: maximum(A,n) max{A[1],A[2],…,A[n]}
Fact: m0 = A[1];
mj+1= max{mj,A[ij]}
i0=2
ij+1 = ij+1
Loop invariant:
Claim: fo all j≥0
mj = max{A[1],A[2],..,A[j+1]}
ij = j + 2
RTP
1. ij+1 = j + 3
2. mj+1 = max{A[1],A[2],..,A[j+2]}
1/21/2019 12
Solution (cont’d)
,+1 = + 1,
= (, + 2) + 1
= ,+3
4, + 1 = max{4,, 7[ ,]}
= max{4,, 7[, + 2]}
= max{max{7[1], . . , 7[, + 1]}, 7[, + 2]}
= max{7[1], . . , 7[, + 2]}
Correctness proof
Claim: the algorithm terminates with 4 containing the maximum value in 7 1 ⋯ .
Termination:
When loop terminates, will equal + 1.
Suppose this happens after / iterations, we have 0 = / + 2 and 0 = + 1. We can conclude that / = − 1.
Based on induction hypothesis: 4/ = max{7[1], 7[2], . . , 7[/ + 1]}, 4 returns max{7[1], 7[2], . . , 7[ ]} (proved.)
1/21/2019 13
Example #3: Multiplications
What is the loop invariant inside the while-loop in the following program ? Prove it that it correctly computes
the multiplication of two numbers >?!
Facts:
=0 "
$ = $ + >$ ?$ mod 2
>$ = 2>$
?$ = ?$ ⁄2
Loop invariants:
∀, ≥ 0, $ + >$ ?$ = >" ?"
Proof by induction on ,.
The base, , = 0.
$ + >$ ?$ = " + >" ?"
= 0 + >" ?"
= >" ?" .
15
Solution (cont’d)
Suppose that , ≥ 0 and $ + >$ ?$ = >" ?" ,
RTP. $ +>$ ?$ = >" ?"
= $ + >$ ?$ mod 2 + 2>$ ?$ ⁄2
= $ + >$ ?$ mod 2 + 2 ?$ ⁄2
= $ +>$ ?$
= >" ?" .
Correctness proof.
Claim: the algorithm terminates with containing the product of > and ?.
Termination: on every iteration, the value of ? is halved (rounding down if it is odd). Therefore there will be
some / at which ?0 = 0.
By the loop invariant,
0 +>0 ?0 = >" ?"
0 +>0 0 = >" ?"
0 = >" ?"
16
Exercises #1
Suppose for all n∈Ν, 2n/2+(n mod 2)=n. Prove that the following algorithms are correct!
Function power(y,z)
Comment Return return yz, where y∈ℜ,z∈Ν
x ← 1
While z>0 do
x←x.y
z←z−1
return(x)
17
Exercises #2
Prove that the following algorithms are correct!
18
Exercises #3
Prove that the following algorithms are correct!
Procedure swap(x,y)
Comment Swap x and y.
x ← x+y
y ← x−y
x ← x−y
19
References
1. Ian Parberry. Lecture notes on algorithm analysis and computational complexity. 2001
2. Ian Parberry & William Gasarch. Problems on Algorithms. 2002
20