Advertisement
darkstar97

grafo matriz

Aug 21st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAXNUMVERTICES 100
  5.  
  6. typedef int tpeso;
  7. typedef int tvertice;
  8. typedef int tapontador;
  9.  
  10. typedef struct {
  11.     tpeso mat[MAXNUMVERTICES][MAXNUMVERTICES];
  12.     int num_vertices;
  13. } tgrafo;
  14.  
  15. void inicializa_grafo(tgrafo *grafo, int num_vertices){
  16.     int i,j;
  17.  
  18.     grafo->num_vertices = num_vertices;
  19.     for (i = 0; i < grafo->num_vertices; i++){
  20.         for(j = 0; j < grafo->num_vertices; j++)
  21.             grafo->mat[i][j] = 0;
  22.     }
  23. }
  24.  
  25. void insere_aresta(tvertice v, tvertice u, tpeso peso, tgrafo *grafo){
  26.     grafo->mat[v][u] = peso;
  27. }
  28.  
  29. int existe_aresta(tvertice v, tvertice u, tgrafo *grafo){
  30.     return grafo->mat[v][u] !=0;
  31. }
  32.  
  33. void retira_aresta(tvertice v, tvertice u, tgrafo *grafo){
  34.     if(grafo->mat[v][u] == 0)
  35.         printf("Erro:Aresta inexistente.");
  36.     else
  37.         grafo->mat[v][u] = 0;    
  38. }
  39.  
  40. void libera_grafo(tgrafo * grafo){
  41.     free(grafo);
  42. }
  43.  
  44. int existe_adj(tvertice v, tgrafo *grafo){
  45.     tvertice aux;
  46.  
  47.     for(aux = 0; aux < grafo->num_vertices; aux++){
  48.         if(grafo->mat[v][aux] !=0)
  49.             return 1;
  50.     }
  51.     return 0;
  52. }
  53.  
  54. tapontador primeiro_adj(tvertice v, tgrafo *grafo){
  55.     tapontador aux;
  56.  
  57.     for(aux = 0; aux < grafo->num_vertices; aux++)
  58.         if(grafo->mat[v][aux] != 0)
  59.             return aux;
  60.     return NULL;
  61. }
  62.  
  63. tapontador proximo_adj(tvertice v, tapontador aux, tgrafo *grafo){
  64.     for(aux++; aux<grafo->num_vertices; aux++)
  65.         if(grafo->mat[v][aux] != 0)
  66.             return aux;
  67.     return NULL;        
  68. }
  69.  
  70. void recupera_adj(tvertice v, tapontador p, tvertice *u, tpeso *peso, tgrafo * grafo){
  71.     *u = p;
  72.     *peso = grafo->mat[v][p];
  73. }
  74.  
  75. void print(tgrafo * grafo){
  76.     int i=0,j=0;
  77.     for(i = 0; i<grafo->num_vertices; i++){
  78.         for(j = 0; j<grafo->num_vertices; j++){
  79.             printf(" %d ", grafo->mat[i][j]);
  80.         }
  81.     printf("\n");
  82.     }
  83. }
  84.  
  85. int main(){
  86.     tgrafo *grafo = malloc(sizeof(tgrafo));
  87.     inicializa_grafo(grafo, 5);
  88.  
  89.     insere_aresta(2,2,1,grafo);
  90.     insere_aresta(1,3,1,grafo);
  91.     insere_aresta(2,3,1,grafo);
  92.     int var2 = primeiro_adj(2, grafo);
  93.     int var =  proximo_adj(2, var2, grafo);
  94.     printf("%d,%d\n", var2, var);
  95.     print(grafo);
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement