Revisit time complexity

Time complexity: is a function that describes the running time, and shows the relation between the operating time and the size of the input.

Input Scale: Enter the size of a problem is the number of bits stored bit input data required

Time complexity measures the time that an algorithm takes as a function of the length in bits of its input


 

Such as O (n), where n represents the size of the input data

 

When we deal with some graph theory, linked lists, arrays, trees and other issues, this polynomial time at standard definition and our traditional polynomial time is almost the same.

However, when we deal with issues related to number theory, things are less optimistic.


 

The determination whether a number is prime, the number of the input number, the number required log number  binary bits to store, so that k = log number

If the number is 8, then k is 3. When k becomes 1 after 4 increases, the number became 16

That is, by definition, the size of the input time complexity is k, rather than the number. Such primes seek time complexity is 2 ^ k, that is O (2 ^ n)


 

 

01 knapsack problem, provided backpack size w, there are n items.

Seems complexity O (w * n) dp solution with time. However, there are problems w of the above examples, in fact, the time complexity is: O (n * 2 ^ logw)


 

The above two examples of time complexity is pseudopolynomial (pseudo-polynomial)

Reference article:

https://blog.csdn.net/zhan723284893/article/details/45014633

https://en.wikipedia.org/wiki/Time_complexity

https://stackoverflow.com/questions/3907545/how-to-understand-the-knapsack-problem-is-np-complete

Guess you like

Origin www.cnblogs.com/liangwenhan/p/11924766.html