Analysis Of Algorithms - Princeton University

Transcription

AlgorithmsR OBERT S EDGEWICK K EVIN W AYNE1.4 A NALYSISOFA LGORITHMS‣ introduction‣ observationsAlgorithmsF O U R T HE D I T I O NR OBERT S EDGEWICK K EVIN W AYNEhttp://algs4.cs.princeton.edu‣ mathematical models‣ order-of-growth classifications‣ theory of algorithms‣ memory

1.4 A NALYSISOFA LGORITHMS‣ introduction‣ observationsAlgorithmsR OBERT S EDGEWICK K EVIN W AYNEhttp://algs4.cs.princeton.edu‣ mathematical models‣ order-of-growth classifications‣ theory of algorithms‣ memory

Running time“ As soon as an Analytic Engine exists, it will necessarily guide the futurecourse of the science. Whenever any result is sought by its aid, the questionwill arise—By what course of calculation can these results be arrived at bythe machine in the shortest time? ” — Charles Babbage (1864)how many times do youhave to turn the crank?Analytic Engine3

Cast of charactersProgrammer needs to developa working solution.Client wants to solveproblem efficiently.Student might playany or all of theseroles someday.Theoretician wantsto understand.4

Reasons to analyze algorithmsPredict performance.Compare algorithms.this course (COS 226)Provide guarantees.Understand theoretical basis.theory of algorithms (COS 423)Primary practical reason: avoid performance bugs.client gets poor performance because programmerdid not understand performance characteristics5

Some algorithmic successesDiscrete Fourier transform.・Break down waveform of N samples into periodic components.・Applications: DVD, JPEG, MRI, astrophysics, .・Brute force: N steps.・FFT algorithm: N log N steps, enables new technology.2Friedrich inear1K 2K4K8K6

Some algorithmic successesN-body simulation.・Simulate gravitational interactions among N bodies.・Brute force: N steps.・Barnes-Hut algorithm: N log N steps, enables new research.2Andrew AppelPU K 2K4K8K7

The challengeQ. Will my program be able to solve a large practical input?Why is my program so slow ?Why does it run out of memory ?Insight. [Knuth 1970s] Use scientific method to understand performance.8

Scientific method applied to analysis of algorithmsA 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.9

1.4 A NALYSISOFA LGORITHMS‣ introduction‣ observationsAlgorithmsR OBERT S EDGEWICK K EVIN W AYNEhttp://algs4.cs.princeton.edu‣ mathematical models‣ order-of-growth classifications‣ theory of algorithms‣ memory

Example: 3-SUM3-SUM. Given N distinct integers, how many triples sum to exactly zero?% more 8ints.txt830 -40 -20 -10 40 0 10 5% java ThreeSum 004-100100Context. Deeply related to problems in computational geometry.11

3-SUM: brute-force algorithmpublic 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 )for (int k j 1; k N; k )if (a[i] a[j] a[k] 0)count ;return count;}check each triplefor simplicity, ignoreinteger overflowpublic static void main(String[] args){In in new In(args[0]);int[] a in.readAllInts();StdOut.println(count(a));}}12

Measuring the running timeQ. How to time a program?% java ThreeSum 1Kints.txtA. Manual.tick tick tick70% java ThreeSum 2Kints.txttick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick tick528% java ThreeSum 4Kints.txt4039tick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick ticktick tick tick tick tick tick tick tickObserving the running time of a program13

Measuring the running timeQ. How to time a program?A. Automatic.public class Stopwatch(part of stdlib.jar )Stopwatch()double elapsedTime()create a new stopwatchtime since creation (in seconds)public static void main(String[] args){In in new In(args[0]);int[] a in.readAllInts();Stopwatch stopwatch new nt codedouble time stopwatch.elapsedTime();StdOut.println("elapsed time " time);}14

Empirical analysisRun the program for various input sizes and measure running time.15

Empirical analysisRun the program for various input sizes and measure running time.Ntime 51.116,000?†16

Data analysisStandard plot. Plot running time T (N) vs. input size N.standard plotlog-log plot5051.2s25.612.8lg(T(N))running time T(N)4030206.43.21.6.8.410.2.11K2K4K8K1problem size NAnalysis of experimental data (the running time of ThreeSum)17

Data analysisLog-log plot. Plot running time T (N) vs. input size N using log-log scale.log-log plot51.225.6straight lineof slope 3lg (T(N ))12.8lg(T (N)) b lg N cb 2.999c -33.21036.43.21.6T (N) a N b, where a 2 c.83 ordersof magnitude.4.2.18K1K2K4K8KlgNental data (the running time of ThreeSum)Regression. Fit straight line through data points: a N b.power lawslopeHypothesis. The running time is about 1.006 10 –10 N 2.999 seconds.18

Prediction and validationHypothesis. The running time is about 1.006 10 –10 N 2.999 seconds."order of growth" of runningtime is about N3 [stay tuned]Predictions.・51.0 seconds for N 8,000.・408.1 seconds for N 16,000.Observations.Ntime validates hypothesis!19

Doubling hypothesisDoubling hypothesis. Quick way to estimate b in a power-law relationship.Run program, doubling the size of the input.N250time (seconds)†ratio0.0lg 0006.48.03.08,00051.18.03.0T (2N )a(2N )b T (N )aN b 2blg (6.4 / 0.8) 3.0seems to converge to a constant b 3Hypothesis. Running time is about a N b with b lg ratio.Caveat. Cannot identify logarithmic factors with doubling hypothesis.20

Doubling hypothesisDoubling hypothesis. Quick way to estimate b in a power-law 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.N8,000time (seconds)†51.151.1 a 800038,00051.08,00051.1 a 0.998 10 –10Hypothesis. Running time is about 0.998 10 –10 N 3 seconds.almost identical hypothesisto one obtained via linear regression21

Experimental algorithmicsSystem independent effects.・Algorithm.・Input data.determines exponentin power lawSystem dependent effects.determines constantin 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 experiments22

1.4 A NALYSISOFA LGORITHMS‣ introduction‣ observationsAlgorithmsR OBERT S EDGEWICK K EVIN W AYNEhttp://algs4.cs.princeton.edu‣ mathematical models‣ order-of-growth classifications‣ theory of algorithms‣ memory

Mathematical models for running timeTotal 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 Knuth1974 Turing AwardIn principle, accurate mathematical models are available.24

Cost of basic operationsChallenge. How to estimate constants.operationexamplenanosecondsinteger adda b2.1integer multiplya * b2.4integer dividea / b5.4floating-point adda b4.6floating-point multiplya * b4.2floating-point dividea / , x)129.0.†† Running OS X on Macbook Pro 2.2GHz with 2GB RAM25

Cost of basic operationsObservation. Most primitive operations take constant time.operationexamplenanosecondsvariable declarationint ac1assignment statementa bc2integer comparea bc3array element accessa[i]c4array lengtha.lengthc51D array allocationnew int[N]c6 N2D array allocationnew int[N][N]c7 N 2†Caveat. Non-primitive operations often take more than constant time.novice mistake: abusive string concatenation26

Example: 1-SUMQ. How many instructions as a function of input size N ?int count 0;for (int i 0; i N; i )if (a[i] 0)count ;N array accessesoperationfrequencyvariable declaration2assignment statement2less than compareN 1equal to compareNarray accessNincrementN to 2 N27

Example: 2-SUMQ. 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 (a[i] a[j] 0)count ;0 1 2 . . . (NPf. [ n even]1) 0 1 2 . . . (N1) 1 2N2half ofsquare1N (N2 N21)1N2half ofdiagonal28

String theory infinite sum1 2 3 4 . e-end-it-all-adds-up-to.html29

Example: 2-SUMQ. 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 (a[i] a[j] 0)count ;0 1 2 . . . (Noperationfrequencyvariable declarationN 2assignment statementN 2less than compare½ (N 1) (N 2)equal to compare½ N (N 1)1) 1N (N2 N21)tedious to count exactlyarray accessN (N 1)increment½ N (N 1) to N (N 1)30

Simplifying the calculations“ It is convenient to have a measure of the amount of work involvedin a computing process, even though it be a very crude one. We maycount up the number of times that various elementary operations areapplied 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 extractionsof figures from tables. In the case of computing with matrices mostof the work consists of multiplications and writing down numbers,and we shall therefore only attempt to count the number ofmultiplications and recordings. ” — Alan TuringROUNDING-OFF ERRORS IN MATRIX PROCESSESBy A. M. TURING{National Physical Laboratory, Teddington, Middlesex)[Received 4 November 1947]THISpaper contains descriptions of a number of methods for solving setsDownloSUMMARYA number of methods of solving sets of linear equations and inverting matricesare discussed. The theory of the rounding-off errors involved is investigated forsome of the methods. In all cases examined, including the well-known 'Gausselimination process', it is found that the errors are normally quite moderate: noexponential build-up need occur.Included amongst the methods considered is a generalization of Choleski's methodwhich appears to have advantages over other known methods both as regardsaccuracy and convenience. This method may also be regarded as a rearrangementof the elimination process.31

Simplification 1: cost modelCost 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 (a[i] a[j] 0)count ;0 1 2 . . . (Noperationfrequencyvariable declarationN 2assignment statementN 2less than compare½ (N 1) (N 2)equal to compare½ N (N 1)array accessN (N 1)increment½ N (N 1) to N (N 1)1) 1N (N2 N21)cost model array accesses(we assume compiler/JVM do notoptimize any array accesses away!)32

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 careN 3/6Ex 1.⅙ N 3 20 N 16 ⅙N3Ex 2.⅙N ⅙NEx 3.⅙N3 - ½N3 100 N24/3 56 ⅓ NN 3/6 ! N 2/2 N /3166,666,6673 ⅙N3166,167,000Ndiscard lower-order terms(e.g., N 1000: 166.67 million vs. 166.17 million)Technical definition. f(N) g(N) means1,000Leading-term approximationf (N) 1 g(N)limN 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 careoperationfrequencytilde notationvariable declarationN 2 Nassignment statementN 2 Nless than compare½ (N 1) (N 2) ½N2equal to compare½ N (N 1) ½N2array accessN (N 1) N2increment½ N (N 1) to N (N 1) ½ N 2 to N 234

Example: 2-SUMQ. 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 )if (a[i] a[j] 0)count ;"inner loop"0 1 2 . . . (N1) 1N (N2 N21)A. N 2 array accesses.Bottom line. Use cost model and tilde notation to simplify counts.35

Example: 3-SUMQ. 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 )if (a[i] a[j] a[k] 0)count ;"inner loop"N3A. ½ N 3 array accesses. N (N1)(N3!2)1 3N6Bottom line. Use cost model and tilde notation to simplify counts.36

Diversion: estimating a discrete sumQ. How to estimate a discrete sum?A1. Take a discrete mathematics course.A2. Replace the sum with an integral, and use calculus! NEx 1. 1 2 N.ix dxx 1i 1NNiEx 2. 1k 2k N k.kkx dxx 1i 1NEx 3. 1 1/2 1/3 1/N.i 1NNN1Ex 4. 3-sum triple loop.i 1 j i k j1 2N2N 1iNx 1 Nx 1 1N k 1k 11dx ln NxNy x Nz ydz dy dx1 3N637

Estimating a discrete sumQ. 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 0x 01212i 2x1dx ln 21.4427Caveat. Integral trick doesn't always work!38

Estimating a discrete sumQ. 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)----------------639

Mathematical models for running timeIn 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)TN c1 A c2 B c3 C c4 D c5 EA B C D E array accessinteger addinteger compareincrementvariable assignmentfrequencies(depend on algorithm, input)Bottom line. We use approximate models in this course: T(N) c N 3.40

1.4 A NALYSISOFA LGORITHMS‣ introduction‣ observationsAlgorithmsR OBERT S EDGEWICK K EVIN W AYNEhttp://algs4.cs.princeton.edu‣ mathematical models‣ order-of-growth classifications‣ theory of algorithms‣ memory

Common order-of-growth classificationsDefinition. If f (N) c g(N) for some constant c 0, then the order of growthof f (N) is g(N).・Ignores leading coefficient.・Ignores lower-order terms.Ex. The order of growth of the running time of this code is N 3.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 (a[i] a[j] a[k] 0)count ;Typical usage. With running times.where leading coefficientdepends on machine, compiler, JVM, .42

timeCommon order-of-growth classifications200TGood news. The set of functions100T1, log N, N, N log N, N 2, N 3, and 2Nlogarithmicconstantsuffices to describe the order of growth of most common algorithms.100K200K500Ksizericeahmlinlineaquar itdraticcubic512Texponentiallog-log 12KTypical orders of growth43

Common order-of-growth classificationsorder ofgrowthnametypical code frameworkdescriptionexampleT(2N) / T(N)1constanta b c;statementadd twonumbers1log Nlogarithmicwhile (N 1)N N / 2; .divide in halfbinary searchNlinearfor (int i 0; i N; i ){ .}loopfind themaximum2N log Nlinearithmic[see mergesort lecture]divideand conquermergesort 2double loopcheck allpairs4for (int i 0; i N; i )for (int j 0; j N; j )for (int k 0; k N; k ){ .}triple loopcheck alltriples8[see combinatorial search lecture]exhaustivesearchcheck allsubsetsT(N)N2N32Nquadraticcubicexponential{}for (int i 0; i N; i )for (int j 0; j N; j ){ .} 144

Binary search demoGoal. 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 4lohi45

Binary search: Java implementationTrivial to implement?・First binary search published in 1946.・First bug-free 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.length-1;while (lo hi){int mid lo (hi - lo) / 2;if(key a[mid]) hi mid - 1;else if (key a[mid]) lo mid 1;else return mid;}return -1;one "3-way compare"}Invariant. If key appears in the array a[], then a[lo] key a[hi].46

Binary search: mathematical analysisProposition. Binary search uses at most 1 lg N key compares to search ina 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.left or right half(floored division)possible to implement with one2-way compare (instead of 3-way)Pf sketch. [assume N is a power of 2]T (N) T (N / 2) 1[ given ] T (N / 4) 1 1[ apply recurrence to first term ] T (N / 8) 1 1 1[ apply recurrence to first term ] T (N / N) 1 1 1[ stop applying, T(1) 1 ] 1 lg N 47

An N2 log N algorithm for 3-SUMAlgorithm.・・Step 2:inputStep 1: Sort the N (distinct) numbers.For each pair of numbers a[i]and a[j], binary search for -(a[i] a[j]).30 -40 -20 -10 400 105sort-40 -20 -1005 10 30 40binary search(-40, -20)60(-40, -10)50N 2 with insertion sort.(-40,0)40N 2 log N with binary search.(-40,5)35(-40,10)30Analysis. Order of growth is・Step 1:・Step 2:N2log N. (-20, -10)Remark. Can achieve N 2 by modifyingbinary search step. (-10,30 0) 10 ( 10,30)-40( 10,40)-50( 30,40)-70only count ifa[i] a[j] a[k]to avoiddouble counting48

Comparing programsHypothesis. The sorting-based N 2 log N algorithm for 3-SUM is significantlyfaster in practice than the brute-force N 3 algorithm.Ntime (seconds)Ntime 4,00059.16ThreeSum.javaThreeSumDeluxe.javaGuiding principle. Typically, better order of growth faster in practice.49

1.4 A NALYSISOFA LGORITHMS‣ introduction‣ observationsAlgorithmsR OBERT S EDGEWICK K EVIN W AYNEhttp://algs4.cs.princeton.edu‣ mathematical models‣ order-of-growth classifications‣ theory of algorithms‣ memory

Types of analysesBest 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 courseAverage case. Expected cost for random input.・Need a model for “random” input.・Provides a way to predict performance.Ex 1. Array accesses for brute-force 3-SUM.Ex 2. Compares for binary search.Best: ½ N3Best: 1Average: ½ N3Average: lg NWorst: ½ N3Worst: lg N51

Theory of algorithmsGoals.・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).52

Commonly-used notations in the theory of algorithmsnotationprovidesexampleshorthand forused to½ N2Big Thetaasymptoticorder of growthΘ(N2)10 N 25 N 2 22 N log N 3Nclassifyalgorithms 10 N 2Big OhΘ(N2)and smallerO(N2)100 N22 N log N 3 Ndevelopupper bounds ½N2Big OmegaΘ(N2)and largerΩ(N2)N5N 3 22 N log N 3 N developlower bounds

Theory of algorithms: example 1Goals.・Establish “difficulty” of a problem and develop “optimal” algorithms.・Ex. 1-SUM “Is there a 0 in the array? ”Upper bound. A specific algorithm.・Ex. Brute-force algorithm for 1-SUM: Look at every array entry.・Running time of the optimal algorithm for 1-SUM 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 1-SUM is Ω(N).Optimal algorithm.・Lower bound equals upper bound (to within a constant factor).・Ex. Brute-force algorithm for 1-SUM is optimal: its running time is Θ(N).54

Theory of algorithms: example 2Goals.・Establish “difficulty” of a problem and develop “optimal” algorithms.・Ex. 3-SUM.Upper bound. A specific algorithm.・Ex. Brute-force algorithm for 3-SUM.・Running time of the optimal algorithm for 3-SUM is O(N3).55

Theory of algorithms: example 2Goals.・Establish “difficulty” of a problem and develop “optimal” algorithms.・Ex. 3-SUM.Upper bound. A specific algorithm.・Ex. Improved algorithm for 3-SUM.・Running time of the optimal algorithm for 3-SUM is O(N2 logN ).Lower bound. Proof that no algorithm can do better.・Ex. Have to examine all N entries to solve 3-SUM.・Running time of the optimal algorithm for solving 3-SUM is Ω(N ).Open problems.・Optimal algorithm for 3-SUM?・Subquadratic algorithm for 3-SUM?・Quadratic lower bound for 3-SUM?56

Algorithm design approachStart.・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.57

Commonly-used notations in the theory of algorithmsnotationTildeprovidesleading termexample 10 N 2shorthand forused to10 N 2provideapproximatemodel10 N 2 22 N log N10 N 2 2 N 37Big Thetaasymptoticorder of growth½ N2Θ(N2)10 Nclassifyalgorithms25 N 2 22 N log N 3N10 N 2Big OhΘ(N2)and smallerO(N2)100 Ndevelopupper bounds22 N log N 3 N½N2Big OmegaΘ(N2) and largerΩ(N2)N5developlower boundsN 3 22 N log N 3 NCommon mistake. Interpreting big-Oh as an approximate model.This course. Focus on approximate models: use Tilde-notation58

1.4 A NALYSISOFA LGORITHMS‣ introduction‣ observationsAlgorithmsR OBERT S EDGEWICK K EVIN W AYNEhttp://algs4.cs.princeton.edu‣ mathematical models‣ order-of-growth classifications‣ theory of algorithms‣ memory

BasicsBit. 0 or 1.NISTmost computer scientistsByte. 8 bits.Megabyte (MB). 1 million or 220 bytes.Gigabyte (GB).1 billion or 230 bytes.64-bit machine. We assume a 64-bit machine with 8-byte pointers.・Can address more memory.・Pointers use more space.some JVMs "compress" ordinary objectpointers to 4 bytes to avoid this cost60

Typical memory usage for primitive types and arraystypebytestypebytesboolean1char[]2 N 24byte1int[]4 N 24char2double[]8 N 24int4float4long8doubleone-dimensional arraystypebyteschar[][] 2MNint[][] 4MNdouble[][] 8MN8primitive typestwo-dimensional arrays61

Typical memory usage for objects in JavaObject overhead. 16 bytes.integer wrapper objectReference. 8 bytes.public class IntegerPadding.{ Each object uses aprivate int x;.}Ex 1. A Date object uses 32date objectpublic class Date{private int day;private int month;private int year;.}24 bytesmultiple of 8 bytes.objectoverheadintxvaluebytes paddingof memory.32 bytesobjectoverhead16 bytes (object overhead)daymonthyearpadding4 bytes (int)intvalues4 bytes (int)4 bytes (int)4 bytes (padding)32 bytescounter objectpublic class Counter32 bytes62

Typical memory usage summaryTotal 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.63

ExampleQ. How much memory does WeightedQuickUnionUF use as a function of N ?Use tilde notation to simplify your answer.public class WeightedQuickUnionUF{private int[] id;private int[] sz;private int count;16 bytes(object overhead)8 (4N 24) bytes each(reference int[] array)4 bytes (int)4 bytes (padding)public WeightedQuickUnionUF(int N){id new int[N];sz new int[N];for (int i 0; i N; i ) id[i] i;for (int i 0; i N; i ) sz[i] 1;}.8N 88 bytes}A. 8 N 88 8 N bytes.64

Turning the crank: summaryEmpirical 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 modelsand to make predictions.65

9 Scientific 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