Algorithms Complexity
Algorithms Complexity
Efficiency
148/382
Lecture Outline
149/382
Fundamentals of the Analysis of Algorithm
Efficiency
Basics of Algorithm Complexity Analysis
Algorithm analysis
What to analyze?
• correctness
• time complexity
• space complexity
• optimality
Possible approaches
• empirical and
• theoretical
150/382
Time and Space Complexity of an Algorithm
151/382
Measuring Input Size
152/382
Measuring Input Size (cont.)
𝑛 = ⌊log2 𝑎⌋ + 1 (1)
153/382
Empirical Measurement of Complexity
154/382
Time complexity of the algorithm
where
155/382
Basic Operations
157/382
Order of Growth of Complexity (cont.)
Problem
How many times faster will my algorithm run on a computer
that is 10× faster than my current computer?
Solution
Of course, 10×, 𝑐𝑜𝑝 is one-tenth.
158/382
Order of Growth of Complexity (cont.)
Problem
How many times longer will my algorithm run for a
twice-as-large input when 𝐶(𝑛) = 12 𝑛(𝑛 − 1)?
Solution
We approximate from above the number of operations 𝐶(𝑛)
1 1 1
𝐶(𝑛) = 𝑛(𝑛 − 1) = 𝑛2 − 𝑛
2 2 2
However, in the context of order of growth, lower-order terms
like − 12 𝑛 are typically ignored. Thus,
1 2
𝐶(𝑛) = 𝑛
2
159/382
Order of Growth of Complexity (cont.)
and
1
𝐶(2𝑛) = (2𝑛)2 = 2𝑛2
2
Therefore,
𝐶(2𝑛) 2𝑛2
= 1 =4
𝐶(𝑛) 𝑛2
2
160/382
Order of Growth of Complexity (cont.)
Remarks
• The base of the logarithm is not significant:
log𝑎 𝑛 = log𝑎 𝑏 ⋅ log𝑏 𝑛.
• A computer with a speed of 1012 (one trillion) operations
per second would take approximately 40 billion years to
perform 2100 ≈ 1.3 ⋅ 1030 operations. The age of the Earth
is approximately 4.4 billion years.
• We will not even consider performing 100! operations...
161/382
Order of Growth of Complexity (cont.)
Problem
How many times longer will my algorithm run for a
twice-as-large input, for algorithms with different orders of
growth?
𝑛 log2 𝑛 𝑛 𝑛 log2 𝑛 𝑛2 𝑛3 2𝑛 𝑛!
2𝑛 +1 2× ≈ 2× 4× 8× (… )2 n/a
because
log2 (2𝑛) = log2 2 + log2 𝑛 = 1 + log2 𝑛
22𝑛 = (2𝑛 )2
162/382
Fundamentals of the Analysis of Algorithm
Efficiency
Worst, Best and Average Case
Worst, Best and Average Case
164/382
Worst-case scenario 𝐶𝑤𝑜𝑟𝑠𝑡 (𝑛)
Example
Linear search: element 𝑥 in array 𝐴 is not found or is found at
the end, thus 𝐶𝑤𝑜𝑟𝑠𝑡 (𝑛) = 𝑛.
165/382
Best-case scenario 𝐶𝑏𝑒𝑠𝑡 (𝑛)
167/382
Average Case 𝐶𝑎𝑣𝑔 (𝑛) – Linear Search
Assumptions
168/382
Average Case 𝐶𝑎𝑣𝑔 (𝑛) – Linear Search (cont.)
Successful Search
𝑝 𝑝 𝑝 𝑝
1 +2 +⋯+𝑖 +⋯+𝑛
𝑛 𝑛 𝑛 𝑛
169/382
Average Case 𝐶𝑎𝑣𝑔 (𝑛) – Linear Search (cont.)
Unsuccessful Search
From this
𝑝 𝑝 𝑝 𝑝
𝐶𝑎𝑣𝑔 (𝑛) = (1 + 2 + ⋯ + 𝑖 + ⋯ + 𝑛 ) + 𝑛(1 − 𝑝)
𝑛 𝑛 𝑛 𝑛
𝑝
= (1 + 2 + ⋯ + 𝑖 + ⋯ + 𝑛) + 𝑛(1 − 𝑝)
𝑛
𝑝 1
= [ 𝑛(𝑛 + 1)] + 𝑛(1 − 𝑝)
𝑛 2
1
= 𝑝(𝑛 + 1) + 𝑛(1 − 𝑝)
2
170/382
Average Case 𝐶𝑎𝑣𝑔 (𝑛) – Linear Search (cont.)
Analysis
171/382
Amortized Complexity
173/382
Fundamentals of the Analysis of Algorithm
Efficiency
Asymptotic Notation of Complexity
Big O Notation
Definition
Let us have functions 𝑡(𝑛) and 𝑔(𝑛), where 𝑡(𝑛), 𝑔(𝑛) ∶ ℕ → ℕ.
We say that function 𝑡(𝑛) belongs to 𝑂(𝑔(𝑛)), if there exists a
positive non-zero real constant 𝑐 and a natural number
𝑛0 ≥ 0 such that
𝑡(𝑛) ≤ 𝑐𝑔(𝑛)
for all 𝑛 ≥ 𝑛0 .
Remark
Instead of saying ”𝑡(𝑛) belongs to 𝑂(𝑔(𝑛))”, we can say that
”𝑡(𝑛) is of order 𝑂(𝑔(𝑛))”.
174/382
Big O notation graphically
𝑡(𝑛) ∈ 𝑂(𝑔(𝑛))
𝑡(𝑛)
𝑐𝑔(𝑛)
irrelevant
𝑛0 𝑛
175/382
Big O notation – formally correct graph
range of values of
functions 𝑡(𝑛) and
irrelevant
𝑔(𝑛) are natural
numbers ⇒ the graph
should consist only of
points, not curves. 𝑛
𝑛0
176/382
Big O notation – example 1
Problem statement
Prove that 3𝑛 + 7 ∈ 𝑂(𝑛).
60
Solution 3𝑛 + 7
4𝑛
1. We seek constants 𝑐
40
and 𝑛0 such that
3𝑛 + 7 ≤ 𝑐𝑛 20
Problem statement
Prove that 3𝑛 + 7 ∈ 𝑂(𝑛2 ).
3𝑛 + 7
𝑛2
Solution 40
3𝑛 + 7 ≤ 𝑐𝑛2
0
0 5 10 15
holds for all 𝑛 ≥ 𝑛0 .
2. If we choose 𝑐 = 1,
then 𝑛0 = 5.
178/382
Big O notation – example 3
Problem statement
Prove that 100𝑛 + 5 ∈ 𝑂(𝑛2 ).
Solution
1. It holds that 4000
100𝑛 + 5
100𝑛 + 5 ≤ 100𝑛 + 𝑛 for all 101𝑛2
3000
𝑛 ≥ 5.
2. Furthermore, it holds that 2000
101𝑛 ≤ 101𝑛2 . 1000
3. From this 0
0 2 4 6
2
100𝑛 + 5 ≤ 101𝑛 ≤ 101𝑛
105𝑛 ≤ 105𝑛2
180/382
Omega notation
Definition
Given functions 𝑡(𝑛) and 𝑔(𝑛), where 𝑡(𝑛), 𝑔(𝑛) ∶ ℕ → ℕ. We
say that function 𝑡(𝑛) belongs to Ω(𝑔(𝑛)), if there exists a
positive non-zero real constant 𝑐 and a natural number
𝑛0 ≥ 0 such that
𝑡(𝑛) ≥ 𝑐𝑔(𝑛)
for all 𝑛 ≥ 𝑛0 .
181/382
Lower bound notation graphically
𝑡(𝑛) ∈ Ω(𝑔(𝑛))
𝑡(𝑛)
𝑐𝑔(𝑛)
irrelevant
𝑛0 𝑛
182/382
Ω-notation – example 1
600
Solution
400
1. Clearly, it holds that
200
𝑛3 ≥ 𝑛2 for all 𝑛 ≥ 0.
0
2. Thus we can choose 𝑐 = 1 0 2 4 6 8 10
and 𝑛0 = 0.
183/382
Omega notation – example 2
Problem statement
Prove that 3𝑛 + 7 ∈ Ω(𝑛).
Solution 40
3𝑛 + 7
Definition
Given functions 𝑡(𝑛) and 𝑔(𝑛), where 𝑡(𝑛), 𝑔(𝑛) ∶ ℕ → ℕ. We
say that the function 𝑡(𝑛) belongs to Θ(𝑔(𝑛)), if there exist
positive nonzero real constants 𝑐1 , 𝑐2 and a natural number
𝑛0 ≥ 0 such that
for all 𝑛 ≥ 𝑛0 .
185/382
Theta notation graphically
𝑡(𝑛) ∈ Θ(𝑔(𝑛))
𝑡(𝑛)
𝑐1 𝑔(𝑛)
𝑐2 𝑔(𝑛)
irrelevant
𝑛0 𝑛
186/382
Θ-notation – example
Problem Statement
Prove that 12 𝑛(𝑛 − 1) ∈ Θ(𝑛2 ).
Solution
187/382
Θ-notation – example (cont.)
1 1 2 1
𝑡(𝑛) = 𝑛(𝑛 − 1) = 𝑛 − 𝑛
2 2 2
1 2 1 1
≥ 𝑛 − 𝑛 𝑛
2 2 2
1 2 1 2
≥ 𝑛 − 𝑛
2 4
1 2
≥ 𝑛
4
188/382
Θ-notation – example (cont.)
50
1
2
𝑛(𝑛 − 1)
1 2
2
𝑛
40 1 2
𝑛
4
30
20
10
0
0 2 4 6 8 10
189/382
Properties of asymptotic notation
Basic properties:
1. 𝑓(𝑛) ∈ 𝑂(𝑓(𝑛))
2. 𝑓(𝑛) ∈ 𝑂(𝑔(𝑛)) ⟺ 𝑔(𝑛) ∈ Ω(𝑓(𝑛))
3. 𝑓(𝑛) ∈ 𝑂(𝑔(𝑛)) ∧ 𝑔(𝑛) ∈ 𝑂(ℎ(𝑛)) ⟹ 𝑓(𝑛) ∈ 𝑂(ℎ(𝑛))
4. Θ(𝑓(𝑛)) = 𝑂(𝑓(𝑛)) ∧ Ω(𝑓(𝑛))
190/382
Properties of Asymptotic Notation – Application
Task
Prove that 3𝑛 + 7 ∈ Θ(𝑛).
40
Solution 3𝑛 + 7
4𝑛
30
1. From previous examples, 3𝑛
191/382
Properties of Asymptotic Notation – Computing Complexity
192/382
Properties of asymptotic notation – auxiliary lemma
Lemma
Let us have arbitrary real numbers 𝑎1 , 𝑎2 , 𝑏1 , 𝑏2 . Then the
following holds:
𝑎1 ≤ 𝑏1 ∧ 𝑎2 ≤ 𝑏2 ⟹ 𝑎1 + 𝑎2 ≤ 2 max(𝑏1 , 𝑏2 ).
193/382
Properties of asymptotic notation – auxiliary lemma (cont.)
Proof.
From the assumption, we know that
𝑎1 ≤ 𝑏1
𝑎2 ≤ 𝑏2
𝑎1 + 𝑎2 ≤ 𝑏1 + 𝑏2 .
𝑏1 + 𝑏2 ≤ 2 max(𝑏1 , 𝑏2 ).
𝑎1 + 𝑎2 ≤ 𝑏1 + 𝑏2 ≤ 2 max(𝑏1 , 𝑏2 ).
194/382
Properties of asymptotic notation – computation of complexity
Theorem
If 𝑡1 (𝑛) ∈ 𝑂(𝑔1 (𝑛)) and simultaneously 𝑡2 (𝑛) ∈ 𝑂(𝑔2 (𝑛)), then
Remark
The same statement can be expressed for Ω and Θ notation.
195/382
Properties of asymptotic notation – computation of complexity
(cont.)
Proof.
Since 𝑡1 (𝑛) ∈ 𝑂(𝑔1 (𝑛)), there exists a positive non-zero
constant 𝑐1 and a non-negative constant 𝑛1 such that
𝑡1 (𝑛) ≤ 𝑐1 𝑔1 (𝑛) ∀𝑛 ≥ 𝑛1 .
Similarly,
𝑡2 (𝑛) ≤ 𝑐2 𝑔2 (𝑛) ∀𝑛 ≥ 𝑛2 .
196/382
Properties of asymptotic notation – computation of complexity
(cont.)
Proof.
Let 𝑐3 = max(𝑐1 , 𝑐2 ) and 𝑛0 ≥ max(𝑛1 , 𝑛2 ). Then,
197/382
Properties of asymptotic notation – complexity calculation, ex-
ample
Problem statement
Test whether two identical values occur in the array.
Solution
198/382
Utilization of limits for computations
It is clear that:
𝑡(𝑛) ∈ 𝑂(𝑔(𝑛)) ⇔ 𝑡(𝑛) grows slower or at the same rate as 𝑔(𝑛)
𝑡(𝑛) ∈ Ω(𝑔(𝑛)) ⇔ 𝑡(𝑛) grows at the same rate or faster than 𝑔(𝑛)
𝑡(𝑛) ∈ Θ(𝑔(𝑛)) ⇔ 𝑡(𝑛) grows at the same rate as 𝑔(𝑛)
199/382
Utilization of limits for computations (cont.)
𝑡(𝑛) 𝑡′ (𝑛)
lim = lim ′
𝑛→∞ 𝑔(𝑛) 𝑛→∞ 𝑔 (𝑛)
Stirling’s formula
𝑛 𝑛
𝑛! ≈ √2𝜋𝑛 ( )
𝑒
200/382
Using limits for calculations – example I
2√𝑛
= log2 𝑒 lim
𝑛→∞ 𝑛
1
= 2 log2 𝑒 lim =0
𝑛→∞
√𝑛
202/382
Using limits for computations – example III
Remarks
• The function 𝑛! therefore grows faster than 2𝑛 .
• The definition of Θ-notation does not exclude that
𝑛! ∈ Ω(2𝑛 ), but the limit calculation clearly states that 𝑛!
grows faster than 2𝑛
203/382
Basic Complexity Classes
205/382
Influence of the Multiplicative Constant
207/382
Fundamentals of the Analysis of Algorithm
Efficiency
Analysis of Non-Recursive Algorithms
Finding the Largest Element in an Array of 𝑛 Numbers
208/382
Finding the Largest Element in an Array of 𝑛 Numbers (cont.)
Working Procedure
209/382
Finding the Largest Element in an Array of 𝑛 Numbers (cont.)
210/382
Finding the largest element in an array of 𝑛 numbers, all opera-
tions
211/382
General procedure for determining the time complexity of non-
recursive algorithms
∑(𝑎𝑖 ± 𝑏𝑖 ) = ∑ 𝑎𝑖 ± ∑ 𝑏𝑖 (2)
∑ 𝑐𝑎𝑖 = 𝑐 ∑ 𝑎𝑖 (3)
𝑛 𝑚 𝑛
∑ 𝑎𝑖 = ∑ 𝑎𝑖 + ∑ 𝑎𝑖 (4)
𝑖=1 𝑖=1 𝑖=𝑚+1
𝑢
∑1 = 1 + 1 + ⋯ + 1 = 𝑢 − 𝑙 + 1 (5)
𝑖=𝑙
Specifically
𝑛
∑ 1 = 𝑛 ∈ Θ(𝑛) (6)
𝑖=1
213/382
Useful Summation Formulas (cont.)
𝑛
1 1
∑𝑖 = 1 + 2 + ⋯ + 𝑛 = 𝑛(𝑛 + 1) ≈ 𝑛2 ∈ Θ(𝑛2 ) (7)
𝑖=1
2 2
𝑛
2 2 2 2 1 1
∑𝑖 = 1 + 2 + ⋯ + 𝑛 = 𝑛(𝑛 + 1)(2𝑛 + 1) ≈ 𝑛3 ∈ Θ(𝑛3 ) (8)
𝑖=1
6 3
𝑛
𝑖 2 𝑛 𝑎𝑛+1 − 1
∑𝑎 = 1 + 𝑎 + 𝑎 + ⋯ + 𝑎 = , for 𝑎 ≠ 1 (9)
𝑖=0
𝑎−1
Specifically
𝑛
𝑖 0 1 𝑛 𝑛+1 𝑛
∑ 2 = 2 + 2 + ⋯ + 2 = 2 − 1 ∈ Θ(2 ) (10)
𝑖=0
214/382
Uniqueness of elements in an array
𝑛−1
𝑗
0 pairs that must be
0 tested
an element with itself
does not need to be
𝑖 tested
pairs already tested in
previous iterations of the
𝑛−1 cycle
216/382
Uniqueness of elements in an array (cont.)
Procedure
217/382
Uniqueness of elements in an array (cont.)
218/382
Uniqueness of elements in an array (cont.)
219/382
Uniqueness of elements in an array (cont.)
𝑛−2 𝑛−2
= ∑ [(𝑛 − 1) − (𝑖 + 1) + 1] = ∑(𝑛 − 1 − 𝑖)
𝑖=0 𝑖=0
𝑛−2 𝑛−2
= ∑(𝑛 − 1) − ∑ 𝑖 by (2)
𝑖=0 𝑖=0
𝑛−2
(𝑛 − 2)(𝑛 − 1)
= (𝑛 − 1) ∑ 1 − by (3) and (7)
𝑖=0
2
(𝑛 − 2)(𝑛 − 1)
= (𝑛 − 1)2 − by (5)
2
1 1 2
= 𝑛(𝑛 − 1) ≈ 𝑛 ∈ Θ(𝑛2 )
2 2
220/382
Multiplication of Square Matrices
A B C 𝑛−1
𝑐𝑖,𝑗 = ∑ 𝑎𝑖,𝑘 𝑏𝑘,𝑗
* = 𝑘=0
row i C [i, j]
for all
col. j 0 ≤ 𝑖, 𝑗 ≤ 𝑛 − 1
221/382
Multiplication of Square Matrices (cont.)
222/382
Multiplication of Square Matrices (cont.)
223/382
Multiplication of Square Matrices (cont.)
Informal Procedure
224/382
Multiplication of Square Matrices (cont.)
𝑇(𝑛) ≈ 𝑐𝑚 𝑀(𝑛) = 𝑐𝑚 𝑛3
225/382
Multiplication of Square Matrices (cont.)
Summary
The running time of the algorithm may vary depending on the
specific computer, but the order of complexity of the
algorithm (𝑛3 ) remains the same.
226/382
Number of bits in the binary representation of a number
227/382
Number of bits in the binary representation of a number (cont.)
⌊log2 𝑛⌋ + 1
228/382
Sources for Independent Study
229/382
Fundamentals of the Analysis of Algorithm
Efficiency
Analysis of Recursive Algorithms
Calculation of Factorial
1 Function F(𝑛)
Input: Natural number 𝑛
Result: Result
2 if 𝑛 = 0 then
3 return 1;
4 end
5 else
6 return 𝑛 ⋅ 𝐹(𝑛 − 1);
7 end
8 end 230/382
Calculation of Factorial (cont.)
Remark
To solve the recurrence relation, we need to find an explicit
expression for 𝑀(𝑛). We will use the method of backward
substitution to solve the recurrence relation.
231/382
Calculation of Factorial (cont.)
232/382
Calculation of Factorial (cont.)
233/382
Calculation of Factorial (cont.)
𝑀(𝑛) = 𝑀(0) + 𝑛 = 0 + 𝑛 = 𝑛 .
234/382
Calculation of Factorial (cont.)
Summary
1. The result 𝑀(𝑛) = 𝑛 was more or less expected.
2. An iterative algorithm performs the same number of
multiplications as a recursive algorithm, without the
overhead of function calls.
3. However, the approach used to solve the recurrence
relation is important and can be applied to other
problems.
235/382
General procedure for determining the time complexity of recur-
sive algorithms
236/382
General procedure for determining the time complexity of recur-
sive algorithms (cont.)
237/382
Tower of Hanoi
• The objective is to move all the disks from the first peg to
the third peg using the second peg.
• Rules of the game:
• Only one disk can be moved at a time.
238/382
Tower of Hanoi (cont.)
• A move consists of taking the upper disk from one of the
pegs and sliding it onto another peg.
• It is forbidden to place a larger disk on top of a smaller
one.
• According to legend, there is a monastery in Hanoi
containing the Towers of Hanoi with 64 golden disks. The
monks move one disk every day at noon. The moment the
last disk is moved, the world will end.
• Don’t panic! Solving this puzzle for 64 disks requires
264 − 1 = 18,446,744,073,709,551,615 moves.
• Even if they moved one disk every second (and proceeded
in the shortest possible way), the time to solve is about
600 billion years.
239/382
Tower of Hanoi (cont.)
240/382
Tower of Hanoi (cont.)
A B C
1 3
241/382
Tower of Hanoi (cont.)
Calling
1 TowerOfHanoi(𝑛, 𝐴, 𝐵, 𝐶);
242/382
Tower of Hanoi (cont.)
General procedure
1 for 𝑛 = 1
𝑀(𝑛) = { (11)
2𝑀(𝑛 − 1) + 1 otherwise
243/382
Tower of Hanoi (cont.)
𝑀(𝑛) = 24 𝑀(𝑛 − 4) + 23 + 22 + 2 + 1
244/382
Tower of Hanoi (cont.)
245/382
Tower of Hanoi (cont.)
6. Conclusion:
6.1 The proposed recursive algorithm performs an exponential
number of basic operations with respect to the input size.
6.2 The algorithm is applicable only for small 𝑛, which is not
caused by an inappropriate design. It is caused by the
nature of the problem – it can be proven that this is the
best possible algorithm.
246/382
Tower of Hanoi (cont.)
𝑛
𝑛−1 𝑛−1
𝑛−2 𝑛−2 𝑛−2 𝑛−2
… … … … … …
2 2 2 2
1 1 1 1 1 1 1 1
248/382
Bibliography
249/382
Thanks for your attention
249/382