标签:
1 /** 2 无向图的邻接表存储 3 深度优先遍历递归 4 广度优先遍历递归+非递归 5 */ 6 #include <stdio.h> 7 #include <string.h> 8 #include <malloc.h> 9 #define N 5 10 #define MAX 50 11 typedef struct A{ 12 int adjvex; 13 struct A* nextArc; 14 }Arc; 15 typedef struct node{ 16 char data[N]; 17 Arc* firstArc; 18 }Node; 19 typedef struct graph{ 20 Node vex[MAX]; 21 int numv; 22 int nume; 23 }Graph; 24 25 int getIndex(Graph G,char s[]){ 26 for(int i = 0; i < G.numv; i++){ 27 if(strcmp(G.vex[i].data,s) == 0) 28 return i; 29 } 30 return -1; 31 } 32 33 void create(Graph& G){ 34 printf("输入顶点和弧的个数:\n"); 35 scanf("%d%d",&G.numv,&G.nume); 36 printf("输入顶点信息:\n"); 37 for(int i = 0; i < G.numv; i++) 38 scanf("%s",G.vex[i].data); 39 ///初始化顶点数组 40 for(int i = 0; i < G.numv; i++) 41 G.vex[i].firstArc = NULL; 42 printf("输入边的信息:\n"); 43 char s[N],e[N]; 44 int u,v; 45 for(int i = 0; i < G.nume; i++){ 46 scanf("%s%s",s,e); 47 u = getIndex(G,s); 48 v = getIndex(G,e); 49 Arc* p = (Arc*)malloc(sizeof(Arc)); 50 p->adjvex = v; 51 p->nextArc = NULL; 52 p->nextArc = G.vex[u].firstArc; 53 G.vex[u].firstArc = p; 54 Arc* t = (Arc*)malloc(sizeof(Arc)); 55 t->nextArc = NULL; 56 t->adjvex = u; 57 t->nextArc = G.vex[v].firstArc; 58 G.vex[v].firstArc = t; 59 } 60 } 61 62 void output(Graph G){ 63 Arc* p; 64 for(int i = 0; i < G.numv; i++){ 65 p = G.vex[i].firstArc; 66 printf("%4s",G.vex[i].data); 67 while(p != NULL){ 68 printf("%4s",G.vex[p->adjvex].data); 69 p = p->nextArc; 70 } 71 printf("\n"); 72 } 73 } 74 75 ///深度优先遍历 (递归) 76 int visit[2*MAX]; 77 void dfs(Graph G,int s){ 78 Arc* p = G.vex[s].firstArc; 79 if(!visit[s]){ 80 printf("%4s",G.vex[s].data); 81 visit[s] = 1; 82 } 83 while(p != NULL){ 84 if(!visit[p->adjvex]) 85 dfs(G,p->adjvex); 86 p = p->nextArc; 87 } 88 } 89 90 ///广度优先遍历 (递归) 91 ///缺点递归最大为G.numv次 92 int q[2*MAX],f=0,r=0; 93 int visit_q[MAX]; 94 void bfs(Graph G,int s){ 95 if(!visit_q[s]){ 96 printf("%4s",G.vex[s].data); 97 visit_q[s] = 1; 98 Arc* p = G.vex[s].firstArc; 99 while(p != NULL){ 100 if(!visit_q[p->adjvex]) 101 q[r++] = p->adjvex; 102 p = p->nextArc; 103 } 104 } 105 while(f < r){ 106 bfs(G,q[f++]); 107 } 108 } 109 110 ///广度优先遍历 (非递归) 111 int Q[2*MAX],F=0,R=0; 112 int visit_Q[MAX]; 113 void bfs_1(Graph G,int s){ 114 printf("%4s",G.vex[s].data); 115 visit_Q[s] = 1; 116 Arc* p = G.vex[s].firstArc; 117 while(p != NULL){ 118 Q[R++] = p->adjvex; 119 p = p->nextArc; 120 } 121 while(F < R){ 122 int node = Q[F++]; 123 if(!visit_Q[node]){ 124 printf("%4s",G.vex[node].data); 125 visit_Q[node] = 1; 126 p = G.vex[node].firstArc; 127 while(p != NULL){ 128 if(!visit_Q[p->adjvex]) 129 Q[R++] = p->adjvex; 130 p = p->nextArc; 131 } 132 } 133 } 134 } 135 136 int main(void){ 137 Graph G; 138 create(G); printf("输出邻接矩阵:\n"); 139 output(G); 140 printf("深度优先遍历(递归):\n"); 141 dfs(G,0); 142 printf("\n广度优先遍历(递归):\n"); 143 bfs(G,0); 144 printf("\n广度优先遍历(非递归):\n"); 145 bfs_1(G,0); 146 return 0; 147 }
测试用例:
8 9
v1 v2 v3 v4 v5 v6 v7 v8
v1 v2
v1 v3
v2 v4
v2 v5
v3 v6
v3 v7
v4 v8
v5 v8
v7 v6
v1
图(b)
8 7
v1 v2 v3 v4 v5 v6 v7 v8
v1 v2
v1 v3
v2 v4
v3 v6
v4 v8
v6 v7
v8 v5
v1
图(a)
8 10
1 2 3 4 5 6 7 8
1 2
1 3
2 4
2 5
3 6
3 7
4 8
5 8
6 8
7 8
1
图(d)
6 7
A B C D E F
A B
A E
B F
B E
F C
F D
D C
图(c)
标签:
原文地址:http://www.cnblogs.com/yfs123456/p/5701369.html