L2- Complexity
L2- Complexity
Algorithm 1: Algorithm 2:
✓Time
✓Space
Experimental Studies
7000
✓Run the program with inputs of varying 6000
Time (ms)
size and composition
5000
4000
✓Use a method like
3000
System.currentTimeMillis() or Clock functions
to get an accurate measure of the actual 2000
running time 1000
0
✓Plot the results 0 50 100
Input Size
Limitations of Experiments
✓Results may not be indicative of the running time as all inputs may not be
included in the experiment.
✓In order to compare two algorithms, the same hardware and software
environments must be used
O(n)
o(n)
Ω(n)
(n)
Ө(n)
Edmund Landau
• 1877~1938
• Inventor of the asymptotic notation
Donald E. Knuth
• 1938 ~
• Turing Award, 1974.
• Father of the analysis of algorithms
• Popularizing the asymptotic notation
Theoretical Analysis
➢A CPU
➢By inspecting the pseudocode, we can determine the maximum number of primitive
operations executed by an algorithm, as a function of the input size.
➢Define:
➢a = Time taken by the fastest primitive operation
➢b = Time taken by the slowest primitive operation
Running Time
80
➢We focus on the worst case running time.
60
➢Easier to analyze and best to bet
➢Crucial to applications such as games, 40
0
1000 2000 3000 4000
Input Size
The Growth Rate of the Six Popular functions
n logn n nlogn n2 n3 2n
4 2 4 8 16 64 16
8 3 8 24 64 512 256
16 4 16 64 256 4,096 65,536
10000000
1000000
100000
10000
1000
100
10
1
1 4 16 64 256 1024 4096 16384 65536
Asymptotic Dominance in Action
Implications of Dominance
Asymptotic Dominance in Action
109 instructions/second
Faster Computer Vs Better Algorithm
Example
O(n2) = {2n2 + 10n + 2000, 125n2 – 233n - 250, 10n + 25, 150, …..}
Big-Oh Notation
10,000
Example: 2n + 10 is O(n) 3n
– 2n + 10 cn 1,000 2n+10
– (c − 2) n 10 n
– n 10/(c − 2) 100
– Pick c = 3 and n0 = 10
10
1
1 10 100 1,000
n
1
1 10 100 1,000
n
More Big-Oh Examples
7n-2
7n-2 is O(n)
need c > 0 and n0 1 such that 7n-2 c•n for n n0
this is true for c = 7 and n0 = 1
◼ 3n3 + 20n2 + 5
3n3 + 20n2 + 5 is O(n3)
need c > 0 and n0 1 such that 3n3 + 20n2 + 5 c•n3 for n n0
this is true for c = 4 and n0 = 21
◼ 3 log n + 5
3 log n + 5 is O(log n)
need c > 0 and n0 1 such that 3 log n + 5 c•log n for n n0
this is true for c = 8 and n0 = 2
Big-Oh Rules
O(n)
o(n)
Ω(n)
(n)
Ө(n)
Relatives of Big-Oh ((n) )
big-Omega
f(n) is (g(n)) if there is a constant c > 0 and an integer constant n0 1 such
that f(n) c•g(n) for n n0
And
Alternatively
o(g(n)) = {f(n): lim [f(n) / g(n)] = 0 }
n→
Example
o(n2) = {10n + 25, 150, …..}
Relatives of Big-Oh ((n))
Alternatively
w(g(n)) = {f(n): lim [f(n) / g(n)] = }
→
n→
Example
w(n2) = {8n3 + 25, 150, …..}
Example Uses of the Relatives of Big-Oh
◼ 3logn+loglogn is (logn)
f(n) is (g(n)) if there is a constant c > 0 and an integer constant n0 1
such that f(n) c•g(n) for n n0
let c = 3 and n0 = 2
◼ 3logn+loglogn is (logn)
◼ f(n) is (g(n)) if f(n) is asymptotically equal to g(n)
◼ 12n2 + 6n is o(n3)
◼ f(n) is o(g(n)) if f(n) is asymptotically strictly less than g(n)
◼ 12n2 + 6n is (n)
◼ f(n) is (g(n)) if f(n) is asymptotically strictly greater than g(n)
Some more Examples
◼ 3n + 2 is (n)
As 3n +2 >= 3n for n>=1, though the inequality holds for n>=0, for
we need n0>0
◼ 3n + 2 is (n)
◼ As 3n+2 >=3n for all n>=2 and 3n+2 <=4n for all n>=2, c1=3,
c2=4 and n0=2
◼ 3n + 2 is o(n2)
Useful Facts about Big O
➢Sums of functions:
If fO(g) and hO(g), then f+hO(g).
O(g) (g)
•g
o(g) (g) (g)
Why o(f)O(x)−(x)
✓(g) = {f | gO(f)}
“The functions that are at least order g.”
Devise an algorithm that finds the sum of all the integers in a list.
Work out the computational complexity of the following piece of code assuming
that n = 2m:
for( int i = n; i > 0; i-- ) {
for( int j = 1; j < n; j *= 2 ) {
for( int k = 0; k < j; k++ ) {
for(int m = 0; m < 10000; m++)
sum = sum + m;
}
}
}
Practice exercises
A()
{
int i=1, s=1;
While(s<=n)
{
i++;
s = s+i;
printf(“hi”);
}
}
A()
{
int i=;
For(i=1; i^2 <=n; i++)
Print(“hi”);
}
A()
{
int I, j, k, n;
for(i=1; i<n; i++)
{
for(j=1; j<i^2; j++
{
for(k=1; k<n/2; k++)
{
print(“hi”);
}
}
}
}
A()
{
for(i=1; i<n; i++)
for(j=1; j<n; j=j+i)
print(“Hello”);
}
A()
{
int n= 2^2^k;
for(i=1; i<=n; i++)
{
j=2;
while(j<=n)
{
j=j^2;
printf(“Hi”);
}
}
}
Thank You!!