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

时间:2014-11-13 04:03:19

标签: c algorithm data-structures graph

我最近正在研究数据结构,我在图中遇到了麻烦。我读过这本书: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;
            }
        }

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

1 个答案:

答案 0 :(得分:0)

  

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

一个字:抽象。

无论编程语言如何,您都需要了解它们。你需要的只是一支笔和一张纸。这将使其在实施方面变得清晰。图形是指抽象对象的选择。语言与概念无关。

  

我仍然想要一个完整的实现,帮助我更好地理解如何   它有效。

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

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

  2. 根据您的实施情况进行检查。

  3. 当然,这些都是太普通的技术。在练习时,您可以结合其他技术修改它们,或者随意做任何事情。现在,您可以将它们视为DFS = Stack,BFS = Queue

相关问题