Advertisement
darkstar97

grafo lista adjacente

Aug 22nd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <queue>
  4. #define BRANCO 0
  5. #define CINZA 1
  6. #define PRETO 2
  7.  
  8. typedef struct Node{
  9.     int vertice;
  10.     int peso;
  11.     struct Node* next;
  12. } Node;
  13.  
  14. typedef struct Lista{
  15.     Node *head;
  16. } Lista;
  17.  
  18. typedef struct Grafo{
  19.     int numVertices;
  20.     Lista* array;
  21. } Grafo;
  22.  
  23. void inicializaGrafo(Grafo *grafo, int n){
  24.     if(grafo==NULL)
  25.         exit(0);
  26.  
  27.     grafo->numVertices = n;
  28.  
  29.     grafo->array = (Lista*) malloc(n * sizeof(Lista));
  30.  
  31.     int i;
  32.     for (i = 0; i < n; i++)
  33.         grafo->array[i].head = NULL;
  34.  
  35.     return;
  36. }
  37.  
  38. void insereAresta(Grafo *grafo, int v, int u, int peso){
  39.     Node* newNode = (Node*) malloc(sizeof(Node));
  40.     newNode->vertice = u;
  41.     newNode->peso = peso;
  42.     newNode->next = NULL;
  43.  
  44.     newNode->next = grafo->array[v].head;
  45.     grafo->array[v].head = newNode;
  46.  
  47.     /**< grafo não direcionado */ /*
  48.     Node* newNode2 = (Node*) malloc(sizeof(Node));
  49.     newNode2->vertice = v;
  50.     newNode2->peso = peso;
  51.     newNode2->next = NULL;
  52.  
  53.     newNode2->next = grafo->array[u].head;
  54.     grafo->array[u].head = newNode2;*/
  55.  
  56.     return;
  57.  }
  58.  
  59. int existeAresta(int v, int u, Grafo * grafo){
  60.     Node* aux = grafo->array[v].head;
  61.     while(aux){
  62.         if(aux->vertice==u){
  63.             return 1;
  64.         }
  65.         aux = aux->next;
  66.     }
  67.  
  68.     return 0;
  69. }
  70.  
  71. void retiraAresta(int v, int u, Grafo * grafo){
  72.     Node*aux = grafo->array[v].head;
  73.     while(aux){
  74.         if(aux->next->vertice==u){
  75.             aux->next = aux->next->next;
  76.             break;
  77.         }
  78.         aux=aux->next;
  79.     }
  80. }
  81.  
  82. void liberaGrafo(Grafo * grafo){
  83.     int n = grafo->numVertices;
  84.     int i;
  85.     for(i=0;i<n;i++){
  86.         free(grafo->array[i].head);
  87.     }
  88.  
  89.     free(grafo->array);
  90.     free(grafo);
  91. }
  92.  
  93. int existeAdjacente(int v, Grafo *grafo){
  94.     if(grafo->array[v].head==NULL)
  95.         return 0;
  96.     else
  97.         return 1;
  98. }
  99.  
  100. Node* primeiroAdjacente(int v, Grafo *grafo){
  101.     return grafo->array[v].head;
  102. }
  103.  
  104. Node* proximoAdjacente(int v, Node* p, Grafo *grafo){
  105.     return p->next;
  106. }
  107.  
  108. void recuperaAdjacente(int v, Node* p, int *u, int *peso, Grafo *grafo){
  109.     *u = p->vertice;
  110.     *peso = p->peso;
  111. }
  112.  
  113. void imprimeGrafo(Grafo * grafo){
  114.     int i;
  115.     for(i=0;i<grafo->numVertices;i++){
  116.         Node* aux = grafo->array[i].head;
  117.         printf("\nLista do vertice %d\n", i);
  118.         while(aux){
  119.             printf("->%d,%d",aux->vertice, aux->peso);
  120.             aux = aux->next;
  121.         }
  122.         printf("\n");
  123.     }
  124. }
  125.  
  126. void transpoeGrafo(Grafo * grafo, Grafo * grafoT){
  127.     Node *aux;
  128.     Node *newNode = (Node*) malloc (sizeof(Node));
  129.  
  130.     for(int i=0;i<grafoT->numVertices;i++){
  131.         aux = grafo->array[i].head;
  132.         if(grafo->array[i].head == NULL) continue;
  133.         while(aux->next != NULL){
  134.             newNode->peso = aux->peso;
  135.             newNode->vertice = i;
  136.             insereAresta(grafoT,aux->vertice,newNode->vertice, newNode->peso);
  137.             aux = aux->next;
  138.         }
  139.         newNode->peso = grafo->array[i].head->peso;
  140.         newNode->vertice = i;
  141.         insereAresta(grafoT,aux->vertice,newNode->vertice, newNode->peso);
  142.     }
  143.  
  144.     return;
  145. }
  146.  
  147. void visitaProfundidade(int v, int cor[], Grafo *grafo){
  148.     int w, peso;
  149.     Node *p;
  150.  
  151.     cor[v] = CINZA;
  152.     p = primeiroAdjacente(v, grafo);
  153.     while(p!=NULL){
  154.         recuperaAdjacente(v, p, &w, &peso, grafo);
  155.         if(cor[w]==BRANCO)
  156.             visitaProfundidade(w, cor, grafo);
  157.         p=proximoAdjacente(v,p,grafo);
  158.     }
  159.     cor[v] = PRETO;
  160. }
  161.  
  162. void buscaProfundidade(Grafo *grafo){
  163.     int v, cor[grafo->numVertices];
  164.  
  165.     for(v=0;v<grafo->numVertices;v++)
  166.         cor[v] = BRANCO;
  167.     for(v=0;v<grafo->numVertices;v++)
  168.         if(cor[v]==BRANCO)
  169.             visitaProfundidade(v, cor, grafo);
  170. }
  171.  
  172. void visitaLargura(int v, int cor[], Grafo *grafo){
  173.     int w, peso;
  174.     Node *p;
  175.     std::queue<int>q;
  176.  
  177.     cor[v] = CINZA;
  178.     q.push(v);
  179.     while(!q.empty()){
  180.         v = q.front(); q.pop();
  181.         p = primeiroAdjacente(v, grafo);
  182.         while(p!=NULL){
  183.             recuperaAdjacente(v, p, &w, &peso, grafo);
  184.             if(cor[w]==BRANCO){
  185.                 cor[w]=CINZA;
  186.                 q.push(w);
  187.                 printf("%d", w);
  188.             }
  189.             p=proximoAdjacente(v,p,grafo);
  190.         }
  191.         cor[v] = PRETO;
  192.     }
  193. }
  194.  
  195. void buscaLargura(Grafo *grafo){
  196.     int v, cor[grafo->numVertices];
  197.  
  198.     for(v=0;v<grafo->numVertices;v++)
  199.         cor[v] = BRANCO;
  200.     for(v=0;v<grafo->numVertices;v++)
  201.         if(cor[v]==BRANCO)
  202.             visitaLargura(v, cor, grafo);
  203. }
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. int main(){
  211.     Grafo *grafo = (struct Grafo*) malloc(sizeof(Grafo));
  212.     Grafo *grafoT = (struct Grafo*) malloc(sizeof(Grafo));
  213.     int u,peso;
  214.     inicializaGrafo(grafo,5);
  215.     inicializaGrafo(grafoT,5);
  216.  
  217.     insereAresta(grafo,0,1,1);
  218.     insereAresta(grafo,0,4,1);
  219.     insereAresta(grafo,1,2,1);
  220.     insereAresta(grafo,1,3,1);
  221.     insereAresta(grafo,1,4,1);
  222.     insereAresta(grafo,2,3,1);
  223.     insereAresta(grafo,3,4,1);
  224.  
  225.     buscaProfundidade(grafo);
  226.     buscaLargura(grafo);
  227.  
  228.     /*
  229.     printf("\n%d\n", primeiroAdjacente(1,grafo)->vertice);
  230.     printf("\n%d\n", proximoAdjacente(1,primeiroAdjacente(1,grafo),grafo)->vertice);
  231.     recuperaAdjacente(1,primeiroAdjacente(1,grafo),&u,&peso,grafo);
  232.     printf("\n\n%d %d\n\n", u, peso);
  233.  
  234.  
  235.  
  236.     if(existeAresta(0,1,grafo)){
  237.         printf("sim\n");
  238.     } else {
  239.         printf("nao\n");
  240.     }
  241.  
  242.     if(existeAresta(4,1,grafo)){
  243.         printf("sim\n");
  244.     } else {
  245.         printf("nao\n");
  246.     }
  247.  
  248.     if(existeAdjacente(1,grafo)){
  249.         printf("sim\n");
  250.     } else {
  251.         printf("nao\n");
  252.     }
  253.  
  254.  
  255.     imprimeGrafo(grafo);
  256.     transpoeGrafo(grafo,grafoT);
  257.     printf("\n\nGrafo Transposto:\n");
  258.     imprimeGrafo(grafoT);
  259.     */
  260.  
  261.     liberaGrafo(grafo);
  262.     return 0;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement