Just reading this book for fun, this isn't homework.
However I am already confused on the first main assignment:
1-1 Comparison of running times
For each function f(n) and time t in the following table, determine the largest size n of a problem that can be solved in time t, assuming that the algorithm to solve the problem takes f(n) microseconds.
What does this even mean?
The next table shows a bunch of times along one axis (1 second, 1 minute, one hour, etc), and the other axis shows different f(n) such as lg n, sqrt(n), n, etc.
I am not sure how to fill in the matrix because I can't understand the question. So if f(n) = lg n, it's asking the largest n that can be solved in, for example, 1 second, but the problem takes f(n) = lg n microseconds to solve? What does that last part even mean? I don't even know how to set up the equations / ratios to solve this problem because I literally can't even put together the meaning of the question.
My hangup is over the sentence "assuming that the algorithm to solve the problem takes f(n) microseconds" because I don't know what this refers to. The time for what algorithm to solve what problem takes f(n) microseconds? So if I call f(100) it'll take lg 100 microseconds? So I need to find some n where f(n) = lg n microseconds = 1 second?
Does this mean lg n microseconds = 1 second when lg n microseconds = 10^6 microseconds, so n = 2^(10^6)?
For each time T, and each function f(n), you are required to find the maximal integer n such that f(n) <= T
For example, f(n) = n^2, T=1Sec = 1000 ms:
n^2 <= 1000
n <= sqrt(1000)
n <= ~31.63 <- not an integer
n <= 31
Given any function f(n), and some time T, you are required to similarly find the maximal value of n, and fill in the table.
I will do the first two as an example to help you do the rest. Since a second is 10^6 microseconds. By solving an equation which relates f(n) to the time we are plotting for f(n) to run we can solve for the largest input n that f can run on within the time limit.
1 second:
log(n2)=1,000,000⟹n2=e1,000,000⟹n=e500,000
1 minute:
log(n2)=60,000,000⟹n2=e60,000,000⟹n=e30,000,000
the rest can be similarly done.
P.S. make sure to floor the values of n you get from these equations because n is an integer length input.
Related
I am new to time complexities and asymptotic notation. I was looking at this video: https://www.youtube.com/watch?v=9TlHvipP5yA to figure out the time complexity of a piece of code shown below
The code concludes that the time complexity for the code below is O(Sqrt(n));
When I supply different n values, I am expecting Sqrt(n) # of outputs but this doesn't verify the O(Sqrt(n)) analysis. Can someone please explain why it is so?
For example if I have n = 10, I am expecting Sqrt(10) outputs which is ~ 3 outputs or 4 if you round up I guess. IS this illogical?
Thanks in advance.
p = 0;
for( int i = 0; p <= n; i++){
p = p + i;
System.out.println(p);
}
Big O is not used to compute the number of instructions you are expected to execute. For a given n, calculating the square root of n will not give you the exact number of times the instruction executes. Big O describes what happens with your function as input size gets very large. Analysis of your function when n is 10 or even 100 does not apply to Big O.
When we say a function's time complexity is O(sqrt(n)), we mean that the function belongs in a class of functions where the time required is proportional to the square root of the value of n, but only for very large values of n.
If you watch the video, the instructor simplifies the k(k+1) / 2 term to k^2 by taking the leading term, because the k term becomes insignificant compared to the k^2 term, when k is very large.
I'm trying to find the time complexity/ big theta of the following:
def f(n):
i = 2
while i <n:
print(i)
i = i*i
The only approach of how I know how to solve this is to find a general formula for i_k and then solve the equation of i_k >= n, however I end up with a log(logn/log2)/log(2) equation as my k value, and that seems awefully wrong to me and I'm not sure how I would translate that into a big theta expression. Any help would be appreciated!
That answer looks good, actually! If you rewrite log x / log 2 as log2 x (or lg x, for short), what you have is that the number of iterations is lg lg n. Since the value of i in iteration k of the loop is 22k, this means that the loop stops when i reaches the value 22lg lg n = 2lg n = n, which matches the loop bound.
More generally, the number of times you can square a value before it exceeds n is Θ(log log n), and similarly the number of square roots you can take before you drop a number n down to a constant is Θ(log log n), so your answer is pretty much what you’d expect.
given the following problem from CLRS algo book.
For each function f (n) and time t in the following table, determine
the largest size n of a problem that can be solved in time t, assuming
that the algorithm to solve the problem takes f(n) microseconds.
how can one calculate n for f(n)=nlog(n) when time is 1 second?
how can one calculate n for f(n)=n! when time is 1 second?
It is mentioned that the algorithm takes f(n) microseconds. Then, one may consider that algorithm to consist of f(n) steps each of which takes 1 microsecond.
The questions given state that relevant f(n) values are bound by 1 second. (i.e. 106 microseconds) Then, since you are looking for the largest n possible to fulfill those conditions, your questions boil down to the inequalities given below.
1) f(n) = nlog(n) <= 106
2) f(n) = n! <= 106
The rest, I believe, is mainly juggling with algebra and logarithmic equations to find the relevant values.
In first case, You can refer to Example of newtons method to calculate cube root Newton’s method to approximate the roots or Lambert W Function. It may help to calculate value of n. As per the my findings mostly there is no other analytical approach can help.
In second case, python script can help to calculate n with manual approch.
def calFact(n):
if(n == 0 or n==1):
return n
return n*calFact(n-1)
nVal=1
while(calFact(nVal)<1000000): # f(n) = n! * 10^-6 sec
nVal=nVal+1 # 10^6 = n!
print(nVal)
So in this case we are trying to find out n such that n! is equal to or near to 10^6.
For functions f(n) : n! , n^2 and n ..
If a problem can be solved in 1 second, given that the algorithm to
solve the problem takes f(n)microsecond.
I know for a fact n! = 9 in one second. but I don't know how this been calculated. Can someone explain to me how these functions were calculated?
From what I understand, you are being asked "when should I use which", should I use an algorithm that takes a constant time of 1 second? or should I use an algorithm that takes f(n) microseconds.
Note that 1 second = 10^6 microseconds, so you are solving:
f(n) <= 1,000,000
where n is natural.
By assigning f(10), you can see f(10) = 3,628,800 > 10^6.
But f(9) = 362880 < 10^6
So, for f(n)=n!, highest number that you want to use the f(n) algorithm is n=9.
Do similarly for other candidates and you will get your answer for them as well.
(Hint: solve the equation f(n) = 10^6, and have a look what happens in the near proximity of the n you found).
I know this is a simple question, but please bear with me. I am given a number of algorithm complexities (log(n), sqrt(n), n, ...) and I need to find the largest size n that that can be solved in a specified time (1 second, 1 minute, 1 hour, ...). The instructions also say "...assuming that the algorithm, to solve the problem takes f(n) microseconds)". Can anyone help me get started please? I already have the solution, I just do not know how to calculate it.
solution for log(n) =>
1 second = 2^(10^6)
1 minute = 2^(6 * 10^7)
1 hour = 2^(3.6 * 10^9)
While this is a horrible exercise, it's actually very easy. You are given a function f(n) and a value for that function. For example, 1 second means 10^6 microseconds. So basically you are asked to solve n for the following equations:
f(n) = 10^6, because 10^6us is 1 second
f(n) = 60 * 10^6, because 60 seconds is 1 minute
f(n) = 3600 * 10^6, because 3600 seconds is 1 hour
In your example f(n) is log(n). The short answer would be that if you have f(n) = k, then to find n you need the inverse function of f. In other words, n = f-1(k).
For f(n) = log(n), f-1(n) = 2^n.
You should now be able to figure the connections yourself and solve your homework for other f(n)s.