

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!


Name:
If you have ever sat in a car on a rainy day you might have noticed that small drops of rain can remain in place on the windscreen, but as a drop gets larger it eventually reaches a point where it will fall off. To see why, think of the forces acting on the drop. The force holding the drop in place is surface tension. The force making it fall down is gravity. If we imagine the drop as a perfect sphere, the surface tension is proportional to the square of the radius, while the force of gravity is proportional to the volume, which grows as the cube of the radius. What you observe is that no matter what constants are placed in front of these two values, a cubic function (c * r^3 , i.e. gravity) will eventually grow larger than a square function (d * r^2 , i.e. surface tension). The same idea is used in algorithmic analysis. We say that one function dominates another if as the input gets larger the second will always be larger than the first, regardless of any constants involved. To see how this relates to algorithms consider the function to initialize an identity matrix, shown at right. If you apply the techniques from the previous lesson it is clear the first part is performing O(n^2 ) work, while the second is O(n). So you might think that the algorithm is O(n^2 +n). But instead, the rule is that when summing big-Oh values you throw away everything except the dominating function. Since n^2 dominates n, the algorithm is said to be O(n^2 ). What functions dominate each other? The table at left lists functions in order from most costly to least. The middle column is the common name for the function. The third column can be used to illustrate why the dominating rule makes sense. Assume that an input is size 10^5 , and you can perform 10^6 operations per second. The third column shows the approximate time it would take to perform a task if the algorithm is of the given complexity. So imagine that we had an algorithm had one part that was O(n^2 ) and another that was O(n). The O(n^2 ) part would be taking about 2.8 hours, Function Common name Running time N! Factorial 2 n^ Exponential > century Nd, d > 3 Polynomial N^3 Cubic 31.7 years N^2 Quadratic 2.8 hours N sqrt n 31.6 seconds N log n 1.2 seconds N Linear 0.1 second sqrt (n) Root-n 3.2 * 10-^4 seconds Log n Logarithmic 1.2 * 10-^5 seconds 1 Constant void makeIdentity (int [][] m) { int n = m.length; for (int I = 0; I < n; i++) for (int j = 0; j < n; j++) m[i][j] = 0; for (int I = 0; I < n; i++) m[i][i] = 1; }
Name: while the O(n) part would contribute about 0.1 seconds. The smaller value gets overwhelmed by the larger. Suppose by careful measurement you have discovered that a program has the running time as shown at left. Describe the running time of each function using big-Oh notation. Using the idea of dominating functions, give the big-Oh execution time for each of the following sequences of code. When elipses (…) are given you can assume that they describe only constant time operations. for (int I = n; I > 0; I = I / 2) { … } for (int j = 0; j * j < n; j++) … for (int I = 0; I < n; i++) { For (int j = n; j > 0; j = j / 2) { … } for (int k = 0; k < n; k++) { … } } for (int I = 0; I < n; i++) … for (int j = 0; j * j < n; j++) … for (int I = 0; I < n; i++) … for (int j = n; j > 0; j--) … for (int I = 1; I * I < n; I += 2) … for (int I = 1; I < n; I += 5) … 3n^3 + 2n + 7 (5 * n) * (3 + log n) 1 + 2 + 3 + … + n n + log n^2 ((n+1) log n ) / 2 n^3 + n! + 3 2 n^ + n^2 n (sqrt(n) + log n)