mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-18 01:36:07 +00:00
add c code for graph operation (#601)
* Create chapter_graph * Delete chapter_graph * add C code for graph * add C code for graph * Create graph_adjacency_list.c add C code for graph * Update CMakeLists.txt * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update graph_adjacency_list.c * Update CMakeLists.txt for c code of graph * Update format of c code * Update format of c code * Update format of c code * Update verticesList Change the data structure of the storage list from a linked list to a linear table * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * Create graph_adjacency_list_test.c * Create graph_bfs * Update CMakeLists.txt * Update graph_adjacency_list.c * mv graph_bfs to graph_bfs.c * Update graph_bfs.c * Delete graph_bfs * Update graph_adjacency_list.c * update c code for graph operation. * Update CMakeLists.txt * Update graph_dfs.c * Update graph_dfs.c * Update CMakeLists.txt * Update graph_dfs.c * Update note of graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update output "初始化后,图为:" of graph_dfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update name of arrayVertex * Update name of arrayVertex * Update note of graph_dfs.c * Update note of graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_adjacency_matrix.c * Update graph_adjacency_list_test.c * Update graph_adjacency_list.c * Update graph_dfs.c * Update graph_bfs.c * Update comment * Update comment * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * update comment * update comment * update comment for graph operation * update comment of graph operation * update comment * update comment --------- Co-authored-by: Yudong Jin <krahets@163.com> Co-authored-by: libr <libr@info2soft.com>
This commit is contained in:
@@ -8,32 +8,38 @@
|
||||
|
||||
/* 基于邻接矩阵实现的无向图类结构 */
|
||||
struct graphAdjMat {
|
||||
int *vertices;
|
||||
unsigned int **adjMat;
|
||||
unsigned int size;
|
||||
unsigned int capacity;
|
||||
int *vertices; // 顶点列表
|
||||
unsigned int **adjMat; // 邻接矩阵,元素代表“边”,索引代表“顶点索引”
|
||||
unsigned int size; // 顶点数量
|
||||
unsigned int capacity; // 图容量
|
||||
};
|
||||
|
||||
typedef struct graphAdjMat graphAdjMat;
|
||||
|
||||
/* 添加边 */
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
void addEdge(graphAdjMat *t, int i, int j) {
|
||||
// 越界检查
|
||||
if (i < 0 || j < 0 || i >= t->size || j >= t->size || i == j) {
|
||||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
exit(1);
|
||||
}
|
||||
// 添加边
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
t->adjMat[i][j] = 1;
|
||||
t->adjMat[j][i] = 1;
|
||||
}
|
||||
|
||||
/* 删除边 */
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
void removeEdge(graphAdjMat *t, int i, int j) {
|
||||
// 越界检查
|
||||
if (i < 0 || j < 0 || i >= t->size || j >= t->size || i == j) {
|
||||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
exit(1);
|
||||
}
|
||||
// 删除边
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
t->adjMat[i][j] = 0;
|
||||
t->adjMat[j][i] = 0;
|
||||
}
|
||||
@@ -42,13 +48,13 @@ void removeEdge(graphAdjMat *t, int i, int j) {
|
||||
void addVertex(graphAdjMat *t, int val) {
|
||||
// 如果实际使用不大于预设空间,则直接初始化新空间
|
||||
if (t->size < t->capacity) {
|
||||
t->vertices[t->size] = val;
|
||||
t->vertices[t->size] = val; // 初始化新顶点值
|
||||
|
||||
// 邻接矩新列阵置0
|
||||
|
||||
for (int i = 0; i < t->size; i++) {
|
||||
t->adjMat[i][t->size] = 0;
|
||||
t->adjMat[i][t->size] = 0; // 邻接矩新列阵置0
|
||||
}
|
||||
memset(t->adjMat[t->size], 0, sizeof(unsigned int) * (t->size + 1));
|
||||
memset(t->adjMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); // 将新增行置 0
|
||||
t->size++;
|
||||
return;
|
||||
}
|
||||
@@ -70,23 +76,21 @@ void addVertex(graphAdjMat *t, int val) {
|
||||
tempMat[k] = tempMatLine + k * (t->size * 2);
|
||||
}
|
||||
|
||||
// 原数据复制到新数组
|
||||
for (int i = 0; i < t->size; i++) {
|
||||
memcpy(tempMat[i], t->adjMat[i], sizeof(unsigned int) * t->size);
|
||||
memcpy(tempMat[i], t->adjMat[i], sizeof(unsigned int) * t->size); // 原数据复制到新数组
|
||||
}
|
||||
|
||||
// 新列置0
|
||||
for (int i = 0; i < t->size; i++) {
|
||||
tempMat[i][t->size] = 0;
|
||||
tempMat[i][t->size] = 0; // 将新增列置 0
|
||||
}
|
||||
memset(tempMat[t->size], 0, sizeof(unsigned int) * (t->size + 1));
|
||||
memset(tempMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); // 将新增行置 0
|
||||
|
||||
// 释放原数组
|
||||
free(t->adjMat[0]);
|
||||
free(t->adjMat);
|
||||
|
||||
// 扩容后,指向新地址
|
||||
t->adjMat = tempMat;
|
||||
t->adjMat = tempMat; // 指向新的邻接矩阵地址
|
||||
t->capacity = t->size * 2;
|
||||
t->size++;
|
||||
}
|
||||
@@ -98,28 +102,21 @@ void removeVertex(graphAdjMat *t, unsigned int index) {
|
||||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// 清除删除的顶点,并将其后所有顶点前移
|
||||
for (int i = index; i < t->size - 1; i++) {
|
||||
t->vertices[i] = t->vertices[i + 1];
|
||||
t->vertices[i] = t->vertices[i + 1]; // 清除删除的顶点,并将其后所有顶点前移
|
||||
}
|
||||
|
||||
// 将被前移的最后一个顶点置0
|
||||
t->vertices[t->size - 1] = 0;
|
||||
t->vertices[t->size - 1] = 0; // 将被前移的最后一个顶点置 0
|
||||
|
||||
// 清除邻接矩阵中删除的列
|
||||
for (int i = 0; i < t->size - 1; i++) {
|
||||
if (i < index) {
|
||||
// 被删除列后的所有列前移
|
||||
for (int j = index; j < t->size - 1; j++) {
|
||||
t->adjMat[i][j] = t->adjMat[i][j + 1];
|
||||
t->adjMat[i][j] = t->adjMat[i][j + 1]; // 被删除列后的所有列前移
|
||||
}
|
||||
} else {
|
||||
// 被删除行的下方所有行上移
|
||||
memcpy(t->adjMat[i], t->adjMat[i + 1], sizeof(unsigned int) * t->size);
|
||||
// 被删除列后的所有列前移
|
||||
} else {
|
||||
memcpy(t->adjMat[i], t->adjMat[i + 1], sizeof(unsigned int) * t->size); // 被删除行的下方所有行上移
|
||||
for (int j = index; j < t->size; j++) {
|
||||
t->adjMat[i][j] = t->adjMat[i][j + 1];
|
||||
t->adjMat[i][j] = t->adjMat[i][j + 1]; // 被删除列后的所有列前移
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -157,45 +154,41 @@ void printGraph(graphAdjMat *t) {
|
||||
}
|
||||
|
||||
/* 构造函数 */
|
||||
graphAdjMat *newGraphic(unsigned int numberVertices, int *vertices, unsigned int **adjMat) {
|
||||
// 函数指针
|
||||
graphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat));
|
||||
|
||||
graphAdjMat *newGraphAjdMat(unsigned int numberVertices, int *vertices, unsigned int **adjMat) {
|
||||
// 申请内存
|
||||
newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2);
|
||||
newGraph->adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2);
|
||||
unsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * numberVertices * 2 * numberVertices * 2);
|
||||
newGraph->size = numberVertices;
|
||||
newGraph->capacity = numberVertices * 2;
|
||||
graphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat)); // 为图分配内存
|
||||
newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // 为顶点列表分配内存
|
||||
newGraph->adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2); // 为邻接矩阵分配二维内存
|
||||
unsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * numberVertices * 2 * numberVertices * 2); // 为邻接矩阵分配一维内存
|
||||
newGraph->size = numberVertices; // 初始化顶点数量
|
||||
newGraph->capacity = numberVertices * 2; // 初始化图容量
|
||||
|
||||
// 配置二维数组
|
||||
for (int i = 0; i < numberVertices * 2; i++) {
|
||||
newGraph->adjMat[i] = temp + i * numberVertices * 2;
|
||||
newGraph->adjMat[i] = temp + i * numberVertices * 2; // 将二维指针指向一维数组
|
||||
}
|
||||
|
||||
// 赋值
|
||||
memcpy(newGraph->vertices, vertices, sizeof(int) * numberVertices);
|
||||
for (int i = 0; i < numberVertices; i++) {
|
||||
memcpy(newGraph->adjMat[i], adjMat[i], sizeof(unsigned int) * numberVertices);
|
||||
memcpy(newGraph->adjMat[i], adjMat[i], sizeof(unsigned int) * numberVertices); // 将传入的邻接矩阵赋值给结构体内邻接矩阵
|
||||
}
|
||||
|
||||
// 返回结构体指针
|
||||
return newGraph;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
|
||||
/* 初始化无向图 */
|
||||
int vertices[5] = {1, 3, 2, 5, 4};
|
||||
unsigned int **edge = (unsigned int **)malloc(sizeof(unsigned int *) * 5);
|
||||
|
||||
// 用于构建二维数组的一维指针
|
||||
unsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * 25);
|
||||
memset(temp, 0, sizeof(unsigned int) * 25);
|
||||
for (int k = 0; k < 5; k++) {
|
||||
edge[k] = temp + k * 5;
|
||||
}
|
||||
|
||||
// 初始化边
|
||||
edge[0][1] = edge[1][0] = 1;
|
||||
edge[0][3] = edge[3][0] = 1;
|
||||
@@ -203,9 +196,8 @@ int main() {
|
||||
edge[2][3] = edge[3][2] = 1;
|
||||
edge[2][4] = edge[4][2] = 1;
|
||||
edge[3][4] = edge[4][3] = 1;
|
||||
|
||||
// 建立无向图
|
||||
graphAdjMat *graph = newGraphic(5, vertices, edge);
|
||||
graphAdjMat *graph = newGraphAjdMat(5, vertices, edge);
|
||||
free(edge);
|
||||
free(temp);
|
||||
printf("\n初始化后,图为:\n");
|
||||
|
||||
Reference in New Issue
Block a user