图的深度优先遍历(DFS)和广度优先遍历(BFS)
body, table{font-family: 微软雅黑; font-size: 13.5pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}
右图是递归遍历的过程,其实每一层都是从A结点开始搜寻满足条件的点 |
|
/* DFS.h */
#ifndef __DFS_H__
#define __DFS_H__
#include"Graph.h"
namespace meihao
{
void DFS(const meihao::Graph& g,int vi,bool*& visited); //参数->图和顶点数组中某个顶点的下标
void DFSTraversal(const meihao::Graph& g);
};
#endif
/* testMain.cpp */ #include"DFS.h"
#include<iostream>
int main()
{
meihao::Graph g("data.txt");
meihao::DFSTraversal(g);
cout<<endl;
system("pause");
}
/* data.txt */ 9
A B C D E F G H I
0 1 0 0 0 1 0 0 0
1 0 1 0 0 0 1 0 1
0 1 0 1 0 0 0 0 1
0 0 1 0 1 0 1 1 1
0 0 0 1 0 1 0 1 0
1 0 0 0 1 0 1 0 0
0 1 0 1 0 1 0 1 0
0 0 0 1 1 0 1 0 0
0 1 1 1 0 0 0 0 0
|
/* DFS.cpp */
#include"DFS.h"
namespace meihao
{
//算法都是基于邻接矩阵实现的
void DFS(const meihao::Graph& g,int vi,bool*& visited)
{
visited[vi] = true; //修改第vi个结点的访问标记为true
cout<<g.getGraphVertexData(vi)<<" ";
for(int idx=0;idx!=g.getGraphVertexNumber();++idx)
{
if(1==g.getGraphEdgeWeight(vi,idx)&&
false==visited[idx]) //如果(vi,idx)之间存在边(==1),并且第idx个顶点还没有访问过
{
DFS(g,idx,visited); //递归遍历第idx个顶点
}
}
}
void DFSTraversal(const meihao::Graph& g)
{
bool* visited = new bool[g.getGraphVertexNumber()]();
for(int idx=0;idx!=g.getGraphVertexNumber();++idx)
{
visited[idx] = false; //初始化访问标记,全部为false,表示未访问
}
for(int idx=0;idx!=g.getGraphVertexNumber();++idx)
{
if(false==visited[idx]) //随便选一个点,如果未访问过,就从它开始深度优先遍历
DFS(g,idx,visited);
}
}
};
|
/* BFS.h */
#ifndef __BFS_H__
#define __BFS_H__
#include"Graph.h"
namespace meihao
{
void BFSTraversal(const meihao::Graph& g);
};
#endif
/* test.cpp */ #include"BFS.h"
#include<iostream>
int main()
{
meihao::Graph g("data.txt");
meihao::BFSTraversal(g);
cout<<endl;
system("pause");
}
/* data.txt */
9
A B C D E F G H I
0 1 0 0 0 1 0 0 0
1 0 1 0 0 0 1 0 1
0 1 0 1 0 0 0 0 1
0 0 1 0 1 0 1 1 1
0 0 0 1 0 1 0 1 0
1 0 0 0 1 0 1 0 0
0 1 0 1 0 1 0 1 0
0 0 0 1 1 0 1 0 0
0 1 1 1 0 0 0 0 0
|
/* BFS.cpp */
#include"BFS.h"
#include<queue>
namespace meihao
{
void BFSTraversal(const meihao::Graph& g)
{ //广度优先遍历相当于层序遍历
queue<int> rootNode; //存放图的顶点
bool* visited = new bool[g.getGraphVertexNumber()];
for(int idx=0;idx!=g.getGraphVertexNumber();++idx)
{
visited[idx] = false;
}
for(int idx=0;idx!=g.getGraphVertexNumber();++idx)
{ //if语句可以确保如果图中有多个连通分量,也能每个点都访问到
if(false==visited[idx]) //如果该结点没有访问到
{
//访问
cout<<g.getGraphVertexData(idx)<<" ";
visited[idx] = true;
rootNode.push(idx);
while(!rootNode.empty()) //把刚刚访问到的结点的下一层结点访问并入队列
{
for(int iidx=0;iidx!=g.getGraphVertexNumber();++iidx)
{
if(1==g.getGraphEdgeWeight(rootNode.front(),iidx)&&
false==visited[iidx])
{
cout<<g.getGraphVertexData(iidx)<<" ";
visited[iidx] = true;
rootNode.push(iidx);
}
}
rootNode.pop(); //最先访问的一个结点出队列
}
}
}
}
};
|
图的深度优先遍历(DFS)和广度优先遍历(BFS)的更多相关文章
- 图的深度优先搜索(DFS)和广度优先搜索(BFS)算法
深度优先(DFS) 深度优先遍历,从初始访问结点出发,我们知道初始访问结点可能有多个邻接结点,深度优先遍历的策略就是首先访问第一个邻接结点,然后再以这个被访问的邻接结点作为初始结点,访问它的第一个邻接 ...
- 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)
深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...
- 深度优先搜索DFS和广度优先搜索BFS简单解析
转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 图的深度优先遍历(DFS)和广度优先遍历(BFS)算法分析
1. 深度优先遍历 深度优先遍历(Depth First Search)的主要思想是: 1.首先以一个未被访问过的顶点作为起始顶点,沿当前顶点的边走到未访问过的顶点: 2.当没有未访问过的顶点时,则回 ...
- 深度优先搜索DFS和广度优先搜索BFS
DFS简介 深度优先搜索,一般会设置一个数组visited记录每个顶点的访问状态,初始状态图中所有顶点均未被访问,从某个未被访问过的顶点开始按照某个原则一直往深处访问,访问的过程中随时更新数组visi ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【C++】基于邻接矩阵的图的深度优先遍历(DFS)和广度优先遍历(BFS)
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- 图的深度优先搜索dfs
图的深度优先搜索: 1.将最初访问的顶点压入栈: 2.只要栈中仍有顶点,就循环进行下述操作: (1)访问栈顶部的顶点u: (2)从当前访问的顶点u 移动至顶点v 时,将v 压入栈.如果当前顶点u 不存 ...
随机推荐
- 2018年浙江理工大学程序设计竞赛校赛 Problem I: 沙僧
沙僧 思路: dfs序+差分数组 分层考虑,通过dfs序来查找修改的区间段,然后用差分数组修改 代码: #include<bits/stdc++.h> using namespace st ...
- Mac 如何安装 chromedriver
1.使用brew 命令 前提是要安装 brew 这里不做介绍 使用命令 brew install chromedriver 如上图出现错误,根据提示可以使用如下命令安装 brew cask inst ...
- Python - requests https请求的坑
#-*-coding:utf-8-*- # Time:2017/9/25 20:41 # Author:YangYangJun import requests import ssl from requ ...
- python中的静态方法、类方法、属性方法(福利:关于几种方法更好的解释)
该部分的三个属性都是高级方法,平时用的地方不是很多 一.静态方法 静态方法的使用不是很多,可以理解的就看一下,用的地方不是很多 class Dog(object): def __init__(self ...
- centos php5.4 升级 php7
接上篇,edusoho需要php5.5以上版本,于是需要升级本地php php是通过yum默认安装的.以下安装参考 link https://blog.csdn.net/u012569217/arti ...
- WARNING: CPU: 0 PID: 1 at ./arch/x86/include/asm/fpu/internal.h:373
------------[ cut here ]------------WARNING: CPU: 0 PID: 1 at ./arch/x86/include/asm/fpu/internal.h: ...
- vue 点击一个div,使input获得焦点
<div class="inputMessage" @click="inputMessage">输入留言</div> <input ...
- Pytorch半精度浮点型网络训练问题
用Pytorch1.0进行半精度浮点型网络训练需要注意下问题: 1.网络要在GPU上跑,模型和输入样本数据都要cuda().half() 2.模型参数转换为half型,不必索引到每层,直接model. ...
- Linux中常用压缩打包工具
Linux中常用压缩打包工具 压缩打包是常用的功能,在linux中目前常用的压缩工具有gzip,bzip2以及后起之秀xz.本文将介绍如下的工具常见压缩.解压缩工具以及打包工具tar. gzip2 直 ...
- bzoj3884: 上帝与集合的正确用法 扩展欧拉定理
题意:求\(2^{2^{2^{2^{...}}}}\%p\) 题解:可以发现用扩展欧拉定理不需要很多次就能使模数变成1,后面的就不用算了 \(a^b\%c=a^{b\%\phi c} gcd(b,c) ...