当前位置:网站首页>Breadth first traversal of Graphs
Breadth first traversal of Graphs
2022-06-26 02:23:00 【chengqiuming】
One The finishing touch
Breadth first traversal is also called breadth first search , Is one of the most common graph search methods . Breadth first search refers to starting from a certain point , Access all the neighboring nodes that are not accessed at one time , Then start from these visited adjacency points in turn , Access... Layer by layer . Breadth first traversal is to traverse the graph in the way of breadth first search .

In the order of breadth first search , The access sequence in the above figure is :1 2 3 4 5 6
The secret of breadth first traversal : First visited node , Its neighboring nodes are accessed first .
According to the breadth first traversal of the script , First come, first served , You can do this with queues . Because each node is accessed only once , So you can set an auxiliary array visited[i] = false, It means the first one i Nodes not accessed ;visited[i] = true, It means the first one i Nodes have been accessed .
Two Algorithm steps
1 Initialize that all nodes are accessed , And initialize an empty queue .
2 From a node in the graph v set out , visit v And mark that it has been accessed , take v The team .
3 If the queue is not empty , Then continue , Otherwise the algorithm ends .
4 Put the team head element v Out of the team , Sequential access v All unreachable adjacency points of , The tag has been accessed and queued , Turn to step 3.
3、 ... and The illustration
A directed graph is shown in the following figure , The breadth first traversal process is as follows .
1 Initialization all nodes are not accessed ,visited[i] = false,i =[1,6]. And initialize an empty queue Q.

2 From the node 1 set out , Mark that it has been accessed ,visited[1] = true, The nodes 1 The team .

3 Put the team head element 1 Out of the team , Sequential access 1 All unreachable adjacency points of 2 and 3, Mark it as visited and queued .

4 For team header element 2 Out of the team , take 2 Unreachable adjacency points 4 Mark as accessed , And put it on the team .

5 Put the team head element 3 Out of the team ,3 The adjacency point 2 Has been interviewed , No need to deal with it , The adjacency points that are not accessed 5 Mark as accessed , And put it on the team .

6 Put the team head element 4 Out of the team ,4 The adjacency point 3 Has been interviewed , No need to deal with it , The adjacency points that are not accessed 6 Mark as accessed , And put it on the team .

7 Put the team head element 5 Out of the team ,5 Adjacent node of 4 and 6 Has been interviewed , No need to deal with it , There are no adjacent points that are not accessed .

8 Put the team head element 6 Out of the team ,6 No adjacency point .

9 The queue is empty , The algorithm ends . The breadth first traversal sequence is 1 2 3 4 5 6.
边栏推荐
- MySQL must master 4 languages!
- How did the thief unlock the password after the iPhone was stolen? After reading the long knowledge
- SQL column value to row value (unpivot)
- 将lua print输出到cocos2d控制台输出窗口中
- 饼图变形记,肝了3000字,收藏就是学会!
- Shell learning record (II)
- 其他代码,,vt,,,k
- Digital commodity DGE -- the dark horse of wealth in digital economy
- 工作一年闲记
- Timer case
猜你喜欢

Chrome browser developer tool usage

V4l2+qt video optimization strategy

Cross server SQL connection configuration

樹莓派 + AWS IoT Greengrass

Disruptor (I) sequence

Jenkins localization and its invalid solution

官方零基础入门 Jetpack Compose 的中文课程来啦!

win32
![[JS] free API to judge holidays, working days, Saturdays and Sundays](/img/e1/8b742082385bb5e60f74beb3b81c7d.png)
[JS] free API to judge holidays, working days, Saturdays and Sundays

Raspberry pie + AWS IOT introductory experiment
随机推荐
Three factors affecting personal growth
Shell learning record (IV)
leetcode 300. Longest increasing subsequence (medium)
FPGA实现图像二值形态学滤波——腐蚀膨胀
【无标题】vsbiji esp....32
What happens when the cloud answer does not display the third-party login button
【缺陷检测】基于matlab GUI印刷电路板自动缺陷检测【含Matlab源码 1912期】
标定。。。
微博评论的高性能高可用计算架构
vtk初始化代码学习1
Advanced cross platform application development (23): an article approaching the launch of testlight
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!
Data analysis - data source, field type, data collection trap
基于邻接表的广度优先遍历
[image filtering] image filtering system based on Matlab GUI [including Matlab source code 1913]
樹莓派 + AWS IoT Greengrass
Breadth first traversal based on adjacency table
Fastadmin applet assistant is purchased, but the work order cannot be published in the problem work order
.NET7之MiniAPI(特别篇) :Preview5优化了JWT验证(下)
云答题不显示第三方登陆按钮是什么情况