Graph with adjacency matrix in C

谁说胖子不能爱 提交于 2019-12-13 08:35:06

问题


I have this struct:

struct graph {
    int** adj; /**< Adjacency matrix. */
    int n; /**< Number of nodes in graph. */
};

and I have to create a void graph into this function:

struct graph *graph_create(int nodes) {
  //To implement
}

How can I create a matrix using that double pointer int** adj?


回答1:


Here is the way to define a matrix with malloc() that i catch from GeeksForGeeks with some edition.

2D integer Matrix with int ** and malloc()

int r = 3, c = 4, i, j, count;

//arr[r][c]
int **arr = (int **)malloc(r * sizeof(int *));

for (i=0; i<r; i++){
    *(arr +i) = (int *)malloc(c * sizeof(int));
    //arr[i] = (int *)malloc(c * sizeof(int));
}

// Note that arr[i][j] is same as *(*(arr+i)+j)
count = 0;
for (i = 0; i <  r; i++)//3
    for (j = 0; j < c; j++)//4
        arr[i][j] = ++count;  // OR *(*(arr+i)+j) = ++count

for (i = 0; i <  r; i++){
    printf("\n");
    for (j = 0; j < c; j++){
        printf("%d ", arr[i][j]);
    }
}

And we can put these code inside graph_create(int nodes) function.

Code

struct graph {
    int** adj; /**< Adjacency matrix. */
    int n; /**< Number of nodes in graph. */
}G;


struct graph *graph_create(int nodes) {

    struct graph * tmp = &G;
    int r = nodes, c = nodes, i, j, count;

    //arr[r][c]
    G.adj = (int **)malloc(r * sizeof(int *));

    for (i=0; i<r; i++){
         *(G.adj + i) = (int *)malloc(c * sizeof(int));
         //arr[i] = (int *)malloc(c * sizeof(int));
    }


    count = 0;
    for (i = 0; i <  r; i++)//3
      for (j = 0; j < c; j++)//4
         G.adj[i][j] = ++count;  // OR *(*(arr+i)+j) = ++count

    for (i = 0; i <  r; i++){
        printf("\n");
        for (j = 0; j < c; j++){
            printf("%d ", G.adj[i][j]);
        }
    }

    return tmp;

}



int main()
{


    struct graph * d = graph_create(5);

    printf("\n");
    return 0;
}

We know the adjacency Matrix of a graph have n*n dimension. for that we use nodes as row and column.

Edit

For secure working with malloc() function we must free the memory locations that is reserved by malloc(), by calling free() with these blocks. (similar to Mobius answer)

just before return 0; statement in main() call this:

free ((*d).adj);

Required header files:

#include <stdio.h>
#include <stdlib.h> // for** malloc()**, **free()**



回答2:


In order to allocate a matrix, you need to allocate room for the actual matrix data (of size width * height * sizeof(int)).

In your setup with an int** matrix, you also need to allocate an array of pointers, which will point to the beginning of each row, if you wanted to avoid this, you could simply compute the offset by doing something like size_t offset = row * width + column.

To allocate the pointers, we need height * sizeof(int*) bytes.

finally you will need to assign the row pointers in your array.

All together the code should look about like this:

int ** allocate_matrix(size_t width, size_t height){
    int *  values = malloc(height * width * sizeof(int));
    int ** rows   = malloc(height * sizeof(int*));

    size_t i;
    for (i = 0; i < height; i++) {
        size_t offset = i * width;
        rows[i] = &values[offset];
    }

    return rows;
}

// the returned matrix can me indexed like `matrix[row][column]`

In order to free our memory pointed to by adj:

void free_matrix(int ** rows) {
    // this points to the beginning of our region of memory for values;
    int * values = rows[0]; 

    free(values);
    free(rows);
}


来源:https://stackoverflow.com/questions/46512314/graph-with-adjacency-matrix-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!