







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of algorithm analysis, focusing on the determination of an algorithm's time and space requirements. It covers the concepts of computational complexity, asymptotic notation, and the analysis of specific algorithms. Examples and comparisons of different algorithms, such as those for finding the minimum element in an array and the closest points in a plane.
Typology: Exams
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Algorithm Analysis Algorithm - a clearly specified set of instructions that the computer will follow to solve a problem. Algorithm Analysis - determining the amount of resources that the algorithm will require, typically in terms of time and space. Areas of study include: Estimation techniques for determining the running time of an algorithm. Techniques to reduce the running time of an algorithm. Mathematical framework for the accurate determination of the running time of an algorithm. Algorithm Analysis The running time of an algorithm is a function of the size of the input. Example: It takes longer to sort 1000 numbers than it does to sort 10 numbers. The value of this function depends upon many things including:
When comparing two functions F(N) and G(N), it does not make sense to state that: F < G, F = G, or G < F. Example: At some point x , F may be smaller than G, yet at some other point y , F may be equal to or greater than G. Instead, the growth rates of the functions need to be determined. Definitions (based on the growth rate of the function):
Asymptotic Notation Big Oh Notation Definition: Let p(n) and q(n) be two nonnegative functions. The function p(n) is asymptotically bigger [p(n) asymptotically dominates q(n)] than the function q(n) iff The function q(n) is asymptotically smaller than p(n) iff p(n) is asympotically bigger than q(n). Functions p(n) and q(n) are asymptotically equal iff neither is asymptotically bigger than the other. Example 1: Let p(n) = 3n^2 + 2n + 6 and q(n) = 10n + 7. divide both functions by n 2 (to reduce dominant term to a constant) which will produce: Thus, 3n^2 + 2n + 6 is asymptotically bigger than 10n + 7. Similarly, 10n + 7 is asymptotically smaller than 3n 2
0 / 8 0 8 9 / n 100 /n 3 / n 8 n 9 n 100 n 3 lim (^2) 4 4 2 3 n A similar technique will show the same is true for the functions 2n^2 + 3n and 83n. Term Name 1 Constant Log N Logarithmic N Linear N log N N log N N^2 Quadratic N^3 Cubic 2 N^ Exponential N! Factorial Asymptotic notation describes the behavior of the time or space complexity for large instance characteristics. When the instance characteristic is described by a single variable, say n , asymptotic notation describes the complexity using a single term, the asymptotically biggest term in the function (step count of algorithm steps). Big-Oh Notation (O) Notation: f(n) = O(g(n)) [read as f(n) is big-oh of g(n)] means that f(n) is asymptotically smaller than or equal to g(n). Meaning: g(n) establishes an upper bound on f(n). The asymptotic growth rate of the function f(n) is bounded from above by g(n). t cg(n) m f(n) n g(n) is an upper bound on f(n) Omega Notation ( )
Algorithm Assumptions: an array named a that holds the values and is of size SIZE. min = a[0] for ( i = 1; i < SIZE; i++) if( a[i] < min) min = a[i] return min Analysis Makes N-1 iterations of the loop and is therefore O(N). (g(n)) = {f(n) | f(n) = (g(n))} (g(n)) = {f(n) | f(n) = (g(n))} Under this interpretation, statements such as O(g1(n)) = O(g2(n)) and (g1(n)) = (g2(n)) are meaningful. This notation also allows one to read f(n) = O(g(n)) as “f of n is in big oh of g of n”, and so on. Little Oh Notation (o) Little Oh notation, o(f(n)), is commonly used in step count analysis and provides a strict upper bound on the asymptotic growth rate of the function. This means that f(n) is o(g(n)) iff f(n) is asymptotically smaller than g(n). [Note that the equal to case provided by the Big-Oh notation is not present in Little-Oh notation, thus the strict upper bound.] Definition: f(n) = o(g(n)) iff f(n) = O(g(n)) and f(n) (g(n)). Examples of Algorithm Running Times
Example – Better Algorithm – Closest Points in a Plane 4 3 Let a = y 2 a[0] a[1] a[2] a[3] 1 0 1 2 3 4 5 x Assume that a is an array (1dimension) that holds the points from the plane. Assume that the function dist(r, s) returns the distance between two points r and s. Assume that the constant SIZE is the number of elements in the array of points a. Operation of the Algorithm for the Example Case Recall that: distance (a[0], a[1]) = 2 distance (a[0], a[2]) = 5 1/ distance (a[0], a[3]) = 3 distance (a[1], a[2]) = 5 1/ distance (a[1], a[3]) = 13 1/ distance (a[2], a[3]) = 2 1/ note: the first algorithm would have also calculated distances for the following pairs of points: (a[1],a[0]), (a[2], a[0]), (a[3],a[0]), (a[2],a[1]), (a[3],a[1]), and (a[3],a[2]) or twice as many points. However, realize that the distance from a[0] to a[1] is exactly the same as the distance from a[1] to a[0]! outer loop iteration #1: i = 0 inner loop iterates from 1 to 3 for this outer iteration checks distances for the following point pairs: (a[0], a[1]), (a[0], a[2]), (a[0], a[3]) {minimum = 2, x = 0, y = 1} outer loop iteration #2: i = 1 inner loop iterates from 2 to 3 for this outer iteration checks distances for the following point pairs: (a[1], a[2]), (a[1], a[3]) {minimum = 2, x = 0, y = 1} outer loop iteration #3: i = 2 inner loop iterates from 3 to 3 for this outer iteration checks distances for the following point pairs:
2 i j 2 (x (^) i x j) (y y)
(a[2], a[3]) {minimum = 21/2, x = 2, y = 3} outer loop terminates minimum returned as 2 1/ with the closest pair of points being a[2] and a[3]
There are some slight mistakes in this algorithm, but the overall idea is here and you will “fix” the mistakes when you write a program to implement this algorithm for your homework. Now, before we finish the lecture, the last thing we must do is analyze the running time of this algorithm. Here is what we see: For each loop iteration, we advance at least one marker. The maximum number of iterations then, would be the total number of names on both lists, which is n, using our previous interpretation. For each iteration, we are doing a constant amount of work. (Essentially a comparison, and sometimes outputting a single name...) Thus, our algorithm runs in O(n) time – an improvement over our previous algorithm. A final question one must ask is, can we solve this question in even less time? If yes, what is such an algorithm, if no, how can we prove it? Our proof goes along these lines: In order to have an accurate list, we must read every name on one of the two lists. If we skip names on BOTH lists, we can NOT deduce whether we would have matches between those names or not. In order to simply “read” all the names on one list, we would take O(n/2) time. But, in order notation, this is still O(n), the running time of our second algorithm. Thus, we know we can not do better in terms of time, (within a constant factor), of our second algorithm.