#include<stdlib.h>
#include<stdio.h>
struct digraph //Estrutura do grafo, V e A são contadores, matrix_adj é um ponteiro para um array de ponteiros
{
int V , A;
int **matrix_adj;
};
typedef struct digraph* Digraph;
int **matrix_alloc(int r,int c,int val);
Digraph graph_alloc(int vertex);
void insert(Digraph G, int v, int w);
void show(Digraph G);
int main(void)
{
Digraph G;
G = graph_alloc(6);
insert(G, 1,2);
insert(G, 1,3);
insert(G, 2,4);
insert(G, 3,4);
insert(G, 4,5);
insert(G, 5,6);
show(G);
return 0;
}
int **matrix_alloc(int r,int c,int val)
{
int i, j;
int **matrix
= (int**)malloc(r
*sizeof(int*));// aloca linhas for(i=0;i<r;i++)
{
matrix
[i
] = (int*)malloc(c
*sizeof(int));//aloca colunas for(j=0;j<c;j++)
{
matrix[i][j] = val;
}
}
return matrix;
}
Digraph graph_alloc(int vertex)
{
Digraph G
= malloc(sizeof(struct digraph
)); G->V = vertex;
G->A = 0;
G->matrix_adj = matrix_alloc(vertex, vertex, 0);
return G;
}
void insert(Digraph G,int v, int w)
{
if(G->matrix_adj[v][w] == 0)
{
G->matrix_adj[v][w] = 1;
G->matrix_adj[w][v] = 1;
G->A++;
}
}
void show(Digraph G)
{
int v, w;
for(v=0; v<G->V; v++)
{
for(w=0; w<G->V;w++)
{
printf("%d ", G
->matrix_adj
[v
][w
]); }
}
}
I2luY2x1ZGU8c3RkbGliLmg+CiNpbmNsdWRlPHN0ZGlvLmg+CgpzdHJ1Y3QgZGlncmFwaCAvL0VzdHJ1dHVyYSBkbyBncmFmbywgViBlIEEgc8OjbyBjb250YWRvcmVzLCBtYXRyaXhfYWRqIMOpIHVtIHBvbnRlaXJvIHBhcmEgdW0gYXJyYXkgZGUgcG9udGVpcm9zCnsKICAgIGludCBWICwgQTsKICAgIGludCAqKm1hdHJpeF9hZGo7Cn07Cgp0eXBlZGVmIHN0cnVjdCBkaWdyYXBoKiBEaWdyYXBoOwppbnQgKiptYXRyaXhfYWxsb2MoaW50IHIsaW50IGMsaW50I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bnQgdykKewogICAgaWYoRy0+bWF0cml4X2Fkalt2XVt3XSA9PSAwKQogICAgewogICAgICAgIEctPm1hdHJpeF9hZGpbdl1bd10gPSAxOwogICAgICAgIEctPm1hdHJpeF9hZGpbd11bdl0gPSAxOwogICAgICAgIEctPkErKzsKICAgIH0KfQoKdm9pZCBzaG93KERpZ3JhcGggRykKewogICAgaW50IHYsIHc7CiAgICBmb3Iodj0wOyB2PEctPlY7IHYrKykKICAgIHsKICAgICAgICBmb3Iodz0wOyB3PEctPlY7dysrKQogICAgICAgIHsKICAgICAgICAgICAgcHJpbnRmKCIlZCAiLCBHLT5tYXRyaXhfYWRqW3ZdW3ddKTsKICAgICAgICB9CiAgICAgICAgcHJpbnRmKCJcbiIpOwogICAgfQogICAgcHJpbnRmKCJcbiIpOwogICAgcHJpbnRmKCJWZXJ0aWNlczogIiwgRy0+Vik7CiAgICBwcmludGYoIkFyZXN0YXM6ICAiLCBHLT5BKTsKfQo=