当前位置:网站首页>Depth first search and breadth first search of graph traversal
Depth first search and breadth first search of graph traversal
2022-07-01 23:26:00 【Life needs depth】
This chapter will first introduce the depth first search and breadth first search of graphs , And then give C/C++/Java The implementation of the .
Catalog
1. Graphic introduction of depth first search
1.1 Depth first search Introduction
1.2 Depth first search graph
2. Graphic introduction of breadth first search
2.1 Introduction to breadth first search
2.2 Breadth first search diagram
3. Search algorithm source codeReprint please indicate the source : If the sky doesn't die - Blog Garden
Graphic introduction of depth first search
1. Depth first search Introduction
Depth first search (Depth First Search), It's similar to the preorder traversal of trees .
Its ideas : Suppose the initial state is that all the vertices in the graph are not accessed , From a certain point v set out , Access the vertex first , Then, the depth first search traversal graph starts from each of its unreached adjacency points , All the way to the sum v All vertices with paths in common are accessed . If there are other vertices of this fashion that have not been visited , Then select another vertex that is not accessed as the starting point , Repeat the process , Until all vertices in the graph are accessed .
obviously , Depth first search is a recursive process .
2. Depth first search graph
2.1 Depth first search of undirected graph
Let's say " Undirected graph " For example , To demonstrate depth first search .
For the above figure G1 Conduct depth-first traversal , From the top A Start .
The first 1 Step : visit A.
The first 2 Step : visit (A The adjacency point )C.
In the 1 Step visit A after , What we should visit next is A The adjacency point , namely "C,D,F" One of them . But in the implementation of this paper , The vertices ABCDEFG Is stored in order ,C stay "D and F" In front of , therefore , First visit C.
The first 3 Step : visit (C The adjacency point )B.
In the 2 Step visit C after , You should visit C The adjacency point , namely "B and D" In a (A Has been visited , Not included ). And because the B stay D Before , First visit B.
The first 4 Step : visit (C The adjacency point )D.
In the 3 Step by step C The adjacency point B after ,B There are no adjacent points that are not accessed ; therefore , Return to visit C Another adjacent point of D.
The first 5 Step : visit (A The adjacency point )F.
I've already visited A, And the interview is over "A The adjacency point B All the adjacency points of ( Including recursive adjacency points )"; therefore , Return to visit A Another adjacent point of F.
The first 6 Step : visit (F The adjacency point )G.
The first 7 Step : visit (G The adjacency point )E.
So the order of access is :A -> C -> B -> D -> F -> G -> E
2.2 Depth first search of digraphs
Let's say " Directed graph " For example , To demonstrate depth first search .
For the above figure G2 Conduct depth-first traversal , From the top A Start .
The first 1 Step : visit A.
The first 2 Step : visit B.
During a visit to the A after , What we should visit next is A The other vertex of the edge of , Vertex B.
The first 3 Step : visit C.
During a visit to the B after , What we should visit next is B The other vertex of the edge of , Vertex C,E,F. In the diagram implemented in this paper , The vertices ABCDEFG Store in order , So first visit C.
The first 4 Step : visit E.
Next visit C The other vertex of the edge of , Vertex E.
The first 5 Step : visit D.
Next visit E The other vertex of the edge of , Vertex B,D. The vertices B Has been visited , So access the vertex D.
The first 6 Step : visit F.
Next, we should go back " visit A The other vertex of the edge of F".
The first 7 Step : visit G.
So the order of access is :A -> B -> C -> E -> D -> F -> G
Graphic introduction of breadth first search
1. Introduction to breadth first search
Breadth first search algorithm (Breadth First Search), Also known as " Width first search " or " Search horizontally first ", abbreviation BFS.
Its idea is : From some vertex in the picture v set out , During a visit to the v And then access v Each of the never visited adjacency points , Then access their adjacency points one by one from each of these adjacency points , And make “ The adjacency point of the first visited vertex is accessed before the adjacency point of the later visited vertex , Until the adjacent points of all the vertices in the graph that have been accessed are accessed . If there are still vertices in the graph that are not accessed , You need to select another vertex that has not been visited as the new starting point , Repeat the process , Until all vertices in the graph are accessed .
let me put it another way , The process of breadth first searching traversal graph is based on v As a starting point , From near to far , Visit and v There are paths connected and the path length is 1,2... The summit of .
2. Breadth first search diagram
2.1 Breadth first search for undirected graphs
Let's say " Undirected graph " For example , To demonstrate breadth first search . Or take the picture above G1 Take an example to illustrate .
The first 1 Step : visit A.
The first 2 Step : Sequential access C,D,F.
During a visit to the A after , Next visit A The adjacency point . I've said that before , In the implementation of this paper , The vertices ABCDEFG Stored in order ,C stay "D and F" In front of , therefore , First visit C. End of visit C after , And then visit D,F.
The first 3 Step : Sequential access B,G.
In the 2 End of step visit C,D,F after , Then visit their neighbors in turn . First visit C The adjacency point B, Revisit F The adjacency point G.
The first 4 Step : visit E.
In the 3 End of step visit B,G after , Then visit their neighbors in turn . Only G There are neighbors E, Therefore access G The adjacency point E.
So the order of access is :A -> C -> D -> F -> B -> G -> E
2.2 Breadth first search of digraph
Let's say " Directed graph " For example , To demonstrate breadth first search . Or take the picture above G2 Take an example to illustrate .
The first 1 Step : visit A.
The first 2 Step : visit B.
The first 3 Step : Sequential access C,E,F.
During a visit to the B after , Next visit B The other vertex of the edge of , namely C,E,F. I've said that before , In the implementation of this paper , The vertices ABCDEFG Stored in order , So we will visit C, And then visit E,F.
The first 4 Step : Sequential access D,G.
After the interview C,E,F after , Then visit the other vertex of their edge in turn . Or in accordance with C,E,F Sequential access to ,C All have been visited , Then there's only... Left E,F; First visit E The adjacency point D, Revisit F The adjacency point G.
So the order of access is :A -> B -> C -> E -> F -> D -> G
Search algorithm source code
Here are respectively " Adjacency matrix undirected graph "、" Adjacency table undirected graph "、" Adjacency matrix digraph "、" Adjacency table digraph " Of C/C++/Java Search algorithm source code . The source code will not be explained here ,please RTFSC; Refer to the comments in the source code .
边栏推荐
- 建模和影视后期有什么关联?
- Glass mosaic
- 问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c
- Redis RDB快照
- Matplotlib常用设置
- 每日三题 6.30(2)
- De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
- Why is PHP called hypertext preprocessor
- Daily three questions 6.29
- CKS CKA ckad change terminal to remote desktop
猜你喜欢

有没有一段代码,让你为人类的智慧所折服
![[applet] realize the left and right [sliding] list through the scroll view component](/img/18/b1b4e9923782856143721dad84cbab.png)
[applet] realize the left and right [sliding] list through the scroll view component

Zero foundation tutorial of Internet of things development

mt管理器测试滑雪大冒险

Istio, ebpf and rsocket Broker: in depth study of service grid

Why is PHP called hypertext preprocessor

flutter Unable to load asset: assets/images/888.png

Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification

2021 RoboCom 世界机器人开发者大赛-本科组初赛

2022年危险化学品经营单位安全管理人员考试题及在线模拟考试
随机推荐
纪念成为首个DAYUs200三方demo贡献者
Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
Wechat personal small store one click opening assistant applet development
实在RPA:银行数字化,业务流程自动化“一小步”,贷款审核效率“一大步”
The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
Linux基础 —— CentOS7 离线安装 MySQL
【微服务|Sentinel】sentinel整合openfeign
会声会影2022智能、快速、简单的视频剪辑软件
Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification
问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64
Huisheng Huiying 2022 intelligent, fast and simple video editing software
Redis AOF日志
每日三题 6.30
The difference between timer and scheduledthreadpoolexecutor
MySQL binlog cleanup
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
Practical application and extension of plain framework
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
每日三题 6.29
Yunxin small class | common cognitive misunderstandings in IM and audio and video