Is my interpretation of this question about runtime algorithms correct?

vaderboi

Junior Member
Joined
Jan 18, 2019
Messages
59
In my textbook, here is the question:

2.11: An algorithm takes 0.4 ms for input size 150. How long will it take for input size 600 if the running time is the following (assume low-order terms are negligible)?
a. linear
b. O(N log N)
c. quadratic
d. cubic

The way I interpreted the first question is that I would just solve for i * c. i represents the input size, c represents the constant which is 0.4ms. Because of this, is it true that I can safely establish this?:

f(150) = i * c

And if this follows, for the linear run-time i = x. For the quadratic run-time i = x^2. For the cubic run-time i = x^3.

Does my logic follow?

Note: For question (b), O represents that the equation is written in big-Oh notation. O is not a variable.
 
I don't think you really mean what you said, that i = input size, and for quadratic, i = x^2. What is x?? Why would input size be a function of that? And for f(150), why didn't you replace i with 150? (No, the constant is not 0.4.)

What I think you really meant is correct. If x = input size, then linear means that time is proportional to x, quadratic means that time is proportional to x^2, and so on. For O(n log n), you can take that to mean that time is proportional to x log(x). You'll have to determine what base they mean, but it turns out not to matter.

Carry out your plan, and show your results so we can see if you are doing it right.

But I have to make a comment about the problem. Big-O only deals with asymptotic behavior, so you can't really know anything about what happens for x=150 or 600! The problem is simplifying a number of things, by telling you to assume low-order terms are negligible. In reality, you never know how big is big enough to make such an assumption.
 
Thank you for the prompt response. I have some questions still, though.

1) Why wouldn't the constant be 0.4ms? That's how long it takes for the compiler to run an algorithm that has an input size of 150. Wouldn't 0.4ms be multiplied by the input size? (i.e. 0.4ms * x, 0.4ms * x^2, ect..). I used i as a variable to be a stand in for any function of x, whether that function be linear, quadratic, O(n log n), cubic, ect.. i could be x, x^2, x^3, ect..

2) What do you mean Big-O deals with asymptotic behavior? I know that big-Oh notation is meant to determine how a function grows by comparing it to another function. It terms of Big-O measuring asymptotic behavior, are you referring to the fact that for a big-Oh relationship to be true you just have to find one constant C and k that make the relationship always true? That you just need to find one point at which the relationship will continue to be true forever (whether that be the minimum y-value or a higher y-value)?

3) Yeah, the question is pretty vague by what it means by low-order terms. But isn't it, in actuality, possible to know exactly how long the program containing the algorithm runs? In Unix, I can use a time() function that measures how long any given program runs. The time() function measures the time that it takes for the program to finish running with three metrics: user, sys, and real. real measures the total time it takes for the program to run but it also includes how long it took for other processes running on the operating system to finish. user and sys specifically measure how long it takes for the program to run. If I add user and sys, I will get a good estimate of how long it took the program to finish running. In this context, I am given a concrete amount of time that the algorithm takes to run given a specific input. That means I could figure out a pretty definitive answer by running it in the terminal, right?
 
a. linear Writing "Ic" doesn't make any sense at all when you haven't said what "i" and "c" are! If you mean that "i" is "input size" and "c" is a constant, then, yes, "linear" means that the running time is "ic" or just "ci". I would NOT write "f(150)= ic" because 150 is a particular value for i. Instead f(i)= ic or f(150)= 150c= 0.4. So what is "c" and what is f(600)?

b. O(N log N) This means that taking "N" as input size and using "T" as running time, T= c N log(N) for some constant, c. 0.4= c (150) log(150). Now what is c and what is T when N= 600?

c. quadratic This means that, again with "N" as input size and using "T" as running time, T= cN^2. 0.4= c(150)^2. What is c and what is T when N= 600?

d. cubic This means that, again with "N" as input size and using "T" as running time, T= cN^3. 0.4= c(150)^3. What is c and what is T when N= 600?
 
1) Why wouldn't the constant be 0.4ms? That's how long it takes for the compiler to run an algorithm that has an input size of 150. Wouldn't 0.4ms be multiplied by the input size? (i.e. 0.4ms * x, 0.4ms * x^2, ect..). I used i as a variable to be a stand in for any function of x, whether that function be linear, quadratic, O(n log n), cubic, ect.. i could be x, x^2, x^3, ect..

Why WOULD the constant be 0.4 ms? Did you try carrying out your plan, as I recommended, to see if it works?

If you were right, then the formula for part (a) would be f(x) = 0.4x, right? But then f(150) = 0.4*150 = 60, but you are told it is 0.4 ms!

The constant (in the linear case) is the "rate" in ms/input: 0.4/150 = 0.002667. You're acting as if you were told the rate is 0.4 ms per input. No, that's the time for an input size of 150, not 1.

The right way to find the constant, as HallsofIvy showed, is to write the equation using the unknown constant, plug in the given numbers, and solve for the constant:

f(x) = cx​
f(150) = 150c​
150c = 0.4​
c = 0.4/150 = 0.002667​

As for what i means, you said explicitly "i represents the input size". You can't define a variable and then ignore what you defined it as! And then you didn't define what you meant by x, which is, presumably, "x represents the input size".

2) What do you mean Big-O deals with asymptotic behavior? I know that big-Oh notation is meant to determine how a function grows by comparing it to another function. It terms of Big-O measuring asymptotic behavior, are you referring to the fact that for a big-Oh relationship to be true you just have to find one constant C and k that make the relationship always true? That you just need to find one point at which the relationship will continue to be true forever (whether that be the minimum y-value or a higher y-value)?

Asymptotic behavior is what Big-O is all about, so I hope you were taught that: It is used for comparing how functions behave for very large values of the input. It does not say that the function has exactly a given form; it says that for large inputs you can ignore all terms but the one that dominates. Other terms are "negligible" (that is, they can be neglected, or ignored) only for those large inputs. So, for example, an asymptotically quadratic function, O(n^2), may actually be, say, f(x) = 4x^2 - 56x + 2000 (and may even have other non-polynomial terms tossed in). This function is like cx^2 only for very large values of x. (Try graphing f(x) and 4x^2 to see how they compare.) When x is, say, a million, x^2 will be a million times any of the other terms, so you can ignore them -- we say they are negligible.

Now, in your problem, they are telling you not to include any lower-order terms; apart from (b), it is not talking about Big-O at all, which is good. But a function that is O(n log n) might really be something like 5x log x - 5900x + sqrt(x+4) or whatever. In telling you to assume lower-order terms are negligible, they are saying that the function has the form c x log x. That's good for your purposes, because they want to keep things simple; I just wanted to remind you that they are simplifying the topic for you.

3) Yeah, the question is pretty vague by what it means by low-order terms. But isn't it, in actuality, possible to know exactly how long the program containing the algorithm runs? In Unix, I can use a time() function that measures how long any given program runs. The time() function measures the time that it takes for the program to finish running with three metrics: user, sys, and real. real measures the total time it takes for the program to run but it also includes how long it took for other processes running on the operating system to finish. user and sys specifically measure how long it takes for the program to run. If I add user and sys, I will get a good estimate of how long it took the program to finish running. In this context, I am given a concrete amount of time that the algorithm takes to run given a specific input. That means I could figure out a pretty definitive answer by running it in the terminal, right?

Actually, the question is not vague, unless you don't know what "low-order terms" means. It's explicitly telling you that they don't mean merely O(x^2), but actually cx^2 with no other terms.

But, no, it isn't possible in reality to know the exact formula for runtime. You can measure particular values, but you can't get a formula from such data. You can analyze simple algorithms theoretically and find an exact formula in principle, but any real algorithm would be far too complex. That's why we use Big-O!
 
So I think I have solved how long the running time would be for (a) if the input size is 600:

1.62 ms; We know that 150c = 0.4 which means that c approximately equals .0027. Since we know that the equation is linear, we know that .0027 * 600 = 1.62ms

Is my reasoning sound here? Because low-order terms are negligible I can assume that the linear equation can be represented by f(x) =cx without any other lower order terms?

I have a question still, though:

What do you mean that I can't find the exact formula for runtime? I was just asking if, in certain cases, it is possible to know the exact runtime for a certain algorithm given that using a time() function will give me an exact result? Or is it that because a program will never run the exact amount of time every time that you could never come up with an exact formula to represent the algorithm?
 
So I think I have solved how long the running time would be for (a) if the input size is 600:

1.62 ms; We know that 150c = 0.4 which means that c approximately equals .0027. Since we know that the equation is linear, we know that .0027 * 600 = 1.62ms

Is my reasoning sound here? Because low-order terms are negligible I can assume that the linear equation can be represented by f(x) =cx without any other lower order terms?

Yes, that's correct. You found c (as we showed you) using the given input size and running time, and then you used that in the function to find the running time for the other input size. And "linear, with no lower order terms" means f(x) = cx.

What do you mean that I can't find the exact formula for runtime? I was just asking if, in certain cases, it is possible to know the exact runtime for a certain algorithm given that using a time() function will give me an exact result? Or is it that because a program will never run the exact amount of time every time that you could never come up with an exact formula to represent the algorithm?

It is one thing to measure the actual runtime in one instance (or even in 1000 instances), and it is an entirely different thing to find a formula that gives the runtime in all instances. That is my point.

I could give you any finite set of values for a function, and you would not be able to determine what my function was, unless I also told you the form of the function (e.g. telling you the form is cx and giving you one pair, as in the problem). I wasn't talking about the running time being indeterminate; that's a different issue. It's just that functions are complicated; most don't have a formula at all, and there are infinite possibilities in writing formulas.

The whole discussion about running time of algorithms is about theoretical running time based on analysis of the algorithm. It has nothing to do with actually measuring something -- all the measurements you could take in a lab would not tell you the asymptotic behavior when the size goes beyond what you have measured (though you could make a good guess).
 
So would my assessment for these other problems be correct as well?:

c. 6.2 ms; 0.4ms = 150^2 * c; c approximately equals .000017; .000017 * 600^2 = 6.2ms.

d. 25.92ms; 0.4ms = 150^3 * c; c approximately equals .00000012; .00000012 * 600^3 = 25.92ms

(I haven't done (b) yet)

Okay, so you are saying that there is no way to give a formula that gives a runtime for all instances? And big-Oh notation is used to represent a growth rate relationship without giving any specifics? i.e. f(x) = O(g(x). This formula doesn't give us C or k or what f(x) or g(x) even equal. We definitely would be able to find values that would make this true, but there is no way to know the growth rate of every single possible combination. Is that what you are saying? That this "formula" can't give us exact runtimes for every possible instance since there are so many possible values of C, k, f(x) and g(x)?
 
No, I didn't say that formulas are impossible; I have previously said that, for simple algorithms, analysis of the algorithm can in principle provide an exact formula. But for any reasonable algorithm, that would be far more work than it is worth. And my main point here was that you can't find any formula by measuring.

Your work for (c) and (d) is correct. For (b), you'll be taking T = c x log(x); you'll have to find out whether in your textbook "log" refers to the base 10 log as in algebra courses, or very likely the base 2 log, as commonly used in this context.
 
I suppose my confusion is that I didn't know there was a difference between a formula and an algorithm. How are you defining formula and algorithm here?

And yeah, we are using base 2.

By the way, thank you for all of your help so far!
 
In this discussion, the algorithm is the thing you're timing. The formula is f(x).

More generally, a formula is an expression whose value gives some quantity you want to calculate; an algorithm is any method for doing something (which could include evaluating a formula, but can be much more complicated).
 
So the algorithm is the steps I am taking to figure something out and the formulas are what I am using to figure out that thing? In this case, the formulas would be the equations that I have come up with to determine c and the algorithm would be the steps I am taking to determine the runtime when the given input is 600?
 
Well, no. The algorithm we're talking about is the one in the question: "An algorithm takes 0.4 ms for input size 150." I haven't described anything else (such as your work) as an algorithm, though I suppose it could be. And the formula we've discussed was the formula for that algorithm's running time, which you called f.

Are you forgetting the context of the discussion?
 
I don't think I am forgetting it. I am just confused as to what the difference is and am trying to figure it out. So the question does not state what the algorithm is and I was supposed to figure out what the algorithm was for the question?
 
In my textbook, here is the question:

2.11: An algorithm takes 0.4 ms for input size 150. How long will it take for input size 600 if the running time is the following (assume low-order terms are negligible)?
a. linear
b. O(N log N)
c. quadratic
d. cubic
I don't think I am forgetting it. I am just confused as to what the difference is and am trying to figure it out. So the question does not state what the algorithm is and I was supposed to figure out what the algorithm was for the question?
The question says "an algorithm" because it doesn't matter at all what algorithm it is. (In fact, each part would have to refer to a different algorithm, since one couldn't have four different behaviors!)

You just have to use the given information to find the running time for size 600 (which you have done, except for part b).
 
Top