Pmscs 623p Lecture 2
Pmscs 623p Lecture 2
Pmscs 623p Lecture 2
Algorithm
(PMSCS 623P)
Lecture 2
Asymptotic Notations
Examples:
T(n) = 3n2+10nlgn+8 is O(n2), O(n2lgn), O(n3),
O(n4), … O(n2lgn), O(n3), O(n4),
T’(n) = 52n2+3n2lgn+8 is
…
Loose upper
bounds
Asymptotic Notations
• - notation (Big Omega)
Examples:
T(n) = 3n2+10nlgn+8 is Θ(n2)
T’(n) = 52n2+3n2lgn+8 is Θ(n2lgn)
Big O
• Informally, O (g(n)) is the set of all functions
with a smaller or same order of growth as g(n),
within a constant multiple
• If we say f(n) is in O(g(n)), it means that g(n) is
an asymptotic upper bound of f(n)
– Intuitively, it is like f(n) ≤ g(n)
• What is O(n2)?
– The set of all functions that grow slower than or in
the same order as n2
Big-O
f n Og n : { there exist positive constants c and n0 such that
0 f n cg n for all n n0 }
• What does it mean?
– If f(n) = O(n2), then f(n) can be larger than n2 sometimes, but…
• We can choose some constant c and some value n0 such
that for every value of n larger than n0 : f(n) ≤ cn2
• That is, for values larger than n0, f(n) is never more than a
constant multiplier greater than n2
• Or, in other words, f(n) does not grow more than a constant
factor faster than n2
Visualization Big-O
cg(n)
f(n)
n0
Examples
• Show that 30n+8 is O(n).
– Show c,n0: 30n+8 cn, n ≥ n0 .
Value of function
• It isn’t even
less than 31n
everywhere.
• But it is less than
30n+8
n
31n everywhere to O(n)
the right of n=8.
n>n0=8
Increasing n
Example: Exponential-Time Algorithm
Example: Quadratic-Time Algorithm
Once upon a time there was an Indian
king who wanted to reward a wise man
for his excellence. The wise man asked
for nothing but some wheat that would
fill up a chess board.
But here were his rules: in the first tile he wants 1 grain of wheat, then 2 on
the second tile, then 4 on the next one…each tile on the chess board
needed to be filled by double the amount of grains as the previous one.
The naïve king agreed without hesitation, thinking it would be a trivial
demand to fulfill, until he actually went on and tried it…
So how many grains of wheat does the king owe the wise man? We know
that a chess board has 8 squares by 8 squares, which totals 64 tiles, so the
final tile should have 2⁶⁴ grains of wheat. If you do a calculation online, you
will end up getting 1.8446744*10¹⁹, that is about 18 followed by 18 zeroes.
Assuming that each grain of wheat weights 0.01 grams, that gives us
184,467,440,737 tons of wheat. And 184 billion tons is quite a lot, isn’t it?
9 iterations
So
30 Operations
Some Examples
Determine the time complexity for the following algorithm.
count = 0;
for(i=0; i<10000; i++)
count++;
Some Examples
Determine the time complexity for the following algorithm.
count = 0; O (1)
for(i=0; i<10000; i++)
count++;
Some Examples
Determine the time complexity for the following algorithm.
count = 0;
for(i=0; i<n; i++)
count++;
Some Examples
Determine the time complexity for the following algorithm.
count = 0; O (n)
for(i=0; i<n; i++)
count++;
Some Examples
Determine the time complexity for the following algorithm.
sum = 0;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
sum += arr[i][j];
Some Examples
Determine the time complexity for the following algorithm.
sum = 0;
O (n2)
for(i=0; i<n; i++)
for(j=0; j<n; j++)
sum += arr[i][j];
Some Examples
Determine the time complexity for the following algorithm.
sum = 0;
for(i=1; i<=n; i=i*2)
sum += i;
Some Examples
Determine the time complexity for the following algorithm.
sum = 0; O (lg
n)
for(i=1; i<=n; i=i*2)
sum += i;
WHY? Show mathematical analysis to
prove that it is indeed O (lg n)
Some Examples
Determine the time complexity for the following algorithm.
sum = 0;
for(i=1; i<=n; i=i*2)
for(j=1; j<=n; j++)
sum += i*j;
Some Examples
Determine the time complexity for the following algorithm.
sum = 0; O (n lg n)
for(i=1; i<=n; i=i*2)
for(j=1; j<=n; j++)
sum += i*j;
Why? Outer for loop runs O(lg n) times (prove it!) and for
each iteration of outer loop, the inner loop runs O(n) times
Big-O Notation
→ Constant time
fA(n)=30n+8
Running time
fB(n)=n2+1
Increasing n
Growth of Functions
Complexity Graphs
log(n)
Complexity Graphs
n log(n)
log(n)
Complexity Graphs
n10 n3
n2
n log(n)
Complexity Graphs (log scale)
3n
nn
n20
2n
n10
1.1n
Types of Time Complexity Analysis
• Worst-case: (usually done)
– Running time on worst possible input
• Best-case: (bogus)
– Running time on best possible input
Here tj = no. of times the condition of while loop is tested for the current value of j.
How can we simplify T(n)? Hint: compute the value of T(n) in the best/worst case
Insertion Sort: Running Time
Here tj = no. of times the condition of while loop is tested for the current value of j.
In the worst case (when input is reverse-sorted), in each iteration of the for loop, all the j-1
elements need to be right shifted, i.e., tj=(j-1)+1 = j :[1 is added to represent the last test].
Putting this in the above eq., we get: T(n) = An 2+Bn+C → O(n2), where A, B, C are constants.
What is T(n) in the best case (when the input numbers are already sorted)?