图的基本遍历算法的实现(BFS & DFS)复习
- #include <stdio.h>
- #define INF 32767
- typedef struct MGraph{
- char vertexs[];
- int edge[][];
- int ver_num, edge_num;
- }MGraph;
- void create_graph(MGraph *graph)
- {
- int i, j;
- getchar();
- for(i = ; i < graph->ver_num; ++i)
- scanf("%c%*c",&(graph->vertexs[i]));
- for(i = ; i < graph->ver_num; ++i)
- {
- for(j = ; j < graph->ver_num; ++j)
- {
- if(i == j)
- graph->edge[i][i] = ;
- else
- graph->edge[i][j] = INF;
- }
- }
- int vi, vj, val;
- for(i = ; i < graph->edge_num; ++i)
- {
- scanf("%d %d %d", &vi, &vj, &val);
- graph->edge[vi][vj] = graph->edge[vj][vi] = val;
- }
- }
- void print_graph(MGraph graph)
- {
- int i,j;
- for(i = ; i < graph.ver_num; ++i)
- {
- for(j = ; j < graph.ver_num; ++j)
- {
- printf("%d\t", graph.edge[i][j]);
- }
- puts("");
- }
- }
- int vis[];
- void dfs(MGraph graph, int vertex)
- {
- int i;
- vis[vertex] = ; //将该结点标记为访问过
- printf("%d -> %c\t", vertex, graph.vertexs[vertex]); //打印结点信息,当然也可以是其他操作
- for(i = ; i < graph.ver_num; ++i)
- { //从vertex 到 i 有路径存在,而且 i 结点未被访问过
- if(graph.edge[vertex][i] > && graph.edge[vertex][i] != INF && !vis[i])
- dfs(graph, i); //进行dfs遍历
- }
- }
- void dfstraverse(MGraph graph)
- {
- int i;
- for(i = ; i < graph.ver_num; ++i) //将所有结点标记为未访问过
- vis[i] = ;
- for(i = ; i < graph.ver_num; ++i) //从每个未被访问过的结点开始进行dfs遍历
- if(!vis[i])
- dfs(graph, i);
- }
- typedef struct Queue{
- int store[];
- int front, rear;
- }Queue;
- void InitQueue(Queue *queue)
- {
- queue->front = queue->rear = ;
- }
- void EnQueue(Queue *queue , int i)
- {
- if(queue->rear == )
- puts("the queue is full!!");
- else{
- queue->store[queue->rear] = i;
- queue->rear++;
- }
- }
- int DeQueue(Queue *queue)
- {
- if(queue->rear == queue->front)
- printf("the queue is empty!!");
- else{
- queue->front++;
- return queue->store[queue->front-];
- }
- }
- Queue queue; //定义队列
- void bfs(MGraph graph, int vertex)
- {
- int i, tmp;
- vis[vertex] = ;
- printf("%d -> %c\t",vertex, graph.vertexs[vertex]);
- EnQueue(&queue, vertex);
- while(queue.front != queue.rear)
- {
- tmp = DeQueue(&queue);
- for(i = ; i < graph.ver_num; ++i)
- {
- if(graph.edge[tmp][i] > && graph.edge[tmp][i] != INF && !vis[i])
- {
- vis[i] = ;
- printf("%d -> %c\t", i, graph.vertexs[i]);
- EnQueue(&queue, i);
- }
- }
- }
- }
- void bfstraverse(MGraph graph)
- {
- int i;
- InitQueue(&queue);
- for(i = ; i < graph.ver_num; ++i)
- vis[i] = ;
- for(i = ; i < graph.ver_num; ++i)
- if(!vis[i])
- bfs(graph, i);
- }
- int main()
- {
- MGraph graph;
- scanf("%d %d", &(graph.ver_num), &(graph.edge_num));
- create_graph(&graph);
- print_graph(graph);
- dfstraverse(graph);
- puts("");
- bfstraverse(graph);
- return ;
- }
- /*
- 4 4
- 0 1 2 3
- 0 1 9
- 1 2 8
- 2 3 7
- 3 0 6
- * */
基于邻接矩阵的 DFS & BFS
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct ArcNode{
- int adjVex; //邻接点域
- struct ArcNode *next; //指针域
- }ArcNode; //边表
- typedef struct VertexNode{
- int data; //顶点域
- struct VertexNode *firstArc;//边表头指针
- }VertexNode, AdjList[]; //顶点表
- typedef struct {
- AdjList adjlist;
- int ArcNum, VertexNum; //图中的顶点数和边数
- }AdjListGraph;
- /*
- * 创建的是无向图,采用头插法建立边表
- * */
- int create_graph(AdjListGraph *graph)
- {
- int i, j;
- for(i = ; i < graph->VertexNum; ++i)
- {
- graph->adjlist[i].data = i;
- graph->adjlist[i].firstArc = NULL;
- }
- int m, n, val;
- for(i = ; i < graph->ArcNum; ++i)
- {
- puts("the every arc");
- scanf("%d %d", &m, &n);
- ArcNode *tmp1 = (ArcNode*)malloc(sizeof(ArcNode));
- if(tmp1 == NULL)
- return ;
- tmp1->adjVex = n;
- tmp1->next = graph->adjlist[m].firstArc;
- graph->adjlist[m].firstArc = tmp1;
- ArcNode *tmp2 = (ArcNode*)malloc(sizeof(ArcNode));
- if(tmp2 == NULL)
- return ;
- tmp2->adjVex = m;
- tmp2->next = graph->adjlist[n].firstArc;
- graph->adjlist[n].firstArc = tmp2;
- }
- return ;
- }
- int vis[];
- void dfs(AdjListGraph *graph, int vertex)
- {
- ArcNode *p;
- vis[vertex] = ;
- printf("%d\t", graph->adjlist[vertex].data);
- p = graph->adjlist[vertex].firstArc;
- while(p)
- {
- if(!vis[p->adjVex])
- dfs(graph, p->adjVex);
- p = p->next;
- }
- }
- void dfstreverse(AdjListGraph *graph)
- {
- int i;
- for(i = ; i < graph->VertexNum; ++i)
- {
- vis[i] = ;
- }
- for(i = ; i < graph->VertexNum; ++i)
- {
- if(!vis[i])
- dfs(graph, i);
- }
- }
- typedef struct Queue{
- int store[];
- int front, rear;
- }Queue;
- void InitQueue(Queue *queue)
- {
- queue->front = queue->rear=;
- }
- void EnQueue(Queue *queue , int i)
- {
- if(queue->rear == )
- puts("the queue is full!!");
- else{
- queue->store[queue->rear] = i;
- queue->rear++;
- }
- }
- int DeQueue(Queue *queue)
- {
- if(queue->rear == queue->front)
- printf("the queue is empty!!");
- else{
- queue->front++;
- return queue->store[queue->front-];
- }
- }
- Queue queue;
- void bfs(AdjListGraph *graph, int vertex)
- {
- int i, tmp;
- ArcNode *p;
- vis[vertex] = ;
- printf("%d\t", graph->adjlist[vertex].data);
- EnQueue(&queue, vertex);
- while(queue.front != queue.rear)
- {
- tmp = DeQueue(&queue);
- p = graph->adjlist[tmp].firstArc;
- while(p)
- {
- if(!vis[p->adjVex])
- {
- vis[p->adjVex] = ;
- printf("%d\t", graph->adjlist[p->adjVex].data);
- EnQueue(&queue, p->adjVex);
- }
- p = p->next;
- }
- }
- }
- void bfstreverse(AdjListGraph *graph)
- {
- InitQueue(&queue);
- int i;
- for(i = ; i < graph->VertexNum; ++i)
- vis[i] = ;
- for(i = ; i < graph->VertexNum; ++i)
- if(!vis[i])
- bfs(graph, i);
- }
- int main()
- {
- AdjListGraph graph;
- int arc_num, ver_num;
- puts("input the arc_num & ver_num\n");
- scanf("%d %d", &arc_num, &ver_num);
- graph.ArcNum = arc_num;
- graph.VertexNum = ver_num;
- create_graph(&graph);
- dfstreverse(&graph);
- bfstreverse(&graph);
- return ;
- }
基于邻接表的 DFS & BFS
手写又不熟了,多多看,多多练!
图的基本遍历算法的实现(BFS & DFS)复习的更多相关文章
- 图的广度优先遍历算法(BFS)
在上一篇文章我们用java演示了图的数据结构以及图涉及到的深度优先遍历算法,本篇文章将继续演示图的广度优先遍历算法.广度优先遍历算法主要是采用了分层的思想进行数据搜索.其中也需要使用另外一种数据结构队 ...
- 图的创建和遍历(BFS/DFS)
图的表示方法主要有邻接矩阵和邻接表.其中邻接表最为常用,因此这里便以邻接表为例介绍一下图的创建及遍历方法. 创建图用到的结构有两种:顶点及弧 struct ArcNode { int vertexIn ...
- 图的深度优先遍历(DFS)和广度优先遍历(BFS)
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 图的深度优先遍历算法(DFS)
搜索算法有很多种,本次文章主要分享图(无向图)的深度优先算法.深度优先算法(DFS)主要是应用于搜索中,早期是在爬虫中使用.其主要的思想有如下: 1.先访问一个节点v,然后标记为已被访问过2.找到第一 ...
- 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)
数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...
- 算法导论—无向图的遍历(BFS+DFS,MATLAB)
华电北风吹 天津大学认知计算与应用重点实验室 最后改动日期:2015/8/22 无向图的存储方式有邻接矩阵,邻接链表,稀疏矩阵等. 无向图主要包括双方面内容,图的遍历和寻找联通分量. 一.无向图的遍历 ...
- 图的深度优先遍历DFS
图的深度优先遍历是树的前序遍历的应用,其实就是一个递归的过程,我们人为的规定一种条件,或者说一种继续遍历下去的判断条件,只要满足我们定义的这种条件,我们就遍历下去,当然,走过的节点必须记录下来,当条件 ...
- 图的深度优先遍历(DFS)—递归算法
实验环境:win10, DEV C++5.11 实验要求: 实现图的深度优先遍历 实验代码: #include <iostream> #define maxSize 255 #includ ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
随机推荐
- Android获取位置信息的方法总结
1.位置服务的简介:位置服务,英文翻译为Location-Based Services,缩写为LBS,又称为定位服务或基于位置的服务,融合了GPS定位.移动通信.导航等多种技术,提供与空间位置相关的综 ...
- 使用CSS隐藏HTML元素的4种常用方法
现在的网页设计越来越动态化,我们经常需要隐藏某些元素,在特定的时候才显示它们.我们通常可以使用4种方法来隐藏和显示元素. 这4种显示和隐藏元素的技术各自有它们自己的优点的缺点,下面来举例说明. 在这篇 ...
- PX 和PT的区别
字体大小的设置单位,常用的有2种:px.pt.这两个有什么区别呢? 先搞清基本概念:px就是表示pixel,像素,是屏幕上显示数据的最基本的点: pt就是point,是印刷行业常用单位,等于1/72英 ...
- 【Python图像】给你的头像+1
早些年,微信朋友圈有段时间非常流行这个头像+1的套路,简直逼死强迫症. 将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果. 类似于图中效果 涉及知识: Pyt ...
- Android中锁定文件的方法
androidSDK中并没有锁定文件相关的api. 但是android是基于linux操作系统的,linux比较底层,灵活性也更大,为了实现锁定文件的效果,大概有以下几种办法: 用chmod命令修改文 ...
- Linux kernel 文件夹说明
Linux 内核代码的目录结构 arch: 包含和硬件体系相关的的代码,每种硬件平台占一个相应的目录,如i386,arm, arm64, powerpc,mips等. block: 块设备驱动程序I/ ...
- TBitmapSurface.StretchFrom
procedure TBitmapSurface.StretchFrom(const Source: TBitmapSurface; const NewWidth, NewHeight: Intege ...
- MySQL 5.7 Replication 相关新功能说明
背景: MySQL5.7在主从复制上面相对之前版本多了一些新特性,包括多源复制.基于组提交的并行复制.在线修改Replication Filter.GTID增强.半同步复制增强等.因为都是和复制相关, ...
- 在Ubuntu 14.04安装和使用Docker
Docker是一个开源软件,它可以把一个Linux应用和它所依赖的一切(比如配置文件)都封装到一个容器.然而,Docker与虚拟机不同,它使用了沙箱机制,Docker容器不运行操作系统,它共享主机上的 ...
- nginx优化
此文章非原创,出自鸟哥之手~ http://blog.chinaunix.net/uid-25266990-id-2985541.html 改排版改得多,当然红色部分要注意下,用得较多 ------- ...