Read a file from a graph and represent it in adjacency matrix format

0

I need to read a file that contains information from a directed graph and build an adjacency matrix of it. The array must contain 1 in the position that there is a loop and 0 in the one that does not exist. The file is in the DIMACS standards and has the following formatting: '

    n e 250 15562
    p 1 2
    p 1 4
    p 1 5
    p 1 6
    p 1 8
   ...
   ...
   ...
   ...
   ...
   p 249 250'

The first line contains the number of nodes in the graph and the number of connections respectively; Already the other lines started with the letter 'p' their arcs of connections. I am having problems in the part of filling the array, after reading the first line and performing the array allocation I do not know how to fill it. Here is the code I started doing in C:

void lerArquivo() {
    FILE* file;
    int quantArestas, arcos;
    int **mat = NULL;

    file = fopen("graph.txt", "rt");
    if (file == NULL) {
        printf("Ocorreu um erro durante a leitura do arquivo. \n");
        exit(EXIT_FAILURE);
    }

    fscanf(file, "n e %d %d \n", &quantArestas, &arcos);

    mat = malloc(quantArestas * sizeof (int*));
    for (int i = 0; i < quantArestas; i++) {
        mat[i] = malloc(quantArestas * sizeof (int));
    }
}
    
asked by anonymous 17.11.2018 / 20:39

0 answers