SlideShare a Scribd company logo
BFS and DFS: Graph
Traversal Algorithms
Today, we'll dive into two fundamental graph traversal algorithms:
Breadth-First Search (BFS) and Depth-First Search (DFS).
Fundamentals of BFS and DFS
BFS
BFS explores a graph level by level. It starts at a source
node and visits all its immediate neighbors, then their
neighbors, and so on.
DFS
DFS explores a graph by going as deep as possible along a
branch before backtracking. It starts at a source node and
visits its first neighbor, then its neighbor's first neighbor,
and so on, until it reaches a dead end.
Choosing the Right Search Algorithm
BFS
Ideal for finding the shortest path, like in a maze or
network routing.
DFS
Useful for detecting cycles, topological sorting, and
finding connected components in a graph.
Algorithmic
Implementation
BFS Implementation
BFS uses a queue to store nodes to be visited, ensuring
level-by-level exploration.
DFS Implementation
DFS employs a stack to track the order of visited nodes,
enabling depth-first traversal.
Time and Space Complexity: BFS vs. DFS
O(V+E)
Time
The time complexity of both BFS and DFS is typically O(V+E),
where V is the number of vertices and E is the number of
edges in the graph.
O(V)
Space
The space complexity of both BFS and DFS is typically O(V), as
they need to store the visited nodes.

More Related Content

Similar to BFS and DFS transversal algorithmsanshdh (20)

PPTX
DFS & BFS in Computer Algorithm
Meghaj Mallick
 
PPTX
Breath first Search and Depth first search
Kirti Verma
 
PPTX
Graph traversals in Data Structures
Anandhasilambarasan D
 
PPTX
Search Algorithms in AI.pptx
Dr.Shweta
 
PPT
Graph traversal-BFS & DFS
Rajandeep Gill
 
PPTX
bfs tree searching ,sortingUntitled presentation.pptx
saurabhpandey679381
 
PPT
Breadth first search and depth first search
Hossain Md Shakhawat
 
PPTX
breadth first search
DeepikaT13
 
PPTX
Sec B Graph traversal.pptx
tabusam1
 
PPTX
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
AzwanAlghifari
 
PDF
graphtraversals.pdf
SeethaDinesh
 
PPTX
Data structure
lalithambiga kamaraj
 
PPTX
Breadth-First Search and Depth-First Search.pptx
MuktarHossain13
 
PPTX
Algorithm
AnupomShill
 
PPTX
Lecture 16 graphs traversal
Abirami A
 
PPTX
temp.pptxsusueueuune ueieoekeebhe ejene h
harshitou754
 
PPTX
Bfs & dfs application
Umme habiba
 
PDF
Breadth First Search .
Supriyo Dana
 
PPT
8-Graph.ppt
ssuser55cbdb
 
DOC
BFS, Breadth first search | Search Traversal Algorithm
MSA Technosoft
 
DFS & BFS in Computer Algorithm
Meghaj Mallick
 
Breath first Search and Depth first search
Kirti Verma
 
Graph traversals in Data Structures
Anandhasilambarasan D
 
Search Algorithms in AI.pptx
Dr.Shweta
 
Graph traversal-BFS & DFS
Rajandeep Gill
 
bfs tree searching ,sortingUntitled presentation.pptx
saurabhpandey679381
 
Breadth first search and depth first search
Hossain Md Shakhawat
 
breadth first search
DeepikaT13
 
Sec B Graph traversal.pptx
tabusam1
 
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
AzwanAlghifari
 
graphtraversals.pdf
SeethaDinesh
 
Data structure
lalithambiga kamaraj
 
Breadth-First Search and Depth-First Search.pptx
MuktarHossain13
 
Algorithm
AnupomShill
 
Lecture 16 graphs traversal
Abirami A
 
temp.pptxsusueueuune ueieoekeebhe ejene h
harshitou754
 
Bfs & dfs application
Umme habiba
 
Breadth First Search .
Supriyo Dana
 
8-Graph.ppt
ssuser55cbdb
 
BFS, Breadth first search | Search Traversal Algorithm
MSA Technosoft
 

Recently uploaded (17)

PPTX
怎么办StoutLetter美国威斯康星大学斯托特分校本科毕业证,Stout学历证书
Taqyea
 
PPTX
Flannel graphFlannel graphFlannel graphFlannel graphFlannel graph
shareesh25
 
PPTX
Dock Line Organization Made Easy – Discover AMARREX, the Mooring Line Holder ...
Seawatt
 
PPTX
哪里购买澳洲学历认证查询伊迪斯科文大学成绩单水印ECU录取通知书
Taqyea
 
PPT
Confined Space.ppth. Bbbb. Bbbbbbbbbbbbbbbbbbbbbbbnnnjjj
eshaiqbal7
 
PPT
it_14.ppt using atharva college of engineering
shkzishan810
 
PPTX
英国学位证(LTU毕业证书)利兹三一大学毕业证书如何办理
Taqyea
 
PPTX
Contingency-Plan-and-Reminders-from-the-PMO.pptx
PrincessCamilleGalle1
 
PPT
Computer Hardware and Software Hw and SW .ppt
MuzaFar28
 
PPTX
西班牙维尔瓦大学电子版毕业证{UHU毕业完成信UHU水印成绩单}原版制作
Taqyea
 
PDF
Utility Software hshdgsvcjdgvbdvcfkcdgdc
imeetrinidadfuertesa
 
PPTX
美国学位证(UDel毕业证书)特拉华大学毕业证书如何办理
Taqyea
 
PPT
(1) Chemotherapeutic drugs Antimicrobials.ppt
mkurdi133
 
PPTX
英国学位证(PSU毕业证书)普利茅斯大学毕业证书如何办理
Taqyea
 
PPTX
ualities-of-Quantitative-Research-1.pptx
jamjamkyong
 
PPTX
diagnosisinfpdpart1-200628063900 (1).pptx
JayeshTaneja4
 
PDF
Development of Portable Spectometer For MIlk Qulaity analysis
ppr9495
 
怎么办StoutLetter美国威斯康星大学斯托特分校本科毕业证,Stout学历证书
Taqyea
 
Flannel graphFlannel graphFlannel graphFlannel graphFlannel graph
shareesh25
 
Dock Line Organization Made Easy – Discover AMARREX, the Mooring Line Holder ...
Seawatt
 
哪里购买澳洲学历认证查询伊迪斯科文大学成绩单水印ECU录取通知书
Taqyea
 
Confined Space.ppth. Bbbb. Bbbbbbbbbbbbbbbbbbbbbbbnnnjjj
eshaiqbal7
 
it_14.ppt using atharva college of engineering
shkzishan810
 
英国学位证(LTU毕业证书)利兹三一大学毕业证书如何办理
Taqyea
 
Contingency-Plan-and-Reminders-from-the-PMO.pptx
PrincessCamilleGalle1
 
Computer Hardware and Software Hw and SW .ppt
MuzaFar28
 
西班牙维尔瓦大学电子版毕业证{UHU毕业完成信UHU水印成绩单}原版制作
Taqyea
 
Utility Software hshdgsvcjdgvbdvcfkcdgdc
imeetrinidadfuertesa
 
美国学位证(UDel毕业证书)特拉华大学毕业证书如何办理
Taqyea
 
(1) Chemotherapeutic drugs Antimicrobials.ppt
mkurdi133
 
英国学位证(PSU毕业证书)普利茅斯大学毕业证书如何办理
Taqyea
 
ualities-of-Quantitative-Research-1.pptx
jamjamkyong
 
diagnosisinfpdpart1-200628063900 (1).pptx
JayeshTaneja4
 
Development of Portable Spectometer For MIlk Qulaity analysis
ppr9495
 
Ad

BFS and DFS transversal algorithmsanshdh

  • 1. BFS and DFS: Graph Traversal Algorithms Today, we'll dive into two fundamental graph traversal algorithms: Breadth-First Search (BFS) and Depth-First Search (DFS).
  • 2. Fundamentals of BFS and DFS BFS BFS explores a graph level by level. It starts at a source node and visits all its immediate neighbors, then their neighbors, and so on. DFS DFS explores a graph by going as deep as possible along a branch before backtracking. It starts at a source node and visits its first neighbor, then its neighbor's first neighbor, and so on, until it reaches a dead end.
  • 3. Choosing the Right Search Algorithm BFS Ideal for finding the shortest path, like in a maze or network routing. DFS Useful for detecting cycles, topological sorting, and finding connected components in a graph.
  • 4. Algorithmic Implementation BFS Implementation BFS uses a queue to store nodes to be visited, ensuring level-by-level exploration. DFS Implementation DFS employs a stack to track the order of visited nodes, enabling depth-first traversal.
  • 5. Time and Space Complexity: BFS vs. DFS O(V+E) Time The time complexity of both BFS and DFS is typically O(V+E), where V is the number of vertices and E is the number of edges in the graph. O(V) Space The space complexity of both BFS and DFS is typically O(V), as they need to store the visited nodes.