This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Algorithm Efficiency Analysis CSE 250 Lecture Note 04 Algorithm Efficiency Analysis Adapted from the KoffmanWolfgang C++ Primer, from the Instructor Materials slides by Elliot Koffman and Paul Wolfgang. This material is used by permission of John Wiley & Sons, Inc For private use only by students in CSE250 Redistribution or public posting in any form is expressly forbidden. c Xin He (University at Buffalo) CSE 250 Data Structures Classnote 04 1 / 46 Algorithm Analysis It means: estimating the resources required. The resources of algorithms: time and space . We mainly consider time: harder to estimate; often more critical. The efficiency of an algorithm is measured by a runtime function T ( n ) . n is the size of the input. Commonly, n is the # of items in the input, if each item is of fixed size . c Xin He (University at Buffalo) CSE 250 Data Structures Classnote 04 3 / 46 Whats T ( n ) ? Defining T ( n ) as the real run time is meaningless, because the real run time depends on many factors, such as the machine speed, the programming language used, the quality of compilers etc . These are not the properties of the algorithm. T ( n ) def = the number of basic instructions performed by the algorithm. Basic instructions: + , , * , / , read from/write into a memory location, comparison, branching to another instruction ... These are not basic instructions: input/output statement, sin ( x ) , exp ( x ) . . . . These actions are done by function calls, not by a single machine instruction. Knowing T ( n ) and the machine speed , we can estimate the real runtime . Example 1: The machine speed is 10 8 ins/sec. T ( n ) = 10 6 . The real runtime would be about 10 8 10 6 = . 01 sec. c Xin He (University at Buffalo) CSE 250 Data Structures Classnote 04 4 / 46 Example Example 2: Consider this simple program: 1: s = 2: for i = 1 to n do 3: for j = 1 to n do 4: s = s + i + j 5: end for 6: end for T ( n ) = ? Its hard to get the exact expression of T ( n ) even for this very simple program. Also, the exact value of T ( n ) depends on factors such as prog language, compiler. These are not the properties of the loop, so not our concern. We can see: the loop iterates n 2 times , and loop body takes constant number of instructions. So T ( n ) = an 2 + bn + c for some constants a , b , c . We say the growth rate of T ( n ) is n 2 . This is the sole property of the algorithm and is our main concern. c Xin He (University at Buffalo) CSE 250 Data Structures Classnote 04 5 / 46 The Order of Performance: (Big) O Basic idea: 1 Ignore constant factor: computer and language implementation details affect that: go for fundamental rate of increase with problem size. 2 Consider fastest growing term: Eventually, for large problems, it will dominate....
View
Full
Document
This document was uploaded on 10/31/2010.
 Spring '09

Click to edit the document details