SlideShare a Scribd company logo
prepared By:
13cse035:Hemanshi maheta
13cse038:Satya parsana
13cse047:Jignesh rathod
 Depth First Search (DFS)
 Breadth First Search (BFS)
Topics
DEPTH-FIRST SEARCH: UNDIRECTED GRAPHS
 Let G = (N , A) be an undirected graph all of whose nodes we wish
to visit.
 Suppose it is somehow possible to mark a node to show it has
already been visited.
 To carry out a depth-first traversal of the graph, choose any node v ϵ
N as the starting point.
 Mark this node to show it has been visited. Next, if there is a
node adjacent to v that has not yet been visited, choose this
node as a new starting point and call the depth-first search
procedure recursively.
 On return from the recursive call, if there is another node
adjacent to v that has not been visited, choose this node as
the next starting point, call the procedure recursively once
again, and so on.
 When all the nodes adjacent to v are marked, the search
starting at v is finished.
 If there remain any nodes of G that have not been
visited,choose any one of them as a new starting point, and
call the procedure yet again.
 Continue thus until all the nodes of G are marked. Here is the
recursive algorithm
EXAMPLE:
DFS and BFS
DFS and BFS
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.
Data Structure and Algorithm
DFS EXAMPLE
source
vertex
Data Structure and Algorithm
DFS EXAMPLE
1 | | |
|||
| |
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 | | |
|||
2 | |
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 | | |
||3 |
2 | |
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 | | |
||3 | 4
2 | |
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 | | |
|5 |3 | 4
2 | |
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 | | |
|5 | 63 | 4
2 | |
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 | | |
|5 | 63 | 4
2 | 7 |
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 | 8 | |
|5 | 63 | 4
2 | 7 |
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 | 8 | |
|5 | 63 | 4
2 | 7 9 |
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 | 8 | |
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 | 8 |11 |
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 |12 8 |11 |
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 |12 8 |11 13|
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 |12 8 |11 13|
14|5 | 63 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 |12 8 |11 13|
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
DFS EXAMPLE
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
BREADTH-FIRST SEARCH (BFS)
 Search for all vertices that are directly reachable from the root
(called level 1 vertices)
 After mark all these vertices, visit all vertices that are directly
reachable from any level 1 vertices (called level 2 vertices), and so
on.
 In general, level k vertices are directly reachable from a level k – 1
vertices
DFS and BFS
EXAMPLE:BFS FOR DIRECTED GRAPH
DFS and BFS
A
ONM
LKJ
E F G H
DCB
I
P
An Example:BFS for undirected graph
A
ONM
LKJ
E F G H
DCB
I
P
0
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
2
2
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
2
2
3 3
3
3
3
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
2
2
3 3
3
3
3
4 4
4
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
2
2
3 3
3
3
3
4 4
4
5
5
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
2
2
3 3
3
3
3
4 4
4
5
5
A
ONM
LKJ
E F G H
DCB
I
P
0
1 1
1
2
2
3 3
3
3
3
4 4
4
5
5
Thnk u…very much..

More Related Content

PPTX
Bfs and Dfs
PPT
Breadth first search and depth first search
PPTX
Graph traversals in Data Structures
PPTX
Graph in data structure
PPTX
linked list in data structure
PPTX
Tree - Data Structure
DOC
An atm with an eye
PPTX
Graph representation
Bfs and Dfs
Breadth first search and depth first search
Graph traversals in Data Structures
Graph in data structure
linked list in data structure
Tree - Data Structure
An atm with an eye
Graph representation

What's hot (20)

