1-Complexity Analysis Basics-23-24
1-Complexity Analysis Basics-23-24
Chapter 1
Complexity analysis
Basics
Complexity analysis
Complexity Analysis determines the
amount of time and space (memory)
resources required to execute an
algorithm with respect to input size
(independent from the machine, language and
compiler).
2
Algorithm analysis
Size n
Basic operations
Input Results
Algorithm
3
Time and Space Complexity
Time complexity
Space complexity
4
Algorithm analysis steps
Step 1 : Let P a problem, this is the problem statement
Step 2 : Let M be a method, to resolve the problem P and
develop an algorithm:
- Input
- Output
- Resolution steps
It’s the description of M in an algorithmic language
Step 3 : Validation,
To prove the correctness and termination of a program.
« Program validation »
Step 4: measure the efficiency of the algorithm independently of the
environment (machine, system, compiler, …)
« Calculating theoretical complexity »
5
Theoretical complexity
Intuition:
• Theoretical approach
• Experimental approach
8
Time complexity
Theoretical approach
• General behavior of the algorithm;
• The details don't matter;
• The behavior of the algorithm is
the order of a function.
9
Time complexity
Experimental approach:
• Obtain a much finer evaluation of the algorithms
• All instructions are taken into account
• Provides fine indications
• Detection of sensitive areas
• Helps in program optimization
10
Asymptotic complexity
Calculation of theoretical complexity
Asymptotic Complexity
13
Function’s order (or rate of growth) 1, log n, n, n2, 2n
2n
n2 n
T(n)
Log n
14
Theoretical complexity
Asymptotic:
- n (size of data) large
- Approximate complexity
- We retain the largest term of the formula
- We ignore the multiplier coefficient (constant).
15
Complexity cases
•Best-case
•Average-case
•Worst-case
16
Example : if T(n)= 2n
We assume that the execution time of a basic instruction is
10-6 seconds
18
Ο (big Oh) Ω (big omega) Θ (big theta)
Landau notation
• Big Oh :
O(g(n))={f:IN →IN| c>0 and n0 ≥ 0/0 ≤ f(n) ≤ c.g(n) n ≥n0 }
20
Big Oh
• f(n) is of the order O(g(n))
O(g(n))={ f :IN →IN | c>0 and n0 ≥0 such as 0 ≤ f(n) ≤ c.g(n) n ≥ n0 }
Example:
𝑓 𝑛 = 2𝑛 + 5 𝑖𝑠 𝑜𝑓 𝑜𝑟𝑑𝑒𝑟 𝑶 𝒏
2𝑛 + 5 ≤ 𝑐. 𝑛
2𝑛 + 5
≤𝑐
𝑛
5 5
2+ ≤ 𝑐 𝑤𝑒 ℎ𝑎𝑣𝑒 ≤ 1 ∀𝑛 ≥ 5
𝑛 𝑛
22
Landau notation
• Big Ω
Ω(g(n )) = { f:IN → IN| c>0 and n0 ≥ 0 / 0 ≤ c.g(n) ≤ f(n) n ≥ n0 }
f(n)= Ω(g(n)).
23
Notation Ω
• f(n) is of order Ω(g(n )) for all
Ω(g(n ))={ f :IN → IN | c>0 and n0 ≥0 such as 0 ≤ c.g(n) ≤ f(n) n ≥ n0 }
Example:
𝑓 𝑛 = 2𝑛 + 5 is of order Ω 𝒏
𝑏𝑒𝑐𝑎𝑢𝑠𝑒 2𝑛 + 5 ≥ 2𝑛 ∀𝑛 ≥ 0
25
Notation Θ
• f(n) is of order Θ(g(n ))
Θ(g(n)) = { f :IN →IN | c1>0, c2>0 and n0 ≥0 such as
0 ≤ c1.g(n) ≤ f(n) ≤ c2.g(n) n≥ n0 } g(n) asymptotic bound
Example:
𝑓 𝑛 = 2𝑛 + 5 is of order Θ 𝒏
𝑏𝑒𝑐𝑎𝑢𝑠𝑒 2𝑛 ≤ 2𝑛 + 5 ≤ 3𝑛 ∀𝑛 ≥ 5
Note:
Notations o and ω with strict inequalities.
Note : we can also determine the complexity
using the limits as follows:
𝑓(𝑛)
• 𝑖𝑓 lim = 𝑘 𝑘 > 0 𝑡ℎ𝑒𝑛 𝑓 𝑛 ∈ Θ(𝑔(𝑛))
𝑛→∞ 𝑔(𝑛)
𝑓(𝑛)
• 𝑖𝑓 lim =0 𝑡ℎ𝑒𝑛 𝑓 𝑛 ∈ O(𝑔(𝑛))
𝑛→∞ 𝑔(𝑛)
𝑓(𝑛)
• 𝑖𝑓 lim = +∞ 𝑡ℎ𝑒𝑛 𝑓 𝑛 ∈ Ω(𝑔(𝑛))
𝑛→∞ 𝑔(𝑛)
Landau notation
Exercise: Show that:
f(n)=5n2 - 6n = Θ(n2)
f(n)=6n3 ≠ Θ(n2)
3 2 3
f n = 10n + 3n + 5n +1=O(n )
Landau notation
Exercise: Show that :
f(n)=5n2 - 6n = Θ(n2)
𝑐2 ∗ 𝑛2 ≤ 5𝑛2 − 6𝑛 ≤ 𝑐1 *𝑛2
6
𝑐2 ≤ 5 − ≤ 𝑐1
𝑛
6
≤ 1 ∀𝑛 ≥ 6
𝑛
𝑐1 = 5, 𝑐2 = 1, 𝑛0 = 6
(𝑜𝑟 𝑛0 = 2)
Landau notation
Exercise: Show that:
f(n)=6n3 ≠ Θ(n2)
6𝑛3 ≤ 𝑐1 ∗ 𝑛2
6𝑛 ≤ 𝑐1 ⇒ 𝑐1 does not exist
So complexity is false
Landau notation
Exercise: Show that: f(n)=n2 = O(10-5 n3)
𝑛2 ≤ 𝑐 ∗ 10−5 ∗ 𝑛3
𝑛2
−5 3
≤𝑐
10 ∗ 𝑛
1
−5
≤𝑐
10 ∗ 𝑛
10+5
≤𝑐
𝑛
10+5
≤ 1 ∀𝑛 ≥ 10+5
𝑛
so c=1 and 𝑛0 = 10+5
Landau notation
Exercise: Show that:
f n = 10n3 + 3n2 + 5n +1=O(n3 )
10n3 + 3n2 + 5n + 1 ≤ 𝑐 ∗ n3
10n3 + 3n2 + 5n + 1
3
≤𝑐
n
3 5 1
10 + + 2 + 3 ≤ 𝑐
𝑛 𝑛 𝑛
3 5 1
≤ 1 ∀𝑛 ≥ 3 ; ≤ 1 ∀𝑛 ≥ 3 ; ≤ 1 ∀𝑛 ≥ 1
𝑛 𝑛2 𝑛3
10 + 1 + 1 + 1 ≤ 𝑐
so 𝑛0 = 3 and c=13
Vocabulary
• O(1) : constant time, independant of size of data.
• This is the most optimal case. (Solving a quadratic equation)
• O(n) : linear (searching a value in an array)
• O(n2) : quadratic (sorting array elements)
• O(n3) : cubic (product of two matrices)
• O(nk) : polynomial complexity,
• O(log n), O(n log n ),… logarithmic
• O(2n), O(n !), … : exponential, factorial, …
34
Properties
• Reflexive : f(n) = O(f(n)).
• Transitive : if f(n) = O(g(n)) and g(n)=O(h(n)) then f(n)=O(h(n)).
• Addition :
if f(n) = O(g1(n)) et h(n) = O(g2(n)) then f(n)+h(n)=O(g1(n)+g2(n)).
c+ f(n) = O(c+g(n))=O(g(n)) with c a constant
• Multiplication :
if f(n) = O(g1(n)) and h(n) = O(g2(n)) then f(n)xh(n) = O(g1(n)x g2(n)) ;
c.f(n) = O(c.g(n))=O(g(n)), because all constant function is O(1).
35
Properties
We can easily deduce the following properties:
36
Summary
Algorithms are compared on the basis of their
complexity.
The exponential function is always stronger than
the polynomial function which is stronger than the
linear function which is stronger than the
logarithmic function.
37
Estimating the complexity
of an iterative algorithm
How to evaluate the execution time of an algorithm?
41
Estimating the cost of an iterative algorithm
Statement_1 = cost_1
Statement_2 = cost_2
…
Statement_i = cost_i
…
Total_cost = cost_1+cost_2+…cost_i+…
42
Estimating the cost of an iterative algorithm
Conditional statement :
f(…)
{ if (…) then g(…) ;
else h(…) ;
end if ;
}
43
Estimating the cost of an iterative algorithm
𝑪𝒐𝒔𝒕 𝒐𝒇 𝒇 … , 𝒏 = 𝑐𝑜𝑠𝑡(𝒈 … , 𝒊 )
𝑖=1
44
Estimating the cost of an iterative algorithm
45
Rules of O notation
Constant factors:
46
Rules of O notation
• Addition rule :
O(h(n)+g(n))=O(max(h(n),g(n)))
• Multiplication rule:
O((h(n)xh(n))) if h(n)>g(n)
O(h(n)xg(n)) =
O((g(n)xg(n))) if g(n)>h(n)
47
Estimating the cost of an algorithm
Example:
x
Calculate e using a limited sums of n terms:
2 n
x x x
S e 1 ...
x
1! 2! n!
What is the order of growth of the execution time?
48
Estimating the cost of an algorithm
1st solution:
s=1;
for i=1 to n do
p=1;
for j=1 to i do
p=p*x/j;
done;
s=s+p
done;
49
Estimating the cost of an algorithm
The internal loop is executed:
𝑛
𝑛(𝑛 + 1)
1 + 2 + ⋯+ 𝑛 = 𝑖=
2
𝑖=1
50
Estimating the cost of an algorithm
2nd solution: i i 1
x x x
*
i! i (i 1)!
S=1; p=1;
for i=1 to n do
p=p*x/i;
s=s+p;
done;
51
Estimating the cost of an algorithm
𝑂(𝑛)
52
Calculating the complexity of an algorithm
Exercises
1. Calculation the maximum of 4 integers
Algorithm1 :
Function maximum(I/ a, b, c, d:integer):integer;
{ max:integer;
maxa ;
if (b>max) then maxb fsi;
if (c>max) then maxc fsi;
if (d>max) then maxd fsi;
return max;
}
Number of comparisons = 3
Complexity = O(1)
1. Calculation the maximum of 4 integers
Algorithm 2 :
Function maximum(I/ a, b, c, d:integer):integer;
{ if (a>b) then
if (a>c) then
if (a>d) then return a
else return d
end if;
else if (c>d) then return c
else return d
end if;
end if
else if (b>c) then
if (b>d) then return b
else return d
end if;
else if (c>d) then return c
else return d
end if;
end if;
end if;
}
1. Calculation the maximum of 4 integers
Complexity = O(1)
Same number of comparisons for the 2 solutions but the
1st solution takes up more memory space
2. Calculation of the maximum of n integers
Algorithm:
Function max_n(I/ t:Array[n] of integers;n:integer):integer;
{ max, i: integer;
maxt[1] ; ------------------------------------- 1 +
for i2 to n do ----------------- n-2+1= (n-1 *
if (t[i]>max) then maxt[i] endif; -------- 2) +
done;
return max; ----------------------------------- 1
cost = 1+2n-2+1 = 2n
}
Complexity=O(n)
3. Trace of a square matrix
This involves calculating the sum of the elements of the
diagonal of a square matrix A of integers (n: number of rows and
columns).
1st solution:
sum=0;
for i1 to n do
for j1 to n do
if (i=j) then sumsum+A[i][j]; end if;
done;
done;
return sum;
3. Trace of a square matrix
Complexity:
𝑛 𝑛 𝑛
1= 𝑛 = 𝑛2 ⇒ 𝜣(𝒏𝟐 )
𝑖=1 𝑗=1 𝑖=1
3. Trace of a square matrix
• 2nd solution:
sum=0;
for i1 to n do
sumsum+A[i][i];
done;
Return sum;
𝑛
Complexity: 𝑖=1 1 = 𝑛 ⇒ 𝜣(𝒏)
4. Give the Big O complexity of the following algorithms.
Algorithm 1 Algorithm 2
1 for i 1 to n do 1 for i 5 to n-5 do
2 for j 1 to i do 2 for j i-5 to i+5 do
3 xx+3 3 x x+3
Algorithm 1:
𝑛 𝑖 𝑛
𝑛(𝑛 + 1)
1= 𝑖= ↝ 𝑂(𝑛2 )
2
𝑖=1 𝑗=1 𝑖=1
Algorithm 2:
1= 11 = 11 𝑛 − 9 ~𝑂(𝑛)
𝑖=5 𝑗=𝑖−5 𝑖=5
4. Give the Big O complexity of the following algorithms.
Algorithm 3 Algorithm 4
1 for i 1 to n do 1 k←0
2 for j 1 to i do 2 for i ← 1 ; i ≤ n ; i ← i*2
3 for k 1 to j do 3 for j ← 1 ; j ≤ n ; j ← j +1
4 x x+a 4 k ← k +1
Algorithm 3:
𝑛 𝑖 𝑗 𝑛 𝑖 𝑛 𝑛 𝑛
𝑖(𝑖 + 1) 1 1 2𝑛 + 1 𝑛 + 1 𝑛 𝑛(𝑛 + 1)
1= 𝑗= = ( 𝑖2 + 𝑖) = ( + )
2 2 2 6 2
𝑖=1 𝑗=1 𝑘=1 𝑖=1 𝑗=1 𝑖=1 𝑖=1 𝑖=1
3
↝ 𝑂(𝑛 )
Algorithm 4:
i=1 = 20 j=1 to n
i=2 = 21 j=1 to n
i=4 = 22 j=1 to n
i=8 = 23 j=1 to n
… …
i=2k j=1 à n 𝑤𝑒 𝑎𝑠𝑠𝑢𝑚𝑒 𝑡ℎ𝑎𝑡 𝑛 = 2𝑘 ⇒ 𝑙𝑜𝑔𝑛 = 𝑙𝑜𝑔2𝑘 = 𝑘𝑙𝑜𝑔2 ;
with log 2 2 = 1 ⇒ 𝑘 = 𝑙𝑜𝑔𝑛
⇒ 𝒄𝒐𝒎𝒑𝒍𝒆𝒙𝒊𝒕𝒚 = 𝑶(𝒏𝒍𝒐𝒈𝒏)
The loop value of the second line doubles each time until it reaches n. It is in log(n).
The loop value of the third line is directly n (by upper bound – lower bound + 1).
We therefore have an algorithm in O(n.log(n))
5. Given an array of n integers, we want to know if
one of its elements is equal to the sum of the n-1 other
elements.
Example A= 3 8 -6 3 -1 9
• The 2nd element with value 8 is equal to the sum of
the other 5.
1st question
1/ A first method consists of calculating the sum
of the other elements for each element of the
array and checking whether it is equal to the
element. Write this algorithm. Give its
complexity.
2nd question
Algorithm 1
for i ←1 to n do
if (i%2=0) then print (" Good morning ")
else for j ←1 to n do
print(" Good morning ") ;
done ;
endif ;
done ;
6. Give the complexity of the following algorithms
(justify) :
Algorithm 2
Function test(I/n : integer) : integer;
Begin
S=0 ;
while(n>=1) do
if (n is even) then n=n/2 else n=n-1 ; endif ;
S=S+1 ;
done ;
return(s) ;
End;
6. Give the complexity of the following algorithms
(justify) :
Algorithm 3
S0 ; i 1 ;
while (i ≤ n) do
for j n2 to 5 with step -1 do
SS+1 ;
done
ii+2 ;
done ;
Return S ;