Computational Complexity: Analyzing Algorithms
Computational Complexity: Analyzing Algorithms
Analyzing algorithms
• Minimize resources
Empirical methods
Linear search:
1. Read the elements in the list from left to right
keeping track of the index
2. If the entry is found, return the index
Search
Algorithm comparison
Binary search:
1. Position in the middle of the list (keeping track of the index)
Benchmarking:
• A state-of-the-art computer A will run linear search.
• A slower computer B will run binary search.
Source: https://en.wikipedia.org/wiki/Analysis_of_algorithms
Search
Algorithm comparison
Source: https://en.wikipedia.org/wiki/Analysis_of_algorithms
Search
Algorithm comparison
Source: https://en.wikipedia.org/wiki/Analysis_of_algorithms
Analysis of algorithms
Assumptions
T(n)
Input size
• Depends on the problem being studied.
k = 17
Find if sum is possible
Sol #1
SUM-EXISTS(L, k):
1. for i = 0 up to L . length − 1:
2. for j = 0 up to L . length − 1:
3. if i ≠ j and L[i] + L[ j] = k:
4. return YES
5. return NO
Find if sum is possible
Sol #1
4. return YES c4 1
5. return NO c5 1
Find if sum is possible
Sol #1
4. return YES c4 1
5. return NO c5 1
2 2
T(n) = c1n + c2n + c3n + c4 + c5
Find if sum is possible
Sol #1
2 2
T(n) = c1n + c2n + c3n + c4 + c5
2
T(n) = n (c2 + c3) + nc1 + (c4 + c5)
2
T(n) = an + bn + c
Find if sum is possible
Sol #2
SUM-EXISTS_2(L, k):
1. sort(L)
2. for x in L:
3. d←k−x
4. if find(L, d):
5. return YES
6. return NO
Find if sum is possible
Sol #2
T(n) = a ⋅ n log(n) + bn + c
Find if sum is possible
Sol #1 Sol #2
Find if sum is possible
Sol #1
2
T(n) = an + bn + c
Sol #2
T(n) = a ⋅ n log(n) + bn + c
Find if sum is possible
Find if sum is possible
Sol #3
SUM-EXISTS_3(L, k):
1. store ← map[int]bool{}
2. for x in L:
3. if store.contains(x)
4. return YES
5. store.add(k − x)
6. return NO
Find if sum is possible
Sol #3
T(n) = an + b
Find if sum is possible
Sol #1
2
T(n) = an + bn + c
Sol #2
T(n) = a ⋅ n log(n) + bn + c
Sol #3
T(n) = an + b
Order of growth
Cost function Order of growth
13n 2 + 1.2n + 9
1 3
n + 1010n 2 + 1
100
30n − 49
2n + n 10 + n 34 + n 2 + 1
Order of growth
Cost function Order of growth
13n 2 + 1.2n + 9 n2
1 3
n + 1010n 2 + 1 n 3
100
30n − 49 n
n 10 34
2 +n +n +n +1 2 2n
Asymptotic notation
Asymptotic notation
Ω Θ O
Best (lower bound) Range (lower & upper bound) Worst (upper bound)
Asymptotic notation
Big-O notation
Source: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. 2009. Introduction to Algorithms, Third Edition (3rd. ed.). The MIT Press.
Asymptotic notation
Big-O notation
Θ O Ω
Source: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. 2009. Introduction to Algorithms, Third Edition (3rd. ed.). The MIT Press.
Asymptotic notation
Conventions
2 3 n
• Even though O(n) ⊂ O(n ) ⊂ O(n ) ⊂ O(c ) ⊂ . . . is true, we use the
closest bound.
Complexity/n
A LOT!
Big-O comparison
Source: https://www.bigocheatsheet.com/
Big-O comparison
We want to find efficient
procedures to solve large-scale
problems.
Computational Complexity
bit.ly/pm-comp-x2