当前位置:网站首页>DFS,BFS以及图的遍历搜索
DFS,BFS以及图的遍历搜索
2022-07-06 23:08:00 【疯疯癫癫才自由】
1)DFS的基本写法:
/**
* \1) DFS的基本写法,可做模板使用
*/
/**
DFS(int index,int nowK,int sum)
{
if(满足条件)
return;
if(nowK,sum的取值是满足条件的情况下不可能的值)
return;
if(judge(index))
{
这里可以加一些约束条件,已经访问了这个节点,就将这个节点设置为已访问;
DFS(index+1);
在这儿可以进行回溯;
}
}
*/
2) BFS的基本写法
/**
2) BFS的基本写法
*/
/**
BFS(int index)
{
queue<int> q;
q.push(index);
inq[index]=true; //这儿需要注意,inq是结点是否入队,而不是是是否访问,否则会造成
while(!q.empty()) //多个相同的结点入队,这与DFS的vis函数是有区别的;
{
int top=q.front();
q.pop();
for(...)
{
将top的下一层结点的元素入队;
将入队的元素设置为已入队;
}
}
}
*/
3)图的深度优先遍历搜索(DFS)
/**
3)图的深度优先遍历搜索(DFS)
*/
/**
邻接矩阵版:
int G[maxv][maxv],maxn;
bool vis[maxv];
void DFS(index) //index为当前访问的顶点标号
{
vis[index]=true; //将index设置为已访问
for(int i=0;i<maxn;++i) //访问与index联通的点
{
if(G[index][i]!=INF&&vis[i]!=true)
DFS(i);
}
}
void DFSTrave()
{
for(int i=0;i<maxn;++i)
if(vis[i]==false)
DFS(i);
}
*/
/**
邻接表版:
int maxn,maxv;
vector<int> adj[maxv];
bool vis[maxv];
void DFS(index) //index为当前访问的顶点标号
{
vis[index]=true; //将index设置为已访问
for(int i=0;i<adj[index].size();++i)
{
int v=adj[index][i]; //注意这里是取得是adj[index]里的值
if(vis[v]==false)
DFS(v);
}
}
void DFSTrave()
{
for(int i=0;i<maxn;++i)
if(vis[i]==false)
DFS(i);
}
*/
4) 图的广度优先搜索算法
/**
4) 图的广度优先搜索算法
*/
/**
邻接矩阵版:
int G[maxv][maxv],maxn;
bool inq[maxv];
void BFS(index) //遍历index所在的连通块
{
queue<int> q;
q.push(index);
inq[index]=true;
while(!q.empty())
{
int top=q.front();
q.pop();
for(int i=0;i<maxn;++i)
{
if(G[index][i]!=INF&&inq[i]==false)
{
q.push(i);
inq[i]=true;
}
}
}
}
void DFSTrave() //遍历图G
{
for(int i=0;i<maxn;++i)
if(inq[i]==false)
DFS(i);
}
*/
/**
邻接表版:
int maxn,maxv;
vector<int> adj[maxv];
bool inq[maxv];
void BFS(index) //遍历index所在的连通块
{
queue<int> q;
q.push(index);
inq[index]=true;
while(!q.empty())
{
int top=q.front();
q.pop();
for(int i=0;i<adj[index].size();++i)
{
int v=adj[index][i]; //注意这里是取得是adj[index]里的值
if(inq[v]==false)
{
q.push(v);
inq[v]=true;
}
}
}
}
void DFSTrave() //遍历图G
{
for(int i=0;i<maxn;++i)
if(vis[i]==false)
DFS(i);
}
*/
边栏推荐
- Thread和Runnable创建线程的方式对比
- U++4 接口 学习笔记
- Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
- When knative meets webassembly
- Stm32f103ze+sht30 detection of ambient temperature and humidity (IIC simulation sequence)
- Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
- 《四》表单
- JS variable case
- Tree map: tree view - draw covid-19 array diagram
- Talk about the importance of making it clear
猜你喜欢
高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
《五》表格
c语言神经网络基本代码大全及其含义
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
A line of R code draws the population pyramid
当 Knative 遇见 WebAssembly
U++ 元数据说明符 学习笔记
Basic knowledge of road loss of 3GPP channel model
[Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
随机推荐
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
If you‘re running pod install manually, make sure flutter pub get is executed first.
3. Type of fund
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
深入解析Kubebuilder
qt 简单布局 盒子模型 加弹簧
AOSP ~Binder 通信原理 (一) - 概要
01 machine learning related regulations
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
sublime使用技巧
If you‘re running pod install manually, make sure flutter pub get is executed first.
JS input and output
ThinkPHP关联预载入with
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
Why do many people misunderstand technical debt
SQL injection HTTP header injection
IMS data channel concept of 5g vonr+
Batch normalization (Standardization) processing