DFS & BFS Graph
-
Upload
gdg-bangla -
Category
Engineering
-
view
207 -
download
6
Transcript of DFS & BFS Graph
![Page 1: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/1.jpg)
Identity
MD.MAHFUZUL YAMINID:141-15-3429SEC:FDEPARTMENT OF CSE DAFFODIL INTERNATIONAL UNIVERSITY
![Page 2: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/2.jpg)
Topic
Depth-First SearchThink Stack
Breadth-First SearchThink Queue
![Page 3: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/3.jpg)
Depth-First Search Search “deeper” in the graph whenever possible Edges are explored out of the most recently
discovered vertex v that still has unexplored edges
• After all edges of v have been explored, the search “backtracks” from the parent of v
• The process continues until all vertices reachable from the original source have been discovered
• If undiscovered vertices remain, choose one of them as a new source and repeat the search from that vertex
• DFS creates a “depth-first forest”
1 2
5 4
3
![Page 4: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/4.jpg)
Depth-first search: Directed graphs
The algorithm is essentially the same as for undirected graphs, the difference residing in the interpretation of the word "adjacent".
In a directed graph, node w is adjacent to node v if the directed edge (v, w) exists.
If (v, w) exists but (w, v) does not, then w is adjacent to v but v is not adjacent to w.
With this change of interpretation the procedures dfs and search apply equally well in the case of a directed graph.
![Page 5: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/5.jpg)
DFS Example
Data Structure and Algorithm
sourcevertex
![Page 6: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/6.jpg)
DFS Example
Data Structure and Algorithm
1 | | |
| | |
| |
sourcevertex d f
![Page 7: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/7.jpg)
DFS Example
Data Structure and Algorithm
1 | | |
| | |
2 | |
sourcevertex d f
![Page 8: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/8.jpg)
DFS Example
Data Structure and Algorithm
1 | | |
| | 3 |
2 | |
sourcevertex d f
![Page 9: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/9.jpg)
DFS Example
Data Structure and Algorithm
1 | | |
| | 3 | 4
2 | |
sourcevertex d f
![Page 10: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/10.jpg)
DFS Example
Data Structure and Algorithm
1 | | |
| 5 | 3 | 4
2 | |
sourcevertex d f
![Page 11: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/11.jpg)
DFS Example
Data Structure and Algorithm
1 | | |
| 5 | 63 | 4
2 | |
sourcevertex d f
![Page 12: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/12.jpg)
DFS Example
Data Structure and Algorithm
1 | | |
| 5 | 63 | 4
2 | 7 |
sourcevertex d f
![Page 13: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/13.jpg)
DFS Example
Data Structure and Algorithm
1 | 8 | |
| 5 | 63 | 4
2 | 7 |
sourcevertex d f
![Page 14: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/14.jpg)
DFS Example
Data Structure and Algorithm
1 | 8 | |
| 5 | 63 | 4
2 | 7 9 |
sourcevertex d f
![Page 15: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/15.jpg)
DFS Example
Data Structure and Algorithm
1 | 8 | |
| 5 | 63 | 4
2 | 7 9 |10
sourcevertex d f
![Page 16: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/16.jpg)
DFS Example
Data Structure and Algorithm
1 | 8 |11 |
| 5 | 63 | 4
2 | 7 9 |10
sourcevertex d f
![Page 17: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/17.jpg)
DFS Example
Data Structure and Algorithm
1 |12 8 |11 |
| 5 | 63 | 4
2 | 7 9 |10
sourcevertex d f
![Page 18: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/18.jpg)
DFS Example
Data Structure and Algorithm
1 |12 8 |11 13|
| 5 | 63 | 4
2 | 7 9 |10
sourcevertex d f
![Page 19: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/19.jpg)
DFS Example
Data Structure and Algorithm
1 |12 8 |11 13|
14| 5 | 63 | 4
2 | 7 9 |10
sourcevertex d f
![Page 20: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/20.jpg)
DFS Example
Data Structure and Algorithm
1 |12 8 |11 13|
14|155 | 63 | 4
2 | 7 9 |10
sourcevertex d f
![Page 21: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/21.jpg)
DFS Example
Data Structure and Algorithm
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
sourcevertex d f
![Page 22: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/22.jpg)
All vertex are visited
![Page 23: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/23.jpg)
Breadth-first search
23
• In graph theory, breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighboring nodes. • Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal.
![Page 24: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/24.jpg)
Adjacency Matrix
Adjacency List
![Page 25: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/25.jpg)
BFS: Start with Node 5
71
5
4
3
2
6
5 1 2 0 4 3 7 6
0
![Page 26: DFS & BFS Graph](https://reader031.fdocuments.net/reader031/viewer/2022012313/58aaca4a1a28ab2f728b5765/html5/thumbnails/26.jpg)
That’s all