0% found this document useful (0 votes)
10 views18 pages

Asd3 Eng

Uploaded by

sarahhamz1233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

Asd3 Eng

Uploaded by

sarahhamz1233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

PEOPLE’S DEMOCRATIC REPUBLIC OF ALGERIA

Ministry of Higher Education and Scientific Research

Algorithmics and data structures ADS3

Presented by Mr.Mahseur Mohammed


CONTENTS

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

1.1 Types of complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2


1.2 Time complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Complexity in summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

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

▷ For a given problem, there may be several different algorithms.

▷ This can open our eyes to a comparison of algorithms, which one is more efficient when
N is very large

▷ For small problems, the comparison is not interesting

▷ Measure the efficiency of an algorithm→ calculate its complexity

1.2 Definition of complexity


• It is a criterion for measuring an algorithm

• It is the quantity of resources (space, time) consumed (mobilized) by the implemented


program

1
CHAPTER 1. COMPLEXITY OF ALGORITHMS

1.3 Types of complexity

Figure 1.1: Types of complexity

1.3.1 Spatial complexity


• It is linked to the size of the memory space occupied by the program during its execution.

• 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

• Today, the MC capacity of a PC is worth a few gigabytes

• So, the worry of optimizing the special complexity has disappeared, all that remains is to
study the temporal complexity

1.3.2 Time complexity


• It describes the time required to execute an algorithm (or program)

• We therefore talk about the practical execution time of a program and the theoretical
execution time of an algorithm

Figure 1.2: Time complexity

2
CHAPTER 1. COMPLEXITY OF ALGORITHMS

Practical execution time

It depends on several factors such as:

• The machine (Processor, RAM, etc.)

• The operating system

• The programming language (Assembler, C, Java, etc.)

• The nature of the data handled

• The algorithm adopted

Several factors intervene, which is why the study of complexity was limited to the theoretical
execution time of the algorithm

Theoretical execution time

• To solve a problem, the algorithm must execute a set of instructions (commands)

• Each instruction is composed of one or more elementary operations

• An elementary operation is a fundamental operation with zero, one or two operands


(generally) such as assignment, arithmetic, logic, input, output, etc. operations.

• 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

• The theoretical execution time is: Nb × t

• 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

• Example: algorithm for finding an existing value in a vector

3
CHAPTER 1. COMPLEXITY OF ALGORITHMS

– The best case is that the value is in the first box


– The average case is that the value lies in the middle of the vector
– The worst case is that the value is in the last box

• The study of complexity was limited to the worst case

1.4 Abstract

Figure 1.3: Complexity in summary

Studying complexity means calculating the number of elementary operations necessary to solve
a problem in the worst case based on its size, .

1.5 Complexity calculation


There is no systematic methodology for counting the elementary operations of any algorithm.
However, rules are commonly accepted and used by the computer science community.

1.5.1 Complexity of a sequence of instructions


Let S be a sequence of k instructions:

Begin
instruction 1
instruction 2
.
.
instruction k
End

Let N bi be the number of elementary operations of instruction (i):

4
CHAPTER 1. COMPLEXITY OF ALGORITHMS

k
X
Cs = N bi
i=1

1.5.2 Complexity of a conditional statement


Let a conditional statement:

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:

Ccond = N bexpression + max(N bbloc1 , N bbloc2 )

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

1.5.3 Complexity of a loop for


Consider a bounded iteration:

For i := Bmin to Bmax do


Block
End for

Let N bheader : the number of elementary operations in the loop header


Let N bblock : the number of elementary operations of the block of instructions of the loop

Cf or = (Bmax + 1 − Bmin) × (N bheader + N bblock ) + N bheader

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

1.5.4 Complexity of a loop while


Consider an unbounded iteration:

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

Cwhile = (N biterations ) × (N bCond + N bblock ) + N bcond

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

1.5.5 Complexity of a loop do while


Consider an unbounded iteration:

6
CHAPTER 1. COMPLEXITY OF ALGORITHMS

do
Block
while (cond)

Cdowhile = (N biterations ) × (N bCond + N bblock )

• 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)

1.5.6 Complexity of calling a function or procedure


The cost of calling a procedure (or a function)=The cost of the procedure body for its param-
eters + The cost of evaluating its parameters Example:

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

1.5.7 Complexity of a recursive function


in this case we must calculate the complexity of the function as a function of the parameter N
which represents the size of the problem.
Example:

7
CHAPTER 1. COMPLEXITY OF ALGORITHMS

function Power(A, N:integer):integer


if (N =0) then .............(1)
return 1;
else
return (A*Power(A,N-1));..........(1)+(1)+(1)+(2)+CN −1
end if
end function

The complexity of the function CN = CN −1 +6= C0 + N ∗ 6 = 6 ∗ N + 2

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.

The complexity of the algorithm =6N+8

1.6 Asymptotic complexity


Asymptotic complexity consists of studying the approximate behavior of the number of ele-
mentary operations of the algorithm when the size of the problem treated is sufficiently large
(tends towards infinity)

1.6.1 Landau concept O(nf (N ))


This notion characterizes the asymptotic behavior of complexity when N → ∞ , (N = the
size of the problem) Let C(n): the number of operations as a function of N We say that:
C(n) = O(f (n)) if:
∃c, ∃n0 such that : ∀n > n0 , C(n) ≤ c × f (n)
We say that C(n) is of the order of magnitude of O(f(n))

8
CHAPTER 1. COMPLEXITY OF ALGORITHMS

1.6.2 Simplification of complexity


Two rules to apply:

• We only keep the dominant term (larger when n → ∞)

• We remove the multiplicative constants

Example: C(n) = 3n5 − 2n3 + 5n2 + 7


The dominant term = 3n5 ; After removing the multiplicative constants: C(n) = n5 ;
We write C(n) = O(n5 )

1.6.3 Main orders of magnitude

9
CHAPTER 1. COMPLEXITY OF ALGORITHMS

Module: ASD3
Teacher : Mahseur Mohammed

Series No. 1 - Complexity

Calculate the complexity and order of magnitude of the following algorithms:

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

procedure ReadingMatrix(E/: N:integer; S/: M: Array[1..1000;1..1000]of integers)


Var i, j: integers;
for i= 1 to N do
for j= 1 to N do
Read(M[i,j]);
end for
end for
end procedure

function frequency(N,V:integers; M: Array[1..1000; 1..1000] of integers):integer


Var i, j, Cpt:integers;
Cpt←0;
for i= 1 to N do
for j= 1 to N do
if (M[i,j]=V) then
Cpt←Cpt+1;
end if
end for
end for
Return Cpt;
end function

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

Algorithm 10 The Factorial


1: Var N: integer;
2: Begin
3: Read(N);
4: Write(Factorial(N));
5: End.

function Fibonacci(N:integer):integer
if (N<2) then
return N;
else
return Fibonacci(N-1)+Fibonacci(N-2);
end if
end function

Algorithm 11 Fibonacci Sequence


1: Var N: integer;
2: Begin
3: Read(N);
4: Write(Fibonacci(N));
5: Fin.

13

You might also like