This commit is contained in:
krahets
2023-07-21 15:14:40 +08:00
parent f4b32349c0
commit 2338b35039
9 changed files with 503 additions and 107 deletions
+74 -83
View File
@@ -575,32 +575,38 @@ comments: true
```c title="graph_adjacency_matrix.c"
/* 基于邻接矩阵实现的无向图类结构 */
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;
}
@@ -609,13 +615,13 @@ comments: true
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;
}
@@ -637,23 +643,21 @@ comments: true
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++;
}
@@ -665,28 +669,21 @@ comments: true
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]; // 被删除列后的所有列前移
}
}
}
@@ -724,28 +721,27 @@ comments: true
}
/* 构造函数 */
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;
}
```
@@ -1552,12 +1548,9 @@ comments: true
```c title="graph_adjacency_list.c"
/* 基于邻接链表实现的无向图类结构 */
struct graphAdjList {
// 顶点列
Vertex **verticesList;
// 顶点
unsigned int size;
// 当前容量
unsigned int capacity;
Vertex **verticesList; // 邻接
unsigned int size; // 顶点数量
unsigned int capacity; // 顶点
};
typedef struct graphAdjList graphAdjList;
@@ -1569,13 +1562,13 @@ comments: true
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
return;
}
// 查找待连接的节点
Vertex *v1 = t->verticesList[i];
Vertex *v2 = t->verticesList[j];
// 查找欲添加边的顶点 vet1 - vet2
Vertex *vet1 = t->verticesList[i];
Vertex *vet2 = t->verticesList[j];
// 连接节点
pushBack(v1->linked, v2);
pushBack(v2->linked, v1);
// 连接顶点 vet1 - vet2
pushBack(vet1->linked, vet2);
pushBack(vet2->linked, vet1);
}
/* 删除边 */
@@ -1586,13 +1579,13 @@ comments: true
return;
}
// 查找删除边的相关节点
Vertex *v1 = t->verticesList[i];
Vertex *v2 = t->verticesList[j];
// 查找删除边的顶点 vet1 - vet2
Vertex *vet1 = t->verticesList[i];
Vertex *vet2 = t->verticesList[j];
// 移除待删除边
removeLink(v1->linked, v2);
removeLink(v2->linked, v1);
// 移除待删除边 vet1 - vet2
removeLink(vet1->linked, vet2);
removeLink(vet2->linked, vet1);
}
/* 添加顶点 */
@@ -1601,16 +1594,15 @@ comments: true
if (t->size >= t->capacity) {
Vertex **tempList = (Vertex **)malloc(sizeof(Vertex *) * 2 * t->capacity);
memcpy(tempList, t->verticesList, sizeof(Vertex *) * t->size);
free(t->verticesList);
// 指向新顶点
t->verticesList = tempList;
t->capacity = t->capacity * 2;
free(t->verticesList); // 释放原邻接表内存
t->verticesList = tempList; // 指向新邻接
t->capacity = t->capacity * 2; // 容量扩大至2倍
}
// 申请新顶点内存并将新顶点地址存入顶点列表
Vertex *newV = newVertex(val);
newV->pos = t->size;
newV->linked = newLinklist(newV);
t->verticesList[t->size] = newV;
Vertex *newV = newVertex(val); // 建立新顶点
newV->pos = t->size; // 为新顶点标记下标
newV->linked = newLinklist(newV); // 为新顶点建立链表
t->verticesList[t->size] = newV; // 将新顶点加入邻接表
t->size++;
}
@@ -1622,31 +1614,30 @@ comments: true
exit(1);
}
// 查找待删节点
Vertex *v = t->verticesList[index];
// 若不存在该节点,则返回
if (v == 0) {
Vertex *vet = t->verticesList[index]; // 查找待删节点
if (vet == 0) { // 若不存在该节点,则返回
printf("index is:%d\n", index);
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
return;
}
// 遍历待删除节点链表,将所有与待删除结点有关的边删除
Node *temp = v->linked->head->next;
// 遍历待删除顶点的链表,将所有与待删除结点有关的边删除
Node *temp = vet->linked->head->next;
while (temp != 0) {
removeLink(temp->val->linked, v);
temp = temp->next;
removeLink(temp->val->linked, vet); // 删除与该顶点有关的边
temp = temp->next;
}
// 定点列表前移
// 将顶点前移
for (int i = index; i < t->size - 1; i++) {
t->verticesList[i] = t->verticesList[i + 1];
t->verticesList[i] = t->verticesList[i + 1]; // 顶点前移
t->verticesList[i]->pos--; // 所有前移的顶点索引值减1
}
t->verticesList[t->size - 1] = 0;
t->verticesList[t->size - 1] = 0; // 将被删除顶点的位置置 0
t->size--;
//释放被删除顶点的内存
freeVertex(v);
freeVertex(vet);
}
/* 打印顶点与邻接矩阵 */
@@ -1668,16 +1659,16 @@ comments: true
}
/* 构造函数 */
graphAdjList *newGraphic(unsigned int verticesNumber) {
graphAdjList *newGraphAdjList(unsigned int verticesCapacity) {
// 申请内存
graphAdjList *newGraph = (graphAdjList *)malloc(sizeof(graphAdjList));
// 建立顶点表并分配内存
newGraph->verticesList = (Vertex **)malloc(sizeof(Vertex *) * verticesNumber);
memset(newGraph->verticesList, 0, sizeof(Vertex *) * verticesNumber);
// 初始化大小和容
newGraph->size = 0;
newGraph->capacity = verticesNumber;
return newGraph;
newGraph->verticesList = (Vertex **)malloc(sizeof(Vertex *) * verticesCapacity); // 为顶点列表分配内存
memset(newGraph->verticesList, 0, sizeof(Vertex *) * verticesCapacity); // 顶点列表置 0
newGraph->size = 0; // 初始化顶点数
newGraph->capacity = verticesCapacity; // 初始化顶点容量
// 返回图指针
return newGraph;
}
```
+71 -3
View File
@@ -219,7 +219,44 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
=== "C"
```c title="graph_bfs.c"
[class]{}-[func]{graphBFS}
/* 广度优先遍历 */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
Vertex **graphBFS(graphAdjList *t, Vertex *startVet) {
// 顶点遍历序列
Vertex **res = (Vertex **)malloc(sizeof(Vertex *) * t->size);
memset(res, 0, sizeof(Vertex *) * t->size);
// 队列用于实现 BFS
queue *que = newQueue(t->size);
// 哈希表,用于记录已被访问过的顶点
hashTable *visited = newHash(t->size);
int resIndex = 0;
queuePush(que, startVet); // 将第一个元素入队
hashMark(visited, startVet->pos); // 标记第一个入队的顶点
// 以顶点 vet 为起点,循环直至访问完所有顶点
while (que->head < que->tail) {
// 遍历该顶点的边链表,将所有与该顶点有连接的,并且未被标记的顶点入队
Node *n = queueTop(que)->linked->head->next;
while (n != 0) {
// 查询哈希表,若该索引的顶点已入队,则跳过,否则入队并标记
if (hashQuery(visited, n->val->pos) == 1) {
n = n->next;
continue; // 跳过已被访问过的顶点
}
queuePush(que, n->val); // 只入队未访问的顶点
hashMark(visited, n->val->pos); // 标记该顶点已被访问
}
// 队首元素存入数组
res[resIndex] = queueTop(que); // 队首顶点加入顶点遍历序列
resIndex++;
queuePop(que); // 队首元素出队
}
// 释放内存
freeQueue(que);
freeHash(visited);
resIndex = 0;
// 返回顶点遍历序列
return res;
}
```
=== "C#"
@@ -557,9 +594,40 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
=== "C"
```c title="graph_dfs.c"
[class]{}-[func]{dfs}
/* 深度优先遍历 DFS 辅助函数 */
int resIndex = 0;
void dfs(graphAdjList *graph, hashTable *visited, Vertex *vet, Vertex **res) {
if (hashQuery(visited, vet->pos) == 1) {
return; // 跳过已被访问过的顶点
}
hashMark(visited, vet->pos); // 标记顶点并将顶点存入数组
res[resIndex] = vet; // 将顶点存入数组
resIndex++;
// 遍历该顶点链表
Node *n = vet->linked->head->next;
while (n != 0) {
// 递归访问邻接顶点
dfs(graph, visited, n->val, res);
n = n->next;
}
return;
}
[class]{}-[func]{graphDFS}
/* 深度优先遍历 DFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
Vertex **graphDFS(graphAdjList *graph, Vertex *startVet) {
// 顶点遍历序列
Vertex **res = (Vertex **)malloc(sizeof(Vertex *) * graph->size);
memset(res, 0, sizeof(Vertex *) * graph->size);
// 哈希表,用于记录已被访问过的顶点
hashTable *visited = newHash(graph->size);
dfs(graph, visited, startVet, res);
// 释放哈希表内存并将数组索引归零
freeHash(visited);
resIndex = 0;
// 返回遍历数组
return res;
}
```
=== "C#"