算法分析和具体步骤解说直接写在代码注释上了

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算法的更多相关文章

  1. 图的全部实现(邻接矩阵 邻接表 BFS DFS 最小生成树 最短路径等)

    1 /** 2 * C: Dijkstra算法获取最短路径(邻接矩阵) 3 * 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> ...

  2. 数据结构实验之图论二:图的深度遍历(SDUT 2107)(简单DFS)

    题解:图的深度遍历就是顺着一个最初的结点开始,把与它相邻的结点都找到,也就是一直往下搜索直到尽头,然后在顺次找其他的结点. #include <bits/stdc++.h> using n ...

  3. 图的基本操作(基于邻接矩阵):图的构造,深搜(DFS),广搜(BFS)

    #include <iostream> #include <stdio.h> #include <cstdlib> #include <cstring> ...

  4. 基于邻接矩阵的深度优先搜索(DFS)

    题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2107&cid=1186 #include<stdio.h> #incl ...

  5. 邻接矩阵实现Dijkstra算法以及BFS与DFS算法

    //============================================================================ // Name : MatrixUDG.c ...

  6. PTA 邻接矩阵存储图的深度优先遍历

    6-1 邻接矩阵存储图的深度优先遍历(20 分) 试实现邻接矩阵存储图的深度优先遍历. 函数接口定义: void DFS( MGraph Graph, Vertex V, void (*Visit)( ...

  7. 算法学习 - 图的广度优先遍历(BFS) (C++)

    广度优先遍历 广度优先遍历是非经常见和普遍的一种图的遍历方法了,除了BFS还有DFS也就是深度优先遍历方法.我在我下一篇博客里面会写. 遍历过程 相信每一个看这篇博客的人,都能看懂邻接链表存储图. 不 ...

  8. C语言实现邻接矩阵创建无向图&图的深度优先遍历

    /* '邻接矩阵' 实现无向图的创建.深度优先遍历*/ #include <stdio.h> #include <stdlib.h> #define MaxVex 100 // ...

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

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

随机推荐

  1. js中的事件代理(委托)

    1,什么是事件委托:通俗的讲,事件就是onclick,onmouseover,onmouseout,等就是事件,委托呢,就是让别人来做,这个事件本来是加在某些元素上的,然而你却加到别人身上来做,完成这 ...

  2. CentOS 利用Yum安装mysql后无法启动(MySQL Daemon failed to start.)

    安装mysql-server [root@iZwz9cl4i8oy1reej7o8pmZ ~]# yum install -y mysql-server 进入/etc执行mysql_install_d ...

  3. 2017-12-24 自定义view相关学习

    学习材料: http://blog.csdn.net/u010661782/article/details/52805870 http://blog.csdn.net/chengyingzhilian ...

  4. 敏捷项目管理工具-Trello(电子看板)

    Trello简介(https://www.trello.com) A Trello board is a list of lists, filled with cards, used by you a ...

  5. 网络通信协议简介(TCP与UDP)

    通过计算机网络可以使多台计算机实现连接,位于同一个网络中的计算机在进行连接和通信时需要遵守一定的规则,这就好比在道路中行驶的汽车一定要遵守交通规则一样.在计算机网络中,这些连接和通信的规则被称为网络通 ...

  6. Codeforces Round #535 (Div. 3) 1108C - Nice Garland

    #include <bits/stdc++.h> using namespace std; int main() { #ifdef _DEBUG freopen("input.t ...

  7. [ 9.9 ]CF每日一题系列—— 259A黑白棋盘检查问题

    http://codeforces.com/problemset/problem/259/A PS9.8日做了但是忘了发博客,所以坚持3天了呦~ 终于读懂了这个题了,心累 Describe: 给你8 ...

  8. 二分图匹配-HK算法

    先把代码贴上,其他南京回来再补了.. #include <cstdio> #include <cstdlib> #include <cstring> #includ ...

  9. AngularJS 单元测试 Karma jasmine

    当AngularJS项目越来越大时候,需要进行单元测试,可以先开发功能再进行测试,也可以先进行测试. 一.karma  是一个基于Node.js(先要安装)的JavaScript测试执行过程管理工具( ...

  10. AngularJS 启动执行过程

    一.浏览器下载HTML/CSS/JavaScript等 当你转到一个页面地址后,浏览器先回下载这个HTML,同时,会开启一些辅助线程下载所关联的script标签和link标签里引用的文件. 二.浏览器 ...