You are on page 1of 8

CSE 551 - FOA -2

Name: Sai Srinivas Somarouthu Email: ssomarou@asu.edu

ASU ID: 1219481822

Problem 1 :
The Fibonacci series can be computed as follows, F (n) = F (n − 1) + F (n − 2) in O(log n)
Given below is the new series
0 0 0 0 0
F (n) = F (n − 1) + F (n − 2) + F (n − 3) + F (n − 4),
0 0 0 0
Where F (0)  = 0, F (1) = 1, F (2) = 1, F0 (3) = 1
 0
F (n) a1 b1 c1 d1 F (n − 1)
F 0 (n − 1) a2 b2 c2 d2 F 0 (n − 2)
F (n − 2) = a3 b3 c3 d3 × F 0 (n − 3)
 0     
0 0
F (n − 3) a4 b4 c4 d4 F (n − 4)
0 0 0 0 0 0
But we know that F (n) = F (n − 1) + F (n − 2) + F (n − 3) + F (n − 4) and F (0) =
0 0 0
0, F (1) = 1, F (2) = 1, F (3) = 1
=⇒ a1 = b1 = c1 = d1 = 1
0 0
As F (n − 1) = F (n − 1)
=⇒ a2 = 1, b2 = 0, c2 = 0, d2 = 0
So,
 solving further
 we get matrix
  as,0
0 
F (n) 1 1 1 1 F (n − 1)
F 0 (n − 1) 1 0 0 0 F 0 (n − 2)
F (n − 2) = 0 1 0 0 × F 0 (n − 3)
 0     
0 0
F (n − 3) 0 0 1 0 F (n − 4)
so
 for n = n-1 we  get,
0   0 
F (n − 1) 1 1 1 1 F (n − 2)
F 0 (n − 2) 1 0 0 0 F 0 (n − 3)
F (n − 3) = 0 1 0 0 × F 0 (n − 4)
 0     
0 0
F (n − 4) 0 0 1 0 F (n − 5)
By
 substituting  the above matrix
  in the original  matrix equation reduces to
0 0 
F (n) 1 1 1 1 1 1 1 1 F (n − 2)
F 0 (n − 1) 1 0 0 0 1 0 0 0 F 0 (n − 3)
F (n − 2) = 0 1 0 0 × 0 1 0 0 × F 0 (n − 4)
 0       
0 0
F (n − 3) 0 0 1 0 0 0 1 0 F (n − 5)
Further reducing
  the above  expression we get
 0      0 
F (n) 1 1 1 1 1 1 1 1 1 1 1 1 F (n − 3)
F 0 (n − 1) 1 0 0 0 1 0 0 0 1 0 0 0 F 0 (n − 4)
F (n − 2) = 0 1 0 0 × 0 1 0 0 × 0 1 0 0 × F 0 (n − 5)
 0         
0 0
F (n − 3) 0 0 1 0 0 0 1 0 0 0 1 0 F (n − 6)

1
 0   3  0 
F (n) 1 1 1 1 F (n − 3)
F 0 (n − 1) 1 0 0 0 F 0 (n − 4)
=⇒  0  =   ×F 0 (n − 5)

F (n − 2) 0 1 0 0
0 0
F (n − 3) 0 0 1 0 F (n − 6)
The above expression
 can be further
 reduce till n=4
 0     0 
F (n) 1 1 1 1 1 1 1 1 1 1 1 1 F (3)
F 0 (n − 1) 1 0 0 0 1 0 0 0 1 0 0 0
  0
F (2)
=⇒  F 0 (n − 2) = 0 1 0 0 × 0 1 0 0 × 0 1 0 ...(n − 3)... ×  0 
      
0 F (1)
0 0
F (n − 3) 0 0 1 0 0 0 1 0 0 0 1 0 F (0)
 0   n−3  0 
F (n) 1 1 1 1 F (3)
F 0 (n − 1) 1 0 0 0 F 0 (2)
=⇒  F 0 (n − 2) = 0 1 0 0
   ×F 0 (1)

0 0
F (n − 3) 0 0 1 0 F (0)
Here using every recursive call the problem size is divide into half like in the Fibonacci
series.
0
We can say that F (n) can be computed in O(log n)

Problem 2 :
(i) O(f (n)) is the running time of this algorithm on an input of size n.
The ”Add up array entries A[i] through A[j]” statement in Algorithm1 will be carried out
at-most n times in the worst case scenario.
The inner most ’for’ loop also runs at-most n times in the worst case scenario. This gives
us an a bound of the form O(n2 ) on the running time for the innermost ’for’ loop and
the ’add up entries’ statement.
The outer most ’for’ loop also runs at-most n times in the worst case scenario. This gives
us an a bound of the form O(n) on the running time for this for loop and overall bound
is O(n3 ).
And we can say that all other operations carried out take constant time. So, the overall
bound by algorithm1 is O(n3 ).

(ii) Algorithm1 outer loop iterates n times and inner loop iterates from
(n-1) + (n-2) + ..........+ 1 + 0 .
= 1/2(n − 1)(n − 1 + 1)
= 1/2(n2 − n).
Lets find no of operations required for adding n iterations of inner loop:
constant time is required for storing result in B[i, j].
operations required to add entries A[i] through A[j] as follows:
For i = 1:
no of addition operations required = 1 + 2 + ... + n-1 = 1/2(n − 1)(n) = n2 /2 − n/2
For i = 2:
no of addition operations required = 1 + 2 + ... + n - 2 = 1/2(n − 2)(n − 1)
For i = 3:
no of addition operations required = 1 + 2 + ... + n - 3 = 1/2(n − 3)(n − 2)

2
For i = n/2: no of addition operations required = 1 + 2 + ... + (n - n/2) = 1/2(n −
(n/2))(n − (n/2) + 1)) = 1/2(n/2)(n/2 + 1) = 1/2(n2 /4 + n/2) = n2 /8 + n/4.
.
.
.
.
.
.
.
.
For i = k:
no of addition operations required = 1 + 2 + ... + n - k = 1/2(n − k)(n − k + 1)
For i = n-2:
no of addition operations required = 1 + (n - (n - 2)) = 1 + 2 = 3
From i = n/2 case we can say that at least we require addition operations as i = n/2.
So, we can say that at least n2 /8 addition operations are required. Therefore, for n/2
iterations at least we require n2 /8 addition operations in each iteration of the outer loop.
So, we can say that there are at least n/2 × n2 /8.
= (1/16) × n3
= f (n) = n3 /16 where c = 1/16 , n0 > 1. we can say that Ω(f (n)) = n3
We also derived that O(f (n)) = n3 as O(f (n)) and Ω(f (n)) are same the asymptotic
tight bound is n3 =⇒ Θ(f (n)) = n3

(iii) In the Algorithm1, we can see that it is calculating the entries from A[i] through
A[j], for each iteration this can be avoided if we can use the previous summations B[i, j]
to compute the present summation B[i, j]. This completely eliminates the necessity of
one loop.
=⇒ B[i, j] = B[i, j − 1] + A[j]

Data: Input A and n


Result: Return B
Algorithm2:
for i = 1, 2, ..., n;
Sum = A[i] ;
for j = i + 1, i + 2, ..., n;
Sum = Sum +A[j];
if j == i + 1;
B[i, j]= Sum;
else
B[i, j] = B[i, j − 1]+Sum;
return B

The above algorithm gives us an run time in the order of O(n2 ) in worst case, since
2 loops are iterating n times. So, g(n) = O(n2 ) and Algorithm1 has run time in the order

3
f (n) = O(n3 ).
Therefore, limn→∞ g(n)/f (n) = limn→∞ 1/n = 0

Problem 3 :
Given an unsorted sequence of numbers (a1 , a2 , ., an). We have to find the 2nd smallest
element in the given sequence.

Step1: perform pairwise comparisons and find the smallest among those ((a1 , a2 ), (a3 , a4 ).......(an−1 , an )),
then total n/2 comparisons will be required in 1st step.

Step 2: Repeating step 1 again with the remaining n/2 elements, total n/22 comparisons
will be required to get the smallest numbers among n/2 elements.

Step 3:Similarly, repeating step 1 again, n/23 comparisons will be required to get the
smallest numbers among the n/22 elements.

Step 4:At the last step, we will be left with only one element. If we assume that k
number of steps are carried out to get the smallest number, then at k th step, the number
of comparisons performed has to be n/2k .But we know that at last step, only one com-
parison will be required to find the smallest element.
=⇒ n/2k = 1
=⇒ n = 2k
=⇒ k = log2 (n)

Step 5: To find the smallest element, total no of comparisons required-


= n/2 + n/22 + n/23 + n/24 + .......... + n/2k
= n(1/2 + 1/22 + 1/23 + ...........1/2k )
using Geometric progression formula, we get
= n/2(1 − (1/2)k )/(1 − 1/2)
by substituting the value of k in the equation and solving, we get:
= n − 1 comparisons.

At each step, the smallest number must be present. So,the 2nd smallest element will
be the one among those elements which when compared with the smallest element at
each step has lost during pairwise comparison.
So, at each step, 1 element must have lost during comparison with the smallest element
and since there are k steps, 2nd smallest element must be present in these k elements.
Repeating above steps from 1-5 using k elements we need k-1 comparisons to obtain the
smallest element in the looser set.
log2 n − 1 comparisons required for second set, where we obtain the second smallest ele-
ment.
So, No of comparisons required in total = n − 1 + dlog2 ne − 1 in the worst case.
=⇒ n + dlog2 ne − 2

4
ALGORITHM :
It returns second minimum in the given sequence of numbers.

SecondMinimum(N, A[1..N]):
result←FindMinimum(1, N , A[1..N]);
result2←FindMinimum(2, result[0], result[2,3..result[0]]);
return result2[1]

FindMinimum function returns an array in which 0th element is length of that array,
1st element is the maximum number from the given array A and from 2nd element on
wards contain the elements which are being compared with the maximum array.
l = lowest index in the array.
h = highest index in the array.
N = length of array.

FindMinimum(l, h, A[l..h], N):


—-base case—–
if l = h then
result[0..N];
result[0]← 1;
result[1]← A[l];
return result;
———————
result1← FindMinimum(l, h+(h-l)/2, A, N);
result2← FindMinimum(1+l+(h-l)/2, h, A, N);
if result1[1]<result2[1]
K←result1[0]+1;
result1[0]←K;
result1[K]←result2[1];
return result1;
else
K←result2[0]+1;
result2[0]←K;
result2[K]←result1[1];
return result2;

Problem 4 :
Given that n = 2p, V = (v1 , ..., vn ), W = (w1 , ..., wn ). Then we can compute the vector
product V W by the formula:
P
= P 1≤i≤p (v2i−1 + w2i ) × (v2i + w2i−1 ) (a)
− P1≤i≤p v2i−1 × v2i (b)
− 1≤i≤p w2i−1 × w2i (c)

5
which requires 3n/2 multiplications i.e 3p and each multiplication requires same amount
of multiplications to compute we can say that they are not dependent on each other and
can be computed separately.
00 00
Lets assume two n × n matrices V and W with rows vi1 , vi2 , .....vin and wi1 , wi2 , ......win
respectively. The resultant matrix V × W is formed by the vector product using the give
formula.
   
v11 v12 ..... v1n w11 w12 ..... w1n
00
 v21 v22 ..... v2n  00
 w21 w22 ..... w2n 
V =  ... ... ... ... 
 W = 
 ...

... ... ... 
vn1 vn2 ..... vnn n×n wn1 wn2 ..... wnn n×n
 
V1 W1 V1 W2 ..... V1 Wn
00 00
 V2 W1 V2 W2 ..... V2 Wn 
V ×W = ...

... ... ... 
Vn W1 Vn W2 ..... Vn Wn n×n
To compute V1 W1 , 3p multiplications will be required as per (a),(b) and (c) in given
expression. For computing the second element V1 W2 , we can use the previously com-
puted result of term(b) obtained while computing V1 W1 , since term (b) is independent
of w and will be same for all other elements in the row. Similarly, to compute remaining
elements of 1st row will require 2p multiplications.
Therefore, in 1st row, total multiplications=(3p+2p×(n-1)).

For computing V2 W1 , 2p multiplications will be required since the term (c) is independent
of v and the previously computed result of term (c) obtained while calculating V1W1
will be used. Similarly, the remaining (n-1) elements will require only p multiplications
as term (b) and (c) were previously computed.

In 2nd row, total multiplications = (2p + p × (n − 1)).

Therefore, total number of multiplications required to compute n × n matrix=


= (3p + 2p(n − 1)) + (n − 1)(2p + p × (n − 1))
= 3p + 2pn − 2p + (n − 1)(p + pn)
= 3p + 2pn − 2p + np + pn2 − p − pn
= 2pn + pn2
Substituting p = n/2,
= n2 + n3 /2

Therefore, total number of multiplications required to compute n×n matrix is = n2 +n3 /2.

Problem 5 :
Given that, T (n) = 16T (n/4) + n2 , for n a power of 4 (or, n = 4k ) and n > 1.
Assume T (1) = 1.

