Docsity

Computing Complexity of Algorithms - Lecture Notes | COP 3502, Study notes of Computer Science

Partial preview of the text

Download Computing Complexity of Algorithms - Lecture Notes | COP 3502 and more Study notes Computer Science in PDF only on Docsity!

Computing Complexity of

Algorithms

Big-O Notation

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

  • 3n 2
  • 10 = f (n)_ The idea is to reduce the expression so that it captures the qualitative behavior in simplest possible terms. We eliminate any term whose contribution to the total ceases to be significant as n becomes large. We also eliminate any constant factors, as these have no effect on the overall pattern as n increases. Thus we may approximate f(n) above as _O (n 4
  • 3 1 n 2
    1. = O( n 4)_ Let g(n) = n 4

2 _n 2

  • 3 n + 1 < = c n 2_ To determine range of values for c, we divide both sides by n 2 2 + (3/n) + ( 1 / n 2 ) < = c You want to find c such that a term in f becomes the largest and stays the largest. Compare first and second term. First will overtake the second at n = 2, so for n= 2, c >= 3.75, for n = 5, c >= slightly more than 2, for very large values of n, c is almost 2. g is almost always > = f if it is multiplied by a constant c Look at it another way : suppose you want to find weight of elephants, cats and ants in a jungle. Now irrespective of how many of each item were there, the net weight would be proportional to the weight of an elephant. So here the first term 2 n 2 dominates the other terms, which can be neglected in comparison with the first term, and we can say f(n) = O(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

  • 35n 2

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)

Example 2:

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

  • am-1n m-
  • ... + a 2 n 2
  • a 1 n + a 0_ are called polynomial algorithms and are of Order O( n 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

a

b

= m

Taking log of both sides with respect to base a,

b log a a = log a m

or

b = log a m

Thus knowing any two quantities, you can find the 3

rd

one.

Comparison of N , logN and N 2

Complexity related Problems

  1. Algorithm A runs in O(N 2 ) time, and for an input size of 4, the algorithm runs in 10 milliseconds, how long can you expect it to take to run on an input size of 16? 4 2 16 2 ----- = ------- 10 x => x = 160ms
  2. Algorithm A runs in O(N 3 ) time. For an input size of 10, the algorithm runs in 7 milliseconds. For another input size, the algorithm takes 189 milliseconds. What was that input size? 10 3 N 3 ----- = ------- => N = 30 7 189
  1. Algorithm A runs in O(log 2 N) time, and for an input size of 16, the algorithm runs in 28 milliseconds, how long can you expect it to take to run on an input size of 64? log 16 log ------------ = ------------ 28 x How to find the log values?

b = log a m , a

b

= m

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

sum =  j

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

nested loops involve j

2 operations , the overall complexity of the algorithm can be expressed as the summation n

 4 j

2 j=

Use of Recurrence relations:

Some complexity analysis problems can be decomposed into subproblems having a mathematical pattern, which can