Skip to content

Commit f02e9a0

Browse files
committed
commit
1 parent 1edaa8b commit f02e9a0

26 files changed

+56559
-0
lines changed

MT_2/PROG2_1617_MT2/prob1/ex1.1

21.9 KB
Binary file not shown.

MT_2/PROG2_1617_MT2/prob1/ex1.1.c

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "avl.h"
5+
#include "grafo.h"
6+
7+
/*** problema 1.1 ***/
8+
char *avl_maiorstring(no_avl *no)
9+
{
10+
/* complexidade do algoritmo: O(n*log(n)) */
11+
if (!no)
12+
{
13+
return NULL;
14+
}
15+
16+
char *str_no = no->str;
17+
char *str_esq = avl_maiorstring(no->esquerda);
18+
char *str_dir = avl_maiorstring(no->direita);
19+
int max, tam=0, tam_esq=0, tam_dir=0;
20+
if (str_no!=NULL)
21+
{
22+
tam = strlen(str_no);
23+
}
24+
if (str_esq!=NULL)
25+
{
26+
tam_esq = strlen(str_esq);
27+
}
28+
if (str_dir!=NULL)
29+
{
30+
tam_dir = strlen(str_dir);
31+
}
32+
max = tam;
33+
char *res=str_no;
34+
if (tam_esq > max)
35+
{
36+
max = tam_esq;
37+
res = str_esq;
38+
}
39+
if (tam_dir > max)
40+
{
41+
max = tam_dir;
42+
res = str_dir;
43+
}
44+
return res;
45+
}
46+
47+
/*** problema 1.2 ***/
48+
int grafo_maisdistante(grafo *g, int origem, int *distancia)
49+
{
50+
if (!g || origem<0 || distancia<0)
51+
{
52+
return -1;
53+
}
54+
int v[g->tamanho];
55+
int n;
56+
for (int i = 0; i < g->tamanho; i++)
57+
{
58+
grafo_bfs(g, origem, i, &n);
59+
v[i]=n-1;
60+
}
61+
int max=0, pos=0;
62+
for (int i = 0; i < g->tamanho; i++)
63+
{
64+
if (v[i]>max)
65+
{
66+
max=v[i];
67+
pos=i;
68+
}
69+
70+
}
71+
*distancia=max;
72+
return pos;
73+
}
74+
75+
/* Aqui comeca o codigo de teste. Nao modificar! */
76+
77+
#define MAX_STR 500
78+
79+
int main()
80+
{
81+
setvbuf(stdout, NULL, _IONBF, 0);
82+
FILE *fp = fopen("paises.txt", "r");
83+
char str_aux[MAX_STR], *str;
84+
85+
/* teste problema 1.1 */
86+
{
87+
arvore_avl *arv;
88+
puts("* Problema 1.1 *");
89+
arv = avl_nova();
90+
while (fgets(str_aux, MAX_STR, fp) != NULL)
91+
{
92+
if (str_aux[strlen(str_aux) - 1] == '\n')
93+
str_aux[strlen(str_aux) - 1] = '\0';
94+
avl_insere(arv, str_aux);
95+
}
96+
str = avl_maiorstring(arv->raiz);
97+
printf("Maior string: ");
98+
if (str != NULL)
99+
puts(str);
100+
else
101+
puts("(null)");
102+
avl_apaga(arv);
103+
}
104+
105+
/* teste problema 1.2 */
106+
{
107+
grafo *g;
108+
int vertice, comprimento;
109+
puts("\n* Problema 1.2 *");
110+
g = grafo_novo(8, DIGRAFO);
111+
grafo_adiciona(g, 0, 4);
112+
grafo_adiciona(g, 1, 0);
113+
grafo_adiciona(g, 1, 5);
114+
grafo_adiciona(g, 2, 1);
115+
grafo_adiciona(g, 3, 2);
116+
grafo_adiciona(g, 4, 3);
117+
grafo_adiciona(g, 5, 4);
118+
grafo_adiciona(g, 6, 3);
119+
grafo_adiciona(g, 7, 2);
120+
grafo_adiciona(g, 7, 6);
121+
122+
vertice = grafo_maisdistante(g, 1, &comprimento);
123+
if (vertice != -1)
124+
printf("Mais distante do vertice 1: %d (distancia %d)\n", vertice, comprimento);
125+
vertice = grafo_maisdistante(g, 6, &comprimento);
126+
if (vertice != -1)
127+
printf("Mais distante do vertice 6: %d (distancia %d)\n", vertice, comprimento);
128+
129+
grafo_apaga(g);
130+
}
131+
132+
fclose(fp);
133+
return 0;
134+
}

MT_2/PROG2_1617_MT2/prob2/ex2.2

21.4 KB
Binary file not shown.

MT_2/PROG2_1617_MT2/prob2/ex2.2.c

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "vetor.h"
5+
#include "heap.h"
6+
7+
/*** problema 2.1 ***/
8+
vetor *heap_ordena(vetor *v)
9+
{
10+
if (!v)
11+
{
12+
return NULL;
13+
}
14+
heap *h = heap_nova(v->tamanho);
15+
if (!h)
16+
{
17+
return NULL;
18+
}
19+
vetor *vec = vetor_novo();
20+
if (!vec)
21+
{
22+
return NULL;
23+
}
24+
25+
for (int i = 0; i < v->tamanho; i++)
26+
{
27+
//testa se insere bem
28+
if (!heap_insere(h, v->elementos[i].str, (v->elementos[i].str[0] << 8) + v->elementos[i].str[1]))
29+
{
30+
heap_apaga(h);
31+
h = NULL;
32+
return NULL;
33+
}
34+
}
35+
int tam = v->tamanho;
36+
for (int i = 0; i < tam; i++)
37+
{
38+
vetor_insere(vec, heap_remove(h), -1);
39+
}
40+
heap_apaga(h);
41+
h=NULL;
42+
//falta ordenar o vetor
43+
v_elemento aux;
44+
int tam2=v->tamanho;
45+
for (int i = 0; i < tam2/2; i++)
46+
{
47+
aux = vec->elementos[i];
48+
vec->elementos[i] = vec->elementos[tam2 - 1 - i];
49+
vec->elementos[tam2 - 1 - i] = aux;
50+
}
51+
52+
return vec;
53+
}
54+
55+
/*** problema 2.2 ***/
56+
/*
57+
complexidade do algoritmo:O(n*log(n))
58+
59+
justificacao:
60+
inserir na heap n vezes: O(n*log(n));
61+
remover da heap n vezes : O(n*log(n));
62+
inserir no vetor n vezes: O(n);
63+
inversao do vetor : O(n);
64+
65+
TOTAL = O(n*log(n)) + O(n*log(n)) O(n) + O(n)= O(n*log(n))
66+
*/
67+
68+
/* Aqui comeca o codigo de teste. Nao modificar! */
69+
70+
#define MAX_LINE 100
71+
72+
int main()
73+
{
74+
setvbuf(stdout, NULL, _IONBF, 0);
75+
76+
/* teste problema 2.1 */
77+
{
78+
int i, j;
79+
char cidade[MAX_LINE];
80+
vetor *v, *res;
81+
FILE *fp;
82+
83+
puts("* Problema 2.1 *");
84+
v = vetor_novo();
85+
fp = fopen("cidades.txt", "r");
86+
while (fgets(cidade, MAX_LINE, fp) != NULL)
87+
{
88+
*strchr(cidade, ',') = '\0';
89+
vetor_insere(v, cidade, v->tamanho);
90+
}
91+
res = heap_ordena(v);
92+
if (res != NULL)
93+
{
94+
for (i = 0; i < 3 && i < vetor_tamanho(res); i++)
95+
printf("%s\n", vetor_elemento(res, i));
96+
if (vetor_tamanho(res) > 3)
97+
{
98+
if (vetor_tamanho(res) > 6)
99+
puts("...");
100+
j = i;
101+
if (vetor_tamanho(res) - 3 > j)
102+
j = vetor_tamanho(res) - 3;
103+
for (i = j; i < vetor_tamanho(res); i++)
104+
printf("%s\n", vetor_elemento(res, i));
105+
}
106+
}
107+
vetor_apaga(v);
108+
vetor_apaga(res);
109+
}
110+
111+
return 0;
112+
}

P12/PROG2_2021_P12_Revisoes.pdf

81.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)