Presentation is loading. Please wait.

Presentation is loading. Please wait.

Big-O notation.

Similar presentations


Presentation on theme: "Big-O notation."— Presentation transcript:

1 big-O notation

2 Definition Big-O Notation is the most widely used method which describes algorithm complexity: the execution time required or the space used in memory or in disk by an algorithm Big O notation is used describe the rough estimate of the number of “steps” to complete the algorithm

3 Assume that doSomething() takes C steps to complete
Part 1: It does doSomething() So it takes constant C steps C is independent to the parameter n When the time complexity is independent to the parameter, big-O notation is O(1) Part 2: It does doSomething() n times Each time it takes C steps So in total it takes n*C steps to complete Then big-O is n*O(1) due to Part1 Important rule, a*O(n) = O(an) So the time complexity is n*O(1) is O(n) Part 3: It has two loops. Inner one is same as Part2. Outer loop does Part2 n times So the time complexity is n*O(n)=O(n2) Part 4: It takes exactly 1 step to return So time complexity O(1) void exampleBigO(int n) { // part 1 doSomething(); // part 2 for(int i=0; i<n; i++) // part 3 for(int j=0; j<n; j++) // part 4 return; }

4 Big-O for time complexity
So total time complexity for exampleBigO(int n) function is equal to sum of time complexities of all parts: O(1)+ O(n)+O(n2)+O(1) If the parameter n becomes a very large number Then Part 1 and Part 4 can be ignored because they are not dependent to the parameter n and spend very less time comparing to Part 2 and Part 3 When the parameter n becomes extremely large number, then Part 2 also can be ignored So when add up big-O notations, the notation with slower increase speed could be ignored by the notation with faster increase speed. So result big-O notation for exampleBigO() is O(n2)

5 Rules summary for big-O
O(1) * O(n) = O(n) O(n) * O(n) = O(n2) O(1) + O(n) = O(n) O(n) + O(n2) = O(n2) O(1) + O(n) + O(n2) = O(n2) Comparison of increase speed: O(1) < O(log(n)) < O(n) < O(nlog(n)) < O(n2)

6 Comparison of Algorithms
Often algorithm with smaller big-O notation is more efficient But it may not be correct for small scale of data But this rule will be efficient for very large number of data Because computer science is always dealing with large scale of data this rule is applicable

7 big-O space complexity
It is easier to understand using big-O to estimate space complexity as it is more concrete When number of elements of an array is constant C and which is independent to n, then the space complexity for using the array is Cn which is O(n)*O(C) = O(n)*O(1) =O(n) When comparing different algorithms, often compare how much ‘extra’ space complexity is needed to solve the problem The algorithm that needs an extra array of size n is not as good as the one which only needs two variables O(1) is more efficient then O(n)


Download ppt "Big-O notation."

Similar presentations


Ads by Google