我最近在研究数据结构,在图表方面有困难我读过这本书: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_顶点改变了它的对象,如何保存颜色和距离信息?
/* 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;
}
}
谢谢你读了这么长的问题。
最佳答案
如何研究图形数据结构和BFS&DFS
一个词:抽象的。
不管编程语言是什么,你都需要理解它们。你只需要一支笔和一张纸。这将使它在实现时变得非常清楚图形是指选择prog的抽象对象。语言与概念无关。
我仍然想要一个完整的实现帮助我更好地理解
它起作用了。
dfs和bfs只是遍历(即遍历)图形的两种不同方式。不多不少这种行走的结果在文献中被称为搜索树。
具体实施如下:
手动在小图上运行dfs和bfs。(这将返回一个搜索树)
对照你的执行情况检查一下。
当然,这些技术太一般了。当涉及到实践,你可以修改他们结合其他技术或做任何你想与他们现在,您可以将它们看作dfs=stack,bfs=queue