15 图-图的遍历-基于邻接矩阵实现的BFS与DFS算法
算法分析和具体步骤解说直接写在代码注释上了
TvT 没时间了等下还要去洗衣服 就先不赘述了
有不明白的欢迎留言交流!(估计是没人看的了)
直接上代码:
#include<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
typedef struct{
int Vex[];//顶点表
int Edge[][];
int vexnum,arcnum;
}MGraph;
bool visited[];
void printGraph(MGraph &G)
{
cout<<"Show the Graph."<<endl;
cout<<"----------------------------"<<endl;
for(int i=;i<;i++){
for(int j=;j<;j++)
{
cout<<" "<<G.Edge[i][j]<<" ";
}
cout<<endl;
}
cout<<"----------------------------"<<endl;
cout<<"There are "<<G.arcnum<<" arcs in the Graph!"<<endl;
cout<<"Arcs are listed as follow:"<<endl;
for(int i=;i<;i++){
for(int j=;j<;j++)
{
if(G.Edge[i][j]==)
cout<<i<<"-->"<<j<<endl;
}
}
}
int initGraph(MGraph &G)
{
G.vexnum=;
G.arcnum=;
for(int i=;i<;i++)
for(int j=;j<;j++)
{
G.Edge[i][j]=;
}
cout<<"input the relation of a pair of dots (A,B) to build a graph.(0<A,B<10)"<<endl;
cout<<"End up with data(0,0)"<<endl;
int a,b;
cin>>a>>b;
while(a!=&&b!=){
G.arcnum++;
G.Edge[a][b]=;
cin>>a>>b;
}
cout<<"successful."<<endl;
printGraph(G);
}
bool isNeibour(MGraph &G,int a,int b)
{
if(G.Edge[a][b]==)
return true;
else return false;
}
int firstNeighbor(MGraph &G,int v)
{
int pos=-;
for(int i=;i<G.vexnum;i++)
{
if((G.Edge[v][i]==))
{pos=i;break;}
}
return pos;
}
int nextNeighbor(MGraph &G,int v,int w)
{
int pos=-;
for(int i=w+;i<G.vexnum;i++)
{
if((G.Edge[v][i]==))
{
pos=i;break;
}
}
return pos;
}
void visit(int v)
{
visited[v]=true;
cout<<v<<" ";
}
queue<int> Q;
void bfs(MGraph G,int v)
{
visit(v);
Q.push(v);
int now;
while(!Q.empty())
{
now=Q.front();
Q.pop();
for(int w=firstNeighbor(G,now);w>=;w=nextNeighbor(G,now,w))
{
if(!visited[w])
{
visit(w);
Q.push(now);
}
}
}
}
void BFS(MGraph &G)
{
for(int i=;i<G.vexnum;i++)
visited[i]=false;
for(int i=;i<G.vexnum;i++)
{
if(!visited[i])
bfs(G,i);
}
}
/*DFS 深度优先搜索*/
/*类似于树的先序遍历
其搜索策略:尽可能【深】地搜索一个图。
遍历思想:
从一个起始顶点v出发,访问与v邻接但未被访问的任一顶点a→继续访问a顶点的下一未被访问的顶点b→……→无法再向下访问时依次退回到最近被访问的顶点
直到所有顶点都被访问为止
*/
void dfs(MGraph &G,int v)
{
visit(v);
int w;
for(w=firstNeighbor(G,v);w>=;w=nextNeighbor(G,v,w))
{
if(!visited[w])
{
dfs(G,w);
}
}
}
void DFS(MGraph &G)
{
for(int i=;i<G.vexnum;i++)
{
visited[i]=false;
}
for(int i=;i<G.vexnum;i++)
{
if(!visited[i])
dfs(G,i);
}
}
int main()
{
MGraph P;
initGraph(P);
cout<<"test of BFS:"<<endl;
BFS(P);
cout<<endl;
cout<<"test of DFS:"<<endl;
DFS(P);
return ;
}
附一张运行截图

