(9) Algorithmic Complexity

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

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

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

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

View Full Document
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
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 Suppose the range of numbers is 1…100, and the secret number happens to be 20 Guesses: low = 1, high = 100, guess 50, Answer = too large low = 1, high = 49, guess 25, Answer = too large low = 1, high = 24, guess 12, Answer = too small low = 13, high = 24, guess 18, Answer = too small low = 19, high = 24, guess 21, Answer = too large

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 ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern