当前位置:网站首页>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);
}
*/
边栏推荐
- IMS data channel concept of 5g vonr+
- Servicemesh mainly solves three pain points
- U++ 游戏类 学习笔记
- ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
- Leetcode(46)——全排列
- JS variable case output user name
- 当 Knative 遇见 WebAssembly
- U++4 接口 学习笔记
- Sublime tips
- Error: No named parameter with the name ‘foregroundColor‘
猜你喜欢
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
最长回文子串(动态规划)
Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
Decorator basic learning 02
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
Auto.js 获取手机所有app名字
【opencv】图像形态学操作-opencv标记不同连通域的位置
[736. LISP syntax parsing]
ThinkPHP关联预载入with
Pointer and array are input in function to realize reverse order output
随机推荐
Weebly mobile website editor mobile browsing New Era
Leetcode longest public prefix
01 machine learning related regulations
Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
JS input and output
《二》标签
《四》表单
《五》表格
Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
记录一次压测经验总结
SQL injection cookie injection
If you‘re running pod install manually, make sure flutter pub get is executed first.
National meteorological data / rainfall distribution data / solar radiation data /npp net primary productivity data / vegetation coverage data
5G VoNR+之IMS Data Channel概念
ThinkPHP关联预载入with
Leetcode(46)——全排列
ScheduledExecutorService定时器
为什么很多人对技术债务产生误解
JS variable case output user name
AOSP ~Binder 通信原理 (一) - 概要