Docsity

Notes on Computational Complexity - Computer Science I | COP 3502, Study notes of Computer Science

Toggle sidebar
bg1
Algorithmic Cost and Complexity
Algorithms can be analyzed from two different points of view: time or
space. For the most part we are more interested in time than space.
oTime: each instruction takes time. How many instructions are
executed by the algorithm? How fast does the algorithm run? What
affects the run-time of the algorithm?
oSpace: data structures require space. What kind of data structures
are used? How does the choice of data structure affect the run-
time?
Measures of work: Worst case performance, best case performance,
and average performance.
oWhat situations will produce worst case performance? What
situations will produce best case performance? What is average
performance? How is average performance determined?
Algorithm Analysis
In some of the labs this term and in class we have looked at techniques where
we determined, essentially the number of times a statement in a loop is
executed. Remember, that baring some type of looping statement, the code in
a program (algorithm) is executed sequentially. This means that without loops,
each statement is executed exactly one time and the running time of the
algorithm is very easy to establish. Loops cause iteration and iteration
increases the running time depending on how much iteration occurs. Therefore,
we need to know, for the statements inside the loop, how many times they are
executed.
Consider the two code segments shown below:
What is the number of addition operations performed in these two code
segments?
Computational Complexity - 1
Computational Complexity
pf3
pf4
pf5
pf8

Partial preview of the text

Download Notes on Computational Complexity - Computer Science I | COP 3502 and more Study notes Computer Science in PDF only on Docsity!

Algorithmic Cost and Complexity

 Algorithms can be analyzed from two different points of view: time or space. For the most part we are more interested in time than space. o Time: each instruction takes time. How many instructions are executed by the algorithm? How fast does the algorithm run? What affects the run-time of the algorithm? o Space: data structures require space. What kind of data structures are used? How does the choice of data structure affect the run- time?  Measures of work: Worst case performance, best case performance, and average performance. o What situations will produce worst case performance? What situations will produce best case performance? What is average performance? How is average performance determined? Algorithm Analysis In some of the labs this term and in class we have looked at techniques where we determined, essentially the number of times a statement in a loop is executed. Remember, that baring some type of looping statement, the code in a program (algorithm) is executed sequentially. This means that without loops, each statement is executed exactly one time and the running time of the algorithm is very easy to establish. Loops cause iteration and iteration increases the running time depending on how much iteration occurs. Therefore, we need to know, for the statements inside the loop, how many times they are executed. Consider the two code segments shown below:  What is the number of addition operations performed in these two code segments?

Computational Complexity

For segment #1 the number of addition operations is 2n^2 while for segment #2 the value is n 2

  • n. Assume that we are working with a hypothetical computer that requires 1 microsecond (
  • ) seconds to perform an addition. If the value of n = 1000 the segment #1 would require just over 2 seconds to execute [(2*(1000) 2 )inst] * 10

sec/inst = 2 seconds. On the other hand, segment #2 would require just over 1 second [(1000)^2 + 1000] inst * 10-6^ sec/inst = 1.001 seconds. If the value of n is increased to 100,000 then code segment #1 would require about 6 hours and code segment #2 would require about 3 hours. The table shown below gives the Big-Oh complexity in terms of the dominant term. Segment #1: grandtotal = 0; for (k = 0; k < n – 1; ++k) { rows[k] = 0; for (j = 0; j < n – 1; ++j) rows[k] = rows[k] + matrix[k][j]; grandtotal = grandtotal + matrix[k][j]; } } Segment #2: grandtotal = 0; for (k = 0; k < n – 1; ++k) { rows[k] = 0; for (j = 0; j < n – 1; ++j) rows[k] = rows[k] + matrix[k][j]; grandtotal = grandtotal + rows]k]; }

When the input size is 32 approximately 4.3 109 steps will be required (since 232 = 4.29 109 ). Given our system performance this algorithm will require a running time of approximately 71.58 minutes. Now consider the effect of increasing the input size to 40, which will require approximately 1.1x 12 steps (since 2 40 = 1.09x 12 ). Given our conditions this function will require about 18325 minutes (12.7 days) to compute. If n is increased to 50 the time required will increase to about 35.7 years. If n increases to 60 the time increases to 36558 years and if n increases to 100 a total of 4x10^16 years will be needed! Suppose that an algorithm takes T(N) time to run for a problem of size N – the question becomes – how long will it take to solve a larger problem? As an example, assume that the algorithm is an O(N 3 ) algorithm. This implies: T(N) = cN 3 . If we increase the size of the problem by a factor of 10 we have: T(10N) = c(10N) 3

