Lecture 4: Analysis of Algorithms II
Majid Khonji
201
1
www.avlab.io
Khalifa University
ROBO
Course Structure
Module 1: Algorithm Fundamentals
Module 2: Path Planning in Discrete Spaces
Module 3: Path Planning in Continuous Spaces
2
ku.ac.ae
Experimental Studies Approach
3
import time
start_time = time.perf_counter()
# --- run the algorithm here ---
end_time = time.perf_counter()
elapsed = (end_time - start_time) * 1000
print("Elapsed time:", elapsed, "milliseconds")
ku.ac.ae
Limitations of Experiments
4
ku.ac.ae
Theoretical Analysis
5
ku.ac.ae
Running Time
6
ku.ac.ae
�Comparison of Two Algorithms
7
Insertion sort is
n2 / 4
Merge sort is
2 n lg n
Sort a million items?
insertion sort takes
roughly 70 hours
while
merge sort takes
roughly 40 seconds
This is a slow machine, but if
100 x as fast then it’s 40 minutes
versus less than 0.5 seconds
ku.ac.ae
Seven Important Functions
8
ku.ac.ae
The Seven Functions
9
g(n) = 2n
g(n) = 1
g(n) = lg n
g(n) = n lg n
g(n) = n
g(n) = n2
g(n) = n3
Slide by Matt Stallmann included with permission
Side by side
ku.ac.ae
�Why Growth Rate Matters
10
if runtime is... | time for n + 1 | time for 2 n | time for 4 n |
c lg n | c lg (n + 1) | c (lg n + 1) | c(lg n + 2) |
c n | c (n + 1) | 2c n | 4c n |
c n lg n | ~ c n lg n + c n | 2c n lg n + 2cn | 4c n lg n + 4cn |
c n2 | ~ c n2 + 2c n | 4c n2 | 16c n2 |
c n3 | ~ c n3 + 3c n2 | 8c n3 | 64c n3 |
c 2n | c 2 n+1 | c 2 2n | c 2 4n |
runtime
quadruples
when problem
size doubles
Growth Rate
11
ku.ac.ae
Big-Oh Notation
f(n) ≤ cg(n) for n ≥ n0
12
Note: n is input size so n₀ ≥ 1
c > 0
ku.ac.ae
More Big-Oh Examples
13
is O(n)
need c > 0 and n0 ≥ 1 such that 7 n - 2 ≤ c n for n ≥ n0
this is true for c = 8 and n0 = 1
is O(n3)
Notice for n or n ≥ 1, 20n² ≤ 20n³, 5 ≤ 5n³
f(n) = 3n³ + 20n² + 5 ≤ (3 + 20 + 5) n³ = 28n³.
You can pick c = 28 and n0 = 1
is O(log n)
We need 3log n + 5 ≤ c log n ⟹ 5 ≤ (c−3) log n
For c > 3, 5/(c-3) ≤ log n. Choose c = 4 ⟹ 5 ≤ log n ⟹ 2⁵ ≤ n
Thus, c = 4, n₀ = 32
Log property�n = a^(logₐ n)
ku.ac.ae
Big-Oh and Growth Rate
14
| f(n) is O(g(n)) | g(n) is O(f(n)) |
g(n) grows more | Yes | No |
f(n) grows more | No | Yes |
Same growth | Yes | Yes |
ku.ac.ae
Big-Oh Rules
15
ku.ac.ae
Analysis Examples
16
Recall: Primitive Operations
17
ku.ac.ae
Counting Primitive Operations
Algorithm arrayMax(A, n)
currentMax ← A[0] 2
for i ← 1 to n − 1 do 2 + n
if A[i] > currentMax then 2(n − 1)
currentMax ← A[i] 2(n − 1)
{ increment counter i } 2(n − 1)
return currentMax 1
18
T(n) = 6 ( n - 1) + n + 5 = 7n + 4
= O(n) why?
7n + 4 ≤ cn�4/(7-c) ≤ n for c = 6 and n0 = 4
ku.ac.ae
Example: Computing Prefix Averages
A[i] = (X[0] + X[1] + … + X[i])/(i+1)
19
ku.ac.ae
Prefix Average Algorithm 1
The following algorithm computes prefix averages:
For simplicity, here we are not counting precisely all primitive operations. In the exams, I’ll give you a python code instead for precise count.
20
A ← new array of n integers
for i ← 0 to n – 1 do
s ← X[0]
for j ← 1 to i do
s ← s + X[j]
A[i] ← s / (i + 1)
return A
n
n
n
1 + 2 + … + (n – 1)
1 + 2 + … + (n – 1)
n
1
ku.ac.ae
Arithmetic Progression
What is the value of 1 + 2 + … + (n – 1)?
21
= (n−1)n
S = (n-1)n/2 = (n² - n)/2
S = 1 + 2 + 3 + ⋯ + (n−1)
S = (n−1) + (n−2) + (n−3) + ⋯ + 1
ku.ac.ae
Arithmetic Progression
Often written as
S = 1 + 2 + 3 + ⋯ + n
S = n + (n-1) + (n-2) + ⋯ + 1
22
= (n+1)n
S = (n+1)n/2 = (n² + n)/2
Carl Friedrich Gauss
1777 - 1855
ku.ac.ae
Arithmetic Progression (Visual Proof)
1+2+⋯+n = n(n + 1) / 2
Example:
Or, for n=6 the area 6 (6+1)/2
23
ku.ac.ae
Complexity of Prefix Average Algorithm 1
# Primitive Operations T(n) = 4n + 2 n(n-1)/2 + 1
24
A ← new array of n integers
for i ← 0 to n – 1 do
s ← X[0]
for j ← 1 to i do
s ← s + X[j]
A[i] ← s / (i + 1)
return A
n
n
n
1 + 2 + … + (n – 1)
1 + 2 + … + (n – 1)
n
1
= O(n²)
ku.ac.ae
Another Algorithm for Prefix Average
A ← new array of size n
total ← 0
for j ← 0 to n − 1 do
total ← total + X[j]
A[j] ← total / (j + 1)
return A
Whats the time complexity T(n)?
25
O(n)
ku.ac.ae
Math you need to Review
a(b+c) = aba c
abc = (ab)c
ab /ac = a(b-c)
b = a^(logₐ b)
bc = a^(c logₐ b)
logb(xy) = logbx + logby
logb (x/y) = logbx - logby
Logbxa= alogbx
logba = logxa/logxb
26