Question

In: Computer Science

4. Given Pseudocode: Algorithm3(n): y = 0 for i = 1 to n do y =...

4. Given Pseudocode:

Algorithm3(n):

y = 0

for i = 1 to n do

y = 1

while y < i2 do

y=2y

return y

a. What's the output of Algorithm3(n) as a function of n?

b. What's the running time of Algorithm3(n) as a function of n? Represent this as a summation.

c. Prove the running time to be O(n log n).

Solutions

Expert Solution

A) Look at the below Python code and its output.

def algorithm3(n):

  y = 0 # This will take constant time (assignment operation)

  for i in range(1,n+1): # This loop will run n times 
    y =1 # This will take constant time (assignment operation)

    while (y<(i*i)): # This loop will run logn times
      y = 2*y # This will take constant time (assignment operation)

  return y 

for i in range(1,21):
  print(algorithm3(i))

B) The first for loop will run n number of times and the subsequent while loop will run logn number of times.

So overall time complexity will be O(n)*O(logn) = O(nlogn).

C) When the iterator value of any loop gets divided or multiplied by a constant factor than its time complexity will always be logn. Beacuse in that case the iteration happens for that constant factor instead of normal unit value iteration.

Happy Learning!


Related Solutions

3. Given Pseudocode: Algorithm2(n): y = 0 for i = 10 to n + 10 do...
3. Given Pseudocode: Algorithm2(n): y = 0 for i = 10 to n + 10 do for k = i to n + 10 do y = y + k return y a. What's the output of Algorithm2(n) as a function of n? b. What's the running time of Algorithm2(n) as a function of n? c. Using Theta notation, describe the running time of Algorithm2(n) in the form of Θ(nC) for some C. d. Using Theta notation, describe the output...
Solve the following recurrence relation for the given initial conditions. y(n+2) - 0.3y(n + 1) + 0.02y(n) = 10 y(0) = 2; y(1) = 0
Solve the following recurrence relation for the given initial conditions.y(n+2) - 0.3y(n + 1) + 0.02y(n) = 10        y(0) = 2;    y(1) = 0
Given the differential equation y''+y'+2y=0,  y(0)=−1,  y'(0)=2y′′+y′+2y=0,  y(0)=-1,  y′(0)=2 Apply the Laplace Transform and solve for Y(s)=L{y}Y(s)=L{y}. You do not...
Given the differential equation y''+y'+2y=0,  y(0)=−1,  y'(0)=2y′′+y′+2y=0,  y(0)=-1,  y′(0)=2 Apply the Laplace Transform and solve for Y(s)=L{y}Y(s)=L{y}. You do not need to actually find the solution to the differential equation.
Solve with Laplace transform 1. y''+ 4 t y'− 4y = 0, y(0) = 0, y'(0)...
Solve with Laplace transform 1. y''+ 4 t y'− 4y = 0, y(0) = 0, y'(0) = −7 2. (1− t) y''+ t y' − y = 0, y(0) = 3, y'(0) = −1
find the general solution of the given differential equation. 1. y''+2y'−3y=0 2.  6y''−y'−y=0 3.  y''+5y' =0 4.  y''−9y'+9y=0
find the general solution of the given differential equation. 1. y''+2y'−3y=0 2.  6y''−y'−y=0 3.  y''+5y' =0 4.  y''−9y'+9y=0
solve the given boundary value problem. y"+6y=24x, y(0)=0, y(1)+y'(1)=0
solve the given boundary value problem. y"+6y=24x, y(0)=0, y(1)+y'(1)=0
la place transform of 1. y´´+4y´+6y_=0, y(0)=1, y´(0)=-4 2. y´+y=t^2 , y(0)=0
la place transform of 1. y´´+4y´+6y_=0, y(0)=1, y´(0)=-4 2. y´+y=t^2 , y(0)=0
Solve the given initial value problem. y'''+2y''-13y'+10y=0 y(0)=4    y'(0)=42 y''(0)= -134 y(x)=
Solve the given initial value problem. y'''+2y''-13y'+10y=0 y(0)=4    y'(0)=42 y''(0)= -134 y(x)=
Solve the given initial-value problem. y'' + y = 0,    y(π/3) = 0,    y'(π/3) = 4
Solve the given initial-value problem. y'' + y = 0,    y(π/3) = 0,    y'(π/3) = 4
Use simulation to prove that when X ∼ N(0, 1), Z ∼ N(0, 1), Y =...
Use simulation to prove that when X ∼ N(0, 1), Z ∼ N(0, 1), Y = X3 + 10X +Z, we have V ar(X +Y ) = V ar(X) +V ar(Y ) + 2Cov(X, Y ) and V ar(X −Y ) = V ar(X) + V ar(Y ) − 2Cov(X, Y ).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT