当前位置:网站首页>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;
}
边栏推荐
- 【web審計-源碼泄露】獲取源碼方法,利用工具
- 特殊版:SpreadJS v15.1 VS SpreadJS v15.0
- 优先使用对象组合,而不是类继承
- 【做题打卡】集成每日5题分享(第三期)
- Anti debugging (basic principles of debugger Design & NT NP and other anti debugging principles)
- [system security] ten thousand words summary system virtualization container bottom layer principle experiment
- 输入的查询SQL语句,是如何执行的?
- Google Chrome CSS will not update unless the cache is cleared - Google Chrome CSS doesn't update unless clear cache
- [PHP features - variable coverage] improper use, improper configuration and code logic vulnerability of the function
- Installation of postman and postman interceptor
猜你喜欢
阿里云ECS使用cloudfs4oss挂载OSS
Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?
UI自動化測試從此告別手動下載瀏覽器驅動
Blue Bridge Cup single chip microcomputer -- PWM pulse width modulation
[web source code code code audit method] audit skills and tools
Quick start of UI component development of phantom engine [umg/slate]
UE4 DMX和grandMA2 onPC 3.1.2.5的操作流程
Pyqt5 displays file names and pictures
深度学习——LSTM基础
ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 1)
随机推荐
A brief introduction to the behavior tree of unity AI
[punch in questions] integrated daily 5-question sharing (phase III)
Special Edition: spreadjs v15.1 vs spreadjs v15.0
Yuancosmic ecological panorama [2022 latest]
English essential vocabulary 3400
[groovy] string (string splicing | multi line string)
What is test development? Why do so many companies hire test developers now?
UE4 DMX和grandMA2 onPC 3.1.2.5的操作流程
DMX parameter exploration of grandma2 onpc 3.1.2.5
Use object composition in preference to class inheritance
我就一写代码的,王总整天和我谈格局...
[web source code code code audit method] audit skills and tools
As soon as I write the code, President Wang talks with me about the pattern all day
[安洵杯 2019]不是文件上传
EasyCVR平台出现WebRTC协议视频播放不了是什么原因?
反絮凝剂-氨碘肽滴眼液
[software reverse - basic knowledge] analysis method, assembly instruction architecture
[web Audit - source code disclosure] obtain source code methods and use tools
MindFusion.Virtual Keyboard for WPF
Unity implements the code of the attacked white flash (including shader)