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.
Related
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.
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.
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).
Could someone help me out with the time complexity analysis on this pseudocode?
I'm looking for the worst-case complexity here, and I can't figure out if it's O(n^4), O(n^5) or something else entirely. If you could go into detail into how you solved it exactly, it would be much appreciated.
sum = 0
for i = 1 to n do
for j = 1 to i*i do
if j mod i == 0 then
for k = 1 to j do
sum = sum + 1
First loop: O(n)
Second loop: i is in average n/2, you could have an exact formula but it's O(n²)
Third loop happens i times inside the second loop, so an average of n/2 times. And it's O(n²) as well, estimating it.
So it's O(n*n²*(1 + 1/n*n²)), I'd say O(n^4). The 1/n comes from the fact that the third loop happens roughly 1/n times inside the second one.
It's all a ballpark estimation, with no rigorous proof, but it should be right. You could confirm it by running code yourself.
for an algorithm time complexity analysis i need to know what is the result of the summation of the function n/i when i runs from 1 to logn, i saw somewhere trustable that is actually the harmonic sum, but i highly doubt it...
the function of the algorithm was originally T(n)=5T(n/5)+n/logn
this question was originally found at the Introduction to algorithms second edition book
save me! :)
http://faculties.sbu.ac.ir/~tahmasbi/DataStructure/Introduction%20to%20Algorithms-Cormen%20Solution.pdf
in page 58,there's a line say:
=n*sigma n/i where i goes from 1 to logn
=n*sigma 1/i where i goes from 1 to logn
thats the only part i have problem with.... cause all they did was taking that n out of the sigma, but where is it gone to? why is it true to just make it disappear?
as opposed to what they're saying i think it should be like:
=n*sigma n/i where i goes from 1 to logn
=n*n*sigma 1/i where i goes from 1 to logn
=n^2*sigma 1/i where i goes from 1 to logn
it is indeed harmonic sum:
n/1 + n/2 + ... + n/logn = n* [ 1 + 1/2 + ... + 1/logn] = n*H(logn) where H(logn) is the logn harmonic number.
the book uses H(m) = theta(logm) and because of this, H(logn) = theta(loglogn). so at overall we get: T(n) = n*H(logn) = n*theta(loglogn) = theta(n*loglogn).