当前位置:   article > 正文

BFS(基于C语言的简单实现)_bfs代码实现c语言

bfs代码实现c语言

BFS(基于C语言的简单实现)

  • 把根结点放到队列的末尾

  • 每次从队列的头部取出一个元素,查看这个元素所有的下一级元素,把它们放到队列的末尾。并把这个元素标记为它下一级元素的前驱

  • 找到所要找的元素时结束程序

  • 如果遍历整个树都还没有找到,则结束程序

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX_VALUE 4
  4. int visit[MAX_VALUE];
  5. typedef struct ArcNode
  6. {
  7. int adjvex;
  8. struct ArcNode*nextarc;
  9. }ArcNode;
  10. typedef struct VNode
  11. {
  12. int data;
  13. ArcNode*firstarc;
  14. }VNode,AdjList[MAX_VALUE];
  15. typedef struct
  16. {
  17. AdjList vertices;
  18. int vexnum, arcnum;
  19. }ALGraph;
  20. typedef struct queue
  21. {
  22. int *pBase;
  23. int front, rear;
  24. }QUEUE;
  25. int LocateVex(ALGraph G, int v);
  26. void CreatUDG(ALGraph *G);
  27. void PrintUDG(ALGraph G);
  28. void init_queue(QUEUE*Q);
  29. bool isfull_queue(QUEUE*Q);
  30. bool isempty_queue(QUEUE*Q);
  31. void in_queue(QUEUE*Q,int val);
  32. int out_queue(QUEUE*Q);
  33. void BFS(ALGraph G,QUEUE*Q);
  34. void BFST(ALGraph G, QUEUE *Q);
  35. int main()
  36. {
  37. ALGraph G;
  38. QUEUE Q;
  39. CreatUDG(&G);
  40. PrintUDG(G);
  41. BFST(G, &Q);
  42. return 0;
  43. }
  44. int LocateVex(ALGraph G, int v)
  45. {
  46. for (int i = 0; i < G.vexnum; i++)
  47. {
  48. if (G.vertices[i].data == v)
  49. {
  50. return i;
  51. }
  52. }
  53. }
  54. void CreatUDG(ALGraph *G)
  55. {
  56. ArcNode*p, *q;
  57. int i, j, v1, v2;
  58. printf("分别输入顶点个数和边的个数:\n");
  59. scanf("%d%d", &(G->vexnum), &(G->arcnum));
  60. printf("请输入各个顶点的值:\n");
  61. for (int i = 0; i < G->vexnum; i++)
  62. {
  63. scanf("%d", &(G->vertices[i].data));
  64. G->vertices[i].firstarc = NULL;
  65. }
  66. printf("分别输入各条边的两个顶点:\n");
  67. for (int k = 0; k < G->arcnum; k++)
  68. {
  69. scanf("%d%d", &v1, &v2);
  70. i = LocateVex(*G, v1);
  71. j = LocateVex(*G, v2);
  72. p = (ArcNode*)malloc(sizeof(ArcNode));
  73. p->adjvex = j;
  74. p->nextarc = NULL;
  75. p->nextarc = G->vertices[i].firstarc;
  76. G->vertices[i].firstarc = p;
  77. q = (ArcNode*)malloc(sizeof(ArcNode));
  78. q->adjvex = i;
  79. q->nextarc = NULL;
  80. q->nextarc = G->vertices[j].firstarc;
  81. G->vertices[j].firstarc = q;
  82. }
  83. }
  84. void PrintUDG(ALGraph G)
  85. {
  86. ArcNode*p = NULL;
  87. for (int i = 0; i < G.vexnum; i++)
  88. {
  89. printf("第%d条边\n", i + 1);
  90. p = G.vertices[i].firstarc;
  91. while (p != NULL)
  92. {
  93. printf("%d ", (p->adjvex) + 1);
  94. p = p->nextarc;
  95. }
  96. printf("\n");
  97. }
  98. }
  99. void init_queue(QUEUE*Q)
  100. {
  101. Q->pBase = (int*)malloc((sizeof(int))*4);
  102. Q->front = 0;
  103. Q->rear = 0;
  104. }
  105. bool isfull_queue(QUEUE*Q)
  106. {
  107. if (((Q->rear + 1) % MAX_VALUE) == Q->front)
  108. {
  109. return true;
  110. }
  111. else
  112. return false;
  113. }
  114. bool isempty_queue(QUEUE*Q)
  115. {
  116. if (Q->rear == Q->front)
  117. {
  118. return true;
  119. }
  120. else
  121. return false;
  122. }
  123. void in_queue(QUEUE*Q, int val)
  124. {
  125. if (isfull_queue(Q))
  126. {
  127. return;
  128. }
  129. Q->pBase[Q->rear] = val;
  130. Q->rear = (Q->rear + 1) % MAX_VALUE;
  131. //printf("入对元素为%d\n", val);
  132. }
  133. int out_queue(QUEUE*Q)
  134. {
  135. int temp = 0;
  136. if (isempty_queue(Q))
  137. return 0;
  138. temp = Q->pBase[Q->front];
  139. Q->front = (Q->front + 1) % MAX_VALUE;
  140. //printf("\n返回的值为%d\n", temp);
  141. return temp;
  142. }
  143. void BFS(ALGraph G,QUEUE*Q)
  144. {
  145. init_queue(Q);
  146. //自己总结: 注意 下面的这两条语句要放在开头或者在下面的for循环里面在加上条件
  147. //判断visit数组是否访问过 否则就会当while循环退出的时候 就会继续执行printf("%d ", G.vertices[i].data)
  148. //会导致多输出结果
  149. visit[0] = 1;
  150. printf("%d ", G.vertices[0].data);
  151. for (int i = 0; i < G.vexnum; i++)
  152. {
  153. //visit[i] = 1;
  154. //printf("%d ", G.vertices[i].data);
  155. in_queue(Q,i);
  156. while (!isempty_queue(Q))
  157. {
  158. int temp=out_queue(Q);
  159. ArcNode*p = G.vertices[temp].firstarc;
  160. while (p!=NULL)
  161. {
  162. if (visit[p->adjvex]==0)
  163. {
  164. visit[p->adjvex] = 1;
  165. printf("%d ", G.vertices[p->adjvex].data);
  166. in_queue(Q, p->adjvex);
  167. }
  168. p = p->nextarc;
  169. }
  170. }
  171. }
  172. }
  173. void BFST(ALGraph G, QUEUE *Q)
  174. {
  175. for (int i = 0; i < G.vexnum; i++)
  176. {
  177. if (!visit[i])
  178. {
  179. BFS(G, Q);
  180. }
  181. }
  182. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/445379
推荐阅读
相关标签
  

闽ICP备14008679号