2023-07-11 23:50:51 +08:00
|
|
|
/**
|
|
|
|
* File: graph_adjacency_matrix.c
|
|
|
|
* Created Time: 2023-07-06
|
|
|
|
* Author: NI-SW (947743645@qq.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../utils/common.h"
|
|
|
|
|
|
|
|
/* 基于邻接矩阵实现的无向图类结构 */
|
2023-10-18 02:16:26 +08:00
|
|
|
typedef struct {
|
|
|
|
int *vertices; // 顶点列表
|
|
|
|
int **adjMat; // 邻接矩阵,元素代表“边”,索引代表“顶点索引”
|
|
|
|
int size; // 顶点数量
|
|
|
|
int capacity; // 图容量
|
|
|
|
} GraphAdjMat;
|
2023-07-11 23:50:51 +08:00
|
|
|
|
|
|
|
/* 添加边 */
|
2023-07-20 19:08:23 +08:00
|
|
|
// 参数 i, j 对应 vertices 元素索引
|
2023-10-18 02:16:26 +08:00
|
|
|
void addEdge(GraphAdjMat *graph, int i, int j) {
|
2023-07-11 23:50:51 +08:00
|
|
|
// 越界检查
|
2023-10-18 02:16:26 +08:00
|
|
|
if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {
|
2023-07-11 23:50:51 +08:00
|
|
|
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-07-20 19:08:23 +08:00
|
|
|
// 添加边
|
|
|
|
// 参数 i, j 对应 vertices 元素索引
|
2023-10-18 02:16:26 +08:00
|
|
|
graph->adjMat[i][j] = 1;
|
|
|
|
graph->adjMat[j][i] = 1;
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 删除边 */
|
2023-07-20 19:08:23 +08:00
|
|
|
// 参数 i, j 对应 vertices 元素索引
|
2023-10-18 02:16:26 +08:00
|
|
|
void removeEdge(GraphAdjMat *graph, int i, int j) {
|
2023-07-11 23:50:51 +08:00
|
|
|
// 越界检查
|
2023-10-18 02:16:26 +08:00
|
|
|
if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {
|
2023-07-11 23:50:51 +08:00
|
|
|
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-07-20 19:08:23 +08:00
|
|
|
// 删除边
|
|
|
|
// 参数 i, j 对应 vertices 元素索引
|
2023-10-18 02:16:26 +08:00
|
|
|
graph->adjMat[i][j] = 0;
|
|
|
|
graph->adjMat[j][i] = 0;
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 添加顶点 */
|
2023-10-18 02:16:26 +08:00
|
|
|
void addVertex(GraphAdjMat *graph, int val) {
|
2023-07-11 23:50:51 +08:00
|
|
|
// 如果实际使用不大于预设空间,则直接初始化新空间
|
2023-10-18 02:16:26 +08:00
|
|
|
if (graph->size < graph->capacity) {
|
|
|
|
graph->vertices[graph->size] = val; // 初始化新顶点值
|
|
|
|
for (int i = 0; i < graph->size; i++) {
|
|
|
|
graph->adjMat[i][graph->size] = 0; // 邻接矩新列阵置0
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
2023-10-18 02:16:26 +08:00
|
|
|
memset(graph->adjMat[graph->size], 0, sizeof(int) * (graph->size + 1)); // 将新增行置 0
|
|
|
|
graph->size++;
|
2023-07-11 23:50:51 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 扩容,申请新的顶点数组
|
2023-10-18 02:16:26 +08:00
|
|
|
int *temp = (int *)malloc(sizeof(int) * (graph->size * 2));
|
|
|
|
memcpy(temp, graph->vertices, sizeof(int) * graph->size);
|
|
|
|
temp[graph->size] = val;
|
2023-07-11 23:50:51 +08:00
|
|
|
// 释放原数组
|
2023-10-18 02:16:26 +08:00
|
|
|
free(graph->vertices);
|
|
|
|
graph->vertices = temp;
|
2023-07-11 23:50:51 +08:00
|
|
|
// 扩容,申请新的二维数组
|
2023-10-18 02:16:26 +08:00
|
|
|
int **tempMat = (int **)malloc(sizeof(int *) * graph->size * 2);
|
|
|
|
int *tempMatLine = (int *)malloc(sizeof(int) * (graph->size * 2) * (graph->size * 2));
|
|
|
|
memset(tempMatLine, 0, sizeof(int) * (graph->size * 2) * (graph->size * 2));
|
|
|
|
for (int k = 0; k < graph->size * 2; k++) {
|
|
|
|
tempMat[k] = tempMatLine + k * (graph->size * 2);
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
2023-10-18 02:16:26 +08:00
|
|
|
for (int i = 0; i < graph->size; i++) {
|
|
|
|
memcpy(tempMat[i], graph->adjMat[i], sizeof(int) * graph->size); // 原数据复制到新数组
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
2023-10-18 02:16:26 +08:00
|
|
|
for (int i = 0; i < graph->size; i++) {
|
|
|
|
tempMat[i][graph->size] = 0; // 将新增列置 0
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
2023-10-18 02:16:26 +08:00
|
|
|
memset(tempMat[graph->size], 0, sizeof(int) * (graph->size + 1)); // 将新增行置 0
|
2023-07-11 23:50:51 +08:00
|
|
|
// 释放原数组
|
2023-10-18 02:16:26 +08:00
|
|
|
free(graph->adjMat[0]);
|
|
|
|
free(graph->adjMat);
|
2023-07-11 23:50:51 +08:00
|
|
|
// 扩容后,指向新地址
|
2023-10-18 02:16:26 +08:00
|
|
|
graph->adjMat = tempMat; // 指向新的邻接矩阵地址
|
|
|
|
graph->capacity = graph->size * 2;
|
|
|
|
graph->size++;
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 删除顶点 */
|
2023-10-18 02:16:26 +08:00
|
|
|
void removeVertex(GraphAdjMat *graph, int index) {
|
2023-07-11 23:50:51 +08:00
|
|
|
// 越界检查
|
2023-10-18 02:16:26 +08:00
|
|
|
if (index < 0 || index >= graph->size) {
|
2023-07-11 23:50:51 +08:00
|
|
|
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-10-18 02:16:26 +08:00
|
|
|
for (int i = index; i < graph->size - 1; i++) {
|
|
|
|
graph->vertices[i] = graph->vertices[i + 1]; // 清除删除的顶点,并将其后所有顶点前移
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
2023-10-18 02:16:26 +08:00
|
|
|
graph->vertices[graph->size - 1] = 0; // 将被前移的最后一个顶点置 0
|
2023-07-11 23:50:51 +08:00
|
|
|
// 清除邻接矩阵中删除的列
|
2023-10-18 02:16:26 +08:00
|
|
|
for (int i = 0; i < graph->size - 1; i++) {
|
2023-07-11 23:50:51 +08:00
|
|
|
if (i < index) {
|
2023-10-18 02:16:26 +08:00
|
|
|
for (int j = index; j < graph->size - 1; j++) {
|
|
|
|
graph->adjMat[i][j] = graph->adjMat[i][j + 1]; // 被删除列后的所有列前移
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
2023-09-02 23:54:38 +08:00
|
|
|
} else {
|
2023-10-18 02:16:26 +08:00
|
|
|
memcpy(graph->adjMat[i], graph->adjMat[i + 1], sizeof(int) * graph->size); // 被删除行的下方所有行上移
|
|
|
|
for (int j = index; j < graph->size; j++) {
|
|
|
|
graph->adjMat[i][j] = graph->adjMat[i][j + 1]; // 被删除列后的所有列前移
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-18 02:16:26 +08:00
|
|
|
graph->size--;
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 打印顶点与邻接矩阵 */
|
2023-10-18 02:16:26 +08:00
|
|
|
void printGraph(GraphAdjMat *graph) {
|
|
|
|
if (graph->size == 0) {
|
2023-07-11 23:50:51 +08:00
|
|
|
printf("graph is empty\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("顶点列表 = [");
|
2023-10-18 02:16:26 +08:00
|
|
|
for (int i = 0; i < graph->size; i++) {
|
|
|
|
if (i != graph->size - 1) {
|
|
|
|
printf("%d, ", graph->vertices[i]);
|
2023-07-11 23:50:51 +08:00
|
|
|
} else {
|
2023-10-18 02:16:26 +08:00
|
|
|
printf("%d", graph->vertices[i]);
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("]\n");
|
|
|
|
printf("邻接矩阵 =\n[\n");
|
2023-10-18 02:16:26 +08:00
|
|
|
for (int i = 0; i < graph->size; i++) {
|
2023-07-11 23:50:51 +08:00
|
|
|
printf(" [");
|
2023-10-18 02:16:26 +08:00
|
|
|
for (int j = 0; j < graph->size; j++) {
|
|
|
|
if (j != graph->size - 1) {
|
|
|
|
printf("%u, ", graph->adjMat[i][j]);
|
2023-07-11 23:50:51 +08:00
|
|
|
} else {
|
2023-10-18 02:16:26 +08:00
|
|
|
printf("%u", graph->adjMat[i][j]);
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("],\n");
|
|
|
|
}
|
|
|
|
printf("]\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 构造函数 */
|
2023-10-18 02:16:26 +08:00
|
|
|
GraphAdjMat *newGraphAjdMat(int numberVertices, int *vertices, int **adjMat) {
|
2023-07-11 23:50:51 +08:00
|
|
|
// 申请内存
|
2023-10-18 02:16:26 +08:00
|
|
|
GraphAdjMat *newGraph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat)); // 为图分配内存
|
|
|
|
newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // 为顶点列表分配内存
|
|
|
|
newGraph->adjMat = (int **)malloc(sizeof(int *) * numberVertices * 2); // 为邻接矩阵分配二维内存
|
|
|
|
int *temp = (int *)malloc(sizeof(int) * numberVertices * 2 * numberVertices * 2); // 为邻接矩阵分配一维内存
|
|
|
|
newGraph->size = numberVertices; // 初始化顶点数量
|
|
|
|
newGraph->capacity = numberVertices * 2; // 初始化图容量
|
2023-07-11 23:50:51 +08:00
|
|
|
// 配置二维数组
|
|
|
|
for (int i = 0; i < numberVertices * 2; i++) {
|
2023-07-20 19:08:23 +08:00
|
|
|
newGraph->adjMat[i] = temp + i * numberVertices * 2; // 将二维指针指向一维数组
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
|
|
|
// 赋值
|
|
|
|
memcpy(newGraph->vertices, vertices, sizeof(int) * numberVertices);
|
|
|
|
for (int i = 0; i < numberVertices; i++) {
|
2023-10-18 02:16:26 +08:00
|
|
|
memcpy(newGraph->adjMat[i], adjMat[i],
|
|
|
|
sizeof(int) * numberVertices); // 将传入的邻接矩阵赋值给结构体内邻接矩阵
|
2023-07-11 23:50:51 +08:00
|
|
|
}
|
2023-07-20 19:08:23 +08:00
|
|
|
// 返回结构体指针
|
2023-07-11 23:50:51 +08:00
|
|
|
return newGraph;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
int main() {
|
|
|
|
/* 初始化无向图 */
|
|
|
|
int vertices[5] = {1, 3, 2, 5, 4};
|
2023-10-18 02:16:26 +08:00
|
|
|
int **edge = (int **)malloc(sizeof(int *) * 5);
|
2023-07-11 23:50:51 +08:00
|
|
|
// 用于构建二维数组的一维指针
|
2023-10-18 02:16:26 +08:00
|
|
|
int *temp = (int *)malloc(sizeof(int) * 25);
|
|
|
|
memset(temp, 0, sizeof(int) * 25);
|
2023-07-11 23:50:51 +08:00
|
|
|
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;
|
|
|
|
edge[1][2] = edge[2][1] = 1;
|
|
|
|
edge[2][3] = edge[3][2] = 1;
|
|
|
|
edge[2][4] = edge[4][2] = 1;
|
|
|
|
edge[3][4] = edge[4][3] = 1;
|
|
|
|
// 建立无向图
|
2023-10-18 02:16:26 +08:00
|
|
|
GraphAdjMat *graph = newGraphAjdMat(5, vertices, edge);
|
2023-07-11 23:50:51 +08:00
|
|
|
free(edge);
|
|
|
|
free(temp);
|
|
|
|
printf("\n初始化后,图为:\n");
|
|
|
|
printGraph(graph);
|
|
|
|
|
|
|
|
/* 添加边 */
|
|
|
|
// 顶点 1, 2 的索引分别为 0, 2
|
|
|
|
addEdge(graph, 0, 2);
|
|
|
|
printf("\n添加边 1-2 后图为\n");
|
|
|
|
printGraph(graph);
|
|
|
|
|
|
|
|
/* 删除边 */
|
|
|
|
// 顶点 1, 3 的索引分别为 0, 1
|
|
|
|
removeEdge(graph, 0, 1);
|
|
|
|
printf("\n删除边 1-3 后,图为\n");
|
|
|
|
printGraph(graph);
|
|
|
|
|
|
|
|
/* 添加顶点 */
|
|
|
|
addVertex(graph, 6);
|
|
|
|
printf("\n添加顶点 6 后,图为\n");
|
|
|
|
printGraph(graph);
|
|
|
|
|
|
|
|
/* 删除顶点 */
|
|
|
|
// 顶点 3 的索引为 1
|
|
|
|
removeVertex(graph, 1);
|
|
|
|
printf("\n删除顶点 3 后,图为\n");
|
|
|
|
printGraph(graph);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|