0% found this document useful (0 votes)
114 views6 pages

Assignment1 AtsalakiXanthoula Data Structures

The document contains solutions to an assignment on data structures and algorithms from 2015. It includes: 1) Ranking common functions by their Big-O notation complexity from least to most complex. 2) Calculating the number of operations various algorithms could perform in given time frames like 1 hour or 1 century. 3) Analyzing the time complexity of loops and algorithms, finding some are O(n) while others are O(n^2) or O(n^4). 4) Proposing strategies to identify a poisoned bottle of wine among n bottles in the least number of tests. 5) Describing an O(n) algorithm to find the row with the most 1s in a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
114 views6 pages

Assignment1 AtsalakiXanthoula Data Structures

The document contains solutions to an assignment on data structures and algorithms from 2015. It includes: 1) Ranking common functions by their Big-O notation complexity from least to most complex. 2) Calculating the number of operations various algorithms could perform in given time frames like 1 hour or 1 century. 3) Analyzing the time complexity of loops and algorithms, finding some are O(n) while others are O(n^2) or O(n^4). 4) Proposing strategies to identify a poisoned bottle of wine among n bottles in the least number of tests. 5) Describing an O(n) algorithm to find the row with the most 1s in a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 6

2015

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)

O(1+n2*(1+2+3+n2) +n2* (1+2+3+n2) ) = O(n4)

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

test 4: b4=11011011 0 1010,


if we want to kill up to 4 soldiers .
Suppose that only test 2 is positive. Given the structure of the tests, we can infer that the
5th bottle B4 is the poisoned one. When n is not a power of 2, there are generally more than
one possible test structures. They all can determine the poisoned bottle, but the king should
choose that collection of tests that have the least number of 1s.
C-1.27
Start at the upper left of the matrix. Walk across the matrix until a 0 is found. Then walk
down the matrix until a 1 is found. This is repeated until the last row or column is
encountered. The row with the most 1s is the last row which was walked across. Clearly this
is an O(n)-time algorithm since at most 2 * n comparisons are made.
C-1.28
Using the two properties of the array, the method we follow is described as follows.
Starting from element A[n 1,0], we scan A moving only to the right and upwards.
If the number at A[i, j] is 1, then we add the number of 1s in that column (i+1) to the current
total of 1s
Otherwise we move up one position until we reach another 1. An example of the array is
shown below.
1
1
1
1
1
1
1
0

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

The pseudo-code of the algorithm is shown below.


The running time is O(n).
In the worst case, you will visit at most 2n 1 places in the array. In the case that the
diagram has all 0s in rows 2 through n and 1s in the first row, then there will be n 1
iterations of the for loop at constant time (since it will never enter the while loop) and 1
iteration of the while loop which has n iterations of constant time.
Algorithm NumberOfOnes(A):
Input: An 2D array nn A with elements 1s and 0s as described.
Output: The total number of 1s.
N0
j0

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.

int product(int m, int n)


{
int result;

if (n == 1)
result = m;
else
result = m + product(m, n-1);
return(result);
}

You might also like