Docsity

Algorithmic Complexity I - Object-Oriented Programming II | CMSC 132, Study notes of Computer Science

Partial preview of the text

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

CMSC 132:

Object-Oriented Programming II

Algorithmic Complexity I

Department of Computer Science

University of Maryland, College Park

Algorithm Efficiency

Efficiency

Amount of resources used by algorithm Time, space

Measuring efficiency

Benchmarking Asymptotic analysis

Benchmarking

Advantages

Precise information for given configuration Implementation, hardware, inputs

Disadvantages

Affected by configuration Data sets (often too small) a dataset that was the right size 3 years ago is likely too small now Hardware Software Affected by special cases (biased inputs) Does not measure intrinsic efficiency

Asymptotic Analysis

Approach

Mathematically analyze efficiency Calculate time as function of input size n T  O( f(n) ) T is on the order of f(n) “Big O” notation

Advantages

Measures intrinsic efficiency Dominates efficiency for large input sizes Programming language, compiler, processor irrelevant

Linear Search Algorithm

Algorithm

Guess number = 1 If incorrect, increment guess by 1 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

Example

Given number between 1… secret number we are trying is find is 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 low = 19, high = 20, guess 19, Answer = too small low = 20, high = 20, guess 20, Answer = correct Required 7 guesses

Binary Search Algorithm

Analysis of # of guesses needed for 1…n

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

Asymptotic Complexity

Comparing two linear functions

Size Running Time n/2 4n+ 64 32 259 128 64 515 256 128 1027 512 256 2051

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 functions log 2 ( n ) and 5 * log 2 ( n ) + 3 behave similarly Run time roughly increases by constant as input size doubles Run time increases logarithmically with input size For large values of n Time(2n) – Time(n) approaches constant Base of logarithm does not matter Simply a multiplicative factor logaN = (logbN) / (logba) Both are O( log(n) ) programs

Asymptotic Complexity

Comparing two quadratic functions

Size Running Time n^2 2 n^2 + 8 2 4 16 4 16 40 8 64 132 16 256 520

Big-O Notation

Represents

Upper bound on number of steps in algorithm For sufficiently large input size Intrinsic efficiency of algorithm for large inputs

f(n)

O(…)

input size

# steps

Formal Definition of Big-O

Function f(n) is ( g(n) ) if

For some positive constants M, N 0 M  g(n)  f(n), for all n  N 0

Intuitively

For some coefficient M & all data sizes  N 0 M  g(n) is always greater than f(n)