Assignment1 AtsalakiXanthoula Data Structures
Assignment1 AtsalakiXanthoula Data Structures
1st Assignment
Data Structures and Algorithms
Xanthoula Atsalaki
SOLUTIONS ASSIGNMENT 1
Atsalaki Xanthoula
R-1.6
The ordered list of functions by the big-Oh notation is as follows (from the smaller increase
rate of increase to the higher increase rate)
1/n
2100
log(logn)
log2n
n0,01
3n0,5
2logn = n
5n
nlogn4
6nlogn
2nlog2n
4n3/2
n2logn
n3
4logn
2n
4n
R-1.7
1 Second
6
logn
210 10300000
n
nlogn
n2
n3
2n
n!
1012
106
105
1000
100
19
9
1 Hour
1 Month
9
23.610
9
1010
1.3 1019
3.6 109
109
6 104
1500
31
12
12
22.610
1 Century
100.810
6.8 1024
2.6 1012
1011
1.6 106
14000
41
15
12
15
23.110
15
1010
9.7 1030
3.12 1015
1014
5.6 107
1500000
51
17
SOLUTIONS ASSIGNMENT 1
Atsalaki Xanthoula
R-1.10
The Loop1 method runs in O(n) time.
Algorithm Loop1(n):
s0
for i1 to n do
s s+I
O(1+n+n) = O(n)
R-1.11
The Loop2 method runs in O(n) time.
Algorithm Loop2(n):
p0
for i1 to 2n do
2n
p p*I
2n
O(1+2n+2n)=O(n)
R-1.12
The Loop3 method runs in O(n2) time.
Algorithm Loop3(n):
p1
for i1 to n2 do
n2
p p*I
n2
O(1+n2+n2)=O(n2)
R-1.13
The Loop4 method runs in O(n2) time.
Algorithm Loop4(n):
s0
for i1 to 2n do
2n
for j 1 to i do
s s+I
O(1+2n*2n+4n2)=O(n2)
2n
4n2
SOLUTIONS ASSIGNMENT 1
Atsalaki Xanthoula
R-1.14
The loop5 method runs in O(n4) time .
Algorithm Loop5(n):
s0
for i1 to n2 do
n2
for j 1 to i do
s s+I
(1+2+3+n2)
n2 * (1+2+3+n2)
C-1.25
Let Bn1, Bn2,..., B1, B0 be the n bottles of wine.
Consider any binary string b = bn1bn2...b1b0 of length n.
A string b corresponds to a test as follows. If b has k 1s, we give a taste tester to drink a
sample that consists of exactly k drops: a drop of bottle Bi is included to the sample only if bi
= 1.
The idea is to have that sufficient collection of tests b1, b2,..., bT running in parallel, so that,
when after a month their outcome is known, we can identify the poisoned bottle.
Lets assume that n is a power of 2 ( n = 2 k ) for some integer k. We define B(t) to be the
binary string of length t that consists of t/2 consecutive 0s and followed by t/2 1s.
Let the first test be b1 = B(n).
Given the output of this test, our search space is reduced by half: if b1 is positive then we
know that the poisoned bottle is one of Bn 21 ,...,B1,B0, otherwise one of Bn1,Bn2,...,Bn
2.
Let now the second test be b2 = B( n 2 )||B( n 2 ), where || means string concatenation.
Clearly tests b1 and b2 reduce the search space to one forth of the initial. We proceed in the
same way:
b3 = B( n 4 )||B( n 4 )||B( n 4 )||B( n 4 ) and, generally,
bi = B( n i+1 )||B( n i+1 )||B( n i+1 )||B( n i+1 ), for 1 i k, where k = logn.
Combining all k tests b1, b2, ..., bk the search space is finally reduced to only one bottle.
A similar reasoning can be followed in the case that n is not an exact power of 2; in that
case, the number of tests are n=10. The tests could be:
test 1: b1=11111100 0 0000
test 2: b2=11100011 1 0000
test 3: b3=10010010 0 1100
SOLUTIONS ASSIGNMENT 1
Atsalaki Xanthoula
1
1
1
1
1
1
1
0
1
1
1
1
1
1
0
0
1
1
1
1
1
1
0
0
1
1
1
1
0
0
0
0
array A
1
1
1
0
0
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
SOLUTIONS ASSIGNMENT 1
Atsalaki Xanthoula
for i n1 to 0 do
while j n1 A[i, j] = 1 do
N N +i+1
j j +1
return N
C-1.30
We could assume that
m x n = m + (m x (n-1))
So we can repeat the multiplication process by repeatedly calling the function, each time
with n decreasing by 1.
The function stops calling itself once n reaches 1. This therefore gives the condition to stop
calling the function, since m x 1 is simply m and the result m is returned.
if (n == 1)
result = m;
else
result = m + product(m, n-1);
return(result);
}