Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Grafo{
- int **A;
- int n;
- int m
- }Grafo;
- Grafo* graphInit(int n)
- {
- int i, j;
- Grafo *grafo = (Grafo*)malloc(sizeof(Grafo));
- grafo->n = n;
- grafo->m = 0;
- grafo->A = malloc(n * sizeof(int *));
- for (i = 0; i < n; i++)
- {
- grafo->A[i] = malloc(n * sizeof(int));
- }
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < n; j++)
- {
- grafo->A[i][j] = 0;
- }
- }
- return grafo;
- }
- void graphInsertArc(Grafo *G, int v, int w)
- {
- if (G->A[v][w] == 0)
- {
- G->A[v][w] = 1;
- G->m++;
- }
- }
- void graphShow(Grafo* G)
- {
- int i, j;
- for (i = 0; i < G->n; i++)
- {
- printf("%2d:", i);
- for (j = 0; j < G->n; j++){
- if (G->A[i][j] == 1){
- printf(" %2d", j);
- }
- }
- printf("\n");
- }
- }
- int main() {
- Grafo* gr = graphInit(5);
- graphInsertArc(gr,0,1);
- graphInsertArc(gr,1,2);
- graphInsertArc(gr,2,4);
- graphInsertArc(gr,3,1);
- graphInsertArc(gr,4,3);
- graphShow(gr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement