Docsity

Algorithms Complexity - Object-Oriented Programming II - Notes | CMSC 132, Study notes of Computer Science

Partial preview of the text

Download Algorithms Complexity - Object-Oriented Programming II - Notes | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

Algorithmic Complexity

Fawzi Emad

Chau-Wen Tseng

Department of Computer Science

University of Maryland, College Park

Where We Are

OO software development

Algorithms & data structures

Asymptotic analysis Data structures Linear Hierarchical Unstructured

Advanced Java

Collections Threads

So far

Coming up

Algorithm Efficiency

Efficiency

Amount of resources used by algorithm Time, space

Measuring efficiency

Benchmarking Asymptotic analysis

Benchmarking

Approach

Pick some desired inputs Actually run implementation of algorithm Measure time & space needed

Industry benchmarks

SPEC – CPU performance MySQL – Database applications WinStone – Windows PC applications MediaBench – Multimedia applications Linpack – Numerical scientific applications

Search Example

Number guessing game

Pick a number between 1…n Guess a number Answer “correct”, “too high”, “too low” Repeat guesses until correct number guessed

Linear Search Algorithm

Algorithm

  1. Guess number = 1
  2. If incorrect, increment guess by 1
  3. Repeat until correct

Example

Given number between 1… Pick 20 Guess sequence = 1, 2, 3, 4 … 20 Required 20 guesses

Linear Search Algorithm

Analysis of # of guesses needed for 1…n

If number = 1, requires 1 guess If number = n, requires n guesses On average, needs n/2 guesses Time = O( n ) = Linear time

Binary Search Algorithm

Algorithm

  1. Set ∆ to n/
  2. Guess number = n/
  3. If too large, guess number – ∆
  4. If too small, guess number + ∆
  5. Reduce ∆ by ½
  6. Repeat until correct

Search Comparison

For number between 1…

Simple algorithm = 50 steps Binary search algorithm = log 2 ( n ) = 7 steps

For number between 1…100,

Simple algorithm = 50,000 steps Binary search algorithm = log 2 ( n ) = 77 steps

Binary search is much more efficient!

Asymptotic Complexity

Comparing two linear functions

n/2 4n+

Size Running Time

Asymptotic Complexity

Comparing two functions

n/2 and 4n+3 behave similarly Run time roughly doubles as input size doubles Run time increases linearly with input size

For large values of n

Time(2n) / Time(n) approaches exactly 2

Both are O(n) programs

Asymptotic Complexity

Comparing two log functions

log 2 ( n ) 5 * log 2 ( n ) + 3

Size Running Time

Asymptotic Complexity

Comparing two functions

n^2 and 2 n^2 + 8 behave similarly Run time roughly increases by 4 as input size doubles Run time increases quadratically with input size

For large values of n

Time(2n) / Time(n) approaches 4

Both are O( n^2 ) programs

Observations

Big O categories

O(log(n)) O(n) O(n^2 )

For large values of n

Any O(log(n)) algorithm is faster than O(n) Any O(n) algorithm is faster than O(n^2 )

Asymptotic complexity is fundamental measure

of efficiency

Comparison of Complexity