当前位置:网站首页>Depth first search DFS (adjacency matrix and adjacency table version)
Depth first search DFS (adjacency matrix and adjacency table version)
2022-04-21 18:57:00 【Brienzz】
Adjacency matrix version
#include<iostream>
using namespace std;
const int MAXV = 1000;// Maximum number of vertices
const int INF = 1000000000;// set up INF For a large number
int n=5;//n For the top points , This is set to 5, Respectively 0,1,2,3,4
int G[MAXV][MAXV] = {// Store the adjacency matrix of the graph ,MAXV Is the maximum number of vertices
{INF,1,INF,INF,INF},
{INF,INF,1,1,INF},
{INF,1,INF,1,1},
{1,1,1,INF,1},
{1,INF,1,1,INF}
};
bool vis[MAXV] = {false};// If the vertex i Has been interviewed , be vis[i]==true. The initial value is false
void DFS(int u,int depth){//u Label the currently accessed vertex ,depth For depth
vis[u] = true;// Set up u Has been interviewed
// If necessary u Do something , It can be done here
// Here's for all u Start to enumerate the branch vertices that can be reached
cout<<" The summit is "<<u<<" "<<" Depth is "<<depth<<endl;// Yes u For the output
for(int v=0;v<n;v++){// For each vertex v
if(vis[v]==false&&G[u][v]!=INF) // If v Not visited , And u You can achieve v
DFS(v,depth+1);// visit v, Depth plus 1
}
}
// Ergodic graph
void DFSTrave(){ // Ergodic graph G
for(int u=0;u<n;u++){// For each vertex u
if(vis[u]==false){// If u Not visited
DFS(u,1); // visit u and u Connected block of the party ,1 Indicates that the initial is the first layer
}
}
}
int main() {
DFS(2,1);
}
Adjacent tables
#include<iostream>
#include<vector>
using namespace std;
const int MAXV = 1000;// Maximum number of vertices
int n;//n For the top points
vector<int> Adj[MAXV];// chart G Adjacency list
bool vis[MAXV] = {false};// If the vertex i Has been interviewed , be vis[i]==true. The initial value is false
void DFS(int u,int depth){//u Label the currently accessed vertex ,depth For depth
vis[u] = true;// Set up u Has been interviewed
/* If necessary u Do something , You can do it here */
for(int i=0;i<Adj[u].size();i++){// From the u All the peaks that can be reached by departure v
int v = Adj[u][v];
if(vis[v] = false){// If v Not accessed
DFS(v,depth+1);// visit v, Depth plus 1
}
}
}
// Ergodic graph G
void DFSTrave(){
for(int u=0;u<n;u++){// For each vertex u
if(vis[u]==false){// If u Not visited
DFS(u,1);// visit u and u The connecting block ,1 Indicates that the initial test is the first layer
}
}
}
int main() {
DFS(0,1);// From the top 0 Start depth first traversal
}
版权声明
本文为[Brienzz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211854125628.html
边栏推荐
- Finally, wechat scanning code login is completed. It's really fragrant..
- The interviewer asked me about my understanding of the transaction isolation mechanism? That's my answer
- Chinese NER Using Lattice LSTM 论文解读
- leetcode:423. 从英文中重建数字
- ubutnu安装go
- 数据库进阶学习:索引介绍及Btree,B+tree,hash
- 重大活动评论狂欢活动链路建设
- 美国IBM研究院Payel Das等人NMI论文:优化分子的通用型机器学习框架
- JS——new Date()实例
- 少儿编程培训发展的重要趋势
猜你喜欢

What brand of wireless headphones has the best quality? Recommended Bluetooth headset with good reputation

有什么蓝牙耳机不贵又实用?适合学生党的无线蓝牙耳机

【王道考研3】OSI七层参考模型,TCP/IP参考模型和5层参考模型

Ubuntu install go

Navicat MySQL连接Linux下MySQL的及2003错误解决方案

Crystal Chem活性 GIP ELISA 试剂盒说明书

Excel表格快速生成LaTeX

Use the replay function of chrome to publish a blog quickly

面试官问我谈谈对事务隔离机制的理解?我是这样回答的

MySQL去重查询
随机推荐
LeetCode1765. 地图中的最高点(BFS)
《Tensorflow 从基础到实战》00 张量,会话,变量,矩阵
This thing is called a jump watch?
牛客 - 另类加法
JVM 类加载机制
Use the replay function of chrome to publish a blog quickly
一文读懂Seek Tiger推出创世节点的意义
Understand the new economic model of platofarm and its ecological progress
Typescript quick start, class, public, private, extensions
[JS learning notes 40] complex factory mode
Abbexa MPO (FITC) / CD3 (PE) 组合抗体
《Vite学习指南---基于腾讯云Webify部署项目》视频课程上线“云+社区”
[Wangdao postgraduate entrance examination 3] OSI seven layer reference model, TCP / IP reference model and five layer reference model
Pretreatment problem
Finally, wechat scanning code login is completed. It's really fragrant..
Read the meaning of seek tiger's launch of Genesis node
数据库进阶学习:存储引擎
编程中的Context(上下文)
Kotlin | these things you should know about lazy
How to adjust the speed and speed of preview video in PR;