Beruflich Dokumente
Kultur Dokumente
Assignment 1
Sergiu Marin
Student number: 0932191
s.c.i.marin@student.tue.nl
February 14, 2016
Solution to Exercise 1: The mentioned functions are ranked by their order of growth(starting
from slowest growing), where functions with the same order of growth are ranked equal.
n2
4 log 8
n+4 n
n log n
n2 and 4log n
nlog 8
2n
Solution to Exercise 2:
f (n) =
Pn
i=1 [4i4]
= [4
Pn
i=1 i][4
Pn
i=1 1]
= 4 n(n+1)
4n = 2n2 +2n4n = 2n2 2n
2
2
P n
log n(log n+1)
g(n) = 2n + n log
= 2n + n (log n)2+log n = 12 n(log n)2 + 12 n log n +
i=1 i = 2n + n
2
+ 2n = 21 n(log n)2 + n( 12 log n + 2)
Now n 2, we have that 21 n(log n)2 12 n(log n)2 + n( 12 log n + 2) 4n(log n)2 ,
where 12 n(log n)2 + n( 12 log n + 2) = g(n). Thus, by definition of -notation, g(n)
(n(log n)2 ), where c1 = 21 , c2 = 4 and n0 = 2. This corresponds to choice (e.)
n(log n)2 .
h(n) =
Pn
Pi
i=0 [
j=1 i]
P0
j=1 0
P1
j=1 1
P2
3+4+4+4+4+...+n
| +n+
{z. . . + n} =
Pn
j=1 2 + . . . +
j=1 n = 1 + 2 + 2 + 3 + 3
2
2
2
1 + 2 + 3 + 4 + . . . + n2 = n(n+1)(2n+1)
6
+
=
n-times
2n3 +3n2 +n
6
= 13 n3 + 12 n2 + 61 n
i(n) =
Pn
i=0 2
= 20 + 21 + 22 + . . . + 2n =
2n+1 1
21
= 2n+1 1
j(n) =
Plog n
i=1
n=n
Plog n
i=1
1 = n log n
k(n) =
Pn
4
i=0 n
4
n
Pn
i=0 1
4(n+1)
n
=4+
4
n
Now, n 4, we have that 1 4 + n4 5, where 4 + n4 = k(n). Thus, by definition of -notation, k(n) (1), where c1 = 1, c2 = 5 and n0 = 4. This corresponds to
choice (a.) 1.
Solution to Exercise 3: We may prove that the presented algorithm is correct by using the
following loop invariant:
At the start of each iteration of the for loop i , the variables leastSmall value is the
largest integer in the subarray A[1..i 1] that is strictly smaller than x.
The following is a proof of correctness using the above-stated loop invariant:
Initialisation: Before the first iteration, i = 1. Since the current subarray is A[1..0],
there is no element in this subarray that is strictly smaller than x. In accordance with
the algorithms description, leastSmall = 0. Thus, we have proved that the invariant
is true prior to the first iteration of the loop.
Maintenance: The loop invariant is true before iteration i of the loop. Well prove
that it remains true before iteration i + 1.
At the start of iteration i, leastSmall contains the value of largest integer in the subarray A[1..i 1] that is strictly smaller than x. Now, during iteration i, a
In conclusion, no matter what case applies to the value at the current index i, leastSmall
would still contain the value of the largest element strictly smaller than x in the
subarray A[1..i]. Incrementing i for the next iteration of the for loop then preserves
the loop invariant - that is: before iteration i + 1 leastSmall0 s value will contain the
largest element strictly smaller than x in subarray A[1..i + 1 1] = A[1..i].
Termination: The condition that causes the loop to terminate is that i > n. Because
each iteration increases i by 1, we must have i = n + 1 at that time. Substituting n + 1
for i in the loop invariant, we have that the variables leastSmall value is the largest
integer in the subarray A[1..n] that is strictly smaller than x. Hence, the algorithm is
correct as the subarray A[1..n] is actually just the array A.
By way of a loop invariant, we have proved that the algorithm is correct. We can now analyse
the running time of the presented algorithm. We denote the running time of the algorithm
as a function of n as T (n).
T (n) = c1 + (n + 1) c2 + n c3 + c4 = n (c2 + c3 ) + c1 + c2 + c4
The above values are obtained by determining how many times each line of code gets executed(a statement takes ci steps to execute). Line 1 gets executed 1 time, so it contributes
only by a constant factor to the final running time. The condition in the for-loop gets executed n + 1 times, contributing c2 (n + 1) to the running time. The statements inside the
for loop get executed n times, contributing by c3 n and, finally, the statement in line 5 gets
executed only one time, also contributing with a constant factor to the final running-time,
namely c4 .
So, T (n) is of the form a n + b, where a > 0 and b 0. Since a n a n + b
a n + n = (a + 1) n, n 1, where a n + b = T (n), by definition of -notation, we may
conclude that T (n) (n), where c1 = a, c2 = a + 1 and n0 = 1. Therefore, the running time
of the algorithm is (n).
Base Case: n = 0 =
() holds for n = 0.
Pn
k=0 x
Pn
P0
k=0 x
k=0 x
xn+1 1
x1 , x
= x0 = 1 =
x1
x1
x0+1 1
x1
xn+1 1
x1 .
Thus,
Pm+1
xk =
Thus,
Pm+1
k=0
k=0 x
k=0
xk =
IH xm+1 1
x1
+ xm+1 =
xm+2 1
x1 .
+ xm+1 =
xm+2 1
x1 .
k=0 x
xn+1 1
x1 .
Solution to Exercise 5:
(a) n +
4+ n
log n c1n c1n = c51 . So, log n c51 which is not true when n since c51 is a
constant. Thus, c1 log n 4+ n4 does not hold. Since n is positive, c1 n log n 5n+4 does
not hold either. But we assumed that c1 n log n 5n + 4 would hold in the beginning.
Thus, we have derived a contradiction. We may conclude that 5n + 4 6= (n log n).
Well, we know that limn lognn = . Thus, n 16, it holds that 0 log n n. We
(f) If f (n) = (g(n)) and g(n) = (f (n)), then f (n) = (g(n)) - This is true. Since
this is an implication, in order to prove it to be true, we assume that f (n) = (g(n)) and
g(n) = (f (n)) are indeed true, and derive that f (n) = (g(n)).
f (n) = (g(n)) so, by the definition of -notation, c3 and n1 positive constants,
such that 0 c3 g(n) f (n), n n1 . (I)
g(n) = (f (n)) so, by the definition of -notation, c4 and n2 positive constants,
such that 0 c4 f (n) g(n), n n2 . (II)
From (II), we have that 0 f (n) c14 g(n), where c14 is a positive constant, since
c4 is a positive constant. When we combine this inequality with (I), using the transitivity of , we arrive at 0 c3 g(n) f (n) c14 g(n), which holds n n0 , where
n0 = max(n1 , n2 ). ()
Thus, through (), we have proved that c1 , c2 and n0 positive constants, such that
0 c1 g(n) f (n) c2 g(n), n n0 , where c1 = c3 , c2 = c14 and n0 = max(n1 , n2 ).
This is indeed the definition of -notation, so we may conclude that f (n) = (g(n)).
NOTE - there is a shorter way to prove this by using the properties of , and O.
This proof works as follows:
We know that g(n) = (f (n)). By the transpose-symmetry property of and O we
have that f (n) = O(g(n)) - but we also know that f (n) = (g(n)) = c1 , c2 , n1
and n2 positive constants, such that 0 c1 g(n) f (n), n n1 and 0 f (n)
c2 g(n), n n2 . From the transitive property of inequalities, we have that 0 c1 g(n)
f (n) c2 g(n), n n0 where n0 = max(n1 , n2 ). Since this is indeed the definition of
-notation, we may conclude that f (n) = (g(n)).