Professional Documents
Culture Documents
Esercizio 1
Esercizio 1
Esercizio 1
for i in range(1,r):
for j in range(1,c):
if S[i-1]!=T[j-1]:
A[i,j]=min(A[i-1,j],A[i,j-1])+1
else:
A[i,j]=A[i-1,j-1]
Since we move thorugh each elements of the matrix and we compute just simple
operation, the computational cost is θ(n · m).
The recursive alghoritm instead has a computetional cost bigger or equal to
θ(2m ) where m is the lenght of the second string in the worst case (that is when
the two strings have completely different characters). In this case the condition
S[-1]!=T[-1] is always satisfied so, each time the alghoritm must compute the
distance between S[:-1] and T, S and T[:-1] and then find the minumum of these
two values. Therefore the recursive equation is:
We noticed that the coefficients are the same coefficient as (a+b)n . Therefore
at the k-th step we get:
k
X k
T (n − j, m − k + j) + θ(2k ) (1)
j=0
j
Now, if we suppose that m < n, at the m-th step the equation became:
1
!
m m n−j
X n − j
X m X m
T (n − j, j) + θ(2m ) = T (n − j − i, 2j − n + i) + θ(2m )
j=0
j j=0
j i=0
i
(2)
The second term of the equality is obtained applying"the same line of reasoning
m
n−j #
m n−j
P P
to T (n−j, j). Whatever is the computational cost of: j i T (n − j − i, 2j − n + i) ,
j=0 i=0
the computational cost of T (n, m) is at least θ(2m ).