当前位置:网站首页>DFS, BFS and traversal search of Graphs
DFS, BFS and traversal search of Graphs
2022-07-07 05:09:00 【Madness makes freedom】
1)DFS The basic way of writing :
/**
* \1) DFS The basic way of writing , It can be used as a template
*/
/**
DFS(int index,int nowK,int sum)
{
if( Meet the conditions )
return;
if(nowK,sum The value of is impossible if the conditions are met )
return;
if(judge(index))
{
Some constraints can be added here , This node has been accessed , Set this node as accessed ;
DFS(index+1);
You can go back here ;
}
}
*/2) BFS The basic way of writing
/**
2) BFS The basic way of writing
*/
/**
BFS(int index)
{
queue<int> q;
q.push(index);
inq[index]=true; // Here we need to pay attention to ,inq Is whether the node is in the team , Not whether to visit , Otherwise, it will cause
while(!q.empty()) // Multiple same nodes join the team , This is related to DFS Of vis Functions are different ;
{
int top=q.front();
q.pop();
for(...)
{
take top The elements of the next node of the join the queue ;
Set the queued element to queued ;
}
}
}
*/3) Depth first traversal search of graph (DFS)
/**
3) Depth first traversal search of graph (DFS)
*/
/**
Adjacency matrix version :
int G[maxv][maxv],maxn;
bool vis[maxv];
void DFS(index) //index Label the currently accessed vertex
{
vis[index]=true; // take index Set to visited
for(int i=0;i<maxn;++i) // Interview and index Unicom's point
{
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);
}
*/
/**
Adjacent tables :
int maxn,maxv;
vector<int> adj[maxv];
bool vis[maxv];
void DFS(index) //index Label the currently accessed vertex
{
vis[index]=true; // take index Set to visited
for(int i=0;i<adj[index].size();++i)
{
int v=adj[index][i]; // Note here is the acquisition yes adj[index] In the value of the
if(vis[v]==false)
DFS(v);
}
}
void DFSTrave()
{
for(int i=0;i<maxn;++i)
if(vis[i]==false)
DFS(i);
}
*/4) Breadth first search algorithm for Graphs
/**
4) Breadth first search algorithm for Graphs
*/
/**
Adjacency matrix version :
int G[maxv][maxv],maxn;
bool inq[maxv];
void BFS(index) // Traverse index The connecting block
{
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() // Ergodic graph G
{
for(int i=0;i<maxn;++i)
if(inq[i]==false)
DFS(i);
}
*/
/**
Adjacent tables :
int maxn,maxv;
vector<int> adj[maxv];
bool inq[maxv];
void BFS(index) // Traverse index The connecting block
{
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]; // Note here is the acquisition yes adj[index] In the value of the
if(inq[v]==false)
{
q.push(v);
inq[v]=true;
}
}
}
}
void DFSTrave() // Ergodic graph G
{
for(int i=0;i<maxn;++i)
if(vis[i]==false)
DFS(i);
}
*/边栏推荐
- Auto.js 获取手机所有app名字
- PMP证书有没有必要续期?
- 谈谈讲清楚这件事的重要性
- 全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
- SQL injection - secondary injection and multi statement injection
- [Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
- Error: No named parameter with the name ‘foregroundColor‘
- ClickHouse(03)ClickHouse怎么安装和部署
- Servicemesh mainly solves three pain points
- Mysql database (basic)
猜你喜欢

Time complexity & space complexity

Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)

A simple and beautiful regression table is produced in one line of code~

Basic knowledge of road loss of 3GPP channel model

U++ game learning notes
[email protected] Mapping relatio"/>Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio

当 Knative 遇见 WebAssembly

Batch normalization (Standardization) processing

Tree map: tree view - draw covid-19 array diagram

Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
随机推荐
app内嵌h5---iphone软键盘遮挡输入文字
使用Thread类和Runnable接口实现多线程的区别
Timer创建定时器
2039: [蓝桥杯2022初赛] 李白打酒加强版 (动态规划)
《二》标签
Using thread class and runnable interface to realize the difference between multithreading
ThinkPHP关联预载入with
JS 的 try catch finally 中 return 的执行顺序
Thread和Runnable创建线程的方式对比
指针与数组在函数中输入实现逆序输出
为什么很多人对技术债务产生误解
想要选择一些部门优先使用 OKR, 应该如何选择试点部门?
U++4 接口 学习笔记
Tree map: tree view - draw covid-19 array diagram
If you‘re running pod install manually, make sure flutter pub get is executed first.
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
CentOS 7.9安装Oracle 21c历险记
JS input and output
Talk about the importance of making it clear
The execution order of return in JS' try catch finally