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

函数基础学习02

grandMA2 onPC 3.1.2.5的DMX参数摸索

【web源码-代码审计方法】审计技巧及审计工具

Rust区块琏开发——签名加密与私钥公钥

This article takes you to understand the relationship between the past and present of Bi and the digital transformation of enterprises

About the recent experience of writing questions

The architect started to write a HelloWorld

程序员的视力怎么样? | 每日趣闻

在线SQL转Excel(xls/xlsx)工具

An elegant program for Euclid‘s algorithm
随机推荐
Unity implements the code of the attacked white flash (including shader)
Difference between MotionEvent. getRawX and MotionEvent. getX
[wp][入门]刷弱类型题目
[learning notes] month end operation -gr/ir reorganization
[an Xun cup 2019] not file upload
Interview summary: This is a comprehensive & detailed Android interview guide
JVM garbage collection
error Couldn‘t find a package.json file in “你的路径“
About the recent experience of writing questions
Thread Basics
Official announcement! The third cloud native programming challenge is officially launched!
Resolved (sqlalchemy+pandas.read_sql) attributeerror: 'engine' object has no attribute 'execution_ options‘
[system security] ten thousand words summary system virtualization container bottom layer principle experiment
已解决(sqlalchemy+pandas.read_sql)AttributeError: ‘Engine‘ object has no attribute ‘execution_options‘
Use object composition in preference to class inheritance
KVM virtualization
优先使用对象组合,而不是类继承
How to define a unified response object gracefully
【无标题】
UI自動化測試從此告別手動下載瀏覽器驅動