PPT
Graph traversal-BFS & DFS
PPT
Depth First Search ( DFS )
PPTX
Breadth First Search & Depth First Search
PPTX
Dfs presentation
PPTX
Depth first search [dfs]
PPTX
Binary Search Tree in Data Structure
PPTX
DFS & BFS Graph
PPTX
Dijkstra's algorithm presentation
PPTX
LL(1) parsing
PPTX
Bellman ford Algorithm
PPTX
Threaded Binary Tree
PPT
Unit 4 external sorting
PPT
Spanning trees
PPTX
Presentation on Breadth First Search (BFS)
PPT
Abstract data types
PPTX
Dijkstra’S Algorithm
PPTX
Binary Tree in Data Structure
PPT
Prim's Algorithm on minimum spanning tree
PPT
NFA or Non deterministic finite automata
PPT
Breadth first search
Graph traversal-BFS & DFS
Depth First Search ( DFS )
Breadth First Search & Depth First Search
Dfs presentation
Depth first search [dfs]
Binary Search Tree in Data Structure
DFS & BFS Graph
Dijkstra's algorithm presentation
LL(1) parsing
Bellman ford Algorithm
Threaded Binary Tree
Unit 4 external sorting
Spanning trees
Presentation on Breadth First Search (BFS)
Abstract data types
Dijkstra’S Algorithm
Binary Tree in Data Structure
Prim's Algorithm on minimum spanning tree
NFA or Non deterministic finite automata
Breadth first search
Ad

Viewers also liked (14)

PPT
Bfs and dfs in data structure
PDF
Algorithm Analysis and Design Class Notes
PDF
ADA complete notes
PPT
Application of dfs
PPTX
Search algorithms master
PPTX
130210107039 2130702
PPTX
Linear and Binary Search Algorithms.(Discrete Mathematics)
PPT
Heuristic Search
ODP
Hillclimbing search algorthim #introduction
PPT
Ch2 3-informed (heuristic) search
PPTX
Design and Analysis of Algorithms
PPT
Hill climbing
PPT
Heuristic Search Techniques {Artificial Intelligence}
PPT
17. Trees and Graphs
Bfs and dfs in data structure
Algorithm Analysis and Design Class Notes
ADA complete notes
Application of dfs
Search algorithms master
130210107039 2130702
Linear and Binary Search Algorithms.(Discrete Mathematics)
Heuristic Search
Hillclimbing search algorthim #introduction
Ch2 3-informed (heuristic) search
Design and Analysis of Algorithms
Hill climbing
Heuristic Search Techniques {Artificial Intelligence}
17. Trees and Graphs
Ad

Similar to DFS and BFS (20)

PPT
YCMOU_FYBCA_DS_Unit-7.ppt
PPT
PPTX
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
PPT
lecture 19
PPT
2.5 bfs & dfs 02
DOCX
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
DOCX
Data Structures and Algorithm Analysis1. How much memory w.docx
PPTX
kumattt).pptx
PDF
Skiena algorithm 2007 lecture12 topological sort connectivity
PPTX
Introduction of Depth First Search with example
PPTX
PPTX
temp.pptxsusueueuune ueieoekeebhe ejene h
PPTX
BFS & DFS in Data Structure
PPTX
LAB7_FILS_DSA_graphs_datastructures.pptx
PPTX
Breath first Search and Depth first search
PPT
Algorithm
PPTX
DFS & BFS in Computer Algorithm
PPTX
Graph Traversing and Seaching - Data Structure- AIUB.pptx
PPTX
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
PPT
lecture 18
YCMOU_FYBCA_DS_Unit-7.ppt
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
lecture 19
2.5 bfs & dfs 02
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm Analysis1. How much memory w.docx
kumattt).pptx
Skiena algorithm 2007 lecture12 topological sort connectivity
Introduction of Depth First Search with example
temp.pptxsusueueuune ueieoekeebhe ejene h
BFS & DFS in Data Structure
LAB7_FILS_DSA_graphs_datastructures.pptx
Breath first Search and Depth first search
Algorithm
DFS & BFS in Computer Algorithm
Graph Traversing and Seaching - Data Structure- AIUB.pptx
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
lecture 18

Recently uploaded (20)

PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Well-logging-methods_new................
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPT
Mechanical Engineering MATERIALS Selection
PDF
Digital Logic Computer Design lecture notes
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPT
Project quality management in manufacturing
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Geodesy 1.pptx...............................................
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Foundation to blockchain - A guide to Blockchain Tech
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
UNIT 4 Total Quality Management .pptx
Well-logging-methods_new................
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Mechanical Engineering MATERIALS Selection
Digital Logic Computer Design lecture notes
bas. eng. economics group 4 presentation 1.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Model Code of Practice - Construction Work - 21102022 .pdf
Project quality management in manufacturing
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Geodesy 1.pptx...............................................
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS

DFS and BFS

  • 2.  Depth First Search (DFS)  Breadth First Search (BFS) Topics
  • 3. DEPTH-FIRST SEARCH: UNDIRECTED GRAPHS  Let G = (N , A) be an undirected graph all of whose nodes we wish to visit.  Suppose it is somehow possible to mark a node to show it has already been visited.  To carry out a depth-first traversal of the graph, choose any node v ϵ N as the starting point.  Mark this node to show it has been visited. Next, if there is a node adjacent to v that has not yet been visited, choose this node as a new starting point and call the depth-first search procedure recursively.
  • 4.  On return from the recursive call, if there is another node adjacent to v that has not been visited, choose this node as the next starting point, call the procedure recursively once again, and so on.  When all the nodes adjacent to v are marked, the search starting at v is finished.  If there remain any nodes of G that have not been visited,choose any one of them as a new starting point, and call the procedure yet again.  Continue thus until all the nodes of G are marked. Here is the recursive algorithm
  • 8. 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.
  • 9. Data Structure and Algorithm DFS EXAMPLE source vertex
  • 10. Data Structure and Algorithm DFS EXAMPLE 1 | | | ||| | | source vertex d f
  • 11. Data Structure and Algorithm DFS EXAMPLE 1 | | | ||| 2 | | source vertex d f
  • 12. Data Structure and Algorithm DFS EXAMPLE 1 | | | ||3 | 2 | | source vertex d f
  • 13. Data Structure and Algorithm DFS EXAMPLE 1 | | | ||3 | 4 2 | | source vertex d f
  • 14. Data Structure and Algorithm DFS EXAMPLE 1 | | | |5 |3 | 4 2 | | source vertex d f
  • 15. Data Structure and Algorithm DFS EXAMPLE 1 | | | |5 | 63 | 4 2 | | source vertex d f
  • 16. Data Structure and Algorithm DFS EXAMPLE 1 | | | |5 | 63 | 4 2 | 7 | source vertex d f
  • 17. Data Structure and Algorithm DFS EXAMPLE 1 | 8 | | |5 | 63 | 4 2 | 7 | source vertex d f
  • 18. Data Structure and Algorithm DFS EXAMPLE 1 | 8 | | |5 | 63 | 4 2 | 7 9 | source vertex d f
  • 19. Data Structure and Algorithm DFS EXAMPLE 1 | 8 | | |5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 20. Data Structure and Algorithm DFS EXAMPLE 1 | 8 |11 | |5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 21. Data Structure and Algorithm DFS EXAMPLE 1 |12 8 |11 | |5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 22. Data Structure and Algorithm DFS EXAMPLE 1 |12 8 |11 13| |5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 23. Data Structure and Algorithm DFS EXAMPLE 1 |12 8 |11 13| 14|5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 24. Data Structure and Algorithm DFS EXAMPLE 1 |12 8 |11 13| 14|155 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 25. Data Structure and Algorithm DFS EXAMPLE 1 |12 8 |11 13|16 14|155 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 26. Data Structure and Algorithm BREADTH-FIRST SEARCH (BFS)  Search for all vertices that are directly reachable from the root (called level 1 vertices)  After mark all these vertices, visit all vertices that are directly reachable from any level 1 vertices (called level 2 vertices), and so on.  In general, level k vertices are directly reachable from a level k – 1 vertices
  • 30. A ONM LKJ E F G H DCB I P An Example:BFS for undirected graph
  • 31. A ONM LKJ E F G H DCB I P 0
  • 32. A ONM LKJ E F G H DCB I P 0 1 1 1
  • 33. A ONM LKJ E F G H DCB I P 0 1 1 1 2 2
  • 34. A ONM LKJ E F G H DCB I P 0 1 1 1 2 2 3 3 3 3 3
  • 35. A ONM LKJ E F G H DCB I P 0 1 1 1 2 2 3 3 3 3 3 4 4 4
  • 36. A ONM LKJ E F G H DCB I P 0 1 1 1 2 2 3 3 3 3 3 4 4 4 5 5
  • 37. A ONM LKJ E F G H DCB I P 0 1 1 1 2 2 3 3 3 3 3 4 4 4 5 5
  • 38. A ONM LKJ E F G H DCB I P 0 1 1 1 2 2 3 3 3 3 3 4 4 4 5 5