how does IF affect complexity?

百般思念 提交于 2019-11-29 10:41:44

O(2n) = O(n). Generalizing, O(kn) = O(n), with k being a constant. Sure, with two IFs it might take twice the time, but execution time will still be a linear function of input size.

Edit: Here is an explanation, with examples, of the big-O notation which is not too mathematic-oriented

Asymptotic complexity (which is what big-O uses) is not dependent on constant factors, more specifically, you can add / remove any constant factor to / from the function and it will remain equivalent (i.e. O(2n) = O(n)).

Assuming an if-statement takes a constant amount of time, it will only add a constant factor to the complexity.

A "constant amount of time" means:

  • The time taken for that if-statement for a given element is not dependent on how many other elements there are in the array
  • So basically if it doesn't call a function which looks through the other elements in the array in some way or something similar to this
  • Any non-function-calling if-statement is probably fine (unless it contains a statement that goes through the array, which some language allows)

Thus 2 (constant-time) if-statements called for each each element will be O(2n), but this is equal to O(n) (well, it might not really be 2n, more on that in the additional note).

See Wikipedia for more details and a more formal definition.

Note: Apart from not being dependent on constant factors, it is also not dependent on asymptotically smaller terms (terms which remain smaller regardless of how big n gets), e.g. O(n) = O(n + sqrt(n)). And big-O is just an upper bound, so saying it is O(n9999) would also be correct (though saying that in a test / exam will probably get you 0 marks).

Additional note: The problem when not ignoring constant factors is - what classifies as a unit of work? There is no standard definition here. One way is to use the operation that takes the longest, but determining this may not always be straight-forward, nor would it always be particularly accurate, nor would you be able to generically compare complexities of different algorithms.

It's related to a question I posted myself today.

In your example it depends on whether you can jump from the first to the last element and if you can't then it also depends on the average length of each entry.

If as you went down through the array you had to read each full entry in order to evaluate your two if statements then your order would be O(1,000,000xN) where N is the average length of each entry. IF N is variable then it will affect the order. An example would be standard multiplication where we perform Log(N) additions of an entry which is Log(N) in lenght and so the order is O(Log^2(N)) or if you prefer O((Log(N))^2).

On the other hand if you can just check the first and last character then N = 2 and is constant so can be ignored.

This is an IMPORTANT point you have to be careful though because how can you decide if your multipler can be ignored. For example say we were doing Log(N) additions of a Log(N/100) number. Now just because Log(N/100) is the smaller term doesn't mean we can ignore it. The multiplying factor cannot be ignored if it is variable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!