当前位置:网站首页>DFS and BFS concepts of trees and graphs
DFS and BFS concepts of trees and graphs
2022-07-05 03:56:00 【Cherish forever】
1. Depth first traversal enters the recursive call itself every time you search
2. Width first traversal is not recursive, that is, search the next node that the current node can reach until the node is searched
Use Adjacency list
Traverse : Each point will be traversed only once
1. Depth-first traversal .
Search as deep as possible , Go back when you hit the bottom , Until you search all the points .
2. Breadth first traversal .
Search widely from the root node , Search all the points on the first floor every time .
void add(int a,int b) {
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int dfs(int x) {
st[x]=true;
int size=0,sum=1;
for(int i=h[x]; i!=-1; i=ne[i]) {
int j=e[i];
if(st[j]) continue;
int s=dfs(j);
size=max(size,s);
sum+=s;
}
size=max(size,n-sum);
ans=min(size,ans);
return sum;
}
边栏推荐
- Test d'automatisation de l'interface utilisateur télécharger manuellement le pilote du navigateur à partir de maintenant
- 【web審計-源碼泄露】獲取源碼方法,利用工具
- provide/inject
- MySQL winter vacation self-study 2022 11 (9)
- 【看完就懂系列】一文6000字教你从0到1实现接口自动化
- An elegant program for Euclid‘s algorithm
- Nmap user manual learning records
- NPM introduction link symbolic link
- 【PHP特性-变量覆盖】函数的使用不当、配置不当、代码逻辑漏洞
- 英语必备词汇3400
猜你喜欢
[wp]bmzclub writeup of several questions
【软件逆向-基础知识】分析方法、汇编指令体系结构
Why do some programmers change careers before they are 30?
Containerd series - what is containerd?
Interview byte, pass the exam and directly work on three sides. As a result, I found an architect to hang me?
25K 入职腾讯的那天,我特么哭了
[brush questions] BFS topic selection
[web source code code code audit method] audit skills and tools
The new project Galaxy token just announced by coinlist is gal
Yuancosmic ecological panorama [2022 latest]
随机推荐
【PHP特性-变量覆盖】函数的使用不当、配置不当、代码逻辑漏洞
NEW:Devart dotConnect ADO.NET
Google Chrome CSS will not update unless the cache is cleared - Google Chrome CSS doesn't update unless clear cache
一文带你了解BI的前世今身与企业数字化转型的关系
C语言课设:影院售票管理系统
[PHP features - variable coverage] improper use, improper configuration and code logic vulnerability of the function
[charging station]_ Secular wisdom_ Philosophical wisdom _
面试字节,过关斩将直接干到 3 面,结果找了个架构师来吊打我?
Technology sharing swift defense programming
Why do some programmers change careers before they are 30?
Clickhouse物化视图
How to make the listbox scroll automatically when adding a new item- How can I have a ListBox auto-scroll when a new item is added?
已解决(sqlalchemy+pandas.read_sql)AttributeError: ‘Engine‘ object has no attribute ‘execution_options‘
【软件逆向-分析工具】反汇编和反编译工具
What is test development? Why do so many companies hire test developers now?
Basic function learning 02
测试开发是什么?为什么现在那么多公司都要招聘测试开发?
NPM introduction link symbolic link
MySQL winter vacation self-study 2022 11 (9)
Web components series (VII) -- life cycle of custom components