当前位置:网站首页>[implementation of depth first search]
[implementation of depth first search]
2022-06-13 09:14:00 【GAOJIN painting】
The idea of depth first search :
Starting from the initial node , Go to the adjacent node without passing through , Then the tag marks the node as having passed , Taking this node as the starting point , Find the adjacent nodes that have not passed again , When you find that there is no way to go ( The surrounding adjacent nodes have passed ) Then the original path returns to the previous node , See if there are any adjacent nodes that have not passed by , If there is one, go , If not, return to the previous node again and repeat the operation . Finally, the original path returns to the initial node , Then the search is completed .
summary :
(1) Go deep , As long as there is an adjacent node that has not passed through
(2) When there is no way to go , Then the original path returns to the current node , See if there is a way to go , If there is one, go , If you don't, you'll go back the same way
(3) When the original path returns to the initial node , Description search ended . At the same time, it will find its way to go on the way back , When it's over , Is to ensure that all nodes go to ( Connected )
Recursive implementation ( Pseudo code )
void dfs(vertex v)
{
visited[v]=true;
while(v Adjacent nodes of i)
{
if( !visited[i])
dfs(i);
}
}
/*visited[n] Used to record whether the
The details that the above code still needs to deal with are how to loop to all adjacent nodes . Different storage methods may lead to different
Time complexity : Number of nodes N, Number of edges E
The main operations of depth first search are
1.visited[v]=true; Access once for each node
2. Every node v Adjacent nodes of i The interview of
matrix :1 operation :N Nodes N Time 2 operation : For each current node, all nodes will be accessed , common N*N Time
O(N+N*N)=O(N*N)
Linked list :1 operation :N Nodes N Time 2 operation : Each current node accesses its own neighboring nodes , common 2*E Time
O(N+2*E)=O(N+E)
边栏推荐
- Simulink如何添加模块到Library Browser
- Necessary and sufficient conditions for diagonalization of 20211115 matrix; The full rank matrix does not necessarily have n linearly independent eigenvectors; Symmetric matrices must be diagonalized
- 20211104 为什么矩阵的迹等于特征值之和,为什么矩阵的行列式等于特征值之积
- 20211006 integral, differential and projection belong to linear transformation
- C language: callback function
- ROS2之OpenCV人脸识别foxy~galactic~humble
- 图数据库Neo4j介绍
- 20211006 积分、微分、投影均属于线性变换
- Z字形变换
- QML(06)——qml. Add a new folder under QRC
猜你喜欢
final 原理
图数据库Neo4j介绍
20220606 Young's inequality for Matrices
Online debugging tool Arthas advanced
BGP Federation +community
Redis fuzzy query batch deletion
Exporting MySQL data table documents using Navicat
Yolov5 face learning notes
Longadder of the source code of JUC atomic accumulator
Detailed explanation of C language callback function
随机推荐
final 原理
Library management system based on wechat applet Rar (thesis + source code)
JUC atomic accumulator
20211028 adjustment and tracking
JUC field Updater
攻防世界-PWN-shell
20211004 矩阵的子空间
Routing - static routing
Summary of the first retrospective meeting
图数据库Neo4j介绍
Exporting MySQL data table documents using Navicat
turtle库的使用数字时钟模拟时钟动态显示
JUC Unsafe
turtle库显示系统时间
How many TCP connections can a machine create at most?
LeetCode 1143. 最长公共子序列
20211104 为什么矩阵的迹等于特征值之和,为什么矩阵的行列式等于特征值之积
共享模型之不可变
20211006 linear transformation
QML(06)——qml. Add a new folder under QRC