








































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









































Algorithm Efficiency
Amount of resources used by algorithm Time, space
Benchmarking Asymptotic analysis
Benchmarking
Precise information for given configuration Implementation, hardware, inputs
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
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
Measures intrinsic efficiency Dominates efficiency for large input sizes Programming language, compiler, processor irrelevant
Linear Search Algorithm
Guess number = 1 If incorrect, increment guess by 1 Repeat until correct
Given number between 1… Pick 20 Guess sequence = 1, 2, 3, 4 … 20 Required 20 guesses
Linear Search Algorithm
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
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
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
Size Running Time n/2 4n+ 64 32 259 128 64 515 256 128 1027 512 256 2051
Asymptotic Complexity
n/2 and 4n+3 behave similarly Run time roughly doubles as input size doubles Run time increases linearly with input size
Time(2n) / Time(n) approaches exactly 2
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
Size Running Time n^2 2 n^2 + 8 2 4 16 4 16 40 8 64 132 16 256 520
Big-O Notation
Upper bound on number of steps in algorithm For sufficiently large input size Intrinsic efficiency of algorithm for large inputs
Formal Definition of Big-O
For some positive constants M, N 0 M g(n) f(n), for all n N 0
For some coefficient M & all data sizes N 0 M g(n) is always greater than f(n)