本文取自《数据结构与算法》(C语言版)(第三版),出版社是清华大学出版社。
图的深度优先搜索算法的基本思想是:从图G的某个顶点V0出发,访问V0,然后选择一个与V0相邻且未被访问过的顶点Vi访问,再从Vi出发选择一个与Vi相邻且未被访问的顶点Vj进行访问,依此下去,直到当前被访问过的顶点的所有邻接顶点都已被访问,则按相反顺序退回到已访问的顶点序列中,如果其中的顶点还存在未被访问的相邻顶点W,则从W出发,按相同的方法继续访问。直到图中的所有顶点均被访问。
无向图的深度优先搜索过程示意图:
其程序如下:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define MaxVertexNum 100 5 int visited[MaxVertexNum]; 6 7 typedef struct node 8 { 9 int adjvex; 10 struct node *nextrarc; 11 int info; 12 }EdgeNode; 13 14 typedef struct vnode 15 { 16 char vexdate; 17 EdgeNode *firstarc; 18 }VertexNode; 19 20 typedef VertexNode AdjList[MaxVertexNum]; 21 22 typedef struct 23 { 24 AdjList adjlist; 25 int n,e; 26 }ALGraph; 27 28 int initGraph(ALGraph* aGraph); 29 int mFind(char aChar, ALGraph* aGraph); 30 int createHead(char aChar, ALGraph* aGraph); 31 void addBody(char aChar, int aPos, ALGraph* aGraph); 32 void showGraph(ALGraph* aGraph); 33 void DFSTraverse(ALGraph* aGraph); 34 void DFS(ALGraph* aGraph, int v); 35 36 int main(void) 37 { 38 char a; 39 char sep1; 40 char sep2; 41 int isBody=0; 42 int isFinish=0; 43 int headPos=-1; 44 ALGraph g_graph; 45 initGraph(&g_graph); 46 47 printf("Input arcs like this ' a-> b c d',end with $\n"); 48 while(isFinish==0) 49 { 50 isBody=0; 51 while(1) 52 { 53 //a=getChar(); 54 scanf("%c",&a); 55 if(a=='-'||a=='\n'||a==' '||a=='>') 56 { 57 if(a=='>') 58 isBody=1; 59 continue; 60 } 61 if(a=='$'||a=='#') 62 { 63 if(a=='#') 64 isFinish=1; 65 break; 66 } 67 68 if(a=='>') 69 { 70 isBody=1; 71 continue; 72 } 73 if(isBody==1) 74 { 75 addBody(a,headPos,&g_graph); 76 } 77 else 78 { 79 if((headPos=mFind(a,&g_graph))==-1) 80 headPos=createHead(a,&g_graph); 81 } 82 } 83 } 84 showGraph(&g_graph); 85 printf("The DFS is:\n"); 86 DFSTraverse(&g_graph); 87 return 0; 88 } 89 90 void DFSTraverse(ALGraph* aGraph) 91 { 92 int i=0; 93 for(i=0; i<aGraph->n; i++) 94 { 95 visited[i]=0; 96 } 97 for(i=0; i<aGraph->n; i++) 98 { 99 if(!visited[i]) 100 DFS(aGraph,i); 101 } 102 } 103 104 void DFS(ALGraph* aGraph, int v) 105 { 106 EdgeNode* w; 107 visited[v]=1; 108 printf(" %c", aGraph->adjlist[v].vexdate); 109 w=aGraph->adjlist[v].firstarc; 110 for(w=aGraph->adjlist[v].firstarc; w; w=w->nextrarc) 111 { 112 if(!visited[w->adjvex]) 113 DFS(aGraph, w->adjvex); 114 } 115 } 116 117 void showGraph(ALGraph* aGraph) 118 { 119 int i=0; 120 for(i=0; i<aGraph->n; i++) 121 { 122 EdgeNode* pos; 123 printf("%c->", aGraph->adjlist[i]); 124 pos=aGraph->adjlist[i].firstarc; 125 while(pos!=NULL) 126 { 127 printf("%d",pos->adjvex); 128 pos=pos->nextrarc; 129 } 130 printf("\n"); 131 } 132 } 133 134 void addBody(char aChar, int aPos, ALGraph* aGraph) 135 { 136 int inversePos; 137 EdgeNode* node=(EdgeNode*) malloc(sizeof(EdgeNode)); 138 if((node->adjvex=mFind(aChar,aGraph))==-1) 139 node->adjvex=createHead(aChar,aGraph); 140 node->info=-1; 141 node->nextrarc=NULL; 142 if(aGraph->adjlist[aPos].firstarc==NULL) 143 { 144 aGraph->adjlist[aPos].firstarc=node; 145 } 146 else 147 { 148 EdgeNode* tail=aGraph->adjlist[aPos].firstarc; 149 while(tail->nextrarc!=NULL) 150 tail=tail->nextrarc; 151 tail->nextrarc=node; 152 } 153 inversePos=node->adjvex; 154 node=(EdgeNode*)malloc(sizeof(EdgeNode)); 155 node->adjvex=aPos; 156 node->info=-1; 157 node->nextrarc=NULL; 158 if(aGraph->adjlist[inversePos].firstarc==NULL) 159 { 160 aGraph->adjlist[inversePos].firstarc=node; 161 } 162 else 163 { 164 EdgeNode* tail=aGraph->adjlist[inversePos].firstarc; 165 while(tail->nextrarc!=NULL) 166 tail=tail->nextrarc; 167 tail->nextrarc=node; 168 } 169 } 170 171 int createHead(char aChar, ALGraph* aGraph) 172 { 173 int currPos=aGraph->n; 174 aGraph->adjlist[currPos].vexdate=aChar; 175 aGraph->n++; 176 return currPos; 177 } 178 179 int mFind(char aChar, ALGraph* aGraph) 180 { 181 int i=0; 182 for(i=0; i<aGraph->n; i++) 183 { 184 if(aChar==aGraph->adjlist[i].vexdate) 185 return i; 186 } 187 return -1; 188 } 189 190 int initGraph(ALGraph* aGraph) 191 { 192 int i=0; 193 aGraph->e=0; 194 aGraph->n=0; 195 for(i=0; i<MaxVertexNum; i++) 196 { 197 aGraph->adjlist[i].firstarc=NULL; 198 } 199 return 0; 200 }