Trial Exam
This exercise is intended to help you prepare your exam. There is absolutely
no guarantee that something similar to these questions will appear in the exam.
You are allowed to write your proofs less formally. You should however present
your arguments clearly. Keep in mind in addition to your intellectual ability to
solve problems, writing readable answer is also important. I cannot make good
judgement out of a chaoticly written answer, or a proof in which the steps are
poorly motivated. A correct answer without good justification and sufficient
explanation will not be graded.
Chapter 1
Loop Invariant
1.1 Exercise: Trial Exam 1 3
i:= n;
while i > 0 do
i := i - 1
The input is : a positive number. The output is : the given number become
0.
(a) F
(b) 0 ≤ i
(c) 0 ≤ i < n
(d) T
explanation:
i:=0;
s:=T;
while i < n - 1 do
{ s := s /\ (a[i] = a[i + 1]) ;
i := i+1
}
explanation:
1.1 Exercise: Trial Exam 1 4
3. Which of the facts below are the most acceptable statements about
"algorithm correctness" ?
(a) a correct algorithm should eventually stop
(b) a correct algorithm will always have trivial invariant
(c) a correct algorithm solve the given problem
(d) a correct algorithm may not terminate
explanation:
explanation:
Part 2
5. Below is an algorithm to find
GCD (Greatest Common Divisor - FPB: Faktor Persekutuan Terbesar)
input: Given two positive numbers.
Output: the GCD of those numbers.
Algorithm:
int GCD(int a, int b){
int a0, a1, temp;
a0 := a;
a1 := b;
while a1 != 0 do {
temp := a1;
a1 := a0 mod a1;
a0 := temp
}
return a0;
}
i := 0;
total = 0;
while i < N/2 {
total := total + aa[i] + aa[N-1-i];
i := i + 1;
}
2. Answer: a
Explanation: The expected P (post condition) of the algorithm is :
s = (∀k : 0 ≤ k < n : a[k] = a[k + 1] by the exit condition requirement
I ∧ ¬E ⇒ P . We find the invariant by weakening the post condition with
substitution on constant N with i. Predicate 0 ≤ i < n is also fulfill by the
algorithm. Adding this predicate will strengthen the invariant.
3. Answer: c
Explanation: In other model of computation such as in reactive system,
we might not expect our algorithm to terminate (answer: a). However,
in imperative programming, to justify the correctness, we required our al-
gorithm to terminate (answer: d). Finding invariant is a hard problem.
There is no standard procedure to find any invariant. It may required some
creativity. Most of algorithm, especially the complex one, has non trivial
invariant (answer b).
4. Answer: d
Explanation: Answer (a) is the exit condition requirement of invariant.
Answer (b) is the initialization requirement of invariant. Answer (c) is
the maintanance requirement of invariant especially in atomic action model.
Answer (d) is not the requirement of invariant. A metric function (variant)
should show the termination of the loop not invariant.
5. GCD algorithm
Constraint: a0 ≥ a1 ∧ a0 > 0.
Invariant: GCD(a0 , a1 ) = GCD(a, b)
Initialization: (a0 , a1 ) = (a, b) ⇒ I
Exit Condition: Definition of GCD : ∀X : X 6= 0 : GCD(X, 0) = X. The
expected post condition: a0 = GCD(a, b). The ¬E : a1 = 0. We will
have : I ∧ ¬E ⇒ P .
Maintanance: We need to show that the invariant holds in every iteration.
We know from the initialisation, the invariant hold initially, we need
to prove that the invariant will hold in the next iteration. Formally,
1.2 Solution: Trial Exam 1 7
6. Sum of Array
The given algorithm is not correct. The dN/2e’th element will be counted
twice if the N is odd. The simplest fix is to constraint the algorithm with
”N is even” problem.
Constraint: N is even
Invariant: total = (Σk : 0 ≤ k < i : a[k] + a[N − 1 − k])
Initialitation: Empty range in the Σ quantifier equal to 0. Variable i = 0
and total = 0, hence we have I.
Exit Condition: The expected post condition: total = (Σk : 0 ≤ k <
N/2 : a[k] + a[N − 1 − k]). The ¬E : i = N/2 within the constrain N
is even. By subtituting I with i = N/2, we will have : I ∧ ¬E ⇒ P .
Maintanance:
In the next iteration state, variable i0 = i + 1 and total0 = total + a[i] +
a[N − 1 − i]
The I 0 is total0 = (Σk : 0 ≤ k < i0 : a[k] + a[N − 1 − k])
Equational Proof:
I0
≡ < Definition of I 0 >
total0 = (Σk : 0 ≤ k < i0 : a[k] + a[N − 1 − k])
≡ <Substitution: i0 = i + 1 >
total0 = (Σk : 0 ≤ k < i + 1 : a[k] + a[N − 1 − k])
≡ <range splitting>
total0 = (Σk : 0 ≤ k < i : a[k] + a[N − 1 − k]) + a[i] + a[N − 1 − i]
≡ <Substitution: total0 = total + a[i] + a[N − 1 − i)] >
total + a[i] + a[N − 1 − i]
= (Σk : 0 ≤ k < i : a[k] + a[N − 1 − k]) + a[i] + a[N − 1 − i]
≡ <arithmatics>
total = (Σk : 0 ≤ k < i : a[k] + a[N − 1 − k])
≡ < Definition of I >
I
Since we have I ≡ I 0 and I holds initially, we have I ⇒ I 0 .
Chapter 2
Growth of Functions
2.1 Exercise: Trial Exam 2 9
• nlg 4 • n! • 4 · 2n
•√22·lg n! • log n! • 2n+1
• n4 • 4lg 2n • 10log n!
(a) Partition the above functions into equivalence classes such that f (n)
and g(n) are in the same class if and only if f (n) = Θ(g(n)). Example:
(b) Rank the above functions by order of growth; that is, find an ar-
rangement g1 , g2 , g3 , g4 , . . . , of the functions satisfying g1 = Ω(g2 ), g2 =
Ω(g3 ), g3 = Ω(g4 ), . . . ,
3. Growth of Functions
{n!,
√ 10log n! } = Θ(n!)
{nlg 4 , n4 , 4lg 2n } = Θ(n2 )
{4 · 2n , 2n+1 } = Θ(2n )
{log n!} = Θ(log n!)
{22·lg n! } = Θ(n!2 )
4. Master Theorem