























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























We want to understand how the performance of an algorithm responds to changes in problem size. Basically the goal is to provide a qualitative insight on number of operations for a problem size of n elements. The total number of operations required by an algorithm can be described through a mathematical expression in n. The Big-O notation is a way of measuring the order of magnitude of a mathematical expression O(n) means of the Order of n Consider _n 4
2 _n 2
The constant term has no significance, and the complexity can be finally expressed as f(n) = O (n 2 ) Incidentally we can also say f is big -O not only of n 2 but also of n 3 , n 4 , n 5 etc (HOW ?) The complexity of an expression O (3n 4
is evaluated by first dropping the insignificant terms, to yield O( 3n 4 ) and then dropping the constant factor to get O( n 4 ) Linear search Algorithm: Best Case - It’s the first value “order 1,” O(1) Worst Case - It’s the last value, n “order n,” O(n) Average - N/2 (if value is present) “order n,” O(n)
Order Notation O(1) or “Order One”: Constant time does not mean that it takes only one operation does mean that the work doesn’t change as n changes is a notation for “constant work” O(n) or “Order n”: Linear time does not mean that it takes n operations does mean that the work changes in a way that is proportional to n is a notation for “work grows at a linear rate ” O( n 2 ) or “Order n 2 ”: Quadratic time O( n 3 ) or “Order n 3 ”: Cubic time Algorithms whose complexity can be expressed in terms of a polynomial of the form _amn m
Some algorithms even take less time than the number of elements in the problem. There is a notion of logarithmic time algorithms. We know 10 3 = Taking log of both sides to base 10 , we can write it as log 10 1000 = 3 Similarly suppose we have 2 6 = Taking log of both sides to base 2 log 2 64 = 6 If the work of an algorithm can be reduced by half in every step, and in k steps we are able to solve the problem then, k and n can be related through 2 k = n or in other words k = log 2 n This algorithm will be having a logarithmic time complexity, usually written as O(ln n) or O(lg n).
Since the loop variable is cut in half each time through the loop, the number of times the statements inside the loop will be executed is log 2 n. Thus, an algorithm that halves the data remaining to be processed on each iteration of a loop will be an O( log 2 n ) algorithm. There are a large number of algorithms whose complexity is O( n log 2 n ). Remember that O(n log 2 n ) will be much less than O( n 2 ). Finally note that in computer science, there are some algorithms whose efficiency is dominated by a term of the form a n . These are called exponential algorithms , and are of order O( 2 n ). They are of more theoretical rather than practical interest because they cannot reasonably run on typical computers for even for moderate values of n.
logarithms
b
rd
Comparison of N , logN and N 2
Complexity related Problems
b
To find log 2 16 use 2 b = 16 This gives b = 4. To find log 2 64 use 2 b = 64 This gives b = 6. log 16 log ------------ = ------------=> 28 ms x 4 6 ------------ = ------------ 28 ms x => x = 42 ms
5.What is the computational complexity of the following algorithm? sum = 0; for(j= 10; j <= N; j++){ sum = sum + j ; } Again it is one operation per loop, but now the loop does not start from 1. We can express this as a summation n
j= What is the value of sum when the loop is finished? n
j=
6. What is the computational complexity of the following algorithm?
sum = 0; for(j=1; j <= N; j++){ for(k=1; k <= j; k++) { sum = sum + j * k; } } Two operations are carried out each time the “k” loop is executed. The k loop runs for “j” times. “j” loop runs N times For N = 1 , j loop runs 1 time and k loop runs 1 time For N = 2 , j loop runs 2 times and k loop runs 1+2 times For N = 3 , j loop runs 3 times and k loop runs 1+2+ times For N = 4 , j loop runs 4 times and k loop runs 1+2+3+ times For any value of N, the number of operations are
grandTotal = grandTotal + rows[k]; } How many addition operations are involved in this case? n n
k=1 j= 8.What is the computational complexity of the following algorithm? sum = 0; for(j=1; j <= N; j++){ for(k=1; k <= j; k++) { for(m=1; m <= j; m++) { sum = sum + a(m,k);
Where a(m,k) is some combination of the running variables m and k. Let us say computation of the sum each time in the innermost loop involves 4 steps. Since the two
2 operations , the overall complexity of the algorithm can be expressed as the summation n
2 j=
Some complexity analysis problems can be decomposed into subproblems having a mathematical pattern, which can