. This gives us: T(10N) = 1000cN 3 = 1000T(N) (since T(N) = cN 3 ) Therefore, the running time of a cubic algorithm will increase by a factor of 1000 if the size of the problem is increased by a factor of 10. Similarly, increasing the problem size by another factor of 10 (increasing N to 100) will result in another 1000 fold increase in the running time of the algorithm (from 1000 to 1 106 ). T(100N) = c(100N) 3 = 1 10 6 cN 3 = 1 10 6 T(N) A similar argument will hold for quadratic and linear algorithms, but a slightly different approach is required for logarithmic algorithms. These are shown below. For a quadratic algorithm, we have T(N) = cN^2. This implies: T(10N) = c(10N)^2. Expanding produces the form: T(10N) = 100cN^2 = 100T(N). Therefore, when the input size increases by a factor of 10 the running time of the quadratic algorithm will increase by a factor of 100. For a linear algorithm, we have T(N) = cN. This implies: T(10N) = c(10N). Expanding produces the form: T(10N) = 10cN = 10T(N). Therefore, when the input size increases by a factor of 10 the running time of the linear algorithm will increase by the same factor of 10. In general, an f -fold increase in input size will yield an f 3 -fold increase in the running time of a cubic algorithm, an f 2 -fold increase in the running time of a quadratic algorithm, and an f -fold increase in the running time of a linear algorithm.

The analysis for the linear, quadratic, cubic (and in general polynomial) algorithms does not work when in the presence of logarithmic terms. When an O(N logN) algorithm experiences a 10-fold increase in input size, the running time increases by a factor which is only slightly larger than 10. For example, increasing the input by a factor of 10 for an O(N logN) algorithm produces: T(10N) = c(10N) log(10N). Expanding this yields: T(10N) = 10cN log(10N) = 10cN logN + 10cN logN = 10T(N) + cN (where c = 10clog10). As N gets very large, the ratio T(10N)/T(N) gets closer to 10 (since cN/T(N)  (10 log10)/logN gets smaller and smaller as N increases. The above analysis implies, for a logarithmic algorithm, if the algorithm is competitive with a linear algorithm for a sufficiently large value of N, it will remain so for slightly larger N. How Much Better Is O(log 2 n) Than O(n)? The table below illustrates the improvement of O(log 2 n) algorithms compared to O(n) algorithms. N O(log 2 n) 16 4 64 6 256 8 1024 (1 kilo) 10 16,384 14 131,072 17 262,144 18 524,288 19 1.048,576 (1 Meg) 20 1,073,741 (1 Gig) 30 Data Structures and Complexity

  1. For an O(n!) algorithm, a problem instance of size n= 4 requires 72 seconds to solve. How long will it take to solve a problem instance of size n = 5? 360 sec 4! ( 5 !) 72 ( 4 !)t ( 5 !) 72 t t 5! 72 4!      
  2. For an O(2n) algorithm, a problem instance of size n = 7 required 96 seconds to solve. If you solved a different problem instance and the algorithm required 12 seconds to execute, how big was the problem instance? 2 16 n 4 96 2 ( 12 ) 2 12 2 96 2 7 n n^7 n       
  3. For an O(n/log 2 n) algorithm, a problem instance of size n = 32 runs in 96 msec. How long will the algorithm require if the problem instance is size n = 64? 12 m sec 64

t t

t log 64

log 32

  1. For the following code segment give (a) the Big-Oh order of the code and (b) the value of x when the loop ends. x = 0; for (i = 1; i < = 2*n; i++) for (j = 1; j <= n; j++) if (j == i) x = x + j; (a) 1 n n(^2 n)^2 n O(n ) 2 n i 1 n j 1 2 n i 1 2 2           (b) Notice that j is only equal to i once for each value of i. Therefore, since j goes only to the limit of n the statement x = x + j is only executed n times. 2 n(n 1 ) j n j 1  ^  
  1. For the following code segment give (a) the Big-Oh order of the code and (b) the value of x when the loop ends. x = 0; for (i = 1; i < = 2*n; i++) for (j = 1; j <= n-2; j++) x = x + j; (a) ^          2 n i 1 2 2 n 2 j 1 1 2 n(n 2 ) 4 n 2 n O(n ) (b) (n 2 )(n 1 ) n 2 n 2 (n 2 )(n 1 ) 1 2 (n 2 )(n 1 ) 2 (n 2 )(n 1 ) j 2 n i 1 2 n i 1 n 2 j 1 2 n i 1              ^       
  2. For the following code segment give (a) the Big-Oh order of the code and (b) the value of x when the loop ends. x = 0; for (i = 1; i < = 2*n; i++) for (j = 1; j <= i; j++) x = x + 3; (a) ^       2 n i 1 2 n j 1 1 ( 2 n)( 2 n) 4 n^2 O(n^2 ) (b) 3 (^2 n n)^6 n^3 n 2 4 n 2 n 3 2 2 n(n 2 1 ) 3 3 1 3 i 3 2 2 2 n^2 i 1 i 2 n i 1 i j 1 2 n i 1                    ^     