Monday, 10 November 2014

Algorithm Complexity

Basically for running time:
- Count the number of steps
- Constant factors doesn't matter
- And finally, only the highest-order term matters

So basically if an n2  and any other similar polynomials with different constant factors or anything that follows the n2 are in the same class of O(n2).

Now O(n2) is an asymptotic notation.
Then, O(f(n)) is an asymptotic upper-bound.
Which means, the set of functions that grows no faster than f(n).

Ω(f(n)) is the asymptotic lower-bound.

θ(f(n)) is the asymptotic tight-bound.

Now, on to the time complexity of a program.
Basically, every line (initialization, giving value to a field, etc) counts as one line. The tricky parts are when a program has loops or recursive functions inside the program. When taking the worst case for loops I was told to always take the last value of an array or something similar to it and therefore most loop's worst case takes n number of steps. Now when there is another loop inside the loop and you take the worst case, you basically assume the same thing which means the inside loop takes n number of steps and therefore the entire loop will take n*n number of steps. Afterwards, you add the other number steps that aren't in a loop and you have your time complexity of a program.

No comments:

Post a Comment