# lec04 - CSE 12 Analysis and Measurement of Algorithms...

This preview shows pages 1–8. Sign up to view the full content.

04-1/33 Algorithm costs: time and space Best case, worst case, average case Counting instructions and asymptotic analysis Big-O, big-Omega, big-Theta notation CSE 12 Analysis and Measurement of Algorithms

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
04-2/33 Writing good software • Three characteristics of software are important: robustness : a program’s ability to spot exceptional conditions and deal with them or shutdown gracefully correctness : does the program do what it is “supposed to” do? efficiency : all programs use resources (time and space, i.e. CPU cycles and memory); how can we measure efficiency so that we can compare algorithms?
04-3/33 Analysis and Measurement An algorithm’s performance can be described by: time complexity or cost – how long it takes to execute. In general, less time is better! space complexity or cost – how much computer memory it uses. In general, less space is better! Time and space costs are usually given as functions of the size of the input to the algorithm A big problem will probably take more time and space to solve than a small one, but how much more?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
04-4/33 Figuring algorithm costs • For a given algorithm, if the size of the input is n , we would like to know: T( n ) , the time cost of solving the problem S( n ) , the space cost of solving the problem • We could implement the algorithm and run it and measure the time and memory usage • Or we can analyze the written algorithm
04-5/33 Algorithm cost analysis To perform algorithm cost analysis: Write down the algorithm that solves the problem Decide what “size of the problem” means for this kind of problem Count up the number of instructions the algorithm would execute, as a function of n , the size of the problem it is solving Note that different languages for writing the same algorithm might require a different number of instructions Java will probably require fewer than C which will require fewer than assembly language So, a precise count of the number of instructions is, arguably, too much detail… we will need to look at ways of abstracting away from nonessential detail

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
04-6/33 Algorithm cost cases Often we want to distinguish among different cases of running a given algorithm to solve a problem of size n Best case : for all problems of size n , which one has the lowest cost? optimistic, but useful for a lower bound on cost Worst case : for all problems of size n , which one has the highest cost? pessimistic, but useful for an upper bound on cost Average case : average the cost over all problems of size n useful, but can be hard to analyze
Time Cost: Count Instructions Statements Cost 1 float findAvg ( int []grades ){ 2 float sum = 0; 1 3 int count = 0; 1 4 while ( count < grades.length ) { n + 1 5 sum += grades[count]; n 6 count++; n 7 } 8 if ( grades.length > 0 ) 1 9 return sum / grades.length; 10 else 1 11 return 0.0f; 12 } TOTAL 3 n + 5 How many times will each instruction execute, as a function of n ?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 34

lec04 - CSE 12 Analysis and Measurement of Algorithms...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online