




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!





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?
For segment #1 the number of addition operations is 2n^2 while for segment #2 the value is n 2
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) + cN (where c = 10clog10). As N gets very large, the ratio T(10N)/T(N) gets closer to 10 (since cN/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
t t
t log 64
log 32