DFS算法的實現


#graph.h頭文件

 1 #ifndef GRAPH_H
 2 #define GRAPH_H
 3 
 4 struct adjNode{
 5     int node;
 6     struct adjNode *next;
 7 };
 8 
 9 
10 /*圖的矩陣表示向鄰接表表示的轉換*/
11 void matrixToAdjlist(int *matrix, adjNode *adjList, int n){
12     int i, j;
13     adjNode *tempNode;
14     for(i=0; i<n; ++i){
15         adjList[i].node=i;
16         adjList[i].next=NULL;
17 
18         for(j=n-1; j>=0; j--){
19             if(*(matrix+i*n+j)== 1){
20                 tempNode=(adjNode *)malloc(sizeof(adjNode));
21                 tempNode->next=adjList[i].next;
22                 tempNode->node=j;
23                 adjList[i].next=tempNode;
24             }
25         }
26     }
27 }
28 
29 /*釋放鄰接表中分配的空間*/
30 void freeAdjList(adjNode *adjList, int n){
31     int i;
32     adjNode *tempNode;
33 
34     for(i=0; i<n; ++i){
35         tempNode=adjList[i].next;
36         while(tempNode != NULL){
37             adjList[i].next=tempNode->next;
38             free(tempNode);
39             tempNode=adjList[i].next;
40         }
41     }
42 
43     free(adjList);
44 }
45 
46 #endif // GRAPH_H

 

main.h

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <vector>
  4 #include "graph.h"
  5 using namespace std;
  6 
  7 const int invalid_p=-1;
  8 int gtm=0;
  9 enum Color{w, g, b};
 10 
 11 struct DFS_struct{
 12     Color color;
 13     int parent;
 14     int dtime, ftime;  //節點的發現時間和鄰接表掃描完成時間
 15 };
 16 
 17 void DFS_visit(adjNode *adjList, DFS_struct *dfsArray, int u){
 18     int v;
 19     adjNode *tempNode;
 20 
 21     dfsArray[u].color=g;
 22     gtm += 1;
 23     dfsArray[u].dtime=gtm;
 24 
 25     tempNode=adjList[u].next;
 26     while(tempNode != NULL){
 27         v=tempNode->node;
 28         if(dfsArray[v].color == w){
 29            dfsArray[v].parent=u;
 30            DFS_visit(adjList, dfsArray, v);
 31         }
 32 
 33         tempNode=tempNode->next;
 34     }
 35 
 36     dfsArray[u].color=b;
 37     gtm += 1;
 38     dfsArray[u].ftime=gtm;
 39 }
 40 
 41 void DFS(adjNode *adjList, DFS_struct *dfsArray, int n, vector<int> &forestRoots){
 42     int i;
 43     for(i=0; i<n; ++i){
 44         dfsArray[i].color=w;
 45         dfsArray[i].parent=invalid_p;
 46         dfsArray[i].dtime=0;
 47         dfsArray[i].ftime=0;
 48     }
 49 
 50     gtm=0;
 51     for(i=0; i<n; ++i)
 52         if(dfsArray[i].color == w){
 53             DFS_visit(adjList, dfsArray, i);    //每次調用都會生成一棵深度優先搜索樹,最終生成深度優先搜索森林
 54             forestRoots.push_back(i);
 55     }
 56 }
 57 
 58 int main(){
 59     int *matrix;
 60     adjNode *adjList, *tempNode;
 61     int nodeNum=0, i, j;
 62     DFS_struct *dfsArray;
 63     vector<int> forestRoots;        //forestRoots中保存每棵深度優先搜索樹的樹根節點編號
 64 
 65     printf("Input node number: ");
 66     scanf("%d", &nodeNum);
 67 
 68     matrix=(int *)malloc(sizeof(int)*nodeNum*nodeNum);
 69     adjList=(adjNode *)malloc(sizeof(adjNode)*nodeNum);
 70 
 71     for(i=0; i<nodeNum; ++i)
 72         for(j=0; j<nodeNum; ++j)
 73             scanf("%d", matrix+i*nodeNum+j);
 74 
 75     /*以矩陣形式輸出圖*/
 76     printf("matrix: \n");
 77     for(i=0; i<nodeNum; ++i){
 78         for(j=0; j<nodeNum; ++j)
 79             printf("%d ", *(matrix+i*nodeNum+j));
 80         printf("\n");
 81     }
 82 
 83     matrixToAdjlist(matrix, adjList, nodeNum);
 84     /*以鄰接表形式輸出圖*/
 85     printf("adjacency list: \n");
 86     for(i=0; i<nodeNum; ++i){
 87         printf("node %d:", adjList[i].node);
 88         tempNode=adjList[i].next;
 89         while(tempNode != NULL){
 90             printf("->%d", tempNode->node);
 91             tempNode=tempNode->next;
 92         }
 93         printf("\n");
 94     }
 95 
 96     dfsArray=(DFS_struct *)malloc(sizeof(DFS_struct)*nodeNum);
 97     DFS(adjList, dfsArray, nodeNum, forestRoots);
 98 
 99     /*在這里深度優先搜索森林已經建立,可以進行別的操作*/
100     printf("DFS learning completed\n");
101     printf("forest roots:");
102     for(i=0; i<forestRoots.size(); ++i)
103         printf("%d ", forestRoots[i]);
104     printf("\n");
105 
106 
107     free(matrix);
108     free(dfsArray);
109     freeAdjList(adjList, nodeNum);
110     return 0;
111 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM