【问题标题】:Graph with adjacency matrix in CC中具有邻接矩阵的图
【发布时间】:2017-10-01 12:18:11
【问题描述】:

我有这个struct

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

我必须在这个函数中创建一个空图:

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

如何使用双指针int** adj 创建矩阵?

【问题讨论】:

  • 请向我们展示您到目前为止所做的尝试。
  • 图形结构的第一个元素是 int **adj 只是一个指向指向矩阵第一个元素的指针整数的指针。首先使用malloc()为其分配内存。
  • 您的函数获取图中的节点数,以邻接矩阵的形式实现该图,然后将其返回为struct graph
  • 如果在create函数中我定义了struct graphgraph,malloc就是graph->adj=(int*)malloc(sizeof(int*)*n)?跨度>
  • 这两个答案都回答了您的问题吗?你应该选择一个作为正确的解决方案,或者提出一个问题。

标签: c graph-algorithm adjacency-matrix


【解决方案1】:

这是用malloc()定义矩阵的方法 我从GeeksForGeeks 那里得到了一些版本。

具有int **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]);
    }
}

我们可以将这些代码放入graph_create(int nodes)函数中。

代码

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

我们知道图的邻接矩阵有 n*n 维。 为此,我们使用节点作为行和列。

编辑

为了安全地使用malloc() 函数,我们必须通过使用这些块调用free() 来释放malloc() 保留的内存位置。 (类似于Mobius的回答)

就在main() 中的return 0; 语句之前调用这个:

free ((*d).adj);

所需的头文件:

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

【讨论】:

    【解决方案2】:

    为了分配一个矩阵,你需要为实际的矩阵数据(大小为width * height * sizeof(int))分配空间。

    在您使用int** 矩阵的设置中,您还需要分配一个指针数组,该数组将指向每一行的开头,如果您想避免这种情况,您可以简单地通过执行类似的操作来计算偏移量size_t offset = row * width + column

    要分配指针,我们需要height * sizeof(int*) 字节。

    最后,您需要在数组中分配行指针。

    所有的代码应该看起来像这样:

    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]`
    

    为了释放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);
    }
    

    【讨论】:

    • @pipox 这能回答你的问题吗?
    猜你喜欢
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 2023-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多