Ir para conteúdo
Fórum Script Brasil

gabi_da_net

Membros
  • Total de itens

    1
  • Registro em

  • Última visita

Sobre gabi_da_net

gabi_da_net's Achievements

0

Reputação

  1. O codigo fonte: #include <stdio.h> #include <stdlib.h> #include "stddef.h" #include "grafo.h" //inclui os Protótipos //Definição do tipo Grafo struct grafo{ int eh_ponderado; int nro_vertices; int grau_max; int** arestas; float** pesos; int* grau; }; Grafo* cria_Grafo(int nro_vertices, int grau_max, int eh_ponderado){ Grafo *gr; gr = (Grafo*) malloc(sizeof(struct grafo)); if(gr != NULL){ int i; gr->nro_vertices = nro_vertices; gr->grau_max = grau_max; gr->eh_ponderado = (eh_ponderado != 0)?1:0; gr->grau = (int*) calloc(nro_vertices,sizeof(int)); gr->arestas = (int**) malloc(nro_vertices * sizeof(int*)); for(i=0; i<nro_vertices; i++) gr->arestas = (int*) malloc(grau_max * sizeof(int)); if(gr->eh_ponderado){ gr->pesos = (float**) malloc(nro_vertices * sizeof(float*)); for(i=0; i<nro_vertices; i++) gr->pesos = (float*) malloc(grau_max * sizeof(float)); } } return gr; } void libera_Grafo(Grafo* gr){ if(gr != NULL){ int i; for(i=0; i<gr->nro_vertices; i++) free(gr->arestas); free(gr->arestas); if(gr->eh_ponderado){ for(i=0; i<gr->nro_vertices; i++) free(gr->pesos); free(gr->pesos); } free(gr->grau); free(gr); } } int insereAresta(Grafo* gr, int orig, int dest, int eh_digrafo, float peso){ if(gr == NULL) return 0; if(orig < 0 || orig >= gr->nro_vertices) return 0; if(dest < 0 || dest >= gr->nro_vertices) return 0; gr->arestas[orig][gr->grau[orig]] = dest; if(gr->eh_ponderado) gr->pesos[orig][gr->grau[orig]] = peso; gr->grau[orig]++; if(eh_digrafo == 0) insereAresta(gr,dest,orig,1,peso); return 1; } int removeAresta(Grafo* gr, int orig, int dest, int eh_digrafo){ if(gr == NULL) return 0; if(orig < 0 || orig >= gr->nro_vertices) return 0; if(dest < 0 || dest >= gr->nro_vertices) return 0; int i = 0; while(i<gr->grau[orig] && gr->arestas[orig] != dest) i++; if(i == gr->grau[orig])//elemento não encontrado return 0; gr->grau[orig]--; gr->arestas[orig] = gr->arestas[orig][gr->grau[orig]]; if(gr->eh_ponderado) gr->pesos[orig] = gr->pesos[orig][gr->grau[orig]]; if(eh_digrafo == 0) removeAresta(gr,dest,orig,1); return 1; } void imprime_Grafo(Grafo *gr){ if(gr == NULL) return; int i, j; for(i=0; i < gr->nro_vertices; i++){ printf("%d: ", i); for(j=0; j < gr->grau; j++){ if(gr->eh_ponderado) printf("%d(%.2f), ", gr->arestas[j], gr->pesos[j]); else printf("%d, ", gr->arestas[j]); } printf("\n"); } }
×
×
  • Criar Novo...