













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!














1
2
Amount of resources used by algorithm Time, space
Benchmarking Asymptotic analysis
3
Benchmarking
Pick some desired inputs Actually run implementation of algorithm Measure time & space needed
SPEC – CPU performance MySQL – Database applications WinStone – Windows PC applications MediaBench – Multimedia applications Linpack – Numerical scientific applications
4
Benchmarking
Precise information for given configuration Implementation, hardware, inputs
Affected by configuration Data sets (usually too small) Hardware Software Affected by special cases (biased inputs) Does not measure intrinsic efficiency
7
Linear Search Algorithm
Given number between 1… Pick 20 Guess sequence = 1, 2, 3, 4 … 20 Required 20 guesses
8
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
9
Binary Search Algorithm
Set ∆ to n/ Guess number = n/ If too large, guess number – ∆ If too small, guess number + ∆ Reduce ∆ by ½ Repeat until correct
10
Binary Search Algorithm
Example Given number between 1… Pick 20 Guesses = 50, ∆ = 25, Answer = too large, subtract ∆ 25, ∆ = 12 , Answer = too large, subtract ∆ 13, ∆ = 6, Answer = too small, add ∆ 19, ∆ = 3, Answer = too small, add ∆ 22, ∆ = 1, Answer = too large, subtract ∆ 21, ∆ = 1, Answer = too large, subtract ∆ 20 Required 7 guesses
13
Asymptotic Complexity
n/2 4n+
Size Running Time
14
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
15
Asymptotic Complexity
log 2 ( n ) 5 * log 2 ( n ) + 3
Size Running Time
16
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 loga N = (logbN) / (logba)
Both are O( log(n) ) programs
19
Big-O Notation
Upper bound on number of steps in algorithm For sufficiently large input size Intrinsic efficiency of algorithm for large inputs
20
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)
21
Big-O Examples
Select M = 6, N 0 = 1000 For n ≥ 1000 6n ≥ 5n+1000 is always true Example ⇒ for n = 1000 6000 ≥ 5000 +
22
Big-O Examples
Select M = 4, N 0 = 100 For n ≥ 100 4n^2 ≥ 2n^2 + 10n + 1000 is always true Example ⇒ for n = 100 40000 ≥ 20000 + 1000 + 1000
25
Complexity Category Example
0
50
100
150
200
250
300
1 2 3 4 5 6 7 Problem Size
2^n n^ nlog(n) n log(n)
26
Complexity Category Example
1
10
100
1000
1 2 3 4 5 6 7 Problem Size
2^n n^ nlog(n) n log(n)
27
Calculating Asymptotic Complexity
Highest complexity term dominates Can ignore lower complexity terms
2 n + 100 ⇒ O(n) n log(n) + 10 n ⇒ O(nlog(n)) ½ n^2 + 100 n ⇒ O(n^2 ) n^3 + 100 n^2 ⇒ O(n^3 ) 1/100 2n^ + 100 n^4 ⇒ O(2n^ )
28
Complexity Examples
0
100000
200000
300000
400000
500000
600000
700000
800000
(^1349120260533) (^10682118417582081611131602)
n nlog(n) 2 n + 100
31
Complexity Examples
1/100 2n^ + 100 n^4 ⇒ O(2n)
1
1E+
1E+
1E+
1E+
1E+
1E+
1E+
1E+
1E+
1E+
2 13 28 49 79 120 178 260 373 533 756
n^2 n^4 2^n 1 / 100 2^n + 100 n^
32
Types of Case Analysis
Best case Worst case Average case
33
Types of Case Analysis
Smallest number of steps required Not very useful Example ⇒ Find item in first place checked
34
Types of Case Analysis
Largest number of steps required Useful for upper bound on worst performance Real-time applications (e.g., multimedia) Quality of service guarantee Example ⇒ Find item in last place checked
37
Types of Case Analysis
Number of steps required for “typical” case Most useful metric in practice Different approaches Average case Expected case Amortized
38
Approaches to Average Case
Average over all possible inputs Assumes some probability distribution, usually uniform
Algorithm uses randomness Worse case over all possible input Average over all possible random values
For all long sequences of operations Worst case total time divided by # of operations
39
Amortization Example
If array is full, allocate new array Allocation cost is O(size of new array) Copy over contents of existing array
Non-amortized If array is full, allocate new array of size k+ Amortized If array is full, allocate new array of size 2k Compare their allocation cost
40
Amortization Example
Allocation cost as table grows from 1..n
Total cost ⇒ n(n+1)/
Best case ⇒ allocation cost = k Worse case ⇒ allocation cost = k Amortized case ⇒ allocation cost = (n+1)/
6
6 Cost 1 2 3 4 5 7 8
Size (k) 1 2 3 4 5 7 8