1. #include <stdio.h>
  2.  
  3. #define INF 32767
  4. typedef struct MGraph{
  5. char vertexs[];
  6. int edge[][];
  7. int ver_num, edge_num;
  8. }MGraph;
  9.  
  10. void create_graph(MGraph *graph)
  11. {
  12. int i, j;
  13. getchar();
  14. for(i = ; i < graph->ver_num; ++i)
  15. scanf("%c%*c",&(graph->vertexs[i]));
  16.  
  17. for(i = ; i < graph->ver_num; ++i)
  18. {
  19. for(j = ; j < graph->ver_num; ++j)
  20. {
  21. if(i == j)
  22. graph->edge[i][i] = ;
  23. else
  24. graph->edge[i][j] = INF;
  25. }
  26. }
  27. int vi, vj, val;
  28. for(i = ; i < graph->edge_num; ++i)
  29. {
  30. scanf("%d %d %d", &vi, &vj, &val);
  31. graph->edge[vi][vj] = graph->edge[vj][vi] = val;
  32. }
  33. }
  34.  
  35. void print_graph(MGraph graph)
  36. {
  37. int i,j;
  38. for(i = ; i < graph.ver_num; ++i)
  39. {
  40. for(j = ; j < graph.ver_num; ++j)
  41. {
  42. printf("%d\t", graph.edge[i][j]);
  43. }
  44. puts("");
  45. }
  46. }
  47.  
  48. int vis[];
  49. void dfs(MGraph graph, int vertex)
  50. {
  51. int i;
  52. vis[vertex] = ; //将该结点标记为访问过
  53. printf("%d -> %c\t", vertex, graph.vertexs[vertex]); //打印结点信息,当然也可以是其他操作
  54.  
  55. for(i = ; i < graph.ver_num; ++i)
  56. { //从vertex 到 i 有路径存在,而且 i 结点未被访问过
  57. if(graph.edge[vertex][i] > && graph.edge[vertex][i] != INF && !vis[i])
  58. dfs(graph, i); //进行dfs遍历
  59. }
  60. }
  61.  
  62. void dfstraverse(MGraph graph)
  63. {
  64. int i;
  65. for(i = ; i < graph.ver_num; ++i) //将所有结点标记为未访问过
  66. vis[i] = ;
  67. for(i = ; i < graph.ver_num; ++i) //从每个未被访问过的结点开始进行dfs遍历
  68. if(!vis[i])
  69. dfs(graph, i);
  70. }
  71.  
  72. typedef struct Queue{
  73. int store[];
  74. int front, rear;
  75. }Queue;
  76.  
  77. void InitQueue(Queue *queue)
  78. {
  79. queue->front = queue->rear = ;
  80. }
  81. void EnQueue(Queue *queue , int i)
  82. {
  83. if(queue->rear == )
  84. puts("the queue is full!!");
  85. else{
  86. queue->store[queue->rear] = i;
  87. queue->rear++;
  88. }
  89. }
  90.  
  91. int DeQueue(Queue *queue)
  92. {
  93. if(queue->rear == queue->front)
  94. printf("the queue is empty!!");
  95. else{
  96. queue->front++;
  97. return queue->store[queue->front-];
  98. }
  99. }
  100.  
  101. Queue queue; //定义队列
  102. void bfs(MGraph graph, int vertex)
  103. {
  104. int i, tmp;
  105. vis[vertex] = ;
  106. printf("%d -> %c\t",vertex, graph.vertexs[vertex]);
  107. EnQueue(&queue, vertex);
  108.  
  109. while(queue.front != queue.rear)
  110. {
  111. tmp = DeQueue(&queue);
  112. for(i = ; i < graph.ver_num; ++i)
  113. {
  114. if(graph.edge[tmp][i] > && graph.edge[tmp][i] != INF && !vis[i])
  115. {
  116. vis[i] = ;
  117. printf("%d -> %c\t", i, graph.vertexs[i]);
  118. EnQueue(&queue, i);
  119. }
  120. }
  121. }
  122. }
  123.  
  124. void bfstraverse(MGraph graph)
  125. {
  126. int i;
  127. InitQueue(&queue);
  128. for(i = ; i < graph.ver_num; ++i)
  129. vis[i] = ;
  130. for(i = ; i < graph.ver_num; ++i)
  131. if(!vis[i])
  132. bfs(graph, i);
  133. }
  134.  
  135. int main()
  136. {
  137. MGraph graph;
  138. scanf("%d %d", &(graph.ver_num), &(graph.edge_num));
  139. create_graph(&graph);
  140. print_graph(graph);
  141. dfstraverse(graph);
  142. puts("");
  143. bfstraverse(graph);
  144. return ;
  145. }
  146. /*
  147. 4 4
  148. 0 1 2 3
  149. 0 1 9
  150. 1 2 8
  151. 2 3 7
  152. 3 0 6
  153. * */

基于邻接矩阵的 DFS & BFS

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct ArcNode{
  6. int adjVex; //邻接点域
  7. struct ArcNode *next; //指针域
  8. }ArcNode; //边表
  9.  
  10. typedef struct VertexNode{
  11. int data; //顶点域
  12. struct VertexNode *firstArc;//边表头指针
  13. }VertexNode, AdjList[]; //顶点表
  14.  
  15. typedef struct {
  16. AdjList adjlist;
  17. int ArcNum, VertexNum; //图中的顶点数和边数
  18. }AdjListGraph;
  19. /*
  20. * 创建的是无向图,采用头插法建立边表
  21. * */
  22. int create_graph(AdjListGraph *graph)
  23. {
  24. int i, j;
  25. for(i = ; i < graph->VertexNum; ++i)
  26. {
  27. graph->adjlist[i].data = i;
  28. graph->adjlist[i].firstArc = NULL;
  29. }
  30. int m, n, val;
  31. for(i = ; i < graph->ArcNum; ++i)
  32. {
  33. puts("the every arc");
  34. scanf("%d %d", &m, &n);
  35. ArcNode *tmp1 = (ArcNode*)malloc(sizeof(ArcNode));
  36. if(tmp1 == NULL)
  37. return ;
  38. tmp1->adjVex = n;
  39. tmp1->next = graph->adjlist[m].firstArc;
  40. graph->adjlist[m].firstArc = tmp1;
  41.  
  42. ArcNode *tmp2 = (ArcNode*)malloc(sizeof(ArcNode));
  43. if(tmp2 == NULL)
  44. return ;
  45. tmp2->adjVex = m;
  46. tmp2->next = graph->adjlist[n].firstArc;
  47. graph->adjlist[n].firstArc = tmp2;
  48. }
  49. return ;
  50. }
  51. int vis[];
  52. void dfs(AdjListGraph *graph, int vertex)
  53. {
  54. ArcNode *p;
  55. vis[vertex] = ;
  56. printf("%d\t", graph->adjlist[vertex].data);
  57. p = graph->adjlist[vertex].firstArc;
  58.  
  59. while(p)
  60. {
  61. if(!vis[p->adjVex])
  62. dfs(graph, p->adjVex);
  63. p = p->next;
  64. }
  65. }
  66. void dfstreverse(AdjListGraph *graph)
  67. {
  68. int i;
  69. for(i = ; i < graph->VertexNum; ++i)
  70. {
  71. vis[i] = ;
  72. }
  73. for(i = ; i < graph->VertexNum; ++i)
  74. {
  75. if(!vis[i])
  76. dfs(graph, i);
  77. }
  78. }
  79.  
  80. typedef struct Queue{
  81. int store[];
  82. int front, rear;
  83. }Queue;
  84.  
  85. void InitQueue(Queue *queue)
  86. {
  87. queue->front = queue->rear=;
  88. }
  89. void EnQueue(Queue *queue , int i)
  90. {
  91. if(queue->rear == )
  92. puts("the queue is full!!");
  93. else{
  94. queue->store[queue->rear] = i;
  95. queue->rear++;
  96. }
  97. }
  98.  
  99. int DeQueue(Queue *queue)
  100. {
  101. if(queue->rear == queue->front)
  102. printf("the queue is empty!!");
  103. else{
  104. queue->front++;
  105. return queue->store[queue->front-];
  106. }
  107. }
  108.  
  109. Queue queue;
  110. void bfs(AdjListGraph *graph, int vertex)
  111. {
  112. int i, tmp;
  113. ArcNode *p;
  114. vis[vertex] = ;
  115. printf("%d\t", graph->adjlist[vertex].data);
  116.  
  117. EnQueue(&queue, vertex);
  118. while(queue.front != queue.rear)
  119. {
  120. tmp = DeQueue(&queue);
  121. p = graph->adjlist[tmp].firstArc;
  122. while(p)
  123. {
  124. if(!vis[p->adjVex])
  125. {
  126. vis[p->adjVex] = ;
  127. printf("%d\t", graph->adjlist[p->adjVex].data);
  128. EnQueue(&queue, p->adjVex);
  129. }
  130. p = p->next;
  131. }
  132.  
  133. }
  134. }
  135.  
  136. void bfstreverse(AdjListGraph *graph)
  137. {
  138.  
  139. InitQueue(&queue);
  140. int i;
  141. for(i = ; i < graph->VertexNum; ++i)
  142. vis[i] = ;
  143. for(i = ; i < graph->VertexNum; ++i)
  144. if(!vis[i])
  145. bfs(graph, i);
  146. }
  147. int main()
  148. {
  149. AdjListGraph graph;
  150. int arc_num, ver_num;
  151. puts("input the arc_num & ver_num\n");
  152. scanf("%d %d", &arc_num, &ver_num);
  153. graph.ArcNum = arc_num;
  154. graph.VertexNum = ver_num;
  155. create_graph(&graph);
  156. dfstreverse(&graph);
  157. bfstreverse(&graph);
  158. return ;
  159. }

基于邻接表的 DFS & BFS

手写又不熟了,多多看,多多练!

图的基本遍历算法的实现(BFS & DFS)复习的更多相关文章

  1. 图的广度优先遍历算法(BFS)

    在上一篇文章我们用java演示了图的数据结构以及图涉及到的深度优先遍历算法,本篇文章将继续演示图的广度优先遍历算法.广度优先遍历算法主要是采用了分层的思想进行数据搜索.其中也需要使用另外一种数据结构队 ...

  2. 图的创建和遍历(BFS/DFS)

    图的表示方法主要有邻接矩阵和邻接表.其中邻接表最为常用,因此这里便以邻接表为例介绍一下图的创建及遍历方法. 创建图用到的结构有两种:顶点及弧 struct ArcNode { int vertexIn ...

  3. 图的深度优先遍历(DFS)和广度优先遍历(BFS)

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  4. 图的深度优先遍历算法(DFS)

    搜索算法有很多种,本次文章主要分享图(无向图)的深度优先算法.深度优先算法(DFS)主要是应用于搜索中,早期是在爬虫中使用.其主要的思想有如下: 1.先访问一个节点v,然后标记为已被访问过2.找到第一 ...

  5. 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)

    数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...

  6. 算法导论—无向图的遍历(BFS+DFS,MATLAB)

    华电北风吹 天津大学认知计算与应用重点实验室 最后改动日期:2015/8/22 无向图的存储方式有邻接矩阵,邻接链表,稀疏矩阵等. 无向图主要包括双方面内容,图的遍历和寻找联通分量. 一.无向图的遍历 ...

  7. 图的深度优先遍历DFS

    图的深度优先遍历是树的前序遍历的应用,其实就是一个递归的过程,我们人为的规定一种条件,或者说一种继续遍历下去的判断条件,只要满足我们定义的这种条件,我们就遍历下去,当然,走过的节点必须记录下来,当条件 ...

  8. 图的深度优先遍历(DFS)—递归算法

    实验环境:win10, DEV C++5.11 实验要求: 实现图的深度优先遍历 实验代码: #include <iostream> #define maxSize 255 #includ ...

  9. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

随机推荐

  1. Android获取位置信息的方法总结

    1.位置服务的简介:位置服务,英文翻译为Location-Based Services,缩写为LBS,又称为定位服务或基于位置的服务,融合了GPS定位.移动通信.导航等多种技术,提供与空间位置相关的综 ...

  2. 使用CSS隐藏HTML元素的4种常用方法

    现在的网页设计越来越动态化,我们经常需要隐藏某些元素,在特定的时候才显示它们.我们通常可以使用4种方法来隐藏和显示元素. 这4种显示和隐藏元素的技术各自有它们自己的优点的缺点,下面来举例说明. 在这篇 ...

  3. PX 和PT的区别

    字体大小的设置单位,常用的有2种:px.pt.这两个有什么区别呢? 先搞清基本概念:px就是表示pixel,像素,是屏幕上显示数据的最基本的点: pt就是point,是印刷行业常用单位,等于1/72英 ...

  4. 【Python图像】给你的头像+1

    早些年,微信朋友圈有段时间非常流行这个头像+1的套路,简直逼死强迫症. 将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果. 类似于图中效果 涉及知识: Pyt ...

  5. Android中锁定文件的方法

    androidSDK中并没有锁定文件相关的api. 但是android是基于linux操作系统的,linux比较底层,灵活性也更大,为了实现锁定文件的效果,大概有以下几种办法: 用chmod命令修改文 ...

  6. Linux kernel 文件夹说明

    Linux 内核代码的目录结构 arch: 包含和硬件体系相关的的代码,每种硬件平台占一个相应的目录,如i386,arm, arm64, powerpc,mips等. block: 块设备驱动程序I/ ...

  7. TBitmapSurface.StretchFrom

    procedure TBitmapSurface.StretchFrom(const Source: TBitmapSurface; const NewWidth, NewHeight: Intege ...

  8. MySQL 5.7 Replication 相关新功能说明

    背景: MySQL5.7在主从复制上面相对之前版本多了一些新特性,包括多源复制.基于组提交的并行复制.在线修改Replication Filter.GTID增强.半同步复制增强等.因为都是和复制相关, ...

  9. 在Ubuntu 14.04安装和使用Docker

    Docker是一个开源软件,它可以把一个Linux应用和它所依赖的一切(比如配置文件)都封装到一个容器.然而,Docker与虚拟机不同,它使用了沙箱机制,Docker容器不运行操作系统,它共享主机上的 ...

  10. nginx优化

    此文章非原创,出自鸟哥之手~ http://blog.chinaunix.net/uid-25266990-id-2985541.html 改排版改得多,当然红色部分要注意下,用得较多 ------- ...