Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to judge the relative efficiency of algorithms given runtimes as functions of 'n'?

Tags:

algorithm

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?

like image 870
Lopa Avatar asked Mar 23 '10 17:03

Lopa


People also ask

How do you evaluate the efficiency of an algorithm?

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.

How do you compare the efficiency of two algorithms?

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...

What do you understand by the efficiency of an algorithm?

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.

Which of the following is used to compare the efficiency of algorithms?

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.


3 Answers

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.

like image 163
Gumbo Avatar answered Oct 26 '22 08:10

Gumbo


I think plotting those functions would be very helpful to understand what's going on.

like image 23
Michael Haren Avatar answered Oct 26 '22 08:10

Michael Haren


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.

like image 21
Rob Lachlan Avatar answered Oct 26 '22 08:10

Rob Lachlan