Problems_week_3_Compexity
Problems_week_3_Compexity
Problems_week_3_Compexity
x <- n
for k <- 0 to n-1 do
j <- 0
while j <= k do
x <- x + x
j <- j + 1
n=3
1 x=3 x=3
2 k=0 x=3, k = 0
4
2. Sa se determine numărul de operații efectuate de către un algoritm care determina
numărul de inversiuni ale unei permutări.
Determine the number of operations performed by an algorithm that determines the
number of inversions of a permutation.
Pentru o permutare (a1, . . . , an), a.i. i∈ {1, . . . , n}, i = 1, n o inversiune este o pereche de indici (i,
j) cu proprietatea ca i < j si ai > aj .
For a permutation (a1, . . . , an), a.i. i∈ {1, . . . , n}, i = 1, n; an inversion is a pair of indices (i,j)
with the property that i<j and ai > aj .
1 1 1
2 2*n 1
3 2*(n-i+1) i=1,n-1
4 1 i=1,n-1; j = i+1,n
5 T i=1,n-1; j = i+1,n
3. Precizati si demonstrati complexitatea timp a următorului algoritm.
Determine and specify the time complexity of the following algorithm:
1 sum <- 0
2 for i <- 0 to n-1 do
3 for j <- i to n-1 do
4 for k <- 0 to 4 do
5 sum <- sum + 2
1 1 1
2 2(n+1) 1
3 2(n-i+1) i=0,n-1
● T(n)=2lgn + 4n + 3nlgn
The dominant term in 2lgn + 4n + 3nlgn is 3nlgn and the growth order is n log n.
● 2 + 4 + 6 + . . . 2n - is it 2n?
In natural language we must prove that n! is bounded by n^n, n^n is an upper bound.
Do not forget O is the notation that means: “worse case scenario of an algorithm”
-----------------------------------------------Base case--------------------------------
P(1) : 1! < = 1 ^ 1 ? Yes
P(2): 2! < = 2 ^ 2 ? 2 < = 4 ? Yes
---------------------------------------------------------------------------------------------
-----------------------------------------------Induction step---------------------------
P(3): Say that: 3! = 3 * 2! write (k + 1) ! = ( k + 1 ) * P(k) - current step * last step
Knowing that k < k +1 din (k+1) * k! < (k+1)* k ^ k it will result that
lim n!/n^n
n-> infinity
limita e 0, daca stam sa calculam, asta inseamna ca n! creste mai greu decat n^n => n^n il
margineste superior => am demonstrat
limit is 0 if we calculate, that means that n! grows slower than n^n hence it is a superior bound,
hence we proved.