Escolar Documentos
Profissional Documentos
Cultura Documentos
#include<stdio.h>
#define MAX 10
#define TEMP 0
#define PERM 1
#define FALSE 0
#define TRUE 1
struct node
int predecessor;
int status;
};
struct aresta
int u;
int v;
};
int adj[MAX][MAX];
int n;
main()
int i,j;
int path[MAX];
int peso_arvore,count;
struct aresta arvore[MAX];
criar_grafo();
mostrar();
count = monta_arvore(arvore,&peso_arvore);
for(i=1;i<=count;i++)
printf("%d->",arvore[i].u);
printf("%d\n",arvore[i].v);
criar_grafo()
int i,max_arestas,origem,destino,peso;
scanf("%d",&n);
max_arestas=n*(n-1)/2;
for(i=1;i<=max_arestas;i++)
scanf("%d %d",&origem,&destino);
scanf("%d",&peso);
printf("INVLIDA!\n");
i--;
}
else
adj[origem][destino]=peso;
adj[destino][origem]=peso;
if(i<n-1)
printf("MST no possvel\n");
exit(1);
mostrar()
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
printf("%3d",adj[i][j]);
printf("\n");
int i,k,min,count,atual,novadist;
int m;
int u1,v1;
*weight=0;
/*Ns temporrios*/
for(i=1;i<=n;i++)
estado[i].predecessor=0;
estado[i].dist = infinity;
estado[i].status = TEMP;
/*N permanente*/
estado[1].predecessor=0;
estado[1].dist = 0;
estado[1].status = PERM;
atual=1;
count=0;
for(i=1;i<=n;i++)
estado[i].predecessor = atual;
estado[i].dist = adj[atual][i];
min=infinity;
for(i=1;i<=n;i++)
min = estado[i].dist;
atual=i;
estado[atual].status=PERM;
u1=estado[atual].predecessor;
v1=atual;
count++;
arvore[count].u=u1;
arvore[count].v=v1;
*weight=*weight+adj[u1][v1];
return (count);
int i;
for(i=1;i<=n;i++)
return FALSE;
return TRUE;
}
HILL CLIMBING
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
//A=0, B=1, C=2, D=3, E=4, F=5, G=6, H=7, I=8, K=9, L=10, M=11, N=12, P=13, Q=14, R=15,
S=16, T=17, U=18, V=19, W=20
void ini_dist_linha_reta()
dist_linha_reta[11] = 9; //M
mat_adj[i][j] = 0;
mat_adj[0][1] = 3; //A - B
mat_adj[1][0] = 3; //B - A
mat_adj[0][8] = 3; //A - I
mat_adj[8][0] = 3; //I - A
mat_adj[1][2] = 4; //B - C
mat_adj[2][1] = 4; //C - B
mat_adj[1][8] = 4; //B - I
mat_adj[8][1] = 4; //I - B
mat_adj[2][3] = 3; //C - D
mat_adj[3][2] = 3; //D - C
mat_adj[2][4] = 3; //C - E
mat_adj[4][2] = 3; //E - C
mat_adj[3][4] = 2; //D - E
mat_adj[4][3] = 2; //E - D
mat_adj[4][6] = 3; //E - G
mat_adj[6][4] = 3; //G - E
mat_adj[6][7] = 3; //G - H
mat_adj[7][6] = 3; //H - G
mat_adj[6][9] = 3; //G - K
mat_adj[9][6] = 3; //K - G
mat_adj[7][9] = 2; //H - K
mat_adj[9][7] = 2; //K - H
mat_adj[9][5] = 8; //K - F
mat_adj[5][9] = 8; //F - K
mat_adj[5][20] = 8; //F - W
mat_adj[20][5] = 8; //W - F
mat_adj[5][11] = 8; //F - M
mat_adj[11][5] = 8; //M - F
mat_adj[20][17] = 3; //W - T
mat_adj[17][20] = 3; //T - W
mat_adj[17][19] = 7; //T - V
mat_adj[19][17] = 7; //V - T
mat_adj[17][16] = 4; //T - S
mat_adj[16][17] = 4; //S - T
mat_adj[16][15] = 2; //S - R
mat_adj[15][16] = 2; //R - S
mat_adj[19][18] = 2; //V - U
mat_adj[18][19] = 2; //U - V
mat_adj[15][14] = 2; //R - Q
mat_adj[14][15] = 2; //Q - R
mat_adj[14][13] = 2; //Q - P
mat_adj[13][14] = 2; //P - Q
mat_adj[18][13] = 3; //U - P
mat_adj[13][18] = 3; //P - U
mat_adj[13][12] = 3; //P - N
mat_adj[12][13] = 3; //N - P
mat_adj[8][10] = 5; //I - L
mat_adj[10][8] = 5; //L - I
int i, j, k;
printf("\tA\tB\tC\tD\tE\tF\tG\tH\tI\tK\tL\tM\t\tN\tP\tQ\tR\tS\tT\tU\tV\tW\n\n");
for(i=0; i<21; i++){
case 0: printf("A\t");
break;
case 1: printf("B\t");
break;
case 2: printf("C\t");
break;
case 3: printf("D\t");
break;
case 4: printf("E\t");
break;
case 5: printf("F\t");
break;
case 6: printf("G\t");
break;
case 7: printf("H\t");
break;
case 8: printf("I\t");
break;
case 9: printf("K\t");
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
printf("%d\t", mat_adj[i][j]);
printf("\n\n");
Fila *aux;
aux = p->ant;
printf("Caminho: ");
while(aux->prox != NULL){
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
aux = aux->prox;
case 0: printf("A");
break;
case 1: printf("B");
break;
case 2: printf("C");
break;
case 3: printf("D");
break;
case 4: printf("E");
break;
case 5: printf("F");
break;
case 6: printf("G");
break;
case 7: printf("H");
break;
case 8: printf("I");
break;
case 9: printf("K");
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
Fila* insere_fila(Fila* F_prim, int nome) //inserir elementos na fila de caminhos anteriores
f->nome = nome;
f->prox = NULL;
if(aux == NULL){ //se for o primeiro elemento a ser inserido na fila F_prim
F_prim = f;
while(aux->prox != NULL)
aux = aux->prox;
aux->prox = f;
return F_prim;
Lista* busca_hillclimbing(int inicio, int fim) //buscar o vizinho com menor heuristica (hill climbing)
atual->nome = vizinho->nome;
atual->dist = vizinho->dist;
atual->dist_reta = vizinho->dist_reta;
atual->prox = vizinho->prox;
main()
//double tempoGasto;
Lista *L_prim=NULL;
printf("\n\nA=0, B=1, C=2, D=3, E=4, F=5, G=6, H=7, I=8, K=9, L=10, M=11, N=12,
P=13, Q=14, R=15, S=16, T=17, U=18, V=19, W=20\n\n");
//tempoInicial = clock();
//tempoFinal = clock();
//tempoGasto = (tempoFinal-tempoInicial)/(CLOCKS_PER_SEC/1000);
}
A*
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
//A=0, B=1, C=2, D=3, E=4, F=5, G=6, H=7, I=8, K=9, L=10, M=11, N=12, P=13, Q=14, R=15,
S=16, T=17, U=18, V=19, W=20
void ini_dist_linha_reta()
dist_linha_reta[5] = 0; //F
dist_linha_reta[11] = 9; //M
mat_adj[i][j] = 0;
mat_adj[0][1] = 3; //A - B
mat_adj[1][0] = 3; //B - A
mat_adj[0][8] = 3; //A - I
mat_adj[8][0] = 3; //I - A
mat_adj[1][2] = 4; //B - C
mat_adj[2][1] = 4; //C - B
mat_adj[1][8] = 4; //B - I
mat_adj[8][1] = 4; //I - B
mat_adj[2][3] = 3; //C - D
mat_adj[3][2] = 3; //D - C
mat_adj[2][4] = 3; //C - E
mat_adj[4][2] = 3; //E - C
mat_adj[3][4] = 2; //D - E
mat_adj[4][3] = 2; //E - D
mat_adj[4][6] = 3; //E - G
mat_adj[6][4] = 3; //G - E
mat_adj[6][7] = 3; //G - H
mat_adj[7][6] = 3; //H - G
mat_adj[6][9] = 3; //G - K
mat_adj[9][6] = 3; //K - G
mat_adj[7][9] = 2; //H - K
mat_adj[9][7] = 2; //K - H
mat_adj[9][5] = 8; //K - F
mat_adj[5][9] = 8; //F - K
mat_adj[5][20] = 8; //F - W
mat_adj[20][5] = 8; //W - F
mat_adj[5][11] = 8; //F - M
mat_adj[11][5] = 8; //M - F
mat_adj[20][17] = 3; //W - T
mat_adj[17][20] = 3; //T - W
mat_adj[17][19] = 7; //T - V
mat_adj[19][17] = 7; //V - T
mat_adj[17][16] = 4; //T - S
mat_adj[16][17] = 4; //S - T
mat_adj[16][15] = 2; //S - R
mat_adj[15][16] = 2; //R - S
mat_adj[19][18] = 2; //V - U
mat_adj[18][19] = 2; //U - V
mat_adj[15][14] = 2; //R - Q
mat_adj[14][15] = 2; //Q - R
mat_adj[14][13] = 2; //Q - P
mat_adj[13][14] = 2; //P - Q
mat_adj[18][13] = 3; //U - P
mat_adj[13][18] = 3; //P - U
mat_adj[13][12] = 3; //P - N
mat_adj[12][13] = 3; //N - P
mat_adj[8][10] = 5; //I - L
mat_adj[10][8] = 5; //L - I
int i, j, k;
printf("\tA\tB\tC\tD\tE\tF\tG\tH\tI\tK\tL\tM\t\tN\tP\tQ\tR\tS\tT\tU\tV\tW\n\n");
for(i=0; i<21; i++){
case 0: printf("A\t");
break;
case 1: printf("B\t");
break;
case 2: printf("C\t");
break;
case 3: printf("D\t");
break;
case 4: printf("E\t");
break;
case 5: printf("F\t");
break;
case 6: printf("G\t");
break;
case 7: printf("H\t");
break;
case 8: printf("I\t");
break;
case 9: printf("K\t");
break;
break;
break;
break;
case 13: printf("P\t");
break;
break;
break;
break;
break;
break;
break;
break;
printf("%d\t", mat_adj[i][j]);
printf("\n\n");
Fila *aux;
aux = p->ant;
printf("Caminho: ");
while(aux != NULL){
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
case 13: printf("P - ");
break;
break;
break;
break;
break;
break;
break;
break;
aux = aux->prox;
case 0: printf("A");
break;
case 1: printf("B");
break;
case 2: printf("C");
break;
case 3: printf("D");
break;
case 4: printf("E");
break;
case 5: printf("F");
break;
case 6: printf("G");
break;
case 7: printf("H");
break;
case 8: printf("I");
break;
case 9: printf("K");
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
Fila* insere_fila(Fila* F_prim, int nome) //inserir elementos na fila de caminhos anteriores
f->nome = nome;
f->prox = NULL;
if(aux == NULL){ //se for o primeiro elemento a ser inserido na fila F_prim
F_prim = f;
while(aux->prox != NULL)
aux = aux->prox;
aux->prox = f;
return F_prim;
Lista* insere_lista(Lista* L_prim, int nome, int dist, double dist_l, Fila* ant, int nome_ant) //inserir
na lista ordenadamente
Lista *p = L_prim;
Lista *prim;
Fila *aux;
double est = dist + dist_l;
aux = aux->prox;
novo->nome = nome;
novo->dist = dist;
novo->dist_reta = dist_l;
novo->est_custo = est;
novo->ant = F_prim;
novo->prox = NULL;
if(p->prox != NULL){
prim = p;
p = p->prox;
prim = p;
p = p->prox;
}
if (est < p->est_custo){ //distancia menor que o primeiro elemento da lista
novo->prox = p;
novo->prox = p->prox;
p->prox = novo;
else{
novo->prox = p->prox;
p->prox = novo;
return L_prim;
novo->nome = inicio;
novo->dist = 0;
novo->dist_reta = dist_linha_reta[inicio];
novo->est_custo = dist_linha_reta[inicio];
novo->ant = NULL;
novo->prox = NULL;
return novo;
}
Lista* retira(Lista* L_prim) //retirar o primeiro elemento da lista
Lista *aux;
aux = L_prim;
L_prim = aux->prox;
free(aux);
return L_prim;
Lista* busca_guloso(Lista* L_prim, int inicio, int fim) //buscar o caminho de menor custo
int i, j, k, dist;
double dist_l;
L_prim = cria_primeiro_no(inicio);
dist_l = dist_linha_reta[i];
}
L_prim = retira(L_prim);//retira o primeiro elemento da lista(pai), pois ele foi
aberto gerando os filhos
return L_prim;
main()
double tempoGasto;
Lista *L_prim=NULL;
printf("\n\nA=0, B=1, C=2, D=3, E=4, F=5, G=6, H=7, I=8, K=9, L=10, M=11, N=12,
P=13, Q=14, R=15, S=16, T=17, U=18, V=19, W=20\n\n");
tempoInicial = clock();
L_prim = busca_guloso(L_prim, inicio, 5); //procurar caminho entre a origem e 'F' com a
menor distncia em linha reta
tempoFinal = clock();
tempoGasto = (tempoFinal-tempoInicial)/(CLOCKS_PER_SEC/1000);