Asd3 Eng
Asd3 Eng
1 Complexity of algorithms 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Definition of complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Types of complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3.1 Spatial complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3.2 Time complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Complexity calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5.1 Complexity of a sequence of instructions . . . . . . . . . . . . . . . . . . 4
1.5.2 Complexity of a conditional statement . . . . . . . . . . . . . . . . . . . 5
1.5.3 Complexity of a loop for . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5.4 Complexity of a loop while . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5.5 Complexity of a loop do while . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5.6 Complexity of calling a function or procedure . . . . . . . . . . . . . . . 7
1.5.7 Complexity of a recursive function . . . . . . . . . . . . . . . . . . . . . 7
1.6 Asymptotic complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.6.1 Landau concept O(nf (N )) . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.6.2 Simplification of complexity . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.6.3 Main orders of magnitude . . . . . . . . . . . . . . . . . . . . . . . . . . 9
I
LIST OF FIGURES
II
LIST OF TABLES
III
LIST OF ALGORITHMS
1 Example1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 Example2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 Maximum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4 Prime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5 TheSum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6 Money . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
7 Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
8 MatrixFrequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
9 Rest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10 The Factorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
11 Fibonacci Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
IV
CHAPTER 1
COMPLEXITY OF ALGORITHMS
1.1 Introduction
▷ An algorithm is a method of solving a problem of size N
▷ This can open our eyes to a comparison of algorithms, which one is more efficient when
N is very large
1
CHAPTER 1. COMPLEXITY OF ALGORITHMS
• At the beginning of the computer age, this parameter had about the same importance as
time complexity
• In the 70s for example, the size of a central memory (MC) of a large machine = 128 KB
• So, the worry of optimizing the special complexity has disappeared, all that remains is to
study the temporal complexity
• We therefore talk about the practical execution time of a program and the theoretical
execution time of an algorithm
2
CHAPTER 1. COMPLEXITY OF ALGORITHMS
Several factors intervene, which is why the study of complexity was limited to the theoretical
execution time of the algorithm
• Example instructions:
X←a; (an assignment operation)
X←a+b; (two operations: addition and assignment)
X← a+b+c; (3 operations: 2 additions and 1 assignment)
• Suppose that the average execution time of an operation is t, and the number of elemen-
tary operations of an algorithm is Nb
• Knowing that the average execution time of an elementary operation is a constant linked
to several factors (machine)
• Therefore, the study of the theoretical execution time was limited to the number of
elementary operations carried out
Worst-case complexity
• When solving a given problem, we can find three cases: The best case; The average case
and the worst case
3
CHAPTER 1. COMPLEXITY OF ALGORITHMS
1.4 Abstract
Studying complexity means calculating the number of elementary operations necessary to solve
a problem in the worst case based on its size, .
Begin
instruction 1
instruction 2
.
.
instruction k
End
4
CHAPTER 1. COMPLEXITY OF ALGORITHMS
k
X
Cs = N bi
i=1
If (expression) then
Block1
Else
Block2
End if
Let N bexpression : the number of elementary operations to evaluate the expression of the
condition
Let N bbloc1 and N bbloc2 : the number of elementary operations of block 1 and 2 respectively
It is impossible to determine which part of the instruction will be executed, so we take an upper
bound:
Example:
1: Begin
2: if (A > B − 1) then
3: A←A−B+1
4: else
5: A ← (A + B) ∗ (B − 1)
6: end if
7: End.
N bexpression = 2; M ax(N bbloc1 , N bbloc2 ) = M ax(3, 4) = 4; Ccond = 2 + 4 = 6
Example:
5
CHAPTER 1. COMPLEXITY OF ALGORITHMS
1: Begin
2: for i = 1 to N do
3: if (A > B − 1) then
4: A←A−B+1
5: else
6: A ← (A + B) ∗ (B − 1)
7: end if
8: end for
9: End.
Bmax = N ; Bmin = 1; N bheader = 2; N bblock = 6
Cf or = (N + 1 − 1) × (2 + 6) + 2 = 8N + 2
While (Cond) do
Block
End while
Let N bCond : the number of elementary operations of the evaluation of the condition
Let N bblock : the number of elementary operations of the loop block
Let N biterations : the number of repetitions, it is evaluated inductively in the worst case
Example:
1: Begin
2: while (N ! = 0) do
3: if (A > B − 1) then
4: A←A−B+1
5: else
6: A ← (A + B) ∗ (B − 1)
7: end if
8: N ← N/3
9: end while
10: End.
N bCond = 1; N bblock = 6; N biterations = Ln(N )/Ln(2);
Cwhile = (Ln(N )/Ln(2)) × (1 + 6) + 1 = 7Ln(N )/Ln(2) + 1
6
CHAPTER 1. COMPLEXITY OF ALGORITHMS
do
Block
while (cond)
• But it is not easy to find the number of iterations in a While loop (in a do while loop too)
• If we notice a small value (i) tends towards a large value (N) by the addition of a step,
or the opposite by the subtraction of a step: then the number of iterations = N/Step
• If we notice a small value (i) tends towards a large value (N) by multiplication by one step,
or the opposite by division by one step: then the number of iterations=Ln(N)/Ln(Step)
function Even(N:integer):boolean
if (N mod 2=0) then
return true;
else
return false;
end if
end function
Algorithm 1 Example1
1: Var A: integer
2: Begin
3: Read(A);
4: if (Even(A)=true) then
5: A←A+1
6: else
7: A←A+2
8: end if
9: End.
Cf unction = 3
Calgo = 1 + 1 + 3 + 1 + 2 = 8
7
CHAPTER 1. COMPLEXITY OF ALGORITHMS
Algorithm 2 Example2
1: Var A,N: integer
2: Begin
3: Read(A,N); .............(2)
4: Write("The Power=", Power(A,N));.........(1)+(1)+(2)+CN
5: End.
8
CHAPTER 1. COMPLEXITY OF ALGORITHMS
9
CHAPTER 1. COMPLEXITY OF ALGORITHMS
Module: ASD3
Teacher : Mahseur Mohammed
Algorithm 3 Maximum
1: Var A, B, C, Max: integers;
2: Begin
3: Read(A,B,C);
4: if (A>B) then
5: if (A>C) then
6: Max ← A;
7: else
8: Max ← C;
9: end if
10: else
11: if (B>C) then
12: Max ← B;
13: else
14: Max ← C;
15: end if
16: end if
17: Write(Max);
18: End.
Algorithm 4 Prime
1: Var N, i, cpt: integers;
2: Begin
3: Read(N);
4: if (N=1) then
5: Write(N,"is not a prime number");
6: else
7: i ← 2; cpt ← 0;
8: while (i≤ N/2)And(cpt=0) do
9: if (N mod i=0) then
10: cpt ← cpt+1;
11: else
12: i ← i+1;
13: end if
14: end while
15: if (cpt=0) then
16: Write(N,"is a prime number");
17: else
18: Write(N,"is not a prime number");
19: end if
20: end if
21: End.
10
CHAPTER 1. COMPLEXITY OF ALGORITHMS
Algorithm 5 TheSum
1: Var N, p, i, fact, puiss: integer; F: real;
2: Begin
3: Read(N,p);
4: F ← 1; puiss ← 1; fact ← 1;
5: for i= 1 to N do
6: puiss ← puiss*p;
7: fact ← fact*i;
8: F ← F+puiss/fact;
9: end for
10: Write(F);
11: End.
Algorithm 6 Money
1: Var N, a, b, c, d: integers
2: Begin
3: Read(N);
4: for a= 0 to N do
5: for b= 0 to N div 10 do
6: for c= 0 to N div 20 do
7: for d= 0 to N div 50 do
8: if (N=a +10*b+20*c+50*d) then
9: Write(a,b,c,d);
10: end if
11: end for
12: end for
13: end for
14: end for
15: End.
Algorithm 7 Sorting
1: Var i, j, sauv: integers;
2: T: Array [1..100] of integers;
3: Begin
4: Read(N);
5: for i= 1 to N-1 do
6: for j= i+1 to N do
7: if (T[i]<T[j]) then
8: sauv←T[i];
9: T[i]←T[j];
10: T[j]←sauv;
11: end if
12: end for
13: end for
14: End.
11
CHAPTER 1. COMPLEXITY OF ALGORITHMS
Algorithm 8 MatrixFrequency
1: Var M: Array[1..1000; 1..1000] of integer ; N, V:integer;
2: Begin
3: Read(N); Read(V); Cpt←0;
4: ReadingMatrix(N,M);
5: Write(Frequency(N,V,M));
6: End.
Algorithm 9 Rest
1: Var N, d, r:integer;
2: Begin
3: Read(N); r←N;
4: while (r>d) do
5: r←r-d;
6: end while
7: Write(r);
8: End.
12
CHAPTER 1. COMPLEXITY OF ALGORITHMS
function Factorial(N:integer):integer
if (N=0) then
return 1;
else
return N*Factorial(N-1);
end if
end function
function Fibonacci(N:integer):integer
if (N<2) then
return N;
else
return Fibonacci(N-1)+Fibonacci(N-2);
end if
end function
13