0% found this document useful (0 votes)
55 views20 pages

Lecture 1 CP Iterative-RMB

The document discusses iterative algorithms and correctness proofs. It provides examples of loop invariants for algorithms that compute Fibonacci numbers, find the maximum value in an array, and multiply two numbers. The key points are: - Iterative algorithms can be proved correct by analyzing loops one by one using loop invariants - A loop invariant captures the progress made in each loop iteration and remains true each time through the loop - To prove correctness, loop invariants must be proved and used to show the algorithm terminates and computes the correct result

Uploaded by

Youth Cibitung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views20 pages

Lecture 1 CP Iterative-RMB

The document discusses iterative algorithms and correctness proofs. It provides examples of loop invariants for algorithms that compute Fibonacci numbers, find the maximum value in an array, and multiply two numbers. The key points are: - Iterative algorithms can be proved correct by analyzing loops one by one using loop invariants - A loop invariant captures the progress made in each loop iteration and remains true each time through the loop - To prove correctness, loop invariants must be proved and used to show the algorithm terminates and computes the correct result

Uploaded by

Youth Cibitung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CSH2G3

Design & Analysis of Algorithms


Lecture 1
Correctness proof of algorithms: ITERATIVE ALGORITHMS
Administrative issues
• Syllabus
• Course policy
• Create group in Telegram
• Join Gclass
• Link of materials

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.

Induction base: for = 1,


1= .
Induction hypothesis: Assume, for an arbitrary positive integer , that
1 + 2 + ⋯+ = .
Induction step: We need to prove that

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

Proving correctness of an iterative algorithm


Analyze the algorithm one loop at a time, starting at the inner loop in case of nested loops.
For each loop devise a loop invariant that remains true each time through the loop, and captures the
progress made by the loop.
Prove that the loop invariants hold.
Use the loop invariants to prove that the algorithm terminates.
Use the loop invariants to prove that the algorithm computes the correct result.

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 >?!

Claim: for all ∈Ν, 2  /2 + ( 4<= 2) =


14
Solution
Claim:
If >, ? ∈ ℕ, then multiply >, ? returns >?. That is, when line 5 is executed, = >?.

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∈Ν, 2n/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!

Consider the following algorithm:


Function mystery(y,x)
z ← 0
While y<>0 do
z←z+x
y←y−1
return(z)

What does the algorithm do?

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

You might also like