Question? Leave a message!




ANALYSIS OF ALGORITHMS

ANALYSIS OF ALGORITHMS
ROBERT SEDGEWICK KEVIN WAYNE Algorithms 1.4 ANALYSIS OF ALGORITHMS introduction ‣ observations ‣ mathematical models ‣ Algorithms FOUR TH EDITION orderofgrowth classifications ‣ theory of algorithms ‣ ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu memory ‣1.4 ANALYSIS OF ALGORITHMS introduction ‣ observations ‣ mathematical models ‣ Algorithms orderofgrowth classifications ‣ theory of algorithms ‣ ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu memory ‣Running time “ As soon as an Analytic Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will arise—By what course of calculation can these results be arrived at by the machine in the shortest time ” — Charles Babbage (1864) how many times do you have to turn the crank Analytic Engine 3Cast of characters Programmer needs to develop a working solution. Student might play any or all of these Client wants to solve roles someday. problem efficiently. Theoretician wants to understand. 4Reasons to analyze algorithms Predict performance. this course (COS 226) Compare algorithms. Provide guarantees. theory of algorithms (COS 423) Understand theoretical basis. Primary practical reason: avoid performance bugs. client gets poor performance because programmer did not understand performance characteristics 5Some algorithmic successes Discrete Fourier transform. Break down waveform of N samples into periodic components. Applications: DVD, JPEG, MRI, astrophysics, …. 2 Brute force: N steps. Friedrich Gauss 1805 FFT algorithm: N log N steps, enables new technology. time quadratic 64T 32T 16T linearithmic 8T linear size 1K 2K 4K 8K 6Some algorithmic successes Nbody simulation. Simulate gravitational interactions among N bodies. 2 Brute force: N steps. BarnesHut algorithm: N log N steps, enables new research. Andrew Appel PU '81 time quadratic 64T 32T 16T linearithmic 8T linear size 1K 2K 4K 8K 7The challenge Q. Will my program be able to solve a large practical input Why does it run out of memory Why is my program so slow Insight. Knuth 1970s Use scientific method to understand performance. 8Scientific method applied to analysis of algorithms A framework for predicting performance and comparing algorithms. Scientific method. Observe some feature of the natural world. Hypothesize a model that is consistent with the observations. Predict events using the hypothesis. Verify the predictions by making further observations. Validate by repeating until the hypothesis and observations agree. Principles. Experiments must be reproducible. Hypotheses must be falsifiable. Feature of the natural world. Computer itself. 91.4 ANALYSIS OF ALGORITHMS introduction ‣ observations ‣ mathematical models ‣ Algorithms orderofgrowth classifications ‣ theory of algorithms ‣ ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu memory ‣Example: 3SUM 3SUM. Given N distinct integers, how many triples sum to exactly zero ai aj ak sum more 8ints.txt 8 30 40 10 0 1 30 40 20 10 40 0 10 5 30 20 10 0 2 java ThreeSum 8ints.txt 3 40 40 0 0 4 4 10 0 10 0 Context. Deeply related to problems in computational geometry. 113SUM: bruteforce algorithm public class ThreeSum public static int count(int a) int N = a.length; int count = 0; for (int i = 0; i N; i++) for (int j = i+1; j N; j++) check each triple for (int k = j+1; k N; k++) for simplicity, ignore if (ai + aj + ak == 0) integer overflow count++; return count; public static void main(String args) In in = new In(args0); int a = in.readAllInts(); StdOut.println(count(a)); 12Measuring the running time Q. How to time a program java ThreeSum 1Kints.txt A. Manual. tick tick tick 70 java ThreeSum 2Kints.txt tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick 528 java ThreeSum 4Kints.txt tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick 4039 13 Observing the running time of a programMeasuring the running time Q. How to time a program A. Automatic. (part of stdlib.jar ) public class Stopwatch public class Stopwatch Stopwatch() Stopwatch() create a new stopwatch double elapsedTime() elapsedTime() time since creation (in seconds) public static void main(String args) In in = new In(args0); int a = in.readAllInts(); Stopwatch stopwatch = new Stopwatch(); StdOut.println(ThreeSum.count(a)); client code double time = stopwatch.elapsedTime(); StdOut.println("elapsed time " + time); 14Empirical analysis Run the program for various input sizes and measure running time. 15Empirical analysis Run the program for various input sizes and measure running time. † N time (seconds) 250 0.0 500 0.0 1,000 0.1 2,000 0.8 4,000 6.4 8,000 51.1 16,000 16Data analysis Standard plot. Plot running time T (N) vs. input size N. standard plot loglog plot 51.2 50 straight line of slope 3 25.6 40 12.8 6.4 30 3.2 1.6 20 .8 .4 10 .2 .1 1K 2K 4K 8K 1K 2K 4K 8K problem size N lgN Analysis of experimental data (the running time of ThreeSum) 17 running time T(N) lg(T(N))Data analysis Loglog plot. Plot running time T (N) vs. input size N using loglog scale. standard plot loglog plot 51.2 50 straight line of slope 3 25.6 40 12.8 lg(T (N)) = b lg N + c 6.4 b = 2.999 30 3.2 c = 33.2103 1.6 b c T (N) = a N , where a = 2 20 .8 .4 3 orders of magnitude 10 .2 .1 1K 2K 4K 8K 1K 2K 4K 8K problem size N lgN Analysis of experimental data (the running time of ThreeSum) power law slope b Regression. Fit straight line through data points: a N . –10 2.999 Hypothesis. The running time is about 1.006 × 10 × N seconds. 18 running time T(N) lg(T(N))Prediction and validation –10 2.999 Hypothesis. The running time is about 1.006 × 10 × N seconds. "order of growth" of running 3 time is about N stay tuned Predictions. 51.0 seconds for N = 8,000. 408.1 seconds for N = 16,000. Observations. † N time (seconds) 8,000 51.1 8,000 51.0 8,000 51.1 16,000 410.8 validates hypothesis 19Doubling hypothesis Doubling hypothesis. Quick way to estimate b in a powerlaw relationship. Run program, doubling the size of the input. † N time (seconds) ratio lg ratio b T(2N) a(2N) = b T(N) aN 250 0.0 – b =2 500 0.0 4.8 2.3 1,000 0.1 6.9 2.8 2,000 0.8 7.7 2.9 lg (6.4 / 0.8) = 3.0 4,000 6.4 8.0 3.0 8,000 51.1 8.0 3.0 seems to converge to a constant b ≈ 3 b Hypothesis. Running time is about a N with b = lg ratio. Caveat. Cannot identify logarithmic factors with doubling hypothesis. 20Doubling hypothesis Doubling hypothesis. Quick way to estimate b in a powerlaw relationship. Q. How to estimate a (assuming we know b) A. Run the program (for a sufficient large value of N) and solve for a. † N time (seconds) 8,000 51.1 3 51.1 = a × 8000 8,000 51.0 –10 ⇒ a = 0.998 × 10 8,000 51.1 –10 3 Hypothesis. Running time is about 0.998 × 10 × N seconds. almost identical hypothesis to one obtained via linear regression 21Experimental algorithmics System independent effects. Algorithm. determines exponent in power law Input data. determines constant System dependent effects. in power law Hardware: CPU, memory, cache, … Software: compiler, interpreter, garbage collector, … System: operating system, network, other apps, … Bad news. Difficult to get precise measurements. Good news. Much easier and cheaper than other sciences. e.g., can run huge number of experiments 221.4 ANALYSIS OF ALGORITHMS introduction ‣ observations ‣ mathematical models ‣ Algorithms orderofgrowth classifications ‣ theory of algorithms ‣ ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu memory ‣Mathematical models for running time Total running time: sum of cost × frequency for all operations. Need to analyze program to determine set of operations. Cost depends on machine, compiler. Frequency depends on algorithm, input data. Donald Knuth 1974 Turing Award In principle, accurate mathematical models are available. 24Cost of basic operations Challenge. How to estimate constants. † operation example nanoseconds integer add a + b 2.1 integer multiply a b 2.4 integer divide a / b 5.4 floatingpoint add a + b 4.6 floatingpoint multiply a b 4.2 floatingpoint divide a / b 13.5 sine Math.sin(theta) 91.3 arctangent Math.atan2(y, x) 129.0 ... ... ... † Running OS X on Macbook Pro 2.2GHz with 2GB RAM 25Cost of basic operations Observation. Most primitive operations take constant time. † operation example nanoseconds variable declaration int a c 1 assignment statement a = b c 2 integer compare a b c 3 array element access ai c 4 array length a.length c 5 1D array allocation new intN c N 6 2 2D array allocation new intNN c N 7 Caveat. Nonprimitive operations often take more than constant time. novice mistake: abusive string concatenation 26Example: 1SUM Q. How many instructions as a function of input size N int count = 0; for (int i = 0; i N; i++) if (ai == 0) count++; N array accesses operation frequency variable declaration 2 assignment statement 2 less than compare N + 1 equal to compare N array access N increment N to 2 N 27Example: 2SUM Q. How many instructions as a function of input size N int count = 0; for (int i = 0; i N; i++) for (int j = i+1; j N; j++) if (ai + aj == 0) count++; 1 0+1+2+...+(N 1) = N (N 1) 2 ⇥ Pf. n even N = 2 1 1 2 0+1+2+...+(N 1) = N N 2 2 half of half of square diagonal 28String theory infinite sum 1 1+2+3+4+ ...= 12 http://www.nytimes.com/2014/02/04/science/intheenditalladdsupto.html 29Example: 2SUM Q. How many instructions as a function of input size N int count = 0; for (int i = 0; i N; i++) for (int j = i+1; j N; j++) if (ai + aj == 0) count++; 1 0+1+2+...+(N 1) = N (N 1) 2 ⇥ N = 2 operation frequency variable declaration N + 2 assignment statement N + 2 less than compare ½ (N + 1) (N + 2) equal to compare ½ N (N − 1) tedious to count exactly array access N (N − 1) increment ½ N (N − 1) to N (N − 1) 30Downloaded from qjmam.oxfordjournals.org at Princeton University Library on September 20, 2011 Simplifying the calculations “ It is convenient to have a measure of the amount of work involved in a computing process, even though it be a very crude one. We may count up the number of times that various elementary operations are applied in the whole process and then given them various weights. We might, for instance, count the number of additions, subtractions, multiplications, divisions, recording of numbers, and extractions of figures from tables. In the case of computing with matrices most of the work consists of multiplications and writing down numbers, and we shall therefore only attempt to count the number of multiplications and recordings. ” — Alan Turing ROUNDINGOFF ERRORS IN MATRIX PROCESSES By A. M. TURING National Physical Laboratory, Teddington, Middlesex) Received 4 November 1947 SUMMARY A number of methods of solving sets of linear equations and inverting matrices are discussed. The theory of the roundingoff errors involved is investigated for some of the methods. In all cases examined, including the wellknown 'Gauss elimination process', it is found that the errors are normally quite moderate: no exponential buildup need occur. Included amongst the methods considered is a generalization of Choleski's method which appears to have advantages over other known methods both as regards accuracy and convenience. This method may also be regarded as a rearrangement of the elimination process. 31 THIS paper contains descriptions of a number of methods for solving sets of linear simultaneous equations and for inverting matrices, but its main concern is with the theoretical limits of accuracy that may be obtained in the application of these methods, due to roundingoff errors. The best known method for the solution of linear equations is Gauss's elimination method. This is the method almost universally taught in schools. It has, unfortunately, recently come into disrepute on the ground that rounding off will give rise to very large errors. It has, for instance, been argued by HoteUing (ref. 5) that in solving a set of n equations we should keep nlog 4 extra or 'guarding' figures. Actually, although 10 examples can be constructed where as many as «log 2 extra figures 10 would be required, these are exceptional. In the present paper the magnitude of the error is described in terms of quantities not considered in HoteUing's analysis; from the inequalities proved here it can imme diately be seen that in all normal cases the Hotelling estimate is far too pessimistic. The belief that the elimination method and other 'direct' methods of solution lead to large errors has been responsible for a recent search for other methods which would be free from this weakness. These were mainly methods of successive approximation and considerably more laborious than the direct ones. There now appears to be no real advantage in the indirect methods, except in connexion with matrices having special properties, for example, where the vast majority of the coefficients are very small, but there is at least one large one in each row. The writer was prompted to cany out this research largely by the practical work of L. Fox in applying the elimination method (ref. 2). FoxSimplification 1: cost model Cost model. Use some basic operation as a proxy for running time. int count = 0; for (int i = 0; i N; i++) for (int j = i+1; j N; j++) if (ai + aj == 0) count++; 1 0+1+2+...+(N 1) = N (N 1) 2 ⇥ N = 2 operation frequency variable declaration N + 2 assignment statement N + 2 less than compare ½ (N + 1) (N + 2) equal to compare ½ N (N − 1) cost model = array accesses array access N (N − 1) (we assume compiler/JVM do not increment ½ N (N − 1) to N (N − 1) optimize any array accesses away) 32Simplification 2: tilde notation Estimate running time (or memory) as a function of input size N. Ignore lower order terms. – when N is large, terms are negligible – when N is small, we don't care 3 N /6 3 3 Ex 1. ⅙ N + 20 N + 16 ⅙ N 3 2 166,666,667 N /6 N /2 + N /3 3 4/3 3 Ex 2. ⅙ N + 100 N + 56 ⅙ N 166,167,000 3 2 3 Ex 3. ⅙ N ½ N + ⅓ N ⅙ N N 1,000 discard lowerorder terms Leadingterm approximation (e.g., N = 1000: 166.67 million vs. 166.17 million) f (N) lim = 1 Technical definition. f(N) g(N) means N→∞ g(N) 33 € Simplification 2: tilde notation Estimate running time (or memory) as a function of input size N. Ignore lower order terms. – when N is large, terms are negligible – when N is small, we don't care operation frequency tilde notation variable declaration N + 2 N assignment statement N + 2 N 2 less than compare ½ (N + 1) (N + 2) ½ N 2 equal to compare ½ N (N − 1) ½ N 2 array access N (N − 1) N 2 2 increment ½ N (N − 1) to N (N − 1) ½ N to N 34Example: 2SUM Q. Approximately how many array accesses as a function of input size N int count = 0; for (int i = 0; i N; i++) for (int j = i+1; j N; j++) "inner loop" if (ai + aj == 0) count++; 1 0+1+2+...+(N 1) = N (N 1) 2 ⇥ N = 2 2 A. N array accesses. Bottom line. Use cost model and tilde notation to simplify counts. 35Example: 3SUM Q. Approximately how many array accesses as a function of input size N int count = 0; for (int i = 0; i N; i++) for (int j = i+1; j N; j++) for (int k = j+1; k N; k++) "inner loop" if (ai + aj + ak == 0) count++; ⇥ N N(N 1)(N 2) = 3 3 3 1 A. ½ N array accesses. 3 ⇥ N 6 Bottom line. Use cost model and tilde notation to simplify counts. 36Diversion: estimating a discrete sum Q. How to estimate a discrete sum A1. Take a discrete mathematics course. A2. Replace the sum with an integral, and use calculus ⇥ N N 1 2 Ex 1. 1 + 2 + … + N. i xdx N 2 x=1 i=1 N N 1 k k k+1 i x dx N k k k Ex 2. 1 + 2 + … + N . k+1 x=1 i=1 ⇥ N N 1 1 dx = lnN Ex 3. 1 + 1/2 + 1/3 + … + 1/N. i x x=1 i=1 ⇥ ⇥ ⇥ N N N N N N 1 3 Ex 4. 3sum triple loop. 1 dzdydx N 6 x=1 y=x z=y i=1 j=i k=j 37Estimating a discrete sum Q. How to estimate a discrete sum A1. Take a discrete mathematics course. A2. Replace the sum with an integral, and use calculus Ex 4. 1 + ½ + ¼ + ⅛ + … i 1 =2 2 i=0 x 1 1 dx = 1.4427 2 ln2 x=0 Caveat. Integral trick doesn't always work 38Estimating a discrete sum Q. How to estimate a discrete sum A3. Use Maple or Wolfram Alpha. wolframalpha.com wayne:nobel.princeton.edu maple15 \/ Maple 15 (X86 64 LINUX) .\ /. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2011 \ MAPLE / All rights reserved. Maple is a trademark of Waterloo Maple Inc. Type for help. factor(sum(sum(sum(1, k=j+1..N), j = i+1..N), i = 1..N)); N (N 1) (N 2) 6 39Mathematical models for running time In principle, accurate mathematical models are available. In practice, Formulas can be complicated. Advanced mathematics might be required. Exact models best left for experts. costs (depend on machine, compiler) T = c A + c B + c C + c D + c E N 1 2 3 4 5 A = array access B = integer add frequencies C = integer compare (depend on algorithm, input) D = increment E = variable assignment 3 Bottom line. We use approximate models in this course: T(N) c N . 401.4 ANALYSIS OF ALGORITHMS introduction ‣ observations ‣ mathematical models ‣ Algorithms orderofgrowth classifications ‣ theory of algorithms ‣ ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu memory ‣Common orderofgrowth classifications Definition. If f (N) c g(N) for some constant c 0, then the order of growth of f (N) is g(N). Ignores leading coefficient. Ignores lowerorder terms. 3 Ex. The order of growth of the running time of this code is N . int count = 0; for (int i = 0; i N; i++) for (int j = i+1; j N; j++) for (int k = j+1; k N; k++) if (ai + aj + ak == 0) count++; Typical usage. With running times. where leading coefficient depends on machine, compiler, JVM, ... 42standard plot exponential 500T cubic quadratic Common orderofgrowth classifications 200T Good news. The set of functions 100T 2 3 N 1, log N, N, N log N, N , N , and 2 logarithmic constant suffices to describe the order of growth of most common algorithms. 100K 200K 500K size loglog plot 512T 64T 8T 4T 2T logarithmic T constant 1K 2K 4K 8K 512K size Typical orders of growth 43 linearithmic linear linearithmic linear quadratic cubic time time exponentialCommon orderofgrowth classifications order of name typical code framework description example T(2N) / T(N) growth add two a = b + c; statement constant 1 1 numbers while (N 1) divide in half binary search logarithmic log N 1 N = N / 2; ... find the for (int i = 0; i N; i++) loop linear N 2 ... maximum divide see mergesort lecture mergesort linearithmic N log N 2 and conquer for (int i = 0; i N; i++) check all 2 for (int j = 0; j N; j++) double loop quadratic N 4 pairs ... for (int i = 0; i N; i++) check all for (int j = 0; j N; j++) 3 triple loop cubic N 8 for (int k = 0; k N; k++) triples ... exhaustive check all N see combinatorial search lecture exponential 2 T(N) search subsets 44Binary search demo Goal. Given a sorted array and a key, find index of the key in the array Binary search. Compare key against middle entry. Too small, go left. Too big, go right. Equal, found. successful search for 33 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi 45Binary search: Java implementation Trivial to implement First binary search published in 1946. First bugfree one in 1962. Bug in Java's Arrays.binarySearch() discovered in 2006. public static int binarySearch(int a, int key) int lo = 0, hi = a.length1; while (lo = hi) int mid = lo + (hi lo) / 2; if (key amid) hi = mid 1; one "3way compare" else if (key amid) lo = mid + 1; else return mid; return 1; Invariant. If key appears in the array a, then alo ≤ key ≤ ahi. 46Binary search: mathematical analysis Proposition. Binary search uses at most 1 + lg N key compares to search in a sorted array of size N. Def. T (N) = key compares to binary search a sorted subarray of size ≤ N. Binary search recurrence. T (N) ≤ T (N / 2) + 1 for N 1, with T (1) = 1. possible to implement with one left or right half 2way compare (instead of 3way) (floored division) Pf sketch. assume N is a power of 2 given T (N) ≤ T (N / 2) + 1 apply recurrence to first term ≤ T (N / 4) + 1 + 1 apply recurrence to first term ≤ T (N / 8) + 1 + 1 + 1 ⋮ ≤ T (N / N) + 1 + 1 + … + 1 stop applying, T(1) = 1 = 1 + lg N 472 An N log N algorithm for 3SUM Algorithm. input 30 40 20 10 40 0 10 5 Step 1: Sort the N (distinct) numbers. Step 2: For each pair of numbers ai sort 40 20 10 0 5 10 30 40 and aj, binary search for (ai + aj). binary search (40, 20) 60 2 Analysis. Order of growth is N log N. (40, 10) 50 2 Step 1: N with insertion sort. (40, 0) 40 (40, 5) 35 2 Step 2: N log N with binary search. (40, 10) 30 ⋮ ⋮ (20, 10) 30 2 ⋮ ⋮ Remark. Can achieve N by modifying (10, 0) 10 binary search step. only count if ⋮ ⋮ ai aj ak ( 10, 30) 40 to avoid double counting ( 10, 40) 50 ( 30, 40) 70 48Comparing programs 2 Hypothesis. The sortingbased N log N algorithm for 3SUM is significantly 3 faster in practice than the bruteforce N algorithm. N time (seconds) N time (seconds) 1,000 0.1 1,000 0.14 2,000 0.8 2,000 0.18 4,000 6.4 4,000 0.34 8,000 51.1 8,000 0.96 16,000 3.67 ThreeSum.java 32,000 14.88 64,000 59.16 ThreeSumDeluxe.java Guiding principle. Typically, better order of growth ⇒ faster in practice. 491.4 ANALYSIS OF ALGORITHMS introduction ‣ observations ‣ mathematical models ‣ Algorithms orderofgrowth classifications ‣ theory of algorithms ‣ ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu memory ‣Types of analyses Best case. Lower bound on cost. Determined by “easiest” input. Provides a goal for all inputs. Worst case. Upper bound on cost. Determined by “most difficult” input. Provides a guarantee for all inputs. this course Average case. Expected cost for random input. Need a model for “random” input. Provides a way to predict performance. Ex 1. Array accesses for bruteforce 3SUM. Ex 2. Compares for binary search. 3 Best: ½ N Best: 1 3 Average: ½ N Average: lg N 3 Worst: ½ N Worst: lg N 51Theory of algorithms Goals. Establish “difficulty” of a problem. Develop “optimal” algorithms. Approach. Suppress details in analysis: analyze “to within a constant factor.” Eliminate variability in input model: focus on the worst case. Upper bound. Performance guarantee of algorithm for any input. Lower bound. Proof that no algorithm can do better. Optimal algorithm. Lower bound = upper bound (to within a constant factor). 52Commonlyused notations in the theory of algorithms notation provides example shorthand for used to 2 ½ N 2 10 N asymptotic classify 2 Big Theta Θ(N ) order of growth algorithms 2 5 N + 22 N log N + 3N ⋮ 2 10 N 100 N develop 2 2 Big Oh Θ(N ) and smaller O(N ) upper bounds 22 N log N + 3 N ⋮ 2 ½ N 5 N develop 2 2 Big Omega Θ(N ) and larger Ω(N ) lower bounds 3 N + 22 N log N + 3 N ⋮ 53Theory of algorithms: example 1 Goals. Establish “difficulty” of a problem and develop “optimal” algorithms. Ex. 1SUM = “Is there a 0 in the array ” Upper bound. A specific algorithm. Ex. Bruteforce algorithm for 1SUM: Look at every array entry. Running time of the optimal algorithm for 1SUM is O(N). Lower bound. Proof that no algorithm can do better. Ex. Have to examine all N entries (any unexamined one might be 0). Running time of the optimal algorithm for 1SUM is Ω(N). Optimal algorithm. Lower bound equals upper bound (to within a constant factor). Ex. Bruteforce algorithm for 1SUM is optimal: its running time is Θ(N). 54Theory of algorithms: example 2 Goals. Establish “difficulty” of a problem and develop “optimal” algorithms. Ex. 3SUM. Upper bound. A specific algorithm. Ex. Bruteforce algorithm for 3SUM. 3 Running time of the optimal algorithm for 3SUM is O(N ). 55Theory of algorithms: example 2 Goals. Establish “difficulty” of a problem and develop “optimal” algorithms. Ex. 3SUM. Upper bound. A specific algorithm. Ex. Improved algorithm for 3SUM. 2 Running time of the optimal algorithm for 3SUM is O(N log N ). Lower bound. Proof that no algorithm can do better. Ex. Have to examine all N entries to solve 3SUM. Running time of the optimal algorithm for solving 3SUM is Ω(N ). Open problems. Optimal algorithm for 3SUM Subquadratic algorithm for 3SUM Quadratic lower bound for 3SUM 56Algorithm design approach Start. Develop an algorithm. Prove a lower bound. Gap Lower the upper bound (discover a new algorithm). Raise the lower bound (more difficult). Golden Age of Algorithm Design. 1970s. Steadily decreasing upper bounds for many important problems. Many known optimal algorithms. Caveats. Overly pessimistic to focus on worst case Need better than “to within a constant factor” to predict performance. 57Commonlyused notations in the theory of algorithms notation provides example shorthand for used to 2 10 N provide 2 2 leading term approximate Tilde 10 N 10 N + 22 N log N model 2 10 N + 2 N + 37 2 ½ N asymptotic classify 2 2 Big Theta Θ(N ) 10 N order of growth algorithms 2 5 N + 22 N log N + 3N 2 10 N develop 2 2 Big Oh Θ(N ) and smaller O(N ) 100 N upper bounds 22 N log N + 3 N 2 ½ N develop 2 2 5 Big Omega Θ(N ) and larger Ω(N ) N lower bounds 3 N + 22 N log N + 3 N Common mistake. Interpreting bigOh as an approximate model. This course. Focus on approximate models: use Tildenotation 581.4 ANALYSIS OF ALGORITHMS introduction ‣ observations ‣ mathematical models ‣ Algorithms orderofgrowth classifications ‣ theory of algorithms ‣ ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu memory ‣Basics Bit. 0 or 1. NIST most computer scientists Byte. 8 bits. 20 Megabyte (MB). 1 million or 2 bytes. 30 Gigabyte (GB). 1 billion or 2 bytes. 64bit machine. We assume a 64bit machine with 8byte pointers. Can address more memory. some JVMs "compress" ordinary object Pointers use more space. pointers to 4 bytes to avoid this cost 60Typical memory usage for primitive types and arrays type bytes type bytes boolean char 1 2 N + 24 byte 1 int 4 N + 24 char 2 double 8 N + 24 int 4 onedimensional arrays float 4 long 8 type bytes double 8 char 2 M N primitive types int 4 M N double 8 M N twodimensional arrays 61Typical memory usage for objects in Java Object overhead. 16 bytes. integer wrapper object 24 bytes Reference. 8 bytes. public class Integer Padding. Each object uses a multiple of 8 bytes. object private int x; overhead ... int x value Ex 1. A Date object uses 32 bytes of memory. padding date object 32 bytes public class Date private int day; object 16 bytes (object overhead) private int month; overhead private int year; ... 4 bytes (int) day int month 4 bytes (int) values year 4 bytes (int) padding 4 bytes (padding) 32 bytes 32 bytes counter object 62 public class Counter private String name; object private int count; overhead ... String reference name int count value padding 40 bytes node object (inner class) public class Node object private Item item; overhead private Node next; ... extra overhead item references next Typical object memory requirementsTypical memory usage summary Total memory usage for a data type value: Primitive type:  4 bytes for int, 8 bytes for double, … Object reference:  8 bytes. Array: 24 bytes + memory for each array entry. Object: 16 bytes + memory for each instance variable. Padding: round up to multiple of 8 bytes. + 8 extra bytes per inner class object (for reference to enclosing class) Shallow memory usage: Don't count referenced objects. Deep memory usage: If array entry or instance variable is a reference, count memory (recursively) for referenced object. 63Example Q. How much memory does WeightedQuickUnionUF use as a function of N Use tilde notation to simplify your answer. 16 bytes public class WeightedQuickUnionUF (object overhead) 8 + (4N + 24) bytes each private int id; (reference + int array) private int sz; 4 bytes (int) private int count; 4 bytes (padding) public WeightedQuickUnionUF(int N) 8N + 88 bytes id = new intN; sz = new intN; for (int i = 0; i N; i++) idi = i; for (int i = 0; i N; i++) szi = 1; ... A. 8 N + 88 8 N bytes. 64Turning the crank: summary Empirical analysis. Execute program to perform experiments. Assume power law and formulate a hypothesis for running time. Model enables us to make predictions. Mathematical analysis. Analyze algorithm to count frequency of operations. Use tilde notation to simplify analysis. Model enables us to explain behavior. Scientific method. Mathematical model is independent of a particular system; applies to machines not yet built. Empirical analysis is necessary to validate mathematical models and to make predictions. 65