Exercises on Algorithm Complexity Analysis
Exercises on Algorithm Complexity Analysis
I. Algorithm Complexity
1. Calculate the execution time T(n) for the algorithm Cosa(n,A). Clearly identify the time.
that was considered for each instruction.
PROCEDURE Thing(n: integer; A: array [0..n, 0..n] of integer); PROCEDURE Rec (n: integer);
var begin
i, j: integer; if n=1 then
begin A[n, n] := A[n, n] + 1;
i:= 1; else begin
repeat Rec (n-1);
A[i, 0] := 0; Rec (n-1);
for j:= 1 to i do begin end;
A[i, j]:= 0; end;
Rec (i);
end;
i:= i+1;
until i >= n;
end;
2. Calculate the execution time T(n) and order O for the algorithm DivVenc(i,j), considering that the
cost of the function Operations 1 and that the initial input esi=1 and j=n. Clearly identify the
time considered for each instruction.
PROCEDURE DivVenc (i, j: integer) PROCEDURE Combine (i, j: integer)
if (i = j) then for k:= i to j do
Combine (i, j); for q:= i to j do
else begin Operation(i, j, k, q);
m := (i + j) / 2;
a := (j - i) / 4;
DivVenc (i, m);
DivVenc (i+a, m+a);
DivVenc (m+1, j);
Combine (i, j);
end;
3. Calculate the execution time and order of the mystery function, assume the declaration of
variables with zero cost.
int mystery(int n){
int i,j,k,s;
s:=0;
for(i=1; i<n; i++)
for(j=i+1; j<=n; j++)
for(k=1; k<=j; k++)
s=s+2;
return s;
}
Exercises - Algorithm Complexity Claudio Henríquez Berroeta
4. Calculate the execution time T(n) for the algorithm First(n). Clearly identify the
time considered for each instruction.
a. Perform the trace for m=2 and n=1. It must include development.
b. Write a recursive function in C (ANSI) or Java ackerman(m,n), which allows obtaining it.
result from two positive integers.
c. Obtain its complexity.
6. Find the recurrence equation and the Order of the following algorithm:
7. Suppose that the input of a certain problem P of size n to solve it is found in the
literature the following alternatives:
8. Find the recurrence equation and the Order of the following algorithm:
9. Calculate the execution time T(k) and Order for the algorithm Div23(k). Clearly identify.
the time he considered for each instruction. Assume that the operation EsPrimo(x) requires a
constant time.
10. Calculate the execution time and order O of the following code.
Sum = 0;
i = 0;
while (i < n) {
j = 0;
while (j < n) {
Sum = Sum + Matrix[i][j];
j ++;
}
i++;
}
11. For a problem of large input size, there are 3 alternatives to solve it:
AlternativeC: model it in a binary tree with vertices and execute for each vertex of the tree a
time algorithm ( ( )3 √ ) .
logn +n n
What alternative do you consider the most efficient?
12. Solve the recurrent equation: t(n) - 2t(n-1) = n + 2n, with n > 0, and t(0) = 0.
13. Suppose that the input of a certain problem P of size n to solve it is found in
the literature the following alternatives:
Alternative B: transform it into another problem for which a time algorithm is known. ,
the transformation takes time
AlternativeC: model it in a binary tree with n vertices and execute for each level of the tree a
time algorithm