Litmus Test
If you think you don't need it, or you know it and never use it, you most likely need to study it.
1 data operation
1 control operation X 3 iterations
1 arithmetic and 1 data operation X 3 iterations
1 control operation
Total = 12 operations
If we are being pedantic, this is how to count operations...
1 control operation
j ← 0
for i∈{1,2,3} do
j←i × 2
end for
return j
\( d \gets \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} \)
1 operation
1 operation X 3 iterations
1 operation X 3 iterations
1 operation
ignore
1 operation
How do machine operations relate to scale?
j ← 0
for i∈{1,2,3} do
j←i × 2
end for
return j
Total = 8 operations
function square(A)
for i ← 0 to |A| −1 do
A[i] = A[i] x A[i]
endfor
end function
1 operation per item
1 operation per item
Total = 2 operations per item
\( T(n) = 2n \)
function findDuplicates(A)
D←∅
for i←0 to |A| −1 do
for j←0 to |A| −1 do
if i≠j and A[i] = A[j] then
D←D ∪ {A[i]}
end if
end for
end for
return D
end function
1 operation
\( n \) operations
\( n^2 \) operations (\( n \) per item)
\( n^2 \) operations (\( n \) per item)
Between 0 and \( n^2 \) operations
1 operation
Upper Bound
Lower Bound
Under reasonable assumptions, square scales much better than findDuplicates
Review: The goal is to concisely express how an algorithm will scale (growth rate)
Loosely consider the asymptotic complexity to be the "shape" of \( T(n) \)
Remove all constants and non-leading terms
Constants
Non-Leading Term
The "shape" does not change
Name | Growth Rate | Example |
---|---|---|
Constant | Hash Table (Look up) | |
Logarithmic | Binary Tree (Search) | |
Linear | Linked List (Search) | |
Quadratic | Bubble Sort | |
Exponential | Traveling Salesman (Dynamic Programming) | |
Factorial | Traveling Salesman (Brute Force) |
Question: Does the linear algorithm always execute fewer machine operations?
Answer: No, for values of \( n \) lower than 50, \( \sigma \) has fewer operations that \( \alpha \)
One Last Time:
The goal is to concisely express how an algorithm will scale (growth rate)
function bubbleSort(A)
sorted ← false
index ← |A| − 2
while sorted is false do
sorted←true
for i ← to index do
if A[i] > A[i + 1] then
swap A[i] and A[i + 1]
sorted ← false
end if
end for
index ← index - 1
end while
end function
Lower \( 2n + 5 \) \( \Omega(n) \)
Upper \( 4n^2 + 3n + 3 \) \( O(n^2) \)
* Continues to loop until the for loop on line 6 makes a complete revolution without triggering the if statement on line 7. The extra operation is the exit condition (sorted is true)
** The number of iterations is reduced by 1 for each iteration of the outer loop by virtue of line 12. So it’s technically \( \sum_{i=1}^{n} i \) ; however, this is a bit of minutia that’s best left rounded up to \( n^2 \).