当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

laravel8 导出Excle文件

A brief introduction to the behavior tree of unity AI

C # use awaiter

Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?

MindFusion.Virtual Keyboard for WPF

我就一写代码的,王总整天和我谈格局...

UI automation test farewell to manual download of browser driver

KVM virtualization

【刷题】BFS题目精选

An elegant program for Euclid‘s algorithm
随机推荐
[groovy] string (string splicing | multi line string)
Unity implements the code of the attacked white flash (including shader)
为什么百度、阿里这些大厂宁愿花25K招聘应届生,也不愿涨薪5K留住老员工?
An elegant program for Euclid‘s algorithm
Nmap user manual learning records
Clickhouse synchronization MySQL (based on materialization engine)
[an Xun cup 2019] not file upload
测试开发是什么?为什么现在那么多公司都要招聘测试开发?
Binary heap implementation (priority queue implementation)
Anti debugging (basic principles of debugger Design & NT NP and other anti debugging principles)
【web审计-源码泄露】获取源码方法,利用工具
EasyCVR更改录像存储路径,不生成录像文件如何解决?
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
UI automation test farewell to manual download of browser driver
反絮凝剂-氨碘肽滴眼液
KVM virtualization
Analysis of glibc strlen implementation mode
天干地支纪年法中为什么是60年一个轮回,而不是120年
laravel8 导出Excle文件
Some enterprise interview questions of unity interview