簡體   English   中英

C中使用鄰接表的無向圖(基於Cormen算法)的DFS實現錯誤

[英]Error in DFS implementation of an undirected graph (based on Cormen algorithm) using adjacency list in C

我在 C 中為使用鄰接表表示的圖的 DFS 遍歷編寫了這段代碼。它基於 Cormen 中給出的算法。 此代碼的問題是“dfs_visit”function 中的 while 循環根本沒有執行。 我不明白為什么。 請告訴我原因。 通過 dfs 和 dfs_visit 函數訪問 go 可能就足夠了,但以防萬一您需要通過其他函數訪問 go,我將它們全部包括在內。

//顏色:'-1'代表白色; '0' 為灰色; '1' 表示黑色 //白色:未訪問; 灰色:已發現但尚未添加; 黑色:訪問過

    #include<stdio.h>
    #include<stdlib.h>

    int time;

    struct AdjListNode
    {
        int dest;
        int color;
        int par;//parent
        int dt;//discovered time
        int ft;//finished time
        struct AdjListNode *next;
    };

    struct AdjList
    {
        struct AdjListNode *head;
    };

    struct Graph
    {
        int V;
        struct AdjList *array;
    };
    
    struct AdjListNode *addNewNode(int dest);
    void DFS_visit(struct Graph* graph,struct AdjListNode *u);
    struct Graph *createGraph(int V);
    void addEdge(struct Graph *graph, int src, int dest);
    void printGraph(struct Graph *graph);
    void printDFS(struct Graph* graph);

    struct AdjListNode *addNewNode(int dest)
    {
        struct AdjListNode *newNode = (struct AdjListNode *)malloc(sizeof(struct AdjListNode));
        newNode->dest = dest;
        newNode->dt = newNode->ft = 0;
        newNode->par = 0;
        newNode->next = NULL;
        return newNode;
    }
    
    struct Graph *createGraph(int V)
    {
        struct Graph *graph = (struct Graph *)malloc(V * (sizeof(struct Graph)));
        graph->array = (struct AdjList*)malloc(V*sizeof(struct AdjList));
        graph->V = V;
        for (int i = 0; i < V; i++)
        {
            graph->array[i].head = addNewNode(i);
        }
        return graph;
    }

    void addEdge(struct Graph *graph, int src, int dest)
    {
        struct AdjListNode *newNode = addNewNode(dest);
        // simply adding the node at the beginning
        newNode->next = graph->array[src].head->next;
        graph->array[src].head->next = newNode;
        // for undirected graph
        newNode = addNewNode(src);
        newNode->next = graph->array[dest].head->next;
        graph->array[dest].head->next = newNode;
    }


    void DFS(struct Graph* graph){
        for(int i=0;i<graph->V;i++){
            graph->array[i].head->color = -1;
            graph->array[i].head->par = 0;
        }
        time = 0;
        for(int i=0;i<graph->V;i++){
            if(graph->array[i].head->color == -1){
                DFS_visit(graph,graph->array[i].head);
            }
        }
    }

    void DFS_visit(struct Graph* graph,struct AdjListNode *u){
        u->color = 0;
        u->dt = ++time;
        printf("%d ",u->dest);
        struct AdjListNode* temp = u->next;
        while(temp!=NULL){
            if(temp->color == -1){
                temp->par = u->dest;
                DFS_visit(graph,graph->array[temp->dest].head);
            }
            temp = temp->next;
        }
        u->color = 1;
        u->ft = ++time;
    }

    void printGraph(struct Graph *graph)
    {
        for (int v = 0; v < graph->V; v++)
        {
            struct AdjListNode *temp = graph->array[v].head;
            printf("\nthe adj list of vertex %d is : head", v);
            while (temp)
            {
                printf("-> %d", temp->dest);
                temp = temp->next;
            }
            printf("\n"); 
        }
    }

    void printDFS(struct Graph* graph){
        printf("\n");
        for(int v=0;v<graph->V;v++){
            printf("\nvertex %d has dt = %d & ft = %d",v,graph->array[v].head->dt,graph->array[v].head->ft);
        }
    }

    int main()
    {
        int V;
        printf("enter # of vertices: ");
        scanf("%d",&V);
        struct Graph *graph = createGraph(V);
    
        addEdge(graph,0,1);
        addEdge(graph,1,2);
        addEdge(graph,1,3);
        addEdge(graph,2,3);
        addEdge(graph,2,4);
        addEdge(graph,4,0);

        printGraph(graph);
        printf("\nDFS of given graph is: ");
        DFS(graph);
        printDFS(graph);
        return 0;
    }

printGraph 的 OUTPUT 如下所示,是正確的。
頂點0的鄰接表:0->4->1->null
頂點 1 的鄰接表:1->3->2->0->null
頂點2的鄰接表:2->4->3->1->null
頂點3的鄰接表:3->2->1->null
頂點 4 的鄰接表:4->0->2->null

重要說明:我在每個頂點的鄰接列表的開頭添加了 data = vertex 的節點,並使其成為列表的頭部。

它給出 0 1 2 3 4 作為 output 但這是錯誤的。 右邊的output是0 4 2 3 1。很明顯,正如我前面提到的,“dfs_visit”的while循環沒有執行。 請讓我知道我必須進行哪些更改才能使此代碼正常工作。 “圖形打印”沒有問題。 鄰接列表正在正確打印。

它為 dfs 提供的Output是:
頂點 0 有 dt = 1 和 ft = 2
頂點 1 有 dt = 3 和 ft = 4
頂點 2 有 dt = 5 和 ft = 6
頂點 3 有 dt = 7 和 ft = 8
頂點 4 有 dt = 9 和 ft = 10
顯然,這是錯誤的 output ,就好像 dfs_visit 的 while 循環根本沒有執行。 但是,我不明白為什么不是。

歡迎您提出任何優化代碼的建議

您的 addEdge 調用 addNewNode 兩次,無論邊緣是否連接新節點。

在這之后

    addEdge(graph,0,1);
    addEdge(graph,1,2);

該圖將有節點 1 的兩個副本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM