Professional Documents
Culture Documents
Assignment 1: 1 Asymptotic Complexity
Assignment 1: 1 Asymptotic Complexity
Assignment 1: 1 Asymptotic Complexity
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.
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
3.
f ib(n + 1) = f ib(n) + f ib(n − 1)
f ib(n) = f ib(n)
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
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)
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 ) :
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
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