Professional Documents
Culture Documents
Lecture 03 - Complexity Analysis
Lecture 03 - Complexity Analysis
Lecture # 03:
Complexity Analysis
• Motivation
• Asymptotic Analysis
2
Motivations for Complexity Analysis
• There are often many different algorithms which can be used to
solve the same problem.
– For example, assume that we want to search for a key in a sorted array.
• Thus, it makes sense to develop techniques that allow us to:
o compare different algorithms with respect to their “efficiency”
o choose the most efficient algorithm for the problem
3
How do we Measure Efficiency?
• Running time in [micro/milli] seconds
– Advantages ?
– Disadvantages ?
• Instead,
o we count the number of basic operations the algorithm performs.
o we calculate how this number depends on the size of the input.
4
Example of Basic Operations:
• Arithmetic operations: *, /, %, +, -
• Assignment statements of simple data types.
• Reading of primitive types
• Writing of a primitive types
• Simple conditional tests: if (x < 12) ...
• Method call (Note: the execution time of the method itself may
depend on the value of parameter and it may not be constant)
• A method's return statement
• Memory access
• Note:
– We consider an operation such as ++ , += , and *= as consisting
of two basic operations.
– To simplify complexity analysis we will not consider memory
access (fetch or store) operations. 5
Simple Complexity Analysis
public class TestAbstractClass
{
public static void main(String[] args)
{
Employee[] list = new Employee[3];
list[0] = new Executive(“Mike", 50000);
list[1] = new HourlyEmployee(“Tyson", 120);
list[2] = new MonthlyEmployee(“Abraham", 9000);
((Executive)list[0]).awardBonus(11000);
for(int i = 0; i < list.length; i++)
if(list[i] instanceof HourlyEmployee)
((HourlyEmployee)list[i]).addHours(60);
for(int i = 0; i < list.length; i++) {
list[i].print();
System.out.println("Paid: " + list[i].pay());
System.out.println("*************************");
} } }
7
Simple Complexity Analysis: Simple Loops
• How to find the cost of an unnested loop?
– Find (a) the cost of the body of the loop
– In the case below, consider the number of multiplications
– Find (b) the number of iterations in loop
– Multiply the two numbers (i.e., (a) * (b)) to get
the total
– E.g. double x, y;
x = 2.5 ; y = 3.0;
for(int i = 0; i < n; i++){
a[i] = x * y;
x = 2.5 * x;
y = y + a[i];
}
8
Simple Complexity Analysis: Complex Loops
• Represent the cost of for loop in summation form.
– The main idea is to make sure that we find an
iterator that increase/decrease its values by 1.
– For example, consider finding the number of
times statements 1, 2 and 3 get executed below:
n −1
for (int i = 1; i < n; i++)
statement1;
1 = n − 1
i =1
for (int i = 1; i <= n; i++) { n n n n
for (int j = 1; j <= n; j++)
statement2;
1 = n =
i =1 j =1
n 1
i =1
= n 2
i =1
}
i
i =1
2
=
6
natural numbers
from 1 to n
n ( n + 1)
2
n
i =
3 Sum of cube of
n ( n + 1)
natural numbers
n
2
i =
i =1 from 1 to n
i =1 2
n +1
n
a −1
i =0
a i
=
a −1
,a 1
Sum of natural
numbers from 1 to n 10
Simple Complexity Analysis: Complex Loops
• Represent the cost of the for loop in
summation form.
– The problem in the example below is that
the value of i does not increase by 1
for (int i = k; i <= n; i = i + m)
statement4;
• i: k , k + m , k + 2m , …, k + rm
– Here, we can assume without loss of generality that
k + rm = n, i.e. r = (n – k)/m
– i.e., an iterator s from 0, 1, 2, …,r can be used
n −k
r m
n −k n −k
1 =
s =0 s =0
1=
m
− 0 +1 =
m
+1
11
Simple Complexity Analysis : Loops (with <=)
• In the following for-loop:
for (int i = k; i <= n; i = i + m){
statement1;
statement2;
}
12
Simple Complexity Analysis: Loops (with <)
• In the following for-loop:
13
Useful Logarithmic Formulas
14
Best, Average, Worst case complexities
• What is the best case complexity analysis?
• The smallest number of basic operations carried out by the
algorithm for a given input.
• What is the worst case complexity analysis?
• The largest number of basic operations carried out by the algorithm
for a given input.
• What is the average case complexity analysis?
• The number of basic operations carried out by the algorithm on average.
for each input i
(Probability of input i * Cost of input i)
17
How to analyze time complexity? (3/4)
Example 2:
COST REPEATITION
sumOfArray(A[ ] , size)
{
total=0 1 1
for i= 0 to n-1 and i=i+1 2 n+1
{
total = total + A[i] 2 n
}
return total 1 1
}
TsumOfArray = 1+ 2(n+1) + 2n +1
= 4n + 4
(OR) T(n) = Cn + C’ Where C and C’ are constants
18
How to analyze time complexity? (4/4)
So,
Tsum = k (a constant)
Plotting a graph
19
Asymptotic Notation- Big oh ‘O’
• Since counting the exact number of operations is
cumbersome / impossible, we can always focus
our attention on asymptotic analysis, where
constants and lower-order terms are ignored.
– E.g. n3, 1000n3, and 10n3+10000n2+5n-1 are all “the same”
– The reason we can do this is that we are always interested in
comparing different algorithms for arbitrarily large number of
inputs.
Asymptote in math is a line that a curve
approaches, as it heads towards infinity:
• So,
Tsum = k (a constant) → O(1)
TsumOfArray = Cn + C’ → O(n)
TsumOfMatrix = a n2 + b n + c → O(n2)
20
Asymptotic Growth of different functions
21
Running Times for Different Sizes
of Inputs of Different Functions
22
centuries
Different Asymptotic Notations (1/3)
‘O’ Big-oh notation:
O(g(n))={ f(n): if there exist constants C and n0 such that
f(n) ≤ Cg(n) , for n ≥ n0 }
For example:
f(n) = 5 n2 + 2 n + 1 O(n2)
g(n) = n2
C = 8 (5+2+1) f(n) ≤ 8g(n) , for n ≥ 1(n≠0)
n0 = 1
UPPER
BOUND
23
Different Asymptotic Notations (2/3)
‘Ω’ Omega notation:
Ω(g(n))={ f(n): there exist constants C and n0 such that
Cg(n) ≤ f(n) , for n ≥ n0 }
For example:
f(n) = 5 n2 + 2 n + 1 Ω(n2)
g(n) = n2
C=5 5n2 ≤ f(n) , for n ≥ 0
n0 = 0
LOWER
BOUND
24
Different Asymptotic Notations (3/3)
‘Ө’ Theta notation:
Ө(g(n))={ f(n): there exist constants C1 , C2 and n0 such that
C1g(n) ≤ f(n) ≤ C2g(n) , for n ≥ n0 }
For example:
f(n) = 5 n2 + 2 n + 1 Ө(n2)
g(n) = n2
C1 = 5 5n2 ≤ f(n) ≤ 8g(n) , for n ≥ 1
C2 = 8
n0 = 1
TIGHT
BOUND
25
Asymptotic Complexity
• Finding the exact complexity, f(n) = number of basic
operations, of an algorithm is difficult.
• We approximate f(n) by a function g(n) in a way that
does not substantially change the magnitude of f(n). --
the function g(n) is sufficiently close to f(n) for large
values of the input size n.
• This "approximate" measure of efficiency is called
asymptotic complexity.
Definition of Big-O:
• Consider a function f(n) that is non-negative n 0. We
say that “f(n) is Big-O of g(n)” i.e., f(n) = O(g(n)), if n0
0 and a constant c > 0 such that f(n) cg(n), n n0
The symbol ∀
means “for all”
or “for any”.
The symbol ∃
means “there
exists”.
27
Big-O (asymptotic) Notation (2/3)
28
Big-O (asymptotic) Notation (3/3)
31
How to determine complexity of code structures (1/6)
i=1;
while (i < n) {
sum = sum + i; O(log n)
i = i*2
}
32
How to determine complexity of code structures (2/6)
Nested Loops:
Examples:
sum = 0
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) O(n2)
sum += i * j ;
i = 1;
while(i <= n) {
j = 1;
while(j <= n){
statements of constant complexity O(n log n)
j = j*2;
}
i = i+1;
}
33
How to determine complexity of code structures (3/6)
Example:
for (int j = 0; j < n * n; j++)
sum = sum + j;
for (int k = 0; k < n; k++)
sum = sum - l;
System.out.print("sum is now ” + sum);
36
How to determine complexity of code structures (6/6)