Consider two algorithms, A, and B. These algorithms both solve the same problem, and have time complexities (in terms of the number of elementary operations they perform) given respectively by
a) (n) = 9n+6
b) (n) = 2(n^2)+1
(i) Which algorithm is the best asymptotically?
(ii) Which is the best for small input sizes n, and for what values of n is this the case? (You may assume where necessary that n>0.)
I think it's A. Am I right?
And what's the answer for part B? What exactly do they want?
The two main measures for the efficiency of an algorithm are time complexity and space complexity, but they cannot be compared directly. So, time and space complexity is considered for algorithmic efficiency. An algorithm must be analyzed to determine the resource usage of the algorithm.
By measuring both the time complexity (which defined is the time needed to execute all instructions) and the space needed for each algorithm. You can also use the amount of space it takes up in bits...
Overview. An algorithm is considered efficient if its resource consumption, also known as computational cost, is at or below some acceptable level. Roughly speaking, 'acceptable' means: it will run in a reasonable amount of time or space on an available computer, typically as a function of the size of the input.
To express the time complexity of an algorithm, we use something called the “Big O notation”. The Big O notation is a language we use to describe the time complexity of an algorithm. It's how we compare the efficiency of different approaches to a problem, and helps us to make decisions.
Which algorithm is the best asymptotically?
To answer this question, you just need to take a look at the exponents of n in both functions: Asymptotically, n2 will grow faster than n. So A ∈ O(n) is asymptotically the better choice than B ∈ O(n2).
Which is the best for small input sizes n, and for what values of n is this the case? (You may assume where necessary that n>0.)
To answer this question, you need to find the point of intersection where both functions have the same value. And for n=5 both functions evaluate to 51 (see 9n+6=2(n^2)+1 on Wolfram Alpha). And since A(4)=42 and B(4)=33, B is the better choice for n < 5.
I think plotting those functions would be very helpful to understand what's going on.
You should probably start by familiarizing yourself with asymptotics, big O notation, and the like. Asymptotically, a will be better. Why? Because it can be proven that for sufficiently large N, a(n) < b(n) for n> N.
Proof left as an exercise for the reader.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With