Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAXNUMVERTICES 100
- typedef int tpeso;
- typedef int tvertice;
- typedef int tapontador;
- typedef struct {
- tpeso mat[MAXNUMVERTICES][MAXNUMVERTICES];
- int num_vertices;
- } tgrafo;
- void inicializa_grafo(tgrafo *grafo, int num_vertices){
- int i,j;
- grafo->num_vertices = num_vertices;
- for (i = 0; i < grafo->num_vertices; i++){
- for(j = 0; j < grafo->num_vertices; j++)
- grafo->mat[i][j] = 0;
- }
- }
- void insere_aresta(tvertice v, tvertice u, tpeso peso, tgrafo *grafo){
- grafo->mat[v][u] = peso;
- }
- int existe_aresta(tvertice v, tvertice u, tgrafo *grafo){
- return grafo->mat[v][u] !=0;
- }
- void retira_aresta(tvertice v, tvertice u, tgrafo *grafo){
- if(grafo->mat[v][u] == 0)
- printf("Erro:Aresta inexistente.");
- else
- grafo->mat[v][u] = 0;
- }
- void libera_grafo(tgrafo * grafo){
- free(grafo);
- }
- int existe_adj(tvertice v, tgrafo *grafo){
- tvertice aux;
- for(aux = 0; aux < grafo->num_vertices; aux++){
- if(grafo->mat[v][aux] !=0)
- return 1;
- }
- return 0;
- }
- tapontador primeiro_adj(tvertice v, tgrafo *grafo){
- tapontador aux;
- for(aux = 0; aux < grafo->num_vertices; aux++)
- if(grafo->mat[v][aux] != 0)
- return aux;
- return NULL;
- }
- tapontador proximo_adj(tvertice v, tapontador aux, tgrafo *grafo){
- for(aux++; aux<grafo->num_vertices; aux++)
- if(grafo->mat[v][aux] != 0)
- return aux;
- return NULL;
- }
- void recupera_adj(tvertice v, tapontador p, tvertice *u, tpeso *peso, tgrafo * grafo){
- *u = p;
- *peso = grafo->mat[v][p];
- }
- void print(tgrafo * grafo){
- int i=0,j=0;
- for(i = 0; i<grafo->num_vertices; i++){
- for(j = 0; j<grafo->num_vertices; j++){
- printf(" %d ", grafo->mat[i][j]);
- }
- printf("\n");
- }
- }
- int main(){
- tgrafo *grafo = malloc(sizeof(tgrafo));
- inicializa_grafo(grafo, 5);
- insere_aresta(2,2,1,grafo);
- insere_aresta(1,3,1,grafo);
- insere_aresta(2,3,1,grafo);
- int var2 = primeiro_adj(2, grafo);
- int var = proximo_adj(2, var2, grafo);
- printf("%d,%d\n", var2, var);
- print(grafo);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement