Sie sind auf Seite 1von 6

Assignment 1

Suchith Prabhu - 15CSE1023


January 17, 2018

1 Asymptotic Complexity
1. Measuring the run-time as a function of basic operations such as arith-
metic, memory access, control flow etc. and the input size will differ from
machine to machine. If an algorithm runs slow on a machine A and runs
faster on machine B then we cannot say anything about how efficient it
is. Machines differ in architecture.

Asymptotic complexity measures the efficiency of the algorithms irrespec-


tive of the external factors. Most importantly, it can tell us how time
depends on input as input size tends to infinity as the time required for
small inputs is tolerable anyhow but it the large inputs that bother us.

2. Asymptotic complexity of an algorithm has two components, c and n0 .


f 2(N ) ≤ c ∗ f 1(N ) for c > 0 and n > n0. In the given example, our
input size has not yet hit n0 . As we increase the input size we would start
noticing better performance from A2 algorithm.
It also possible that the hardware configuration in the machine in which
A1 is running is better than machine in which A2 is running.

2 Recurrences
1. Linear Algorithm

1
Algorithm 1 O(n) complexity
a=0
b=1
for i=2 to n do
c=a+b
if c >= 232 then
c = c − 232
end
b=a
a=c

end

2. The above algorithm is polynomial.


If recursion is used then the complexity would be exponential.

3.
f ib(n + 1) = f ib(n) + f ib(n − 1)
f ib(n) = f ib(n)

The above equations can be written in matrix form.


    
f ib(n + 1) 1 1 f ib(n)
=
f ib(n) 1 0 f ib(n − 1)

which is of the form Uk+1 = AUk ,which gives the following,


   n−1  
f ib(n + 1) 1 1 1
=
f ib(n) 1 0 1
   n−1  
f ib(n) 1 1 1
=
f ib(n − 1) 1 0 0

from above we can conclude,


   n
f ib(n + 1) f ib(n) 1 1
=
f ib(n) f ib(n − 1) 1 0

4. Algorithm using matrix

2
Algorithm 2 O(n log n) complexity
Procedure: fibonacci(I, n)
if n <= 1 then
return(I)
else
A=fibonacci(I, n/2)
return(A*A)

end

5. Code with O(log(n) running time complexity


def m u l t i p l y s q u a r e m a t r i x (A, B, n ) :

C = [ [ 0 f o r i in range ( 0 , n ) ] f o r i in range ( 0 , n ) ]

f o r i in range ( 0 , n ) :
f o r j in range ( 0 , n ) :
f o r k in range ( 0 , n ) :
C [ i ] [ j ]=(C [ i ] [ j ]+A[ i ] [ k ] ∗B [ k ] [ j ] ) % ( p−1)

return (C)

def numberPower (num , pow ) :

i f pow == 0 :
return ( 1 )
e l i f pow ==1 :
return (num)
else :

q=pow//2
r=pow%2

a = numberPower (num , q )
a = ( a ∗ a)%p

i f r ==1:
a=(a ∗num)%p

return ( a )

def matrixPower ( I , n ) :

3
i f n<=1:
return ( I )
else :
q = n//2
r = n%2

A = matrixPower ( I , q )
A = m u l t i p l y s q u a r e m a t r i x (A, A, 2 )

i f r==1 :
A=m u l t i p l y s q u a r e m a t r i x (A, I , 2 )

return (A)

def f i b o n a c c i ( n ) :

I =[[1 ,1] ,[1 ,0]]

return ( matrixPower ( I , n ) )

def F s e r i e s ( n , x , y ) :
fib = fibonacci (n)

F = ( numberPower ( x+1, f i b [ 0 ] [ 1 ] )
∗numberPower ( y+1, f i b [ 0 ] [ 0 ] ) −1)%p

return (F)

while True :
try :
u s e r d a t a = int ( input ( ” Enter n : ” ) )
x = int ( input ( ” Enter x : ” ) )
y = int ( input ( ” Enter y : ” ) )
p = int ( input ( ” Enter p : ” ) )
except V a l u e E r r o r :
print ( ” I n t e r g e r r e q u i r e d . ” )
else :
break

print ( ” Value : ” , F s e r i e s ( u s e r d a t a , x , y ) )

4
3 Recursive Construction
1. Let us consider that we require less than N steps to check if there are any
1. That is we only search less than a bits,a < N , in the worst case all the
a bits could be 0 and the remaining N-a could have 1. So we could report
no 1 whereas there could be a 1 which is a contradiction.
Therefore we require exsctly N steps in the worst case.
2. If we that the ith bit in B(D) is 0, then the path from the root till the bit
will contain all 0’s.
Also the if there is 0 in B(D), then this 0 will be part of B which is a
substring of B(D).
From here we can conclude,

i = p1 n + r1

where is r1 is the position of 0 in B, and p1 is the position of 0 in B(D-1)


which generated the 0 at position i in B(D).
Repeating this process till D-1 times, number of distinct ri ’s will give us
the least number of zeros the can be there in B.
which is by solving,
 
rD−1
rD−2 
 
nD−2 . . . n2 n1 1  ... 
 D−1 
i= n
 
 
 r2 
r1

Distinct ri ’s in the integer solution to this linear equation with ri < n will
give us the least number of 0’s present in B.
3. If at ith position there is a 1 then B has atleast one 1 present.If this 1 had
0 at the parent position then remainder(i, n) will give us the position of
1 in B, or else there is 1 at position remainder(p1 , n) where, i = p1 n + r1
4. The minimum teams required will be 1 to have running time O(N ).
There will be N-positions to search to confirm if B(D) has a zero, given
D
−1
by i ∗ NN −1 for i = 0 − N − 1.

5
Algorithm 3 Find atleast one 0
found=0
for i=0 to N − 1 do
D
−1
if B(D)[i ∗ NN −1 ] == 0 then
found=found+1 break
end
end
if found == 1 then
Kings tomb found.
else
Kings tomb not found.
end

5. Minimum team required is 1 to achieve O(N ) running time.

Algorithm 4 Find atleast one 1


found=0
for i=0 to N − 1 do
if B(D)[i] == 1 then
found=found+1 break
end
end
if found == 1 then
Kings tomb present.
else
Kings tomb not present.
end

Das könnte Ihnen auch gefallen