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) ...
随机推荐
- Language Oriented Programming:下一代编程样式 Part I (翻译)
原文信息 原文地址 作者信息 Sergey Dmitriev JetBrains Sergey Dmitriev is the cofounder and CEO of JetBrains Inc., ...
- 2.1.4synchronized方法与锁对象
为了证明线程锁的是对象 测试 package com.cky.bean; /** * Created by chenkaiyang on 2017/12/4. */ public class MyOb ...
- C++之引用和指针
作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7050431.html C++之引用和指针 C++引用 引用的基本用法: in ...
- gcc和vs在c的一些区别
1.switch中每个标签后面的命令在gcc中需要{}括起来以指明作用域. 2._itoa是非标准的c和c++扩展函数,在linux下可以使用sprintf(string, "%d &q ...
- 【MySQL】死锁问题分析
1.MySQL常用存储引擎的锁机制: MyISAM和MEMORY采用表级锁(table-level locking) BDB采用页面锁(page-level locking)或表级锁,默认为页面锁 ...
- Django URLs error: view must be a callable or a list/tuple in the case of include()
Django 1.10 no longer allows you to specify views as a string (e.g. 'myapp.views.home') in your URL ...
- NoSQL数据库的分布式算法
本文译自 Distributed Algorithms in NoSQL Databases 系统的可扩展性是推动NoSQL运动发展的的主要理由,包含了分布式系统协调,故障转移,资源管理和许多其他特性 ...
- 中大 9095. Islands
9095. Islands 限制条件 时间限制: 2 秒, 内存限制: 256 兆 题目描述 Whenever it rains, Farmer John's field always ends up ...
- C++中的四种类型转换
//1.常见的类型转换,使用static_cast float f = 1.234; int i =static_cast<int>(f);//等价于 int i = (int)f; // ...
- 免 Google Play 的安卓应用下载平台
本文已过时,以后会在我的新博客内更新 https://blog.clso.fun/posts/2019-03-22/non-google-play-apk-download.html 鉴于那啥你懂的原 ...