简体   繁体   中英

Graph with adjacency matrix in C

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 ?

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()**

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM