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) ...
随机推荐
- GIS矢量数据化简:一种改进的道格拉斯-普克算法以及C++实现
GIS领域的同志都知道,传统的道格拉斯-普克算法都是递归实现.然而有时候递归的层次太深的话会出现栈溢出的情况.在此,介绍一种非递归的算法. 要将递归算法改为非递归算法,一般情况下分为两种场景.第一种是 ...
- css 特殊使用技巧
1 border颜色设置 border-color: transparent black black black; 分别设置四条边框的颜色 上边transparent 透明无色 2 阴影 t ...
- LoadRunner改脚本
加action,不支持嵌套大括号 Action(){int iCt = 0; iCt = lr_output_message("abcdefg"); iCt = funA(3,5) ...
- 去除html页面中GET《 http://localhost:8080/favicon.ico 404 (Not Found)》
html5页面中经常会遇见以下问题,总是看着碍眼,那么怎么解决呢? GET http://localhost:8080/favicon.ico 404 (Not Found) 解决的方法: <l ...
- 我们学习Java今后的发展前景
Java应用的前景 Java技术自1995年问世以来,在我国的应用和开发也得到了迅速普及,总体来看,这 些应用主要集中于企业应用开发.据有关单位调查显示,从开发领域的分布情况上看Web 开发占 ...
- php开发工具,zendstudio13使用方法补丁
官网原版下载http://downloads.zend.com/studio ... win32.win32.x86.exe 破解补丁:链接:http://pan.baidu.com/s/1gdi4U ...
- 一致性hash(整理版)
简单解释: 简单解释一致性hash的原理:网上通篇都是用服务器做的举例,我这里也如此,主要是便于理解. 通常:有N个客户端请求服务器,假设有M台web服务器,通常为了均衡访问会进行N%M的取模,然后分 ...
- Maven 集成Tomcat7插件
Maven已经是Java的项目管理标配,如何在JavaEE开发使用Maven调用Web应用,是很多同学关心的问题.本文将介绍,Maven如何介绍Tomcat插件. Maven Tomcat插件现在主要 ...
- C#开发邮件收发(同步)
发邮件界面: 收邮件界面: 先分析邮件发送类 邮件发送类使用smtp协议,这里以QQ邮箱为例 using System; using System.Collections.Generic; using ...
- ArrayBlockingQueue源码解析(2)
此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 3.3.public void put(E e) throws InterruptedException 原 ...