当前位置:网站首页>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);
}
*/
边栏推荐
- 高数中值定理总结
- When knative meets webassembly
- 想要选择一些部门优先使用 OKR, 应该如何选择试点部门?
- 线程池的创建与使用
- Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
- 一文搞懂常见的网络I/O模型
- Function pointer and pointer function in C language
- 5G VoNR+之IMS Data Channel概念
- Monitoring cannot be started after Oracle modifies the computer name
- App embedded H5 --- iPhone soft keyboard blocks input text
猜你喜欢
Auto.js 获取手机所有app名字
A simple and beautiful regression table is produced in one line of code~
一个酷酷的“幽灵”控制台工具
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
[Yugong series] go teaching course 005 variables in July 2022
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
U++ metadata specifier learning notes
Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
【问道】编译原理
Tree map: tree view - draw covid-19 array diagram
随机推荐
Addressable 预下载
批量归一化(标准化)处理
Stm32f103ze+sht30 detection of ambient temperature and humidity (IIC simulation sequence)
Batch normalization (Standardization) processing
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
Clickhouse (03) how to install and deploy Clickhouse
National meteorological data / rainfall distribution data / solar radiation data /npp net primary productivity data / vegetation coverage data
Development thoughts of adding new requirements in secondary development
PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
3GPP信道模型路损基础知识
Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
Pointer and array are input in function to realize reverse order output
【PHP SPL笔记】
Leetcode(46)——全排列
qt 简单布局 盒子模型 加弹簧
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
Redis如何实现多可用区?
Techniques d'utilisation de sublime
If you‘re running pod install manually, make sure flutter pub get is executed first.