Il existe plusieurs -Notations, comme ou et ainsi de suite. Je me demandais s'il y avait des variations de celles en réalité comme or , or if those are mathematically incorrect.
Or would it be a right thing to say that it is possible to improve a to a ? I can't and don't need to figure out runtimes yet and I do not need to improve anything, but I'd need to know if this how you describe your functions in reality.
Réponses:
Yes,O(2n2) or O(log(n2)) are valid variations.
However, you will see them rarely if you would see them at all, especially in the end results. The reason is thatO(2n2) is O(n2) . Similarly, O(log(n2)) is O(logn) . That might be surprising to beginners. However, those equalities are more or less the very reason why big O -notations were introduced, to hide a multiplicative constant factor that is often hard to pin down and relatively insignificant.
It is not an improvement at all if the time-complexity of an algorithm is changed fromO(5n2) to a O(3n2) or from Ω(5n2) to Ω(3n2) , because O(5n2) is O(3n2) while Ω(5n2) is Ω(3n2) . So it is incorrect to say the time-complexity is improved from O(5n2) to O(3n2) . It is correct to say the time-complexity of an algorithm is improved from 5n2 to 3n2 , of course.
Exercise 1. Show thatO(5n2)=O(3n2)=O(n2) .
Exercise 2. Show thatO(logn)=O(log(n2)) .
Exercise 3. Show thatΩ(n2+n)=Ω(n2) .
la source
You are always free to not use this notation at all. That is, you can determine a functionf(n) as precisely as possible, and then try to improve on that. For example, you might have a sorting algorithm that makes f(n) comparisons, so you could try to come up with another sorting algorithm that only does g(n) comparisons. Of course, all kinds of functions f(n) exist (in theory) and can also come up (in practice).
Instead of treating the Big Oh notation as mysterious magic where you have to consult wizards to ask whether you can do something, you should look at its definition. Respect the definition, and then do whatever you need to get your job done.
la source
While the accepted answer is quite good, it still doesn't touch at the real reason whyO(n)=O(2n) .
Big-O Notation describes scalability
At its core, Big-O Notation is not a description of how long an algorithm takes to run. Nor is it a description of how many steps, lines of code, or comparisons an algorithm makes. It is most useful when used to describe how an algorithm scales with the number of inputs.
Take a binary search, for example. Given a sorted list, how do you find an arbitrary value inside it? Well, you could start at the middle. Since the list is sorted, the middle value will tell you which half of the list your target value is in. So the list you have to search is now split in half. This can be applied recursively, then going to the middle of the new list, and so on until the list size is 1 and you've found your value (or it doesn't exist in the list). Doubling the size of the list only adds one extra step to the algorithm, which is a logarithmic relationship. Thus this algorithm isO(logn) . The logarithm is base 2, but that doesn't matter - the core of the relationship is that multiplying the list by a constant value only adds a constant value to the time.
Contrast a standard search through an unsorted list - the only way to search for a value in this case is to check each one. Worst-case scenario (which is what Big-O specifically implies) is that your value is at the very end, which means for a list of sizen , you have to check n values. Doubling the size of the list doubles the number of times you must check, which is a linear relationship. O(n) . But even if you had to perform two operations on each value, some processing, for example, the linear relationship still holds. O(2n) simply isn't useful as a descriptor, since it would describe the exact same scalability as O(n) .
I appreciate that a lot of these answers are basically telling you to come to this conclusion yourself by reading the definition of Big-O. But this intuitive understanding took me quite a while to wrap my head around and so I lay it out to you as plainly as I can.
la source
You can writeO(f) for any function f and it makes perfect sense. As per the definition, g(n)=O(f(n)) if there is some constant c such that g(n)≤cf(n) for all large enough n . Nothing in that definition says that f must be some sort of "nice" function.
But, as other answers have pointed out,g(n)=O(f(n)) and g(n)=O(2f(n)) describe exactly the same situation: if g(n)≤cf(n) for all all large enough n , then we also have g(n)≤c22f(n) , so g(n)=O(2f(n)) , also (taking the constant to be c/2 ).
As a side issue, don't write "logn2 ", because it's not 100% clear what it means. You could say that it obviously means log(n2) but almost everybody would write that as 2logn , so it puts doubt in the reader's mind.
Also, note that big-O notation has nothing to do with runtimes per se. It's just a notation for relationships between functions. Those functions are often used to measure the runtimes of algorithms but that's just one application, just like measuring people's heights is just one application of numbers.
la source
Look at the definition of O(f(n)), and you see that for example O(2n^2) and O(n^2) are exactly the same. Changing an algorithm from 5n^2 to 3n^2 operations is a 40 percent improvement. Changing from O(5n^2) to O(3n^2) isn’t actually any change, they are the same.
Again, read the definition of O(f(n)).
la source
It may be helpful to understand that Big-O describes a set of functions. That isO(f(n))={g(n)|∃n,c>0:∀m>n:c×g(m)≤f(m)}
The usage of= is kind of unfortunate and using ∈ would make that relationship a lot clearer. but the set notation symbols are a bit difficult to type so now we are stuck with the current convention.
This then shows thatO(n)=O(2n) Or that constant factors don't matter when defining the Big O.
la source