SlideShare a Scribd company logo
BREADTH FIRST SEARCH
&
DEPTH FIRST SEARCH
PRESENTED BY,
K.LALITHAMBIGA,
II- Msc (CS & IT),
Nadar Saraswathi College of
Arts and Science, Theni.
SYNOPSIS
 Introduction
 BFS
 Diagram
 Algorithm
 DFS
 Diagram
 Algorithm
TECHNIQUES FOR GRAPHS
 Definition 1: Traversal of a binary tree involves
examining every node in the tree.
 Definition 2: Search involves visiting nodes in a graph
in a systematic manner, and may or may not result into a
visit to all nodes.
 Different nodes of a graph may be visited, possibly more
than once, during traversal or search
 If search results into a visit to all the vertices, it is called
traversal
BFS (BREADTH FIRST SEARCH)
 Breadth First Search (BFS) algorithm traverses a graph in a breadth
ward motion and uses a queue to remember to get the next vertex to
start a search, when a dead end occurs in any iteration.
 As in the example given, BFS algorithm traverses from A to B to E to F
first then to C and G lastly to D. It employs the following rules.
 Rule 1 − Visit the adjacent unvisited vertex.
 Mark it as visited.
 Display it.
 Insert it in a queue.
 Rule 2 − If no adjacent vertex is found,
remove the first vertex from the queue.
 Rule 3 − Repeat Rule 1 and Rule 2
until the queue is empty.
Step Traversal Description
1 Initialize the queue.
2 We start from
visiting S(starting node),
and mark it as visited.
3 We then see an unvisited
adjacent node from S.
In this example, we have
three nodes but
alphabetically we
choose A, mark it as
visited and enqueue it.
4 Next, the unvisited
adjacent node from S& B.
We mark it as visited and
enqueue it.
5 Next, the unvisited
adjacent node from S is C.
We mark it as visited and
enqueue it.
6 Now, S is left with no
unvisited adjacent nodes.
So, we dequeue and
find A.
7 From A we have D as
unvisited adjacent node.
We mark it as visited and
enqueue it.
Atthisstage,weareleftwithnounmarked(unvisited)nodes.Butasperthealgorithm
wekeepondequeuinginordertogetallunvisitednodes.
Whenthequeuegetsemptied,theprogramisover.
ALGORITHM
 Algorithm for Breadth First Search
Algorithm BFS(v)
//A breadth first search of G is carried out beginning
//at vertex v. For any node i, visited[i] = 1 if i has
//already been visited. The graph G and array visited[]
//are global; visited[] is initialized to zero.
{
u:=v; //q is a queue of unexplored vertices.
visited[v]:=1;
repeat
{
for all vertices w adjacent from u do
{
if(visited[w]=0) then
{
Add w to q; //w is unexplored.
visited[w]:=1;
}
}
if q is empty then return;// No unexplored vertex.
DFS (DEPTH FIRST SEARCH)
 Depth First Search (DFS) algorithm traverses a graph in a
depth ward motion and uses a stack to remember to get the next
vertex to start a search, when a dead end occurs in any iteration.
 As in the example given, DFS algorithm traverses from S to A to D
to G to E to B first, then to F and lastly to C. It employs the
following rules.
 Rule 1 − Visit the adjacent unvisited vertex.
 Mark it as visited.
 Display it.
 Push it in a stack.
 Rule 2 − If no adjacent vertex is found,
pop up a vertex from the stack.
(It will pop up all the vertices from the stack,
which do not have adjacent vertices.)
 Rule 3 − Repeat Rule 1 and Rule 2 until
the stack is empty.
Step Traversal Description
1
Initialize the stack.
2 Mark S as visited and put it onto the
stack.
 Explore any unvisited adjacent node
from S.
 We have three nodes and we can pick
any of them.
 Example, we shall take the node in an
alphabetical order.
3 Mark A as visited and put it onto the
stack.
Explore any unvisited adjacent node from
A.
Both Sand D are adjacent to A but we are
concerned for unvisited nodes only.
4 Visit D and mark it as visited and put onto the
stack. Here, we have B and C nodes, which are
adjacent to D and both are unvisited.
However, we shall again choose in an alphabetical
order.
5
We choose B, mark it as visited and put onto the
stack.
Here B does not have any unvisited adjacent
node. So, we pop B from the stack.
6
we check the stack top for return to the previous
node and check if it has any unvisited nodes.
Here, we find D to be on the top of the stack.
7
Only unvisited adjacent node is from D is C now.
So we visit C, mark it as visited and put it onto the
stack.
AsCdoesnothaveanyunvisitedadjacentnodesowekeeppoppingthestackuntil
wefindanodethathasanunvisitedadjacentnode.
Inthiscase,there'snoneandwekeeppoppinguntilthestackisempty.
ALGORITHM
 Algorithm for Depth First Search
Algorithm DFS(v)
//Given an undirected(directed)graph G=(V,E) with
//n vertices and an array visited[] initially set
//to zero, this algorithm visits all vertices
//reachable from v. G and visited[] are global.
{
visited[v]:=1;
for each vertex w adjacent from v do
{
if(visited[w]=0) then
DFS(w);
Data structure

More Related Content

PPTX
Breadth first search (bfs)
PDF
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...
PDF
Breadth first search signed
PPT
Graph traversal-BFS & DFS
PPTX
DFS and BFS
PPTX
Depth first search and breadth first searching
PPTX
DFS & BFS in Computer Algorithm
Breadth first search (bfs)
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...
Breadth first search signed
Graph traversal-BFS & DFS
DFS and BFS
Depth first search and breadth first searching
DFS & BFS in Computer Algorithm

What's hot (20)

PPTX
finite automata
PPT
2.5 bfs & dfs 02
PPT
String Matching with Finite Automata,Aho corasick,
PPTX
PPTX
Graph traversals in Data Structures
PPTX
Dfs presentation
PPT
2_4 Finite Automata.ppt
PDF
AI - Backtracking vs Depth-First Search (DFS)
PPT
2.5 graph dfs
PPTX
DFS & BFS Graph
PPTX
Breadth First Search (BFS)
PDF
AB-RNA-alignments-2011
PPT
(148064384) bfs
PDF
11 x1 t12 07 primitive function
DOC
Chapter 2 2 1 2
PPTX
Chirp z algorithm 1
PPTX
Bfs and dfs
PPTX
Turing Machine
PPTX
Pertemuan 4
PDF
Missilecommand
finite automata
2.5 bfs & dfs 02
String Matching with Finite Automata,Aho corasick,
Graph traversals in Data Structures
Dfs presentation
2_4 Finite Automata.ppt
AI - Backtracking vs Depth-First Search (DFS)
2.5 graph dfs
DFS & BFS Graph
Breadth First Search (BFS)
AB-RNA-alignments-2011
(148064384) bfs
11 x1 t12 07 primitive function
Chapter 2 2 1 2
Chirp z algorithm 1
Bfs and dfs
Turing Machine
Pertemuan 4
Missilecommand
Ad

Similar to Data structure (20)

PPTX
Breath first Search and Depth first search
PPTX
kumattt).pptx
PPTX
Sec B Graph traversal.pptx
PPTX
BFS & DFS in Data Structure
PPTX
Data structure note
PPTX
Breadth First Search or BFS for a Graph traversal
PPTX
Data Structure and Algorithms Graph Traversal
PPTX
Topological Sort and BFS
PPTX
BFS (Breadth First Search) Tree Traversal
PDF
Breadth First Search and Depth First Search Algorithm
PPTX
bfs tree searching ,sortingUntitled presentation.pptx
PPTX
Understanding Graph Traversal Algorithms A Deep Dive into BFS and DFS
PPTX
breadth first search
PPTX
Breadth-First-Search algorithm with Code
PPTX
Introduction of Depth First Search with example
PPTX
Breadth-First Search and Depth-First Search.pptx
PPTX
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
PDF
graphtraversals.pdf
PPTX
Data structure and algorithm
PPTX
Breadth First Searching Algorithm (BFS).pptx
Breath first Search and Depth first search
kumattt).pptx
Sec B Graph traversal.pptx
BFS & DFS in Data Structure
Data structure note
Breadth First Search or BFS for a Graph traversal
Data Structure and Algorithms Graph Traversal
Topological Sort and BFS
BFS (Breadth First Search) Tree Traversal
Breadth First Search and Depth First Search Algorithm
bfs tree searching ,sortingUntitled presentation.pptx
Understanding Graph Traversal Algorithms A Deep Dive into BFS and DFS
breadth first search
Breadth-First-Search algorithm with Code
Introduction of Depth First Search with example
Breadth-First Search and Depth-First Search.pptx
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
graphtraversals.pdf
Data structure and algorithm
Breadth First Searching Algorithm (BFS).pptx
Ad

More from lalithambiga kamaraj (20)

PPTX
Firewall in Network Security
PPTX
Data Compression in Multimedia
PPTX
Data CompressionMultimedia
PPTX
Digital Audio in Multimedia
PPTX
Network Security: Physical security
PPTX
Graphs in Data Structure
PPTX
Package in Java
PPTX
Exception Handling in Java
PPTX
Digital Image Processing
PPTX
Digital Image Processing
PPTX
Estimating Software Maintenance Costs
PPTX
PPTX
Digital Components
PPTX
Deadlocks in operating system
PPTX
Io management disk scheduling algorithm
PPTX
Recovery system
PPTX
File management
PPTX
Preprocessor
PPTX
PPTX
Managing console of I/o operations & working with files
Firewall in Network Security
Data Compression in Multimedia
Data CompressionMultimedia
Digital Audio in Multimedia
Network Security: Physical security
Graphs in Data Structure
Package in Java
Exception Handling in Java
Digital Image Processing
Digital Image Processing
Estimating Software Maintenance Costs
Digital Components
Deadlocks in operating system
Io management disk scheduling algorithm
Recovery system
File management
Preprocessor
Managing console of I/o operations & working with files

Recently uploaded (20)

PPTX
Institutional Correction lecture only . . .
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
RMMM.pdf make it easy to upload and study
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
master seminar digital applications in india
PDF
Computing-Curriculum for Schools in Ghana
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Basic Mud Logging Guide for educational purpose
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
Institutional Correction lecture only . . .
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
RMMM.pdf make it easy to upload and study
102 student loan defaulters named and shamed – Is someone you know on the list?
master seminar digital applications in india
Computing-Curriculum for Schools in Ghana
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Module 4: Burden of Disease Tutorial Slides S2 2025
O5-L3 Freight Transport Ops (International) V1.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Microbial disease of the cardiovascular and lymphatic systems
FourierSeries-QuestionsWithAnswers(Part-A).pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Basic Mud Logging Guide for educational purpose
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Final Presentation General Medicine 03-08-2024.pptx
O7-L3 Supply Chain Operations - ICLT Program

Data structure

  • 1. BREADTH FIRST SEARCH & DEPTH FIRST SEARCH PRESENTED BY, K.LALITHAMBIGA, II- Msc (CS & IT), Nadar Saraswathi College of Arts and Science, Theni.
  • 2. SYNOPSIS  Introduction  BFS  Diagram  Algorithm  DFS  Diagram  Algorithm
  • 3. TECHNIQUES FOR GRAPHS  Definition 1: Traversal of a binary tree involves examining every node in the tree.  Definition 2: Search involves visiting nodes in a graph in a systematic manner, and may or may not result into a visit to all nodes.  Different nodes of a graph may be visited, possibly more than once, during traversal or search  If search results into a visit to all the vertices, it is called traversal
  • 4. BFS (BREADTH FIRST SEARCH)  Breadth First Search (BFS) algorithm traverses a graph in a breadth ward motion and uses a queue to remember to get the next vertex to start a search, when a dead end occurs in any iteration.  As in the example given, BFS algorithm traverses from A to B to E to F first then to C and G lastly to D. It employs the following rules.  Rule 1 − Visit the adjacent unvisited vertex.  Mark it as visited.  Display it.  Insert it in a queue.  Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.  Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
  • 5. Step Traversal Description 1 Initialize the queue. 2 We start from visiting S(starting node), and mark it as visited. 3 We then see an unvisited adjacent node from S. In this example, we have three nodes but alphabetically we choose A, mark it as visited and enqueue it.
  • 6. 4 Next, the unvisited adjacent node from S& B. We mark it as visited and enqueue it. 5 Next, the unvisited adjacent node from S is C. We mark it as visited and enqueue it. 6 Now, S is left with no unvisited adjacent nodes. So, we dequeue and find A. 7 From A we have D as unvisited adjacent node. We mark it as visited and enqueue it. Atthisstage,weareleftwithnounmarked(unvisited)nodes.Butasperthealgorithm wekeepondequeuinginordertogetallunvisitednodes. Whenthequeuegetsemptied,theprogramisover.
  • 7. ALGORITHM  Algorithm for Breadth First Search Algorithm BFS(v) //A breadth first search of G is carried out beginning //at vertex v. For any node i, visited[i] = 1 if i has //already been visited. The graph G and array visited[] //are global; visited[] is initialized to zero. { u:=v; //q is a queue of unexplored vertices. visited[v]:=1; repeat { for all vertices w adjacent from u do { if(visited[w]=0) then { Add w to q; //w is unexplored. visited[w]:=1; } } if q is empty then return;// No unexplored vertex.
  • 8. DFS (DEPTH FIRST SEARCH)  Depth First Search (DFS) algorithm traverses a graph in a depth ward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration.  As in the example given, DFS algorithm traverses from S to A to D to G to E to B first, then to F and lastly to C. It employs the following rules.  Rule 1 − Visit the adjacent unvisited vertex.  Mark it as visited.  Display it.  Push it in a stack.  Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all the vertices from the stack, which do not have adjacent vertices.)  Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
  • 9. Step Traversal Description 1 Initialize the stack. 2 Mark S as visited and put it onto the stack.  Explore any unvisited adjacent node from S.  We have three nodes and we can pick any of them.  Example, we shall take the node in an alphabetical order. 3 Mark A as visited and put it onto the stack. Explore any unvisited adjacent node from A. Both Sand D are adjacent to A but we are concerned for unvisited nodes only.
  • 10. 4 Visit D and mark it as visited and put onto the stack. Here, we have B and C nodes, which are adjacent to D and both are unvisited. However, we shall again choose in an alphabetical order. 5 We choose B, mark it as visited and put onto the stack. Here B does not have any unvisited adjacent node. So, we pop B from the stack. 6 we check the stack top for return to the previous node and check if it has any unvisited nodes. Here, we find D to be on the top of the stack. 7 Only unvisited adjacent node is from D is C now. So we visit C, mark it as visited and put it onto the stack. AsCdoesnothaveanyunvisitedadjacentnodesowekeeppoppingthestackuntil wefindanodethathasanunvisitedadjacentnode. Inthiscase,there'snoneandwekeeppoppinguntilthestackisempty.
  • 11. ALGORITHM  Algorithm for Depth First Search Algorithm DFS(v) //Given an undirected(directed)graph G=(V,E) with //n vertices and an array visited[] initially set //to zero, this algorithm visits all vertices //reachable from v. G and visited[] are global. { visited[v]:=1; for each vertex w adjacent from v do { if(visited[w]=0) then DFS(w);