Question

Given the following code, find their big(O) for I in range of n:          for j...

  1. Given the following code, find their big(O)

for I in range of n:

         for j in range of n:

                 for k in range of 10000:

                                print(“test”)

  1. If an algorithm takes n3+1000n2+1000n2+9999 time, what is the Big O for this algorithm?
  1. Proof: O(nK) < O (2n), need to do some research on polynomial time and exponential time.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

lets understand some basic layman points about any program or algorithms of time complexity:

while analyzing the time complexity for any program or algorithm you should keep in mind that when we use asymptotic notations to analyze or find complexity(time or space)  we do not exactly calculate the time(or space )taken by that particular program to execute, rather we always try to estimate the maximum or minimum amount of time or space that could be(approximately) consumed by the program.

while estimating the complexity of the program using asymptotic notations you should remember that only loops and function calls contribute to that complexity , any operation happening outside loop or recursion does not contribute to the complexity we are estimating and hence we can safely ignore them as they are considered to be constant amount of time taking operations.

when to add or multiply times taken by individual loops and functions:-

whenever you see nested loops you need to multiply their individual times.

If there are multiple loops inside a function you add all the times individually for each loop.

In case of recursive function calls you count number of total function calls and add their times up.

Order of an exponential function depends upon both, base and multiplication factor of exponent.

After adding the individual times of independent loops or functions we may get a mathematical expression(or function) with different exponents of a variable, in that case since we are estimating the complexity(not calculating exact time) ,we find the term which is dominating in that expression means find the term having highest exponent or the term which is contributing to the sum most, we pick that term up and ignore all other terms. the term you picked will now decide the complexity of your program.

note- these are just some layman terms which help while analyzing algorithms and programs. they may not be correct all the times but those cases are very rare and you can easily remember them as some strong and general concepts.

now i can answer the questions you asked:-

first question-

there are three nested loops hence you multiply their number of times they are executing.

and you get 10000n2  . to find big-Oh notation we ignore the constant associated with the variable.so the upper bound time for the program is O(n2).

second question-

n3+1000n2+1000n2+9999.

this is a polynomial expression in terms of variable n, here to find big-Oh of this expression you catch the term which is dominating in the sum and you can clearly see the n3  is the term with highest power so this is dominating in the sum.

so the upper bound time for the algorithm describes by this expression will be the order of n3 and will be represented as O(n3)

don't worry about some big constants associated with lower power of n (viz. 1000). because after a specific value of n they won't make n2 bigger than n3 . also associating some constant (c ) with n3 it will already be bigger than all those other terms since in asymptotic analysis we consider only variable's (input size which the time depends upon) order and not the constants as they can be compensated by some other constants by using appropriate operations. ( would recommend revise the asymptotic analysis definitions and concepts). most importantly constants will always be constants, it is the input size (to which i was referring as variables, that is n in this case) which the program depends upon and they have the capability to take the program time to any direction that's why we only consider them in our estimated analysis as they are the only big fishes in the pond of  numerous small creatures.

third question-

dive deep in some basic maths and you would easily understand that.

i have learnt this from various other sources.

Add a comment
Know the answer?
Add Answer to:
Given the following code, find their big(O) for I in range of n:          for j...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q-1: Given the following code fragment, what is its Big-O running time? test = 0 for...

    Q-1: Given the following code fragment, what is its Big-O running time? test = 0 for i in range(n):             for j in range(n):                         test= test + i *j Q-2: Given3 the following code fragment what is its Big-O running time? for i in range(n):             test=test+1 for j in range(n):             test= test - 2 Q-3: Given the following code fragment what is its Big-O running time? i = n while i > 0:             k=2+2             i...

  • Analyze the following code and provide a "Big-O" estimate of its running time in terms of...

    Analyze the following code and provide a "Big-O" estimate of its running time in terms of n. Explain your analysis. Assume k is a constant given by the problem. for (i=1; i<=n; i++)      p = pow(i,k); // p = i to the power of k      for (j=1; j<=p; j++)           Some O(1) work      end for   end for

  • For each code write the time complexity. For each of the following pieces of code, write...

    For each code write the time complexity. For each of the following pieces of code, write down the time complexity that the code will run in, choosing from O(1), O(log n), O(n), O(n log n), O(n^2): def something (n) for i in range (n) return n Big-O:_____ for i in range (n) for j in range (5) print (i*j) Big-O:______ for i in range (n) for j in range (n n/3, 9): print (i*j) Big-O:_____ for i in range (521313*2213*11);...

  • Show how to get the big-Oh for the following code: void CountSort (int A[N], int range)...

    Show how to get the big-Oh for the following code: void CountSort (int A[N], int range) { // assume 0 <= A[i] < range for any element A[i] int *pi = new int[range]; for ( int i = 0; i < N; i++ ) pi[A[i]]++; for ( int j = 0; j < range; j++ ) for ( int k = 1; k <= pi[j]; k++ ) cout << j << endl; }

  • pointsConsider this code def f (n,p): for i in range(n): for j in range(i,p): for k...

    pointsConsider this code def f (n,p): for i in range(n): for j in range(i,p): for k in range(n*p): dosomething(i,j,k) + dosomething(j,i,k) Write down the number of calls to dosomething T(n, p) in summation notation

  • 1 question) Arrange the following in the order of their growth rates, from least to greatest:...

    1 question) Arrange the following in the order of their growth rates, from least to greatest: (5 pts) n3                     n2         nn        lg n     n!       n lg n              2n                     n 2 question)Show that 3n3 + n2 is big-Oh of n3. You can use either the definition of big-Oh (formal) or the limit approach. Show your work! (5 pts.) 3 question)Show that 6n2 + 20n is big-Oh of n3, but not big-Omega of n3. You can use either the definition of big-Omega...

  • Using C++ please explain What is the Big-O time complexity of the following code: for (int...

    Using C++ please explain What is the Big-O time complexity of the following code: for (int i=0; i<N; i+=2) { ... constant time operations... Select one: o a. O(n^2) O b. O(log n) c. O(n) O d. 0(1) What is the Big-O time complexity of the following code: for(int i=1; i<N; i*=2) { ... constant time operations... Select one: O O a. O(n^2) b. 0(1) c. O(n) d. O(log n) O What is the Big-O time complexity of the following...

  • PYTHON: Im stuck here, big O notation and runtime. What is it and Why are they...

    PYTHON: Im stuck here, big O notation and runtime. What is it and Why are they those? Please look at the pic, need help as Im confused. Thank You! def method3(n): for i in range(n): for j in range(100): for k in range(n): print(i+j+k) What is the runtime (tightest/closest bound in terms of O) for the above python function (method 3)? Please briefly explain. Enter your answer here def method4(n): for i in range(n): for j in range(n, o, -2):...

  • Determine the Big-O value for the following functions. The valid Big-O options are: 'O(1)' 'O(N)' 'O(LOG2(N))'...

    Determine the Big-O value for the following functions. The valid Big-O options are: 'O(1)' 'O(N)' 'O(LOG2(N))' 'O(N * LOG2(N))' 'O(N^2)' 'O(inf)' # This is Big-O of infinity def function_one(list_): for pass_ in range(len(list_) - 1, 0, -1): for i in range(pass_): if list_[i] > list_[i+1]: temp = list_[i] list_[i] = list_[i+1] list_[i+1] = temp def function_two(list_): if len(list_) > 1: midpoint_index = len(list_) // 2 left_half = list_[:midpoint_index] right_half = list_[midpoint_index:] function_two(left_half) function_two(right_half) left_half_pos = 0 right_half_pos = 0 new_position...

  • In Big-Θ notation, analyze the running time of the following pieces of code/pseudo-code. Describe the running...

    In Big-Θ notation, analyze the running time of the following pieces of code/pseudo-code. Describe the running time as a function of the input size (here, n) for(int i=n-1; i >=0; i--){ for(int k=0; k < i*n; k++){ // do something that takes O(1) time } }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT