1. 程式人生 > >無向圖遍歷(廣度、深度)

無向圖遍歷(廣度、深度)

昨天偷懶,今天發無向圖的遍歷。明天英語並不是很慌,雖然沒怎麼複習= =,可能這就是廢柴大學生的淡定吧。

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #define Max 20
  4 bool visited[Max];
  5 //鄰接矩陣
  6 typedef struct {
  7     char vexs[Max];
  8     int arc[Max][Max];//鄰接矩陣
  9     int vexnum, arcnum;//頂點數和邊數
 10 }MGraph;
 11 //佇列
 12 
 13
typedef struct QNode { 14 int data; 15 struct QNode *next; 16 }QNode,*QueuePtr; 17 18 typedef struct { 19 QueuePtr front, rear; 20 }LinkQueue; 21 22 int initQueue(LinkQueue *q) { 23 q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); 24 if (!q->front) { 25
return 0; 26 } 27 q->front->next = NULL; 28 return 1; 29 } 30 31 int EnQueue(LinkQueue *q, int e) { 32 QueuePtr s = (QueuePtr)malloc(sizeof(QNode)); 33 if (!s) { 34 return 0; 35 } 36 s->data = e; 37 s->next = NULL; 38 q->rear->next = s;
39 q->rear = s; 40 return 1; 41 42 } 43 int DeQueue(LinkQueue *q, int *e) { 44 QueuePtr p; 45 if (q->front == q->rear) { 46 return 0; 47 } 48 p = q->front->next; 49 *e = p->data; 50 q->front->next = p->next; 51 if (q->rear == p) { 52 q->rear = q->front; 53 } 54 free(p); 55 return 1; 56 } 57 int QueueEmpty(LinkQueue q) { 58 if (q.front == q.rear) { 59 return 1; 60 } 61 return 0; 62 } 63 64 65 66 //返回頂點所在的位置 67 int Locatevex(MGraph G,char c) { 68 for (int i = 0; i < G.vexnum; i++) { 69 if (c == G.vexs[i]) { 70 return i; 71 } 72 } 73 } 74 75 void CreateMGraph(MGraph *G) { 76 printf("請輸入頂點數和邊數:\n"); 77 scanf_s("%d%d", &G->vexnum, &G->arcnum); 78 79 printf("輸入頂點名稱:\n"); 80 for (int i = 0; i < G->vexnum; i++) { 81 getchar(); 82 scanf_s("%c", &G->vexs[i]); 83 } 84 for (int i = 0; i < G->vexnum; i++) { 85 for (int j = 0; j < G->vexnum; j++) { 86 G->arc[i][j] = 0; 87 } 88 } 89 for (int m = 0; m < G->arcnum; m++) { 90 char a, b; 91 int i, j; 92 93 printf("輸入邊:\n"); 94 getchar(); 95 scanf_s("%c",&a); 96 getchar(); 97 scanf_s("%c", &b); 98 i = Locatevex(*G, a); 99 j = Locatevex(*G, b); 100 G->arc[i][j]= 1; 101 G->arc[j][i] = 1; 102 } 103 } 104 105 106 void DFS(MGraph G, int i) { 107 visited[i] = true; 108 printf("%c", G.vexs[i]); 109 for (int j = 0; j < G.vexnum; j++) { 110 if (G.arc[i][j] == 1 && !visited[j]) { 111 DFS(G, j); 112 } 113 } 114 } 115 116 void DFSTraverse(MGraph G) { 117 for (int i = 0; i < G.vexnum; i++) { 118 visited[i] = false; 119 } 120 for (int j = 0; j < G.vexnum; j++) { 121 if (!visited[j]) { 122 DFS(G, j); 123 } 124 } 125 } 126 127 void BFSTraverse(MGraph G) { 128 LinkQueue Q; 129 for (int i = 0; i < G.vexnum; i++) { 130 visited[i] = false; 131 } 132 initQueue(&Q); 133 for (int i = 0; i < G.vexnum; i++) { 134 if (!visited[i]) { 135 visited[i] = true; 136 printf("%c", G.vexs[i]); 137 EnQueue(&Q, i); 138 } 139 while (!QueueEmpty(Q)) { 140 DeQueue(&Q, &i); 141 for (int j = 0; j < G.vexnum; j++) { 142 if (G.arc[i][j] == 1 && !visited[j]) { 143 visited[j] = true; 144 printf("%c", G.vexs[j]); 145 EnQueue(&Q, j); 146 } 147 } 148 } 149 150 } 151 } 152 153 154 //鄰接表 155 //邊表節點 156 typedef struct EdgeNode { 157 int adjvex; 158 struct EdgeNode *next; 159 }EdgeNode; 160 161 //頂點表節點 162 typedef struct VertexNode { 163 char data; 164 EdgeNode *first; 165 }VertexNode,AdjList[Max]; 166 167 typedef struct { 168 AdjList adjList; 169 int vexnum, arcnum;//頂點數和邊數 170 }graphAdjList,*GraphAdjList; 171 //構建鄰接表 172 void CreateALGraph(MGraph G, GraphAdjList *GL) 173 { 174 EdgeNode *e; 175 176 *GL = (GraphAdjList)malloc(sizeof(graphAdjList)); 177 178 (*GL)->vexnum = G.vexnum; 179 (*GL)->arcnum = G.arcnum; 180 181 for (int i = 0; i<G.vexnum; i++) 182 { 183 (*GL)->adjList[i].data = G.vexs[i]; 184 (*GL)->adjList[i].first = NULL; 185 } 186 for (int k = 0; k<G.vexnum; k++) 187 { 188 for (int j = 0; j<G.vexnum; j++) 189 { 190 if (G.arc[k][j] == 1) 191 { 192 e = (EdgeNode *)malloc(sizeof(EdgeNode)); 193 e->adjvex = j; 194 e->next = (*GL)->adjList[k].first; 195 (*GL)->adjList[k].first = e; 196 } 197 } 198 } 199 } 200 //鄰接表的深度優先遞迴演算法 201 void DFS(GraphAdjList GL, int i) 202 { 203 EdgeNode *p; 204 visited[i] = true; 205 printf("%c ", GL->adjList[i].data); 206 p = GL->adjList[i].first; 207 while (p) 208 { 209 if (!visited[p->adjvex]) 210 { 211 DFS(GL, p->adjvex); 212 } 213 p = p->next; 214 215 } 216 } 217 218 //鄰接表的深度遍歷操作 219 void DFSTraverse(GraphAdjList GL) 220 { 221 int i; 222 for (i = 0; i<GL->vexnum; i++) 223 visited[i] = false;//初始化 224 for (i = 0; i<GL->vexnum; i++) 225 if (!visited[i]) 226 DFS(GL, i); 227 } 228 229 230 //鄰接表的廣度遍歷演算法 231 void BFSTraverse(GraphAdjList GL) 232 { 233 EdgeNode *p; 234 LinkQueue Q; 235 for (int i = 0; i < GL->vexnum; i++) 236 { 237 visited[i] = false; 238 } 239 initQueue(&Q); 240 for (int i = 0; i < GL->vexnum; i++) 241 { 242 if (!visited[i]) 243 { 244 visited[i] = true; 245 printf("%c ", GL->adjList[i].data); 246 EnQueue(&Q, i); 247 while (!QueueEmpty(Q)) 248 { 249 DeQueue(&Q, &i); 250 p = GL->adjList[i].first; 251 while (p) 252 { 253 if (!visited[p->adjvex]) 254 { 255 visited[p->adjvex] = true; 256 printf("%c ", GL->adjList[p->adjvex].data); 257 EnQueue(&Q, p->adjvex); 258 } 259 p = p->next; 260 } 261 } 262 } 263 } 264 } 265 int main() { 266 267 MGraph G; 268 GraphAdjList GL; 269 CreateMGraph(&G); 270 CreateALGraph(G, &GL); 271 printf("深度優先搜尋:\n"); 272 DFSTraverse(GL); 273 printf("\n"); 274 printf("廣度優先搜尋:\n"); 275 BFSTraverse(GL); 276 return 0; 277 /* 278 MGraph G; 279 CreateMGraph(&G); 280 DFSTraverse(G); 281 printf("\n"); 282 BFSTraverse(G); 283 return 0; 284 */ 285 }