4

I was going through this link :
http://www.geeksforgeeks.org/horners-method-polynomial-evaluation/

Here it says that the time complexity using normal method is O(n^2).But I wonder how?Here is my analysis about this:

Suppose we have an equation like:2x^2 + x + 1
Here the for loop will be executed 3 times i.e.(order+1)times

    for(int i = order ; i>=0 ; i++){
         result = result + (mat[i] * coefficient^i);
    }

So according to this ,the time complexity should be O(n+1) i.e. O(n).Why does it say that its O(n^2)?I'm getting a little lost here.Even a hint would do.

OmG
  • 18,337
  • 10
  • 57
  • 90
Reckoner
  • 1,041
  • 2
  • 13
  • 29

3 Answers3

2

Well it might not be so obvious but some primitive operations have different time complexity. Computer is very fast in computing the result of + operation, * - is slower, % - is even more slower. These operations are evaluated on hardware so they take constant number of processor ticks.

But ^ operation is not so simple. coefficient^i complexity is not O(1)/constant. Trivial way to calculate the result is to multiply the coefficient i number of times. This will be done in O(i) time. Another way is to use binary exponentiation method that gives faster O(log(i)) time.

Why does it say that its O(n^2)?

You have n members in polynomial, you spend O(n) time to calculate the result of the exponentiation operation for each of them. That is why it is O(n2).

Ivan Gritsenko
  • 4,166
  • 2
  • 20
  • 34
1

The link suggests that a naive way to evaluate coefficient^i would be multiplying coefficient i times. That is O(i). Repeating this for all exponents from 0 to n would be O(n^2)

A naive way to evaluate a polynomial is to one by one evaluate all terms. First calculate x^n, multiply the value with cn, repeat the same steps for other terms and return the sum. Time complexity of this approach is O(n^2) if we use a simple loop for evaluation of x^n.

It also claims that if you use a better algorithm to compute coefficient^i, say, exponentiation by squaring, that has O(log n) complexity, it would have a lower total complexity (O(n log n)), but Horner's method would be even better than that.

Juan Lopes
  • 10,143
  • 2
  • 25
  • 44
  • As per me,you are correct.The time complexity of calculating power operation will be O(n) if we use divide and conquer.So the overall complexity for the above code will be O(n^2).But then why does it say here that the direct method also has a complexity of O(n).Check this link(Section algebraic functions) https://en.wikipedia.org/wiki/Computational_complexity_of_mathematical_operations – Reckoner Mar 20 '16 at 13:33
0

Yes, you are right. The form you show is often called Horner's Method. It is linear in the sense that the number of elementary operations (additions, multiplications) is O(n), where n is the highest coefficient.


Incidentally, your code above seems to contain a mistake. It probably should be

for(int i = order ; i>=0 ; i--){

(the original is an infinited loop). Otherwise, it can be considered an implementation of Horner.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185