O time complexity
I have been doing some self-study on Big-O. I understand how to give examples of the following notations to algorithms:
O(N):
for(int i = 0; i < n; i++)
sum++;
O(N^2):
for(int i = 0; i < n; i++)
for( int j = 0; j < n; j++)
sum++;
O(N^3):
for(int i = 0; i < n; i++)
for( int j = 0; j < n * n; j++)
sum++;
I have come across these notations which I don't quite comprehend. How do I give examples of these in terms of algorithms?
Maybe I should phrase it this way: write an algorithm which takes running time in proportion to:
I fear you are misunderstanding "Big-O" notation.
A notation is not "expressed" as an algorithm. Rather, the Big-O notation describes a property of an algorithm.
So it's not "O(N) can be expressed as XXX", but rather "algorithm XXX has complexity of O(N)".
That said, it is quite reasonable to ask for examples of algorithms with a certain complexity; you already list some. To address your questions:
O(4^n) is the same as O(e^n), often written as O(exp(n)) (try to understand why it is the same). O(4^n) belongs to the class of algorithms with "exponential complexity" (EXPTIME). Many important problems in math/CS have exponential complexity (or nearly exponential complexity).
An algorithm with exponential complexity would be for example a naive solution to the discrete logarithm problem.
For the other complexities I cannot give an example, but you will probably find one by googling a bit.
The algorithms you have given are strictly speaking not Big-O but Theta. Big-O is an asymptotic upper bound meaning that on some worse case input the running time will be the one given but isn't for all inputs, where as Theta is a tight bound meaning that the running time will always be that. Consider for instance a sorting algorithm that is given an already sorted list as input, or a search algorithm where the things to be found is the first element in a list (how does binarysearch compare to linear search in this case).
链接地址: http://www.djcxy.com/p/6810.html上一篇: 大O符号比例因子问题
下一篇: O时间复杂度