当前位置:网站首页>[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)
边栏推荐
- Simple implementation of database link pool
- 谨记! 写代码别太自信! 一定要写点关键日志info输出,不然问题都定位不到。
- 20211018 some special matrices
- LeetCode 5259. 计算应缴税款总额
- C language: shortcut keys commonly used in VS
- 20211020 段院士全驱系统
- Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5
- strcpy_ S precautions for use. (do not use strcpy_s where memcpy_s can be used)
- JUC Unsafe
- BGP Federation +community
猜你喜欢

C language: recursive function to realize Hanoi Tower

IP address introduction

turtle库的使用数字时钟模拟时钟动态显示

C language: Simulated Implementation of library function strcpy

20220606 关于矩阵的Young不等式

Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing

C language: deep understanding of character functions and string functions (1)

Cmake Learning Series I

Completely uninstall PostgreSQL under Linux

C language: deep understanding of character functions and string functions (2)
随机推荐
Routing - static routing
20211108 det(AB)=det(A)det(B)
LeetCode 1143. 最长公共子序列
Download address of QT source code of each version
Some websites of QT (software download, help documents, etc.)
20211006 积分、微分、投影均属于线性变换
BGP Federation +community
Message Oriented Middleware
Module build failed: TypeError: this. getOptions is not a function at Object. stylusLoader
20211115 任意n阶方阵均与三角矩阵(上三角或者下三角)相似
C language: data storage in memory
Visual studio tools using shortcut keys (continuous update)
Simulink如何添加模块到Library Browser
20211104 why are the traces of similar matrices the same
Spectre record
JUC原子数组
202012 CCF test questions
C language: shortcut keys commonly used in VS
JUC field Updater
20211018 一些特殊矩阵