hello-algo/codes/c/chapter_graph/graph_dfs.c

111 lines
2.8 KiB
C
Raw Normal View History

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>
2023-07-20 19:08:23 +08:00
/**
* File: graph_dfs.c
* Created Time: 2023-07-13
* Author: NI-SW (947743645@qq.com)
*/
#include "graph_adjacency_list.c"
/* 哈希表 */
typedef struct {
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>
2023-07-20 19:08:23 +08:00
unsigned int size;
unsigned int *array;
} HashTable;
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>
2023-07-20 19:08:23 +08:00
/* 初始化哈希表 */
HashTable *newHash(unsigned int size) {
HashTable *h = (HashTable *)malloc(sizeof(HashTable));
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>
2023-07-20 19:08:23 +08:00
h->array = (unsigned int *)malloc(sizeof(unsigned int) * size);
memset(h->array, 0, sizeof(unsigned int) * size);
h->size = size;
return h;
}
/* 标记索引过的顶点 */
void hashMark(HashTable *h, int index) {
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>
2023-07-20 19:08:23 +08:00
h->array[index % h->size] = 1;
}
/* 查询顶点是否已被标记 */
int hashQuery(HashTable *h, int index) {
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>
2023-07-20 19:08:23 +08:00
// 若顶点已被标记,则返回 1
if (h->array[index % h->size] == 1) {
return 1;
} else {
return 0;
}
}
/* 释放哈希表内存 */
void freeHash(HashTable *h) {
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>
2023-07-20 19:08:23 +08:00
free(h->array);
free(h);
}
/* 深度优先遍历 DFS 辅助函数 */
int resIndex = 0;
void dfs(GraphAdjList *graph, HashTable *visited, Vertex *vet, Vertex **res) {
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>
2023-07-20 19:08:23 +08:00
if (hashQuery(visited, vet->pos) == 1) {
return; // 跳过已被访问过的顶点
}
hashMark(visited, vet->pos); // 标记顶点并将顶点存入数组
res[resIndex] = vet; // 将顶点存入数组
resIndex++;
// 遍历该顶点链表
Node *n = vet->list->head->next;
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>
2023-07-20 19:08:23 +08:00
while (n != 0) {
// 递归访问邻接顶点
dfs(graph, visited, n->val, res);
n = n->next;
}
return;
}
/* 深度优先遍历 DFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
Vertex **graphDFS(GraphAdjList *graph, Vertex *startVet) {
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>
2023-07-20 19:08:23 +08:00
// 顶点遍历序列
Vertex **res = (Vertex **)malloc(sizeof(Vertex *) * graph->size);
memset(res, 0, sizeof(Vertex *) * graph->size);
// 哈希表,用于记录已被访问过的顶点
HashTable *visited = newHash(graph->size);
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>
2023-07-20 19:08:23 +08:00
dfs(graph, visited, startVet, res);
// 释放哈希表内存并将数组索引归零
freeHash(visited);
resIndex = 0;
// 返回遍历数组
return res;
}
/* Driver Code */
int main() {
GraphAdjList *graph = newGraphAdjList(10);
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>
2023-07-20 19:08:23 +08:00
for (int i = 0; i < 7; i++) {
addVertex(graph, i);
}
addEdge(graph, 0, 1);
addEdge(graph, 0, 3);
addEdge(graph, 1, 2);
addEdge(graph, 2, 5);
addEdge(graph, 5, 4);
addEdge(graph, 5, 6);
printf("\n初始化后,图为:\n");
printGraph(graph);
// 深度优先遍历 DFS
Vertex **vet = graphDFS(graph, graph->vertices[0]);
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>
2023-07-20 19:08:23 +08:00
// 输出遍历结果
printf("\n深度优先遍历DFS顶点序列为\n");
printf("[");
printf("%d", vet[0]->val);
for (int i = 1; i < graph->size && vet[i] != 0; i++) {
printf(", %d", vet[i]->val);
}
printf("]\n");
// 释放内存
free(vet);
return 0;
}