Anda di halaman 1dari 13

Design and Analysis of Algorithms

Trial Exam

Ade Azurat - Fasilkom UI

September 30, 2010


1

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

1.1 Exercise: Trial Exam 1


Part 1
1. Which invariants would be sufficient to prove the validity of this program:

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:

2. Below is an algorithm to check if all element in an array, are having the


same value. input: an array output: are the elements of the array having
the same value? algorithm:

i:=0;
s:=T;
while i < n - 1 do
{ s := s /\ (a[i] = a[i + 1]) ;
i := i+1
}

Choose the correct invariant:

(a) (s = (∀k : 0 ≤ k < i : a[k] = a[k + 1])) ∧ 0 ≤ i < n


(b) 0 ≤ i < n
(c) (s = (∀k : 0 ≤ k < i : a[k])) ∧ 0 ≤ i < n
(d) (s = (Σk : 0 ≤ k < i : a[k])) ∧ 0 ≤ i < n

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:

4. Which of the following are not relevant in proving the correctness


of an algorithm:
(a) The invariant should show that the expected result
is achieved when the loop terminate
(b) The inisialisation of the loop should maintain the invariant.
(c) The invariant should always be correct with in the execution
of the loop.
(d) The invariant should show that the loop will terminate

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;
}

Choose the correct invariant and prove the algorithm!


1.1 Exercise: Trial Exam 1 5

6. Consider the sum of array example.


input : an array aa[0..N]
output: the sum of all element in array aa.

We would like to have less iteration,


we modify the algorithm such as:

i := 0;
total = 0;
while i < N/2 {
total := total + aa[i] + aa[N-1-i];
i := i + 1;
}

Is the algorithm still correct?


if yes, prove it.
if no, fix it and prove that your revised version is correct.
1.2 Solution: Trial Exam 1 6

1.2 Solution: Trial Exam 1


1. Answer: b
Explanation: Predicate f alse is not an invariant. Predicate true is the
weakest invariant, it cannot tell much about the program and cannot help
us on verifying an algorithm. An incorrect algorithm will be proven correct
with this invariant. Predicate 0 ≤ i < n is not valid in the initialisation.

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

we have to prove I ⇒ I 0 where I 0 is the invariant in the next state of


iteration.
In the next iteration state, variable a00 := a1 and a01 := a0 mod a1 . We
have GCD definition saying : GCD(x, y) = GCD(y, x mod y) where
x ≥ y. Hence, we have I ⇒ I 0 .

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

2.1 Exercise: Trial Exam 2


1. Answer true or false. O(f (n)) − O(f (n)) = 0.

2. Answer true or false. O(f (n) + g(n)) = f (n) + O(g(n)).

3. For the following functions: (lg = log2 )

• 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:

• {4n, 3n} = Θ(n)

(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 ), . . . ,

4. Find the asymptotic upper bound (O) of these recurrences:

(a) T (n) = T (n − 1) + O(n)


(b) T (n) = T (9n/10) + T (n/10) + O(n)
(c) T (n) = 2 T (n/2) + O(n lg n)
2.1 Exercise: Trial Exam 2 10

Solution: Trial Exam 2


1. Answer true or false. O(f (n)) − O(f (n)) = 0.
Answer: false.
Hint: Falsify by counter-example. For example: Choose f (n) = n2 and
g(n) = n.

2. Answer true or false. O(f (n) + g(n)) = f (n) + O(g(n)).


Answer: false.
Hint: Falsify by counter-example. For example: Choose f (n) = n2 and
g(n) = n.

3. Growth of Functions

(a) Equivalence classes:

{n!,
√ 10log n! } = Θ(n!)
{nlg 4 , n4 , 4lg 2n } = Θ(n2 )
{4 · 2n , 2n+1 } = Θ(2n )
{log n!} = Θ(log n!)
{22·lg n! } = Θ(n!2 )

(b) First of all, classify the functions:


? Exponential Functions : 4 · 2n ≥ 2n+1 √
? Polinomial Functions : 4lg 2n ≥ nlg 4 ≥ n4
? Combined & others : 22·lg n! ≥ n! ≥ 10log n! ≥ log n!
The overall rank is:

22·lg n! ≥ n! ≥ 10log n! ≥ 4 · 2n ≥ 2n+1 ≥ 4lg 2n ≥ nlg 4 ≥ n4 ≥ log n!

4. Master Theorem

(a) T (n) = T (n − 1) + O(n)

Guess : T (n) = O(n2 )


Assume : T (n − 1) = O(n − 1)2
P roof : T (n) = T (n − 1) + O(n)
≤ c(n − 1)2 + O(n)
= c(n2 − 2n + 1) + O(n)
= O(n2 )
Therefore, T (n) = O(n2 ), where c = 2, n ≥ 2
(b) T (n) = T (9n/10) + T (n/10) + O(n)
2.1 Exercise: Trial Exam 2 11

Guess : T (n) = O(n lg n)


Assume : T (n − 1) = O((n − 1) lg (n − 1))
P roof : T (n) = T (9n/10) + T (n/10) + O(n)
9
≤ c 10 n lg n + c n 10
lg n
+ O(n)
= cn lg n + O(n)
= O(n lg n)
Therefore, T (n) = O(n lg n), where c = 1, n ≥ 2
(c) T (n) = 2 T (n/2) + O(n lg n)
Draw recursive tree. The height of the tree is lg n. In level i, the cost
is : n lg 2ni
= n lg 2ni
= n (lg n − lg 2i )
= n (lg n − i)
Plg n
The total cost is: n · (lg2 n − j=1 j) which is O(n · lg2 n) we use this
calculation as our guess.

Guess : T (n) = O(n lg2 n)


Assume : T ( n2 ) = O( n2 lg2 n2 )
P roof : T (n) = 2 T ( n2 ) + O(n lg n)
≤ 2 · c · n2 lg2 n + O(n lg n)
= c · n lg2 n + O(n lg n)
= O(n lg2 n)

Therefore, T (n) = O(n lg2 n), where c = 2, n ≥ 2


Please send feedback to ade@cs.ui.ac.id.
Thank you!

Anda mungkin juga menyukai