6
So, we need to break down the given recurrence relation till T (n/4) = T (1).
=⇒ T (n) = 16T (n/4) + n2
=⇒ T (n/4) = 16T (n/16) + n2 /16
By Substituting the T (n/4) value in the given recurrence relation we get,
=⇒ T (n) = 16[16T (n/16) + n2 /16] + n2
=⇒ T (n) = [162 T (n/16) + n2 ] + n2
=⇒ T (n) = 162 T (n/16) + 2 × n2
calculating T (n/16)
=⇒ T (n/16) = 16T (n/64) + n2 /256
By Substituting the T (n/16) value in the given recurrence relation we get,
=⇒ T (n) = 162 [16T (n/64) + n2 /256] + 2 × n2
=⇒ T (n) = 163 T (n/64) + n2 + 2 × n2
=⇒ T (n) = 163 T (n/64) + 3 × n2
calculating T (n/64)
=⇒ T (n/64) = 16T (n/256) + n2 /163
By Substituting the T (n/64) value in the given recurrence relation we get,
=⇒ T (n) = 163 [16T (n/256) + n2 /163 ] + 3 × n2
=⇒ T (n) = 164 T (n/256) + n2 + 3 × n2
=⇒ T (n) = 164 T (n/256) + 4 × n2
This relation can also be written as,
=⇒ T (n) = 164 T (n/44 ) + 4 × n2
Breaking down the recurrence relation further, till we get T (1) after some x steps we get,
=⇒ T (n) = 16x T (n/4x ) + x × n2
=⇒ n/4x = 1
Given that n = 4k , by substituting we get
=⇒ 4k /4x = 1
=⇒ k = x
Now substitute the x value in T (n),
=⇒ T (n) = 16k T (1) + (k) × n2 , we know that T (1) = 1
=⇒ T (n) = 16k + (k) × n2
=⇒ T (n) = 42k + k × n2
2
=⇒ T (n) = (4k ) + k × n2
We also know that n = 4k , Applying log on both sides
=⇒ log n = k × log 4
using logarithmic division rule we know that loga x = (logb x)/(logb a), we get k = log4 n
substituting the values of k in recurrence relation T (n).
=⇒ T (n) = n2 + n2 × (log4 n)
=⇒ T (n) = n2 (1 + log4 n)

Problem 6 :
Given that for n a power of 2 (or, n = 2k ) and n > 1.
T (1) = 1
T (n) = 3n T (n/2)

7
For T (1) we know that now of comparisons required will be 1. So, we need to break down
the given recurrence relation till T (n/2) = T (1).
=⇒ T (n) = 3n T (n/2)
=⇒ T (n/2) = 3n/2 T (n/4)
By Substituting the T (n/2) value in the given recurrence relation we get,
=⇒ T (n) = 3n [3n/2 T (n/4)]
=⇒ T (n) = 33n/2 T (n/4)
calculating T (n/4)
=⇒ T (n/4) = 3n/4 T (n/8)
By Substituting the T (n/4) value in the given recurrence relation we get,
=⇒ T (n) = 33n/2 [3n/4 T (n/8)]
=⇒ T (n) = 37n/4 T (n/8)
calculating T (n/8)
=⇒ T (n/8) = 3n/8 T (n/16)
By Substituting the T (n/4) value in the given recurrence relation we get,
=⇒ T (n) = 37n/4 [3n/8 T (n/16)]
=⇒ T (n) = 315n/8 T (n/16)
calculating T (n/16)
=⇒ T (n/16) = 3n/16 T (n/32)
By Substituting the T (n/4) value in the given recurrence relation we get,
=⇒ T (n) = 315n/8 [3n/16 T (n/32)]
=⇒ T (n) = 331n/16 T (n/32)
Breaking down the recurrence relation further, till we get T (1) after some x steps we get,
x x−1
=⇒ T (n) = 3(2 −1)n/2 T (n/2x )
=⇒ n/2x = 1
=⇒ 2x = n
Given that n = 2k , by substituting we get
=⇒ 2x = 2k
=⇒ x = k
substituting the values of k in recurrence relation T (n)
k k−1
=⇒ T (n) = 3(2 −1)n/2 T (1)
k k−1
=⇒ T (n) = 3(2 −1)n/2
=⇒ T (n) = 3(n−1)n/(n/2)
=⇒ T (n) = 32(n−1)

You might also like