Algorithms. Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Size: px
Start display at page:

Download "Algorithms. Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS"

Transcription

1 Announcements Algorithms ROBERT SEDGEWICK KEVI WAYE First programming assignment. Due Tomorrow at :00pm. Try electronic submission system today. "Check All Submitted Files. will perform checks on your code. You may use this up to 0 times. Can still submit after you use up your checks. Should not be your primary testing technique! Registration. Register for Piazza. Register for Coursera. Register for Poll Everywhere. ROBERT Algorithms F O U R T H E D I T I O SEDGEWICK KEVI WAYE AALYSIS OF ALGORITHMS introduction empirical observations mathematical models order-of-growth classifications theory of algorithms memory Efficiency 6 vs. 6 6: Techniques for solving problems. 6: Techniques for solving problems efficiently. Algorithms ROBERT SEDGEWICK KEVI WAYE AALYSIS OF ALGORITHMS introduction empirical observations mathematical models order-of-growth classifications theory of algorithms memory Simple Example: Checking symmetry of an x matrix aive: Scan all elements. Better: Scan only elements above the diagonal (>x speedup). 4

2 Efficiency (more insidious example) Running time public static String concatenateospace(string s, String s) for (int i = 0; i < s.length(); i++) if (s.charat(i)!= ) s = s + s.charat(i); return s; 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 (864) Common Problem. ovice programmer does not understand performance characteristics how many times do you have to turn the crank? of data structure. Results in poor performance that gets WORSE with input size. Today Precise definitions of program performance. Experimental and theoretical techniques for measuring performance. Analytic Engine 5 The Life of the Philosopher 6 Reasons to analyze algorithms Predict performance. The iron folding-doors of the small-room or oven were opened. Captain Kater and myself entered, and they were closed upon us... The thermometer marked, if I recollect rightly, 65 degrees. The pulse was quickened, and I ought to have to have counted but did not count the number of inspirations per minute. Perspiration commenced immediately and was very copious. We remained, I believe, about five or six minutes without very great discomfort, and I experienced no subsequent inconvenience from the result of the experiment Charles Babbage, From the Life of the Philosopher this course Compare algorithms. Provide guarantees. Understand theoretical basis. theory of algorithms Primary practical reasons: avoid performance bugs enable new technologies example:simulation of galaxy formation client gets poor performance because programmer did not understand performance characteristics 65 Fahrenheit / 30 Celsius 7 8

3 Running time of programs The challenge Q. Will my program be able to solve a large practical input? Why is my program so slow? Why does it run out of memory? Programs Mathematical objects. Running on physical hardware. Mathematical model Left program runtime: c Right program runtime: c( / - /) Empirical observations Runtime of a program varies even when run on the same input. 9 Insight. [Knuth 970s] Use scientific method to understand performance. 0 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 by making further observations. Validate by repeating until the hypothesis and observations agree. Principles. Experiments must be reproducible. Hypotheses must be falsifiable. Algorithms ROBERT SEDGEWICK KEVI WAYE AALYSIS OF ALGORITHMS introduction empirical observations mathematical models order-of-growth classifications theory of algorithms memory Feature of the natural world. Computer itself.

4 Example: 3-SUM 3-SUM: brute-force algorithm 3-SUM. Given distinct integers, how many triples sum to exactly zero? % more 8ints.txt % java ThreeSum 8ints.txt a[i] a[j] a[k] sum public class ThreeSum public static int count(int[] a) int = a.length; int count = 0; for (int i = 0; i < ; i++) for (int j = i+; j < ; j++) for (int k = j+; k < ; k++) if (a[i] + a[j] + a[k] == 0) count++; return count; check each triple for simplicity, ignore integer overflow Context. Deeply related to problems in computational geometry. public static void main(string[] args) int[] a = In.readInts(args[0]); StdOut.println(count(a)); 3 4 Measuring the running time Measuring the running time Q. How to time a program? A. Manual. % java ThreeSum Kints.txt tick tick tick Q. How to time a program? A. Automatic. 70 % java ThreeSum Kints.txt 58 % java ThreeSum 4Kints.txt 4039 public class Stopwatch Stopwatch() (part of stdlib.jar ) create a new stopwatch double elapsedtime() time since creation (in seconds) public static void main(string[] args) int[] a = In.readInts(args[0]); Stopwatch stopwatch = new Stopwatch(); StdOut.println(ThreeSum.count(a)); double time = stopwatch.elapsedtime(); client code 5 6

5 Empirical analysis Empirical analysis Run the program for various input sizes and measure running time. Run the program for various input sizes and measure running time. time (seconds) ,000 0., , , ,000? 7 8 Data analysis Data analysis Standard plot. Plot running time T () vs. input size. Hard to form a useful hypothesis. standard plot running time T() Log-log plot. Plot running time T () vs. input size using log-log scale. log-log plot 3 orders of magnitude lg(t()) straight line of slope 3 T() = a b power law lg(t ()) = b lg + lg a lg(t ()) = b lg + c b =.999 c = a = K K 4K 8K lg K K 4K 8K problem size Regression. Fit straight line through data points: lg(t()) y = b x + = cb lg() + c Interpretation. T() = a b, where a = c Hypothesis. The running time is about seconds. slope 9 0

6 Prediction and validation Doubling hypothesis Hypothesis. The running time is about seconds. Predictions. 5.0 seconds for = 8, seconds for = 6,000. "order of growth" of running time is about 3 [stay tuned] Doubling hypothesis. Another way to build models of the form T() = a b Run program, doubling the size of the input. time (seconds) ratio lg ratio Observations. time (seconds) 8, , ,000 5., , , , lg (5. / 6.40) = 3.0 6, validates hypothesis! Hypothesis. Running time is about a b with b = lg ratio. seems to converge to a constant b 3 Doubling hypothesis Experimental algorithmics Doubling 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 ) and solve for a. time (seconds) 8, = a , a = System independent effects. Algorithm. Input data. System dependent effects. Hardware: CPU, memory, cache, Software: compiler, interpreter, garbage collector, determines exponent b in power law System: operating system, network, other apps, determines constant a in power law 8, Caveat. In some cases, b can depend on system (e.g. virtualization) Bad news. Difficult to get precise measurements. Hypothesis. Running time is about seconds. Good news. Much easier and cheaper than other sciences. almost identical hypothesis to one obtained via linear regression 3 e.g., can run huge number of experiments 4

7 Mathematical models for running time.4 AALYSIS OF ALGORITHMS Total running time: sum of cost frequency for all operations. eed to analyze program to determine set of operations. Cost depends on machine, compiler. Frequency depends on algorithm, input data. Algorithms ROBERT SEDGEWICK KEVI WAYE introduction empirical observations mathematical models order-of-growth classifications theory of algorithms memory Donald Knuth 974 Turing Award In principle, accurate mathematical models are available. 6 Timing basic operations (a hopeless endeavor) Cost of basic operations operation example nanoseconds integer add a + b. operation example nanoseconds integer multiply a * b.4 variable declaration int a c integer divide a / b 5.4 assignment statement a = b c floating-point add a + b 4.6 integer compare a < b c3 floating-point multiply a * b 4. array element access a[i] c4 floating-point divide a / b 3.5 sine Math.sin(theta) 9.3 arctangent Math.atan(y, x) 9.0 array length a.length c5 D array allocation new int[] c6 D array allocation new int[][] c string length s.length() c8 substring extraction s.substring(/, ) c9 Running OS X on Macbook Pro.GHz with GB RAM string concatenation s + t c0 Computer Architecture Caveats (see COS 475). Most computers are more like assembly lines than oracles (pipelining). ovice Register vs. cache vs. RAM vs. hard disk (Java is a high level language) 7 mistake. Abusive string concatenation. 8

8 Example: -SUM Example: -SUM Q. How many instructions as a function of input size? Q. How many instructions as a function of input size? int count = 0; for (int i = 0; i < ; i++) if (a[i] == 0) count++; int count = 0; for (int i = 0; i < ; i++) for (int j = i+; j < ; j++) if (a[i] + a[j] == 0) count++; array accesses operation frequency Frequency, =0000 variable declaration assignment statement less than compare equal to compare 0000 Alternate Pf ( ) = ( ) = array access 0000 increment to 0000 to ( ) = half of square half of diagonal 30 Example: -SUM Simplifying the calculations Q. How many instructions as a function of input size? int count = 0; for (int i = 0; i < ; i++) for (int j = i+; j < ; j++) if (a[i] + a[j] == 0) count++; operation frequency ( ) = ( ) = 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 variable declaration + assignment statement + less than compare ½ ( + ) ( + ) equal to compare ½ ( ) array access ( ) increment ½ ( ) to ( ) tedious to count exactly ROUDIG-OFF ERRORS I MATRIX PROCESSES By A. M. TURIG ational Physical Laboratory, Teddington, Middlesex) [Received 4 ovember 947] SUMMARY A number of methods of solving sets of linear equations and inverting matrices are discussed. The theory of the rounding-off errors involved is investigated for some of the methods. In all cases examined, including the well-known 'Gauss elimination process', it is found that the errors are normally quite moderate: no exponential build-up need occur. 3 3

9 Simplification : cost model Cost model. Use some basic operation as a proxy for running time. int count = 0; for (int i = 0; i < ; i++) for (int j = i+; j < ; j++) if (a[i] + a[j] == 0) count++; operation frequency variable declaration + assignment statement ( ) = ( ) = Simplification : tilde notation Estimate running time (or memory) as a function of input size. Ignore lower order terms. when is large, terms are negligible when is small, we don't care Ex. ⅙ ! ~ ⅙ 3 Ex. ⅙ /3 + 56! ~ ⅙ 3 Ex 3. ⅙ 3 - ½ + ⅓! ~ ⅙ 3 discard lower-order terms (e.g., = 000: million vs million) 66,666,667,000 3 /6 3 /6 / + /3 66,67,000 Leading-term approximation less than compare ½ ( + ) ( + ) equal to compare ½ ( ) array access ( ) increment ½ ( ) to ( ) cost model = array accesses (we assume compiler/jvm do not optimize any array accesses away!) Technical definition. f() ~ g() means lim f () g() = Simplification : tilde notation Estimate running time (or memory) as a function of input size. Ignore lower order terms. when is large, terms are negligible when is small, we don't care Example: -SUM Q. Approximately how many array accesses as a function of input size? int count = 0; for (int i = 0; i < ; i++) for (int j = i+; j < ; j++) if (a[i] + a[j] == 0) count++; "inner loop" operation frequency tilde notation variable declaration + ~ assignment statement + ~ less than compare ½ ( + ) ( + ) ~ ½ equal to compare ½ ( ) ~ ½ array access ( ) ~ A. ~ array accesses. Because (½ ) = ( ) = ( ) = increment ½ ( ) to ( ) ~ ½ to ~ Bottom line. Use cost model and tilde notation to simplify counts

10 Example: 3-SUM Estimating a discrete sum Q. Approximately how many array accesses as a function of input size? int count = 0; for (int i = 0; i < ; i++) for (int j = i+; j < ; j++) for (int k = j+; k < ; k++) if (a[i] + a[j] + a[k] == 0) count++; "inner loop" Q. How to estimate a discrete sum? A. Take discrete mathematics course. A. Replace the sum with an integral, and use calculus! Ex i= i x= xdx A. ~ ½ 3 array accesses. Because (3/6 3 ) = ½ 3 3 = ( )( ) 3! 6 3 Ex. k + k + + k. Ex 3. + / + /3 + + /. i= i= i k i x= x= x k dx dx x = ln k + k+ Bottom line. Use cost model and tilde notation to simplify counts. Ex 4. 3-sum triple loop. i= j=i k=j x= y=x z=y dz dy dx Estimating a discrete sum Estimating a discrete sum Q. How to estimate a discrete sum? A. Take discrete mathematics course. A. Replace the sum with an integral, and use calculus! Q. How to estimate a discrete sum? A3. Use Maple or Wolfram Alpha. Ex 4. + ½ + ¼ + ⅛ + i=0 i = x=0 x dx = ln.447 wolframalpha.com [wayne:nobel.princeton.edu] > maple5 \^/ Maple 5 (X86 64 LIUX)._ \ / _. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 0 \ MAPLE / All rights reserved. Maple is a trademark of < > Waterloo Maple Inc. Type? for help. > factor(sum(sum(sum(, k=j+..), j = i+..), i =..)); Caveat. Integral trick doesn't always work! ( - ) ( - )

11 Mathematical models for running time In principle, accurate mathematical models are available. In practice, Formulas can be complicated. Realities of hardware impact accuracy of formulas. Advanced mathematics might be required. Exact models best left for experts. costs (depend on machine, compiler) T = c A + c B + c3 C + c4 D + c5 E A = array access B = integer add C = integer compare D = increment E = variable assignment frequencies (depend on algorithm, input) Algorithms ROBERT SEDGEWICK KEVI WAYE AALYSIS OF ALGORITHMS introduction empirical observations mathematical models order-of-growth classifications theory of algorithms memory Bottom line. We use approximate models in this course: T() ~ c 3. 4 Order-of-growth Definition. If f() ~ a g(), then the order-of-growth of f() is just g() Example: Runtime of 3SUM: ~ /6 t 3 [see page 8] Order-of-growth of the runtime of 3SUM: 3 We often say order-of-growth of 3SUM as shorthand for the runtime. Common order-of-growth classifications Good news. the small set of functions, log,, log,, 3, and suffices to describe order-of-growth of typical algorithms. log-log plot 5T exponential cubic quadratic order of growth discards leading coefficient linearithmic linear 64T time int count = 0; for (int i = 0; i < ; i++) for (int j = i+; j < ; j++) for (int k = j+; k < ; k++) if (a[i] + a[j] + a[k] == 0) count++; Time to execute: t 8T 4T T T logarithmic constant K K 4K 8K 5K size Typical orders of growth 43 44

12 Common order-of-growth classifications Practical implications of order-of-growth order of growth name typical code framework description example T() / T() constant a = b[0] + b[]; statement log logarithmic linear while ( > ) = / ;... for (int i = 0; i < ; i++)... log linearithmic [see mergesort lecture] quadratic for (int i = 0; i < ; i++) for (int j = 0; j < ; j++)... add two array elements divide in half binary search ~ loop divide and conquer double loop find the maximum mergesort ~ check all pairs 4 problem size solvable in minutes growth rate 970s 980s 990s 000s any any any any log any any any any millions tens of hundreds of millions millions billions log hundreds of hundreds of millions millions thousands millions hundreds thousand thousands tens of thousands game changer 3 cubic for (int i = 0; i < ; i++) for (int j = 0; j < ; j++) for (int k = 0; k < ; k++)... triple loop check all triples 8 3 hundred hundreds thousand thousands 0 0s 0s 30 exponential [see combinatorial search lecture] exhaustive search check all subsets T() Bottom line. eed linear or linearithmic alg to keep pace with Moore's law Some algorithmic successes Binary search demo -body simulation. Simulate gravitational interactions among bodies. Brute force: steps. Barnes-Hut algorithm: log steps, enables new research. Andrew time 64T quadratic Appel PU '8 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 3T T linearithmic T linear lo hi size K K 4K 8K Worst case: lg c 47 see Coursera for rigorous proof 48

13 Binary search: Java implementation An log algorithm for 3-SUM Trivial to implement? First binary search published in 946; first bug-free one in 96. Bug in Java's Arrays.binarySearch() discovered in 006. Sorting-based algorithm. Step : Sort the (distinct) numbers. Step : For each pair of numbers a[i] and a[j], binary search for -(a[i] + a[j]). input sort public static int binarysearch(int[] a, int key) int lo = 0, hi = a.length-; while (lo <= hi) int mid = lo + (hi - lo) / ; if (key < a[mid]) hi = mid - ; else if (key > a[mid]) lo = mid + ; else return mid; return -; Invariant. If key appears in the array a[], then a[lo] key a[hi]. one "3-way compare" Analysis. Order of growth is log. Step : with insertion sort. Step : log with binary search. binary searches, each log Remark. Can achieve by modifying binary search step. binary search (-40, -0) 60 (-40, -0) 50 (-40, 0) 40 (-40, 5) 35 (-40, 0) 30 (-40, 40) 0 (-0, -0) 30 (-0, 0) 0 ( 0, 30) -40 ( 0, 40) -50 ( 30, 40) -70 only count if a[i] < a[j] < a[k] to avoid double counting Comparing programs Hypothesis. The sorting-based log algorithm for 3-SUM is significantly faster in practice than the brute-force 3 algorithm. time (seconds), time (seconds), AALYSIS OF ALGORITHMS, , , ThreeSum.java, , , , , , Algorithms ROBERT SEDGEWICK KEVI WAYE introduction empirical observations mathematical models order-of-growth classifications theory of algorithms memory ThreeSumDeluxe.java Guiding principle. Typically, better order of growth faster in practice. 5

14 Types of analyses: Performance depends on input Types of analyses: Performance depends on input 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. Average case. Expected cost for random input. eed a model for random input. Provides a way to predict performance. 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. Average case. Expected cost for random input. eed a model for random input. Provides a way to predict performance. Where you lie depends on your input! Ex. Compares for binary search. Best: Ex. Array accesses for brute-force 3-SUM. Best: 3 Ex. Compares for binary search. Ex. Array accesses for brute-force 3-SUM. Average: lg Average: 3 Worst: lg Worst: 3 log log Types of analyses Best case. Lower bound on cost. Worst case. Upper bound on cost (guarantee). Average case. Expected cost. Primary practical reason: avoid performance bugs. Example: Algorithm selection Given arbitrary data, performance may be anywhere in our bounds. Approach : depend on worst case guarantee. Example: Use Mergesort instead of Quicksort Approach : randomize, depend on probabilistic guarantee. Example: Randomize input before giving to Quicksort Mergesort. [next week] Quicksort. [next week] Theory of algorithms Previous slides Best, average, and worst case for a specific algorithm. ew goals. Establish difficulty of a problem, e.g. how hard is 3SUM? Develop optimal algorithm. Approach: Use order-of-growth in worst case Use order-of-growth (just like we ve been doing). Analysis is asymptotic, i.e. for very large. Analysis is to within a constant factor, using OaG instead of Tilde. Consider only worst case. Analysis avoids messy input models. Analysis focuses on guarantees. log log log log log 55 56

15 Theory of algorithms Theory of algorithms Testing optimality of algorithm A for problem P Find worst case order of growth guarantee for specific algorithm A, g() Find lower bound on guarantee for any algorithm that solves P, B() If they match, i.e. g() = B(), then: Worst case performance of A is asymptotically optimal. Optimal algorithm for P has order of growth g() If they don t, g() at least provides an upper bound. Example: The -SUM problem (how many 0s?) Let A be the brute force algorithm where we simply look at each entry and count the zeros. Worst case order of growth: g() = Of any algorithm that solves -SUM, must at least examine every entry. Lower bound on worst case order of growth: B() = g() = B(). A is optimal! Algorithm A g() Brute force algorithm???? g() don t care don t care Lower bound for best algorithm B() Lower bound for best algorithm???? B() Worst case performance for optimal algorithm 57 don t care don t care Worst case performance for optimal -SUM algorithm 58 Theory of algorithms: example Theory of algorithms: example Example: The 3-SUM problem (how many 0s?) Let A be the brute force algorithm where we look at each triple. Worst case order of growth: g() = 3 Of any algorithm that solves 3-SUM, must at least examine every entry. Lower bound on worst case order of growth: B() = g() B() Brute force algorithm don t care don t care 3 Lower bound for best algorithm 3 Brute force algorithm don t care don t care 3 3 don t care don t care Worst case performance for optimal -SUM algorithm What this tells us It is possible to solve 3SUM in 3 time in the worst case. The optimal algorithm has worst case running time OaG between and 3. Lower bound for best algorithm don t care don t care Worst case performance for optimal 3-SUM algorithm 59 What this doesn t tell us Is there an algorithm with better running time than 3 in the worst case? Is there some clever way of finding a better lower bound than? 60

16 Theory of algorithms terminology Theory of algorithms terminology Testing optimality of algorithm A for problem P Find worst case order of growth guarantee for specific algorithm A, g() Find lower bound on guarantee for any algorithm that solves P, B() Testing optimality of brute force algorithm for 3-SUM Find worst case order of growth guarantee for brute force, 3 Find lower bound on guarantee for any algorithm that solves P, Algorithm A g() O(g()) Algorithm A 3 O( 3 ) don t care don t care g() don t care don t care 3 Lower bound for best algorithm B() Ω(B()) Lower bound for best algorithm Ω() don t care don t care B() Worst case performance for optimal algorithm don t care don t care Worst case performance for optimal algorithm Standard terminology The running time of the optimal algorithm for problem P is O(g()) The running time of the optimal algorithm for problem P is Ω(B()) 6 Standard terminology The running time of the optimal algorithm for problem P is O(3 ) The running time of the optimal algorithm for problem P is Ω() 6 Commonly-used notations in the theory of algorithms Theory of algorithm: 3SUM notation provides example shorthand for used to Big Theta asymptotic order of growth Θ( ) ½ log + 3 classify algorithms The run time of 3SUM s optimal solution... =O(3 ) based on brute force solution. =O( log ) based on binary search based solution. =O( ) based on solution developed in precept this week. =Ω() based on a simple argument about accessing all data. Grows at least as slow as, and at least as fast as. Big Oh Θ( ) and smaller O( ) Big Omega Θ( ) and larger Ω( ) 0 00 develop log + 3 upper bounds ½ 5 develop 3 + log + 3 lower bounds Example: Optimal algorithm of 3-SUM is O( 3 ) based on brute force solution. (i.e. its order of growth is 3 or less) Open questions What is the order of growth of the optimal solution for 3-SUM? Equivalent question: If it is Θ(B(n)) in the worst case, what is B(n)? We know it is between and. Does there exist an algorithm with worst case run time better than? i.e. an algorithm that is better than Θ( ) in the worst case? Does there exist a way to provide a quadratic lower bound on 3SUM? i.e. can we prove that the optimal algorithm for 3-SUM is Ω( )? 64 63

17 Algorithm design approach To-within a constant factor 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 (970s-Present*). Steadily decreasing upper bounds for many important problems. Many known optimal algorithms. Caveats. Overly pessimistic to focus on worst case? Can do better than to within a constant factor to predict performance. Worst case performance for optimal algorithm Asymptotic performance not always useful (e.g. matrix multiplication). O Ω 65 notation provides example shorthand for used to Tilde leading term ~ log 0 Big Theta asymptotic growth rate Θ( ) Big Oh Θ( ) and smaller O( ) Big Omega Θ( ) and larger Ω( ) ½ log log + 3 ½ Common practice. Analysis to within a constant factor. provide approximate model classify algorithms develop upper bounds Easy to be more precise. Use Tilde-notation instead of Big Theta (or Big Oh) log + 3 develop lower bounds 66 Order of growth isn t everything - Matrix multiplication year algorithm order of growth? brute force Strassen Pan Bini.780 Only faster for huge matrices!.4 AALYSIS OF ALGORITHMS 98 Schönhage.5 98 Romani Coppersmith-Winograd Strassen Coppersmith-Winograd Strother.3737 Algorithms ROBERT SEDGEWICK KEVI WAYE introduction empirical observations mathematical models order-of-growth classifications theory of algorithms memory 0 Williams.377 number of floating-point operations to multiply two -by- matrices Asymptotic performance not always useful (e.g. matrix multiplication). 67

18 Basics Typical memory usage for primitive types and arrays Bit. 0 or. Byte. 8 bits. Megabyte (MB). million or 0 bytes. Gigabyte (GB). hard drives IST billion or 30 bytes. 64-bit machine. We assume a 64-bit machine with 8 byte pointers. Can address more memory. Pointers use more space. most computer scientists some JVMs "compress" ordinary object pointers to 4 bytes to avoid this cost type bytes boolean byte char int 4 float 4 long 8 double 8 for primitive types type bytes char[] + 4 int[] double[] for one-dimensional arrays type bytes char[][] ~ M int[][] ~ 4 M double[][] ~ 8 M for two-dimensional arrays Typical memory usage for objects in Java Typical memory usage summary Object overhead. 6 bytes (+8 if inner class). Reference. 8 bytes. Padding. Each object uses a multiple of 8 bytes. Ex. A Date object uses 3 bytes of memory. Total memory usage for a data type value: Primitive type: 4 bytes for int, 8 bytes for double, Object reference: 8 bytes. Array: 4 bytes + memory for each array entry. Object: 6 bytes + memory for each instance variable + 8 bytes if inner class (for pointer to enclosing class). Padding: round up to multiple of 8 bytes. public class Date private int day; private int month; private int year;... object overhead day month year padding int values 6 bytes (object overhead) 4 bytes (padding) Shallow memory usage: Don't count referenced objects. Deep memory usage: If array entry or instance variable is a reference, add memory (recursively) for referenced object. 3 bytes 7 7

19 Typical memory usage for objects in Java Typical memory usage for objects in Java Total memory usage for a data type value: Primitive type: 4 bytes for int, 8 bytes for double, Object reference: 8 bytes. Array: 4 bytes + memory for each array entry. Object: 6 bytes + memory for each instance variable + 8 bytes if inner class (for pointer to enclosing class). Padding: round up to multiple of 8 bytes. Total memory usage for a data type value: Primitive type: 4 bytes for int, 8 bytes for double, Object reference: 8 bytes. Array: 4 bytes + memory for each array entry. Object: 6 bytes + memory for each instance variable + 8 bytes if inner class (for pointer to enclosing class). Padding: round up to multiple of 8 bytes. public class String private char[] value; private int offset; private int count; private int hash;... object overhead value offset count hash padding reference int values 6 bytes (object overhead) 8 bytes (reference to array) + 4 bytes (char[] array) 4 bytes (padding) public class String private char[] value; private int offset; private int count; private int hash;... object overhead value offset count hash padding reference int values 6 bytes (object overhead) 8 bytes (reference to array) + 4 bytes (char[] array) 4 bytes (padding) Ex. A Java 6 string object uses ~ bytes (deep). Deep memory: + 64 bytes ~ bytes 73 Ex. A Java 6 string object uses 40 bytes (shallow memory). Shallow memory: 40 bytes 74 Example Turning the crank: summary Q. How much memory does WeightedQuickUnionUF use as a function of? Use tilde notation to simplify your answer. public class WeightedQuickUnionUF private int[] id; private int[] sz; private int count; public WeightedQuickUnionUF(int ) id = new int[]; sz = new int[]; for (int i = 0; i < ; i++) id[i] = i; for (int i = 0; i < ; i++) sz[i] = ;... A ~ 8 bytes. 6 bytes (object overhead) 8 + (4 + 4) each reference + int[] array 4 bytes (padding) 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 or order of growth 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

Algorithms. Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS. introduction. introduction observations. observations

Algorithms. Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS. introduction. introduction observations. observations Algorithms ROBERT SEDGEWICK KEVI WAYE.4 AALYSIS OF ALGORITHMS.4 AALYSIS OF ALGORITHMS introduction introduction observations observations Algorithms F O U R T H E D I T I O mathematical models order-of-growth

More information

Algorithms. Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Algorithms. Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS Announcements Algorithms ROBERT SEDGEWICK KEVI WAYE First programming assignment. Due Tuesday at pm. Try electronic submission system today. "Check All Submitted Files. will perform checks on your code

More information

1.4 Analysis of Algorithms

1.4 Analysis of Algorithms 1.4 Analysis of Algorithms Cast of characters Programmer needs to develop a working solution. Client wants to solve problem efficiently. Student might play any or all of these roles someday. observations

More information

Algorithms. Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS. introduction. introduction observations. observations

Algorithms. Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS. introduction. introduction observations. observations Algorithms ROBERT SEDGEWICK KEVI WAYE.4 AALYSIS OF ALGORITHMS.4 AALYSIS OF ALGORITHMS introduction introduction observations observations Algorithms F O U R T H E D I T I O mathematical models order-of-growth

More information

Data Structures 1.4 ANALYSIS OF ALGORITHMS. introduction observations mathematical models order-of-growth classifications theory of algorithms memory

Data Structures 1.4 ANALYSIS OF ALGORITHMS. introduction observations mathematical models order-of-growth classifications theory of algorithms memory Data Structures 1.4 ANALYSIS OF ALGORITHMS introduction observations mathematical models order-of-growth classifications theory of algorithms memory 1.4 ANALYSIS OF ALGORITHMS introduction observations

More information

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 1.4 ANALYSIS OF ALGORITHMS Algorithms F O U R T H E D I T I O N http://algs4.cs.princeton.edu introduction observations mathematical

More information

1.4 Analysis of Algorithms

1.4 Analysis of Algorithms 1.4 Analysis of Algorithms observations mathematical models order-of-growth classifications dependencies on inputs memory Algorithms, 4 th Edition Robert Sedgewick and Kevin Wayne Copyright 2002 2010 September

More information

Algorithms 1.4 ANALYSIS OF ALGORITHMS. observations mathematical models order-of-growth classifications dependencies on inputs memory

Algorithms 1.4 ANALYSIS OF ALGORITHMS. observations mathematical models order-of-growth classifications dependencies on inputs memory 1.4 ANALYSIS OF ALGORITHMS Algorithms F O U R T H E D I T I O N observations mathematical models order-of-growth classifications dependencies on inputs memory R O B E R T S E D G E W I C K K E V I N W

More information

ANALYSIS OF ALGORITHMS

ANALYSIS OF ALGORITHMS BBM 202 - ALGORITHMS DEPT. OF COMPUTER EGIEERIG Analysis of Algorithms Observations Mathematical models Order-of-growth classifications Dependencies on inputs Memory TODAY AALYSIS OF ALGORITHMS Acknowledgement:

More information

4.1 Performance. Running Time. The Challenge. Scientific Method

4.1 Performance. Running Time. The Challenge. Scientific Method Running Time 4.1 Performance 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

More information

Running Time. Analytic Engine. Charles Babbage (1864) how many times do you have to turn the crank?

Running Time. Analytic Engine. Charles Babbage (1864) how many times do you have to turn the crank? 4.1 Performance Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 3/30/11 8:32 PM Running Time As soon as an Analytic Engine exists,

More information

Analysis of Algorithms

Analysis of Algorithms Running time Analysis of Algorithms 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 -

More information

4.1 Performance. Running Time. The Challenge. Scientific Method. Reasons to Analyze Algorithms. Algorithmic Successes

4.1 Performance. Running Time. The Challenge. Scientific Method. Reasons to Analyze Algorithms. Algorithmic Successes Running Time 4.1 Performance 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

More information

4.1 Performance. Running Time. Scientific Method. Algorithmic Successes

4.1 Performance. Running Time. Scientific Method. Algorithmic Successes Running Time 4.1 Performance 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

More information

Running Time. Analytic Engine. Charles Babbage (1864) how many times do you have to turn the crank?

Running Time. Analytic Engine. Charles Babbage (1864) how many times do you have to turn the crank? 4.1 Performance Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2008 March 30, 2009 7:37 tt Running Time As soon as an Analytic Engine exists,

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming Running Time CS 112 Introduction to Programming 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

More information

4.1 Performance Analysis

4.1 Performance Analysis 4.1 Performance Analysis 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

More information

Algorithms and Data Structures, Fall Introduction. Rasmus Pagh. Based on slides by Kevin Wayne, Princeton. Monday, August 29, 11

Algorithms and Data Structures, Fall Introduction. Rasmus Pagh. Based on slides by Kevin Wayne, Princeton. Monday, August 29, 11 Algorithms and Data Structures, Fall 2011 Introduction Rasmus Pagh Based on slides by Kevin Wayne, Princeton Why study algorithms? Their impact is broad and far-reaching. Internet. Web search, packet routing,

More information

4.1, 4.2 Performance, with Sorting

4.1, 4.2 Performance, with Sorting 1 4.1, 4.2 Performance, with Sorting 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

More information

8. Performance Analysis

8. Performance Analysis COMPUTER SCIENCE S E D G E W I C K / W A Y N E 8. Performance Analysis Section 4.1 http://introcs.cs.princeton.edu COMPUTER SCIENCE S E D G E W I C K / W A Y N E 8. Performance analysis The challenge Empirical

More information

CS.7.A.Performance.Challenge

CS.7.A.Performance.Challenge PA R T I : P R O G R A M M I G I J AVA PA R T I : P R O G R A M M I G I J AVA Computer Science Computer Science The challenge Empirical analysis Mathematical models Doubling method An Interdisciplinary

More information

Analysis of Algorithms 1 / 18

Analysis of Algorithms 1 / 18 Analysis of Algorithms 1 / 18 Outline 1 Complexity of Algorithms 2 Time Complexity 3 Space Complexity 2 / 18 Complexity of Algorithms Questions that arise when we write programs that process large amounts

More information

http://www.flickr.com/photos/exoticcarlife/3270764550/ http://www.flickr.com/photos/roel1943/5436844655/ Performance analysis Why we care What we measure and how How functions grow Empirical analysis The

More information

1.4 ANALYSIS OF ALGORITHMS

1.4 ANALYSIS OF ALGORITHMS 1.4 ANALYSIS OF ALGORITHMS AS people gain experience using computers, they use them to solve difficult problems or to process large amounts of data and are invariably led to questions like these: How long

More information

Sorting. 4.2 Sorting and Searching. Sorting. Sorting. Insertion Sort. Sorting. Sorting problem. Rearrange N items in ascending order.

Sorting. 4.2 Sorting and Searching. Sorting. Sorting. Insertion Sort. Sorting. Sorting problem. Rearrange N items in ascending order. 4.2 and Searching pentrust.org Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 23/2/2012 15:04:54 pentrust.org pentrust.org shanghaiscrap.org

More information

CS/COE 1501

CS/COE 1501 CS/COE 1501 www.cs.pitt.edu/~nlf4/cs1501/ Introduction Meta-notes These notes are intended for use by students in CS1501 at the University of Pittsburgh. They are provided free of charge and may not be

More information

CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction

CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction Meta-notes These notes are intended for use by students in CS1501 at the University of Pittsburgh. They are provided free of charge and may not be sold

More information

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS Lecture 03-04 PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS By: Dr. Zahoor Jan 1 ALGORITHM DEFINITION A finite set of statements that guarantees an optimal solution in finite interval of time 2 GOOD ALGORITHMS?

More information

4.2 Sorting and Searching. Section 4.2

4.2 Sorting and Searching. Section 4.2 4.2 Sorting and Searching 1 Sequential Search Scan through array, looking for key. Search hit: return array index. Search miss: return -1. public static int search(string key, String[] a) { int N = a.length;

More information

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims Lecture 10: Asymptotic Complexity and What Makes a Good Algorithm? Suppose you have two possible algorithms or

More information

csci 210: Data Structures Program Analysis

csci 210: Data Structures Program Analysis csci 210: Data Structures Program Analysis Summary Summary analysis of algorithms asymptotic analysis and notation big-o big-omega big-theta commonly used functions discrete math refresher Analysis of

More information

Lecture Notes for Chapter 2: Getting Started

Lecture Notes for Chapter 2: Getting Started Instant download and all chapters Instructor's Manual Introduction To Algorithms 2nd Edition Thomas H. Cormen, Clara Lee, Erica Lin https://testbankdata.com/download/instructors-manual-introduction-algorithms-2ndedition-thomas-h-cormen-clara-lee-erica-lin/

More information

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014 CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014 Previously, on CSE 373 We want to analyze algorithms for efficiency (in time and space) And do so generally

More information

Computational biology course IST 2015/2016

Computational biology course IST 2015/2016 Computational biology course IST 2015/2016 Introduc)on to Algorithms! Algorithms: problem- solving methods suitable for implementation as a computer program! Data structures: objects created to organize

More information

Week 12: Running Time and Performance

Week 12: Running Time and Performance Week 12: Running Time and Performance 1 Most of the problems you have written in this class run in a few seconds or less Some kinds of programs can take much longer: Chess algorithms (Deep Blue) Routing

More information

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search 10/5/2016 CSE373: Data Structures and Algorithms Asymptotic Analysis (Big O,, and ) Steve Tanimoto Autumn 2016 This lecture material represents the work of multiple instructors at the University of Washington.

More information

CS240 Fall Mike Lam, Professor. Algorithm Analysis

CS240 Fall Mike Lam, Professor. Algorithm Analysis CS240 Fall 2014 Mike Lam, Professor Algorithm Analysis HW1 Grades are Posted Grades were generally good Check my comments! Come talk to me if you have any questions PA1 is Due 9/17 @ noon Web-CAT submission

More information

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance Algorithm (03F) Lecture3: Algorithm Analysis A step by step procedure to solve a problem Start from an initial state and input Proceed through a finite number of successive states Stop when reaching a

More information

CS240 Fall Mike Lam, Professor. Algorithm Analysis

CS240 Fall Mike Lam, Professor. Algorithm Analysis CS240 Fall 2014 Mike Lam, Professor Algorithm Analysis Algorithm Analysis Motivation: what and why Mathematical functions Comparative & asymptotic analysis Big-O notation ("Big-Oh" in textbook) Analyzing

More information

Computer Science Approach to problem solving

Computer Science Approach to problem solving Computer Science Approach to problem solving If my boss / supervisor / teacher formulates a problem to be solved urgently, can I write a program to efficiently solve this problem??? Polynomial-Time Brute

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Spring 2019 Alexis Maciel Department of Computer Science Clarkson University Copyright c 2019 Alexis Maciel ii Contents 1 Analysis of Algorithms 1 1.1 Introduction.................................

More information

Performance. CSCI 135: Fundamentals of Computer Science Keith Vertanen. h"p://

Performance. CSCI 135: Fundamentals of Computer Science Keith Vertanen. hp:// Performance h"p://www.flickr.com/photos/exo3ccarlife/3270764550/ h"p://www.flickr.com/photos/roel1943/5436844655/ CSCI 135: Fundamentals of Computer Science Keith Vertanen Performance analysis Overview

More information

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation Comp2711 S1 2006 Correctness Oheads 1 Efficiency Issues Comp2711 S1 2006 Correctness Oheads 2 Run Times An implementation may be correct with respect to the Specification Pre- and Post-condition, but nevertheless

More information

Principles of Algorithm Analysis. Biostatistics 615/815

Principles of Algorithm Analysis. Biostatistics 615/815 Principles of Algorithm Analysis Biostatistics 615/815 Lecture 3 Snapshot of Incoming Class 25 Programming Languages 20 15 10 5 0 R C/C++ MatLab SAS Java Other Can you describe the QuickSort Algorithm?

More information

Analysis of Algorithms. 5-Dec-16

Analysis of Algorithms. 5-Dec-16 Analysis of Algorithms 5-Dec-16 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based on the size of the input (time complexity), and/or developing

More information

CS 310: Order Notation (aka Big-O and friends)

CS 310: Order Notation (aka Big-O and friends) CS 310: Order Notation (aka Big-O and friends) Chris Kauffman Week 1-2 Logistics At Home Read Weiss Ch 1-4: Java Review Read Weiss Ch 5: Big-O Get your java environment set up Compile/Run code for Max

More information

Algorithms. Algorithms 6.5 REDUCTIONS. introduction designing algorithms establishing lower bounds classifying problems intractability

Algorithms. Algorithms 6.5 REDUCTIONS. introduction designing algorithms establishing lower bounds classifying problems intractability Algorithms ROBERT SEDGEWICK KEVIN WAYNE 6.5 REDUCTIONS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction designing algorithms establishing lower bounds classifying problems

More information

(Refer Slide Time: 1:27)

(Refer Slide Time: 1:27) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 1 Introduction to Data Structures and Algorithms Welcome to data

More information

Recursion & Performance. Recursion. Recursion. Recursion. Where Recursion Shines. Breaking a Problem Down

Recursion & Performance. Recursion. Recursion. Recursion. Where Recursion Shines. Breaking a Problem Down Recursion & Performance Recursion Part 7 The best way to learn recursion is to, first, learn recursion! Recursion Recursion Recursion occurs when a function directly or indirectly calls itself This results

More information

Choice of C++ as Language

Choice of C++ as Language EECS 281: Data Structures and Algorithms Principles of Algorithm Analysis Choice of C++ as Language All algorithms implemented in this book are in C++, but principles are language independent That is,

More information

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 1 Introduction Today, we will introduce a fundamental algorithm design paradigm, Divide-And-Conquer,

More information

Algorithm Analysis. CENG 707 Data Structures and Algorithms

Algorithm Analysis. CENG 707 Data Structures and Algorithms Algorithm Analysis CENG 707 Data Structures and Algorithms 1 Algorithm An algorithm is a set of instructions to be followed to solve a problem. There can be more than one solution (more than one algorithm)

More information

Measuring algorithm efficiency

Measuring algorithm efficiency CMPT 225 Measuring algorithm efficiency Timing Counting Cost functions Cases Best case Average case Worst case Searching Sorting O Notation O notation's mathematical basis O notation classes and notations

More information

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs Big-Oh 1 asymptotic growth rate or order 2 compare two functions, but ignore constant factors, small inputs asymptotic growth rate or order 2 compare two functions, but ignore constant factors, small inputs

More information

Lecture 2: Getting Started

Lecture 2: Getting Started Lecture 2: Getting Started Insertion Sort Our first algorithm is Insertion Sort Solves the sorting problem Input: A sequence of n numbers a 1, a 2,..., a n. Output: A permutation (reordering) a 1, a 2,...,

More information

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri 1 Introduction Today, we will introduce a fundamental algorithm design paradigm,

More information

Chapter 2: Complexity Analysis

Chapter 2: Complexity Analysis Chapter 2: Complexity Analysis Objectives Looking ahead in this chapter, we ll consider: Computational and Asymptotic Complexity Big-O Notation Properties of the Big-O Notation Ω and Θ Notations Possible

More information

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation Week 12 - Friday What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation Here is some code that sorts an array in ascending order

More information

Algorithmic Analysis. Go go Big O(h)!

Algorithmic Analysis. Go go Big O(h)! Algorithmic Analysis Go go Big O(h)! 1 Corresponding Book Sections Pearson: Chapter 6, Sections 1-3 Data Structures: 4.1-4.2.5 2 What is an Algorithm? Informally, any well defined computational procedure

More information

CSE 373: Data Structures and Algorithms. Memory and Locality. Autumn Shrirang (Shri) Mare

CSE 373: Data Structures and Algorithms. Memory and Locality. Autumn Shrirang (Shri) Mare CSE 373: Data Structures and Algorithms Memory and Locality Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber,

More information

Analysis of Algorithms Part I: Analyzing a pseudo-code

Analysis of Algorithms Part I: Analyzing a pseudo-code Analysis of Algorithms Part I: Analyzing a pseudo-code Introduction Pseudo-code representation of an algorithm Analyzing algorithms Measuring the running time and memory size of an algorithm Calculating

More information

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms DATA STRUCTURES AND ALGORITHMS Asymptotic analysis of the algorithms Summary of the previous lecture Algorithm definition Representation of the algorithms: Flowchart, Pseudocode Description Types of the

More information

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 4 Notes. Class URL:

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 4 Notes. Class URL: Notes slides from before lecture CSE 21, Winter 2017, Section A00 Lecture 4 Notes Class URL: http://vlsicad.ucsd.edu/courses/cse21-w17/ Notes slides from before lecture Notes January 23 (1) HW2 due tomorrow

More information

Elementary maths for GMT. Algorithm analysis Part I

Elementary maths for GMT. Algorithm analysis Part I Elementary maths for GMT Algorithm analysis Part I Algorithms An algorithm is a step-by-step procedure for solving a problem in a finite amount of time Most algorithms transform input objects into output

More information

Introduction to Asymptotic Running Time Analysis CMPSC 122

Introduction to Asymptotic Running Time Analysis CMPSC 122 Introduction to Asymptotic Running Time Analysis CMPSC 122 I. Comparing Two Searching Algorithms Let's begin by considering two searching algorithms you know, both of which will have input parameters of

More information

Algorithms 1.5 UNION FIND. dynamic connectivity quick find quick union improvements applications

Algorithms 1.5 UNION FIND. dynamic connectivity quick find quick union improvements applications 1.5 UNION FIND Algorithms F O U R T H E D I T I O N dynamic connectivity quick find quick union improvements applications R O B E R T S E D G E W I C K K E V I N W A Y N E Algorithms, 4 th Edition Robert

More information

Algorithms 1.5 UNION FIND. dynamic connectivity quick find quick union improvements applications

Algorithms 1.5 UNION FIND. dynamic connectivity quick find quick union improvements applications 1.5 UNION FIND Algorithms F O U R T H E D I T I O N dynamic connectivity quick find quick union improvements applications R O B E R T S E D G E W I C K K E V I N W A Y N E Algorithms, 4 th Edition Robert

More information

Adam Blank Lecture 2 Winter 2017 CSE 332. Data Structures and Parallelism

Adam Blank Lecture 2 Winter 2017 CSE 332. Data Structures and Parallelism Adam Blank Lecture 2 Winter 2017 CSE 332 Data Structures and Parallelism CSE 332: Data Structures and Parallelism Algorithm Analysis 1 Outline 1 Comparing Algorithms 2 Asymptotic Analysis Comparing Programs

More information

Algorithm Analysis. Part I. Tyler Moore. Lecture 3. CSE 3353, SMU, Dallas, TX

Algorithm Analysis. Part I. Tyler Moore. Lecture 3. CSE 3353, SMU, Dallas, TX Algorithm Analysis Part I Tyler Moore CSE 5, SMU, Dallas, TX Lecture how many times do you have to turn the crank? Some slides created by or adapted from Dr. Kevin Wayne. For more information see http://www.cs.princeton.edu/~wayne/kleinberg-tardos.

More information

Organisation. Assessment

Organisation. Assessment Week 1 s s Getting Started 1 3 4 5 - - Lecturer Dr Lectures Tuesday 1-13 Fulton House Lecture room Tuesday 15-16 Fulton House Lecture room Thursday 11-1 Fulton House Lecture room Friday 10-11 Glyndwr C

More information

Data Structures Lecture 8

Data Structures Lecture 8 Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 8 Recap What should you have learned? Basic java programming skills Object-oriented

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithm Analysis Page 1 - Algorithm Analysis Dr. Fall 2008 Algorithm Analysis Page 2 Outline Textbook Overview Analysis of Algorithm Pseudo-Code and Primitive Operations Growth Rate and Big-Oh Notation

More information

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017 CS 137 Part 8 Merge Sort, Quick Sort, Binary Search November 20th, 2017 This Week We re going to see two more complicated sorting algorithms that will be our first introduction to O(n log n) sorting algorithms.

More information

input sort left half sort right half merge results Mergesort overview

input sort left half sort right half merge results Mergesort overview Algorithms OBT S DGWICK K VIN W AYN Two classic sorting algorithms: mergesort and quicksort Critical components in the world s computational infrastructure. Full scientific understanding of their properties

More information

6. Asymptotics: The Big-O and Other Notations

6. Asymptotics: The Big-O and Other Notations Chapter 7 SEARCHING 1. Introduction, Notation 2. Sequential Search 3. Binary Search 4. Comparison Trees 5. Lower Bounds 6. Asymptotics: The Big-O and Other Notations Outline Transp. 1, Chapter 7, Searching

More information

Introduction to Algorithms

Introduction to Algorithms Lecture 1 Introduction to Algorithms 1.1 Overview The purpose of this lecture is to give a brief overview of the topic of Algorithms and the kind of thinking it involves: why we focus on the subjects that

More information

Analysis of algorithms

Analysis of algorithms Analysis of algorithms Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based on the size of the input (time complexity), and/or developing a

More information

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1 CS241 - week 1 review Special classes of algorithms: logarithmic: O(log n) linear: O(n) quadratic: O(n 2 ) polynomial: O(n k ), k 1 exponential: O(a n ), a > 1 Classifying algorithms is generally done

More information

1.1 Your First Program

1.1 Your First Program 1.1 Your First Program Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 1/29/11 6:37 AM! Why Programming? Why programming? Need to

More information

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018 CS 580: Algorithm Design and Analysis Jeremiah Blocki Purdue University Spring 2018 Recap: Stable Matching Problem Definition of a Stable Matching Stable Roomate Matching Problem Stable matching does not

More information

Algorithms. Algorithms 1.5 UNION-FIND. dynamic connectivity quick find quick union improvements applications ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 1.5 UNION-FIND. dynamic connectivity quick find quick union improvements applications ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 1.5 UNION-FIND Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE dynamic connectivity quick find quick union improvements applications http://algs4.cs.princeton.edu

More information

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space when run on different computers! for (i = n-1; i > 0; i--) { maxposition

More information

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs Algorithms in Systems Engineering IE172 Midterm Review Dr. Ted Ralphs IE172 Midterm Review 1 Textbook Sections Covered on Midterm Chapters 1-5 IE172 Review: Algorithms and Programming 2 Introduction to

More information

1 Motivation for Improving Matrix Multiplication

1 Motivation for Improving Matrix Multiplication CS170 Spring 2007 Lecture 7 Feb 6 1 Motivation for Improving Matrix Multiplication Now we will just consider the best way to implement the usual algorithm for matrix multiplication, the one that take 2n

More information

Data Structures and Algorithms

Data Structures and Algorithms Berner Fachhochschule - Technik und Informatik Data Structures and Algorithms Topic 1: Algorithm Analysis Philipp Locher FS 2018 Outline Course and Textbook Overview Analysis of Algorithm Pseudo-Code and

More information

Introduction to the Analysis of Algorithms. Algorithm

Introduction to the Analysis of Algorithms. Algorithm Introduction to the Analysis of Algorithms Based on the notes from David Fernandez-Baca Bryn Mawr College CS206 Intro to Data Structures Algorithm An algorithm is a strategy (well-defined computational

More information

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept 9/10/2018 Algorithms & Data Structures Analysis of Algorithms Siyuan Jiang, Sept. 2018 1 Email me if the office door is closed Siyuan Jiang, Sept. 2018 2 Grades have been emailed github.com/cosc311/assignment01-userid

More information

Analysis of Algorithms

Analysis of Algorithms Analysis of Algorithms Data Structures and Algorithms Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++ Goodrich, Tamassia and Mount (Wiley, 2004)

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 08 : Algorithm Analysis MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Algorithm Analysis 2 Introduction Running Time Big-Oh Notation Keep in Mind Introduction Algorithm Analysis

More information

4.4 Algorithm Design Technique: Randomization

4.4 Algorithm Design Technique: Randomization TIE-20106 76 4.4 Algorithm Design Technique: Randomization Randomization is one of the design techniques of algorithms. A pathological occurence of the worst-case inputs can be avoided with it. The best-case

More information

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES DESIGN AND ANALYSIS OF ALGORITHMS Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES http://milanvachhani.blogspot.in USE OF LOOPS As we break down algorithm into sub-algorithms, sooner or later we shall

More information

Data Structures and Algorithms. Course slides: String Matching, Algorithms growth evaluation

Data Structures and Algorithms. Course slides: String Matching, Algorithms growth evaluation Data Structures and Algorithms Course slides: String Matching, Algorithms growth evaluation String Matching Basic Idea: Given a pattern string P, of length M Given a text string, A, of length N Do all

More information

UNIT 1 ANALYSIS OF ALGORITHMS

UNIT 1 ANALYSIS OF ALGORITHMS UNIT 1 ANALYSIS OF ALGORITHMS Analysis of Algorithms Structure Page Nos. 1.0 Introduction 7 1.1 Objectives 7 1.2 Mathematical Background 8 1.3 Process of Analysis 12 1.4 Calculation of Storage Complexity

More information

Sorting is a problem for which we can prove a non-trivial lower bound.

Sorting is a problem for which we can prove a non-trivial lower bound. Sorting The sorting problem is defined as follows: Sorting: Given a list a with n elements possessing a total order, return a list with the same elements in non-decreasing order. Remember that total order

More information

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych CS125 : Introduction to Computer Science Lecture Notes #38 and #39 Quicksort c 2005, 2003, 2002, 2000 Jason Zych 1 Lectures 38 and 39 : Quicksort Quicksort is the best sorting algorithm known which is

More information

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview 400 lecture note #0 [.2,.3,.4] Analysis of Algorithms Complexity of Algorithms 0. Overview The complexity of an algorithm refers to the amount of time and/or space it requires to execute. The analysis

More information

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018 Analysis of Algorithms & Big-O CS16: Introduction to Algorithms & Data Structures Spring 2018 Outline Running time Big-O Big-Ω and Big-Θ Analyzing Seamcarve Dynamic programming Fibonacci sequence 2 Algorithms

More information

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES DESIGN AND ANALYSIS OF ALGORITHMS Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES http://milanvachhani.blogspot.in USE OF LOOPS As we break down algorithm into sub-algorithms, sooner or later we shall

More information

CS:3330 (22c:31) Algorithms

CS:3330 (22c:31) Algorithms What s an Algorithm? CS:3330 (22c:31) Algorithms Introduction Computer Science is about problem solving using computers. Software is a solution to some problems. Algorithm is a design inside a software.

More information

AM205: lecture 2. 1 These have been shifted to MD 323 for the rest of the semester.

AM205: lecture 2. 1 These have been shifted to MD 323 for the rest of the semester. AM205: lecture 2 Luna and Gary will hold a Python tutorial on Wednesday in 60 Oxford Street, Room 330 Assignment 1 will be posted this week Chris will hold office hours on Thursday (1:30pm 3:30pm, Pierce

More information