gpt4 book ai didi

c - 如何研究图数据结构和 BFS & DFS

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:22:46 25 4
gpt4 key购买 nike

我最近在研究数据结构,我在图方面遇到了麻烦。我读了这本书:Data Structures and Algorithm Analysis in C (2nd Edition) .

其实我也看了一些其他的算法书籍,我发现几乎没有一本书给我完整的图实现。尽管我可以阅读伪代码并理解 BFS 和 DFS 的运行方式,以及图形中用于解决问题的其他一些算法,但我仍然想要一个完整的实现来帮助我更好地理解它的工作原理。但是,在学习图的时候在这里写代码不重要吗?我不确定。

另外,我发现了一些关于 BFS 和 DFS 的 ACM 问题。不知道怎么表达,好像BFS和DFS只是解决思路,并没有写出标准的BFS代码。所以这让我很难学习数据结构。

不好意思表达不好,我现在是美国的留学生。

最后,我从Mastering Algorithms with C复制了这段代码并对其进行了分析。但我仍然无法理解其中的一部分。这是代码的一部分:

typedef struct BfsVertex_ {
void *data;
VertexColor color;
int hops;
} BfsVertex;

typedef struct AdjList_ {
void *vertex;
Set adjacent;
} AdjList;

typedef struct Graph_ {
int vcount;
int ecount;
List adjlists;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
} Graph;

int BFS(Graph *graph, BfsVertex *start, List *hops) {

Queue queue;
AdjList *adjlist, *clr_adjlist;
BfsVertex *clr_vertex, *adj_vertex;
ListElem *element, *member;

/* init all of the vertices in the graph */
for (element = list_head(&graph_adjlists(graph));
element != NULL;
element = list_next(element)) {

clr_vertex = ((AdjList *)list_data(element))->vertex;

if (graph->match(clr_vertex, start)) {
clr_vertex->color = gray;
clr_vertex->hops = 0;
} else {
clr_vertex = white;
clr_vertex->hops = -1;
}
}

/* init the queue with the adjacency list of the start vertex */
queue_init(&queue, NULL);
if (graph_adjlist(graph, start, &clr_adjlist) != 0) {
queue_destroy(&queue);
return -1;
}

if (queue_enqueue(&queue, clr_adjlist) != 0) {
queue_destroy(&queue);
return -1;
}

/* perform Breadth-First Search */
while (queue_size(&queue) > 0) {

adjlist = queue_peek(&queue);

/* traverse each vertex in the current adjacency list */
for (member = list_head(&adjlist->adjacent);
member != NULL;
member = list_next(member)) {

adj_vertex = list_data(member);

/* determine the color of the next adjacent vertex */
if (graph_adjlist(graph, adj_vertex, &clr_adjlist) != 0) {
queue_destroy(&queue);
return -1;
}
clr_vertex = clr_adjlist->vertex;

/* color each white vertex gray and enqueue its adjacency list */
if (clr_vertex->color == white) {
clr_vertex->color = gray;
clr_vertex->hops = ((BfsVertex *)adjlist->vertex)->hops + 1;

if (queue_enqueue(&queue, clr_adjlist) != 0) {
queue_destroy(&queue);
return -1;
}
}
}

/* dequeue the current adjacency list and color its vertex black */
if (queue_dequeue(&queue, (void **)&adjlist) == 0) {
((BfsVertex *)adjlist->vertex)->color = black;
} else {
queue_destroy(&queue);
return -1;
}
}

queue_destroy(&queue);

/* pass back the hop count for each vertex in a list */
list_init(hops, NULL);

for (element = list_head(&graph_adjlists(graph));
element != NULL;
element = list_next(element)) {

/* skip vertices that were not visited (those with hop counts of -1) */
clr_vertex = ((AdjList *)list_data(element))->vertex;
if (clr_vertex->hops != -1) {
if (list_ins_next(hops, list_tail(hops), clr_vertex) != 0) {
list_destroy(hops);
return -1;
}
}
}

return 0;
}

我在这里遇到了麻烦:这个初始化是如何工作的?它遍历图的邻接表,并将彩色顶点分配给图中的每个顶点。但是着色后,clr_vertex 改变了它的对象,颜色和距离信息是如何保存的?

 /* init all of the vertices in the graph */
for (element = list_head(&graph_adjlists(graph));
element != NULL;
element = list_next(element)) {

clr_vertex = ((AdjList *)list_data(element))->vertex;

if (graph->match(clr_vertex, start)) {
clr_vertex->color = gray;
clr_vertex->hops = 0;
} else {
clr_vertex = white;
clr_vertex->hops = -1;
}
}

感谢您阅读这么长的问题。

最佳答案

How to study graph data structures and BFS & DFS

一个词:抽象。

无论使用何种编程语言,您都需要理解它们。您只需要一支笔和一张纸。这将使实现变得非常清楚。图应该是抽象对象,其中选择了 prog。语言与概念无关。

I still want a full implementation helping me better understand how it works.

DFS 和 BFS 只是遍历(即遍历)图形的两种不同方式。不多不少。这种行走的结果就是文献中所谓的搜索树。完整实现如下:

  1. 在小图上手动运行 DFS 和 BFS。 (这将返回一个搜索树)

  2. 根据您的实现检查它。

当然,这些都是太笼统的技巧。说到练习,你可以结合其他技巧来修改它们,或者用它们做任何你想做的事。现在,您可以将它们视为 DFS=Stack,BFS=Queue

关于c - 如何研究图数据结构和 BFS & DFS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26901130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com