15 图-图的遍历-基于邻接矩阵实现的BFS与DFS算法的更多相关文章
- 图的全部实现(邻接矩阵 邻接表 BFS DFS 最小生成树 最短路径等)
1 /** 2 * C: Dijkstra算法获取最短路径(邻接矩阵) 3 * 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> ...
- 数据结构实验之图论二:图的深度遍历(SDUT 2107)(简单DFS)
题解:图的深度遍历就是顺着一个最初的结点开始,把与它相邻的结点都找到,也就是一直往下搜索直到尽头,然后在顺次找其他的结点. #include <bits/stdc++.h> using n ...
- 图的基本操作(基于邻接矩阵):图的构造,深搜(DFS),广搜(BFS)
#include <iostream> #include <stdio.h> #include <cstdlib> #include <cstring> ...
- 基于邻接矩阵的深度优先搜索(DFS)
题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2107&cid=1186 #include<stdio.h> #incl ...
- 邻接矩阵实现Dijkstra算法以及BFS与DFS算法
//============================================================================ // Name : MatrixUDG.c ...
- PTA 邻接矩阵存储图的深度优先遍历
6-1 邻接矩阵存储图的深度优先遍历(20 分) 试实现邻接矩阵存储图的深度优先遍历. 函数接口定义: void DFS( MGraph Graph, Vertex V, void (*Visit)( ...
- 算法学习 - 图的广度优先遍历(BFS) (C++)
广度优先遍历 广度优先遍历是非经常见和普遍的一种图的遍历方法了,除了BFS还有DFS也就是深度优先遍历方法.我在我下一篇博客里面会写. 遍历过程 相信每一个看这篇博客的人,都能看懂邻接链表存储图. 不 ...
- C语言实现邻接矩阵创建无向图&图的深度优先遍历
/* '邻接矩阵' 实现无向图的创建.深度优先遍历*/ #include <stdio.h> #include <stdlib.h> #define MaxVex 100 // ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
随机推荐
- day32(表单校验js和jquery表单校验)
校验用户名.密码.密码一直性. <style> .error { color: red } .success { color: green } </style> <scr ...
- js-图片时间困难版(倒计时)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- bootstrap4相关文档
本节课我们主要学习一下Bootstrap的两个个组件功能:输入框组件和导航导航条组件. 一.输入框组件 文本输入框就是可以在<input>元素前后加上文字或按钮,可以实现对表单控件的扩 展 ...
- iOS开发中与库相关的术语
动态库 VS 静态库 Static frameworks are linked at compile time. Dynamic frameworks are linked at runtime
- 10.Date对象
Date()对象 Date对象用于处理日期和时间. Math对象 ◆Math.ceil() 天花板函数 向上取整 ★如果是整数,取整之后是这个数本身 ★如果是小数,对数进行向上舍入. ◆Ma ...
- easyUi DataGrid 显示日期列,时间为空也可,的正常显示,及普通居中列情况
$('#tt').datagrid({ url: '@Url.Content("~/kpi/FindList")', w ...
- 加密算法(DES,AES,RSA,MD5,SHA1,Base64)比较和项目应用(转载)
加密技术通常分为两大类:"对称式"和"非对称式". 对称性加密算法:对称式加密就是加密和解密使用同一个密钥.信息接收双方都需事先知道密匙和加解密算法且其密匙是相 ...
- 企业项目开发--cookie(1)
此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 注:本章代码基于<第五章 企业项目开发--mybatis注解与xml并用>的代码,链接如下: h ...
- JQuery Mobile - html5+CSS 禁止IOS长按复制粘贴实现
因为在移动端APP需要实现长按执行别的事件,但是在IOS系统有默认的长按选择复制粘贴,禁止此功能在网上找了很多资料,最终整理出目前最好的解决方法.实际测试,也并不是很理想,但是可能没有更好办法了! / ...
- 【062有新题】OCP 12c 062出现大量之前没有的新考题-16
choose one Which users are created and can be used for database and host management of your DBaaS da ...