(9) Algorithmic Complexity

(9) Algorithmic Complexity - Reading CMSC 132:...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CMSC 132: Object-Oriented Programming II Algorithmic Complexity Department of Computer Science University of Maryland, College Park Reading Read Section 2.8 in the Koffman text 2 3 Algorithm efficiency Efficiency has to do with the amount of resources used by an algorithm: Time Space Two approaches to measuring efficiency are: Benchmarking Asymptotic analysis 4 Benchmarking Approach Code the algorithm up as a program Pick some desired inputs and actually run the implementation on them Measure the time and space it uses Industry benchmarks SPEC – CPU performance MySQL – database applications WinStone – windows PC applications MediaBench – multimedia applications Linpack – numerical scientific applications
Background image of page 1

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

View Full DocumentRight Arrow Icon
5 Benchmarking, con't. Advantages Gives precise information for a given configuration (implementation, hardware, inputs) Limitations Is affected by the specific configuration Data sets (often too small) a dataset that was the right size in the past is likely too small now Hardware Software Is affected by special cases (biased inputs) Does not measure intrinsic efficiency 6 Asymptotic analysis Approach Mathematically analyze an algorithm's efficiency, and express its running time as function of the input size n Time O (f(n)) Time is on the order of f(n) Called “big O” notation Advantages Measures intrinsic efficiency Dominates efficiency for large input sizes Programming language, compiler, and processor are irrelevant 7 Search example Number guessing game One player picks a secret number between 1 and n The second player guesses a number The first answers either “correct”, “too high”, or “too low” The second player repeats guessing numbers until the correct number is guessed 8 Sequential search algorithm Algorithm Guess that the number is 1 If incorrect, increment the guess by 1 Repeat until guess is correct Example Suppose the range of numbers is 1…100, and the secret number happens to be 20 The sequence of guesses made will be 1, 2, 3, 4,… 20, which requires 20 guesses
Background image of page 2
9 Sequential search algorithm, con't. Informal analysis of number of the number of guesses needed for guessing a number that's in the range 1…n, using the sequential search algorithm: If the secret number is 1, how many guesses are required? If the secret number is n, how many guesses are required? On average, how many guesses are needed? Time = O (n) = linear time 10 Binary search algorithm Algorithm Set low and high to be lowest and highest possible value Guess that the number is the middle value between low and high ((low + high) / 2, using integer division) If the guess is too large, set high = middle -1 If the guess is too small, set low = middle + 1 Repeat until guess is correct 11 Binary search algorithm, con't. Example
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

(9) Algorithmic Complexity - Reading CMSC 132:...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online