4. Still on Selection Sort
void selectionSort(int A[], int n){
for(int i=0; i<n; i++){
int min = i;
for(int j=i+1; j<n; j++){
if(A[j] < A[min])
min = j;
What is this function’s T(n)? What
} we need is to compute for T(n)
int temp = A[i]; from the inner loop going
A[i] = A[min]; outwards.
A[min] = temp;
We need to use the summation
} notation to solve the T(n).
}
8. Going back to the Selection Sort
void selectionSort(int A[], int n){
for(int i=0; i<n; i++){ n n n
int min = i;
for(int j=i+1; j<n; j++){ ∑ ∑ c =∑ c(n-i+1-1)
if(A[j] < A[min]) i=1 j=i+1 i=1
min = j;
} n n
int temp = A[i];
A[i] = A[min]; ∑ c(n-i) = c n-i∑
A[min] = temp; i=1 i=1
}
n n n
}
∑ n-i = ∑ n - ∑ i
i=1 i=1 i=1
9. Time Complexity
A function that maps problem size into the
time required to solve the problem.
Typically, we are interested in the inherent
complexity of computing the solution to
problems in a particular class.
10. Lower Bound
We might want to know how fast we can sort a list of n
items, initially in an arbitrary order, regardless of the
algorithm used.
What is sought here is the lower bound, L(n), on sorting,
a property of the sorting problem and not of any
particular algorithm.
This says that no algorithm can do the job in fewer than
L(n) time units for arbitrary inputs.
11. Upper Bound
We might also like to know how long it would
take to sort such list using a known algorithm
with a worst-case input.
What is sought here is the upper bound, U(n),
which says that for arbitrary inputs we can
always sort in time at most U(n).
12. Goal of Time Complexity Analysis
While there are apparently two complexity functions for
problems, L(n) and U(n), the ultimate goal is to make
these two bounds coincide.
This is the optimal algorithm which has L(n) = U(n).
For some of the problems, this goal has not been
realized yet!
13. Invitation
Consider this, CS 213, as you journey into
finding optimal solutions to classes of
problems!
Who knows, you might win a million
dollars ($$$) from Claymath Foundation!
14. Upper Bound Complexity
There are two ways in analyzing this
bound:
Counting instructions
Solving recurrences
Both are used to find the worst case of an
algorithm.
15. Big O-Notation (O(g(n)))
The O-notation is used to describe the
worst-case running time of an algorithm.
O(n) means that the growth of the running
time of the algorithm is a function of n.
O-notation computes for the upper bound.
16. O-Notation Defined
O(g(n)) = {f(n): ∃ c>0, n0>0 s.t. 0 ≤ f(n) ≤
cg(n) for all n ≥ n0}.
Example: Check if (n2/2) – 3n ∈ O(n2)
(n2/2) – 3n ≤ cn2
½ - 3/n ≤ c
Choosing c = ½, n0 = 6 proves the claim.
17. Another Example
3n2 - 100n + 6 ∈ O(n2) ?
3n2 - 100n + 6 ≤ cn2
3 – 100/n + 6/n2 ≤ c
At this point, we have to choose a c>0 and an
n0
What values will prove our claim?
18. Lower Bound Complexity
This is the more difficult of the bounds.
There is no algorithm to analyze.
Ω(g(n)) is used to describe the lower bound of
the running time of the algorithm or minimum
possible running time of the algorithm.
19. Ω-Notation
Ω(g(n)) = {f(n): ∃ c>0, n0>0 s.t. 0 ≤ cg(n) ≤
f(n) for all n ≥ n0}.
Example: Check if (n2/2) – 3n ∈ Ω(n2)
cn2 ≤ (n2/2) – 3n
c ≤ ½ - 3/n
Choosing c = 1/14, n0 = 7 proves the claim.
20. Another Example
Check if 3n2 - 100n + 6 ∈ Ω(n)
cn ≤ 3n2 - 100n + 6
c ≤ 3n – 100 + 6/n
At this point we need to find a c and an n0 that will prove
our claim.
What values of c and n0 will suffice the inequality??
21. θ-Notation
Used to denote that the lower and upper
bounds of the running time of the
algorithm is tight, i.e. the growth rate of the
upper and lower bounds are the same.
22. θ-Notation Defined
θ(g(n)) = {f(n): ∃ c1>0, c2>0, n0>0 s.t. 0 ≤
c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0}.
f(n) ∈ θ(g(n)) if f(n) ∈ O(g(n)) and f(n) ∈
Ω(g(n))
23. Complexity Classes
Description O-notation
constant O(1)
logarithmic O(log n)
linear O(n)
n log n O(n log n)
quadratic O(n2)
cubic O(n3)
polynomial O(nk), k≥1
exponential O(an), a>1
24. Growth rate of complexity classes
class n=2 n=16 n=256 n=1024
1 1 1 1 1
log n 1 4 8 10
n 2 16 256 1024
n log n 2 64 2048 10240
n^2 4 256 65536 1048576
n^3 8 4096 16777216 1.07E+09
2^n 4 65536 1.16E+77 1.8E+308