Você está na página 1de 9

Sistemas de Informação IFMG - Ouro Branco

6º Período Diego Santos Seabra


19/01/2021 0040251

Tarefa 2
Projeto e Análise de Algoritmos Hospedado no Github

Contents
Exercício 1 2
Ordem de Complexidade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Código Implementado . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Exercício 2 4
Ordem de Complexidade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Código Implementado . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Exercício 3 6

1
Ordem de Complexidade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

4 02 5
Código Implementado . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

00
Exercício 4 8
Código Implementado . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Ordem de Complexidade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1
1) (1 pt) Calcule a ordem de complexidade para a função abaixo.

1: funcao1(A, n, x):
2: y←0
3: for k ← n to 1 do
4: y ← A[k] + y ∗ x
5: end for
6: return y

Solução:
Ordem de Complexidade

código custo
y←0 1
f or(k ← n to 1) n+1
y ← A[k] + y ∗ x n
return y 1

O(n) = 1 + (n + 1) + n + 1

1
= 2 n + 3
O(n) = n

4 02 5
00

2
Código Implementado

1 ///////////////////////////////////////
2 // //
3 // Autor: Diego S. Seabra //
4 // Matricula: 0040251 //
5 // //
6 ///////////////////////////////////////
7
8 #include <stdio.h>
9
10 int funcao1(int A[], int n, int x)
11 {
12 int y = 0; // custo: 1
13 for (int k = n; k > 0; k−−) // custo: n+1
14 {
15 y = A[k] + y * x; // custo: n
16 }
17 return y; // custo: 1
18 }
19
20 int main()
21 {
22 int A[5] = { 3, 2, 3, 4, 5};
23 int res = funcao1(A,2,5);
24 printf("%i\n",res);

1
25 }

4 02 5
00

3
2) (2pts) Calcule a ordem de complexidade para a função abaixo

1: funcao2(n):
2: soma ← 0
3: for i ← 0 to n − 1 do
4: for j ← 0 to n − 1 do
5: for x ← 0 to n − 1 do
6: soma ← soma + n − 1
7: end for
8: end for
9: end for
10: return soma

Solução:
Ordem de Complexidade

código custo
soma ← 0 1
f or(i ← 0 to n − 1) n+1
f or(j ← 0 to n − 1) n × (n + 1)
f or(x ← 0 to n − 1) n × n × (n + 1)
soma ← soma + n − 1 n×n×n
return soma

02 5 1 1

00 4
O(n) = 1 + (n + 1) + (n × (n + 1) + (n × n × (n + 1)) + (n × n × n))
= 1 + (n + 1) + (n2 + n) + (n3 + n2 ) + n3 + 1
=2n
+ 2n2 + 2 n3 + 3
O(n) = n3

4
Código Implementado

1 ///////////////////////////////////////
2 // //
3 // Autor: Diego S. Seabra //
4 // Matricula: 0040251 //
5 // //
6 ///////////////////////////////////////
7
8 #include <stdio.h>
9
10 int funcao2(n)
11 {
12 int soma = 0; // custo: 1
13 for (int i = 0; i ≤ n − 1; i++) // custo: n+1
14 {
15 // printf("for1\n");
16 for (int j = 0; j ≤ n − 1; j++) // custo: n * n+1
17 {
18 // printf("for2\n");
19 for (int x = 0; x ≤ n − 1; x++) // custo: n * n * n+1
20 {
21 // printf("for3\n");
22 soma = soma + (n − 1); // custo: n * n * n
23 }
24 }

1
25 }

5
26

2
return soma; // custo: 1

0
27

4
28 }

00
29

30 int main()
31 {
32 int soma = funcao2(2);
33 printf("%i\n", soma);
34 }

5
3) (3pts) Calcule a ordem de complexidade para a função abaixo

1: funcao3(A, n):
2: for j ← 3 to n do
3: aux ← A[j]
4: i←j−1
5: while i > 1 && A[i] > aux do
6: A[i + 1] ← A[i]
7: i←i−1
8: end while
9: A[i + 1] ← aux
10: end for

Solução:
Ordem de Complexidade

código custo
f or(j ← 3 to n) n
aux ← A[j] n−1
i←j−1 n−1
Pn−1
while(i > 1&&A[i] > aux j
Pj=3
n−1
A[i + 1] ← A[i] j−1

1
Pj=3
n−1

5
i←i−1 j=3 j − 1
A[i + 1] ← aux

4 02 n−1

00 
Pn−1
O(n) = n + (3 × (n − 1)) + ( j=3
n(n − 1)
 
Pn−1
j) + (2 × ( j=3 j − 1))
n(n − 1)

= n + 3n − 3 + −3 +2× −n
2 2
n2 − n n2 − n
= 4n − 3 + −3+2× −n
2 2
n2 − n
= −6
3n +3 ×
2
2
O(n) = n

6
Código Implementado

1 ///////////////////////////////////////
2 // //
3 // Autor: Diego S. Seabra //
4 // Matricula: 0040251 //
5 // //
6 ///////////////////////////////////////
7
8 #include <stdio.h>
9
10 #define n 6
11

12 // Mesma ideia do Insertion Sort


13 void funcao3(int A[n])
14 {
15 int aux, i; // custo: 1
16 for (int j = 2; j < n; j++) // custo: n
17 {
18 aux = A[j]; // custo: n−1
19 i = j − 1; // custo: n−1
20
21 while (i > 0 && A[i] > aux) // custo: somatorio (j=3) (n−1) (j)
22 {
23 A[i + 1] = A[i]; // custo: somatorio (j=3) (n−1) (j−1)
24 i = i − 1; // custo: somatorio (j=3) (n−1) (j−1)

1
25 }

5
26 A[i + 1] = aux; // custo: n−1

2
}

0
27

4
28 }

00
29

30 void imprimeArranjo(int A[n])


31 {
32 for (int i = 0; i < n; i++)
33 {
34 printf("%i ", A[i]);
35 }
36 printf("\n");
37 }
38
39 int main()
40 {
41 int A[n] = {31, 41, 59, 26, 41, 58};
42 printf("Antes: ");
43 imprimeArranjo(A);
44 funcao3(A);
45 printf("Depois: ");
46 imprimeArranjo(A);
47 }

7
4) (4 pts) Dadas n variáveis booleanas, escreva um algoritmo que gere todas
as combinações possíveis. Por exemplo, para três variáveis deverá ser gerado:
000 - 001 - 010 - 011 - 100 - 101 - 110 - 111. Em seguida análise a complexidade
deste algoritmo.
Solução:
Código Implementado

1 ///////////////////////////////////////
2 // //
3 // Autor: Diego S. Seabra //
4 // Matricula: 0040251 //
5 // //
6 ///////////////////////////////////////
7
8 #include <stdio.h>
9 #include <math.h>
10

11 #define n 5
12
13 int main()
14 {
15 int potenciaBinaria = pow(2, n); // custo: 1
16 int k; // custo: 1
17

1
18 printf("%i\n", potenciaBinaria);

5
19
20
21 {

4 02
for (int i = 0; i < potenciaBinaria; i++) // custo: 2^n + 1

00
22 for (int j = n − 1; j ≥ 0; j−−) // custo: 2^n * (n+1)
23 {
24 k = i >> j; // custo: 2^n * n
25
26 if (k & 1) // custo: 2^n * n
27 printf("1"); // custo: 2^n * n
28 else // custo: 2^n * n
29 printf("0"); // custo: 2^n * n
30 }
31
32 if (i != potenciaBinaria − 1) // custo: 2^n
33 {
34 printf(" − "); // custo: 2^n
35 }
36 }
37 printf("\n"); // custo: 1
38 }

8
Ordem de Complexidade

O(n) = 1 + 1 + (2n + 1) + ((2n + 1) × (n + 1)) + (2n + 1 × n)


+ ((2n + 1) × n) + ((2n + 1) × n)
+ ((2n + 1) × n) + ((2n + 1) × n) + (2n ) + (2n ) + 1
= 3 + 2n + 1 + ((2n + 1) × (n + 1)) + 5 × ((2n + 1) × n) + 2 × 2n
2n+1
= 3 ×  n + + 2n+2 + 5
6n


n+2
O(n) = 2

02 5 1
00 4

Diego Santos Seabra


0040251

Você também pode gostar