SlideShare ist ein Scribd-Unternehmen logo
1 von 12
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);
BFS & DFS Search Techniques

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

finite automata
 finite automata finite automata
finite automata
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,
 
BFS
BFSBFS
BFS
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
 
2_4 Finite Automata.ppt
2_4 Finite Automata.ppt2_4 Finite Automata.ppt
2_4 Finite Automata.ppt
 
AI - Backtracking vs Depth-First Search (DFS)
AI - Backtracking vs Depth-First Search (DFS)AI - Backtracking vs Depth-First Search (DFS)
AI - Backtracking vs Depth-First Search (DFS)
 
2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
 
AB-RNA-alignments-2011
AB-RNA-alignments-2011AB-RNA-alignments-2011
AB-RNA-alignments-2011
 
(148064384) bfs
(148064384) bfs(148064384) bfs
(148064384) bfs
 
11 x1 t12 07 primitive function
11 x1 t12 07 primitive function11 x1 t12 07 primitive function
11 x1 t12 07 primitive function
 
Chapter 2 2 1 2
Chapter 2 2 1 2Chapter 2 2 1 2
Chapter 2 2 1 2
 
Chirp z algorithm 1
Chirp z algorithm 1Chirp z algorithm 1
Chirp z algorithm 1
 
Bfs and dfs
Bfs and dfsBfs and dfs
Bfs and dfs
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Pertemuan 4
Pertemuan 4Pertemuan 4
Pertemuan 4
 
Missilecommand
MissilecommandMissilecommand
Missilecommand
 

Ähnlich wie BFS & DFS Search Techniques

Topological Sort and BFS
Topological Sort and BFSTopological Sort and BFS
Topological Sort and BFSArchanaMani2
 
Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)bhuvaneshwariA5
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Shuvongkor Barman
 
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptxRiya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptxRIYABEPARI
 
LAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptxLAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptxionutionescuionut
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsSigSegVSquad
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx pptDhruvilSTATUS
 
artificial intelligence
artificial intelligence artificial intelligence
artificial intelligence ilias ahmed
 
Breath first Search and Depth first search
Breath first Search and Depth first searchBreath first Search and Depth first search
Breath first Search and Depth first searchKirti Verma
 
Depth first Search.pptx
Depth first Search.pptxDepth first Search.pptx
Depth first Search.pptxAleezaShakeel3
 
Analysis & design of algorithm
Analysis & design of algorithmAnalysis & design of algorithm
Analysis & design of algorithmrahela bham
 
graphtraversals.pdf
graphtraversals.pdfgraphtraversals.pdf
graphtraversals.pdfSeethaDinesh
 

Ähnlich wie BFS & DFS Search Techniques (20)

Data structure note
Data structure noteData structure note
Data structure note
 
Topological Sort and BFS
Topological Sort and BFSTopological Sort and BFS
Topological Sort and BFS
 
Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptxRiya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
 
LAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptxLAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptx
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
artificial intelligence
artificial intelligence artificial intelligence
artificial intelligence
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
A star algorithms
A star algorithmsA star algorithms
A star algorithms
 
Breath first Search and Depth first search
Breath first Search and Depth first searchBreath first Search and Depth first search
Breath first Search and Depth first search
 
Graphs
GraphsGraphs
Graphs
 
5 searching
5 searching5 searching
5 searching
 
Adsa u2 ver 1.0.
Adsa u2 ver 1.0.Adsa u2 ver 1.0.
Adsa u2 ver 1.0.
 
Ai1.pdf
Ai1.pdfAi1.pdf
Ai1.pdf
 
Depth first Search.pptx
Depth first Search.pptxDepth first Search.pptx
Depth first Search.pptx
 
Unit 4.2(graphs)
Unit 4.2(graphs)Unit 4.2(graphs)
Unit 4.2(graphs)
 
Analysis & design of algorithm
Analysis & design of algorithmAnalysis & design of algorithm
Analysis & design of algorithm
 
graphtraversals.pdf
graphtraversals.pdfgraphtraversals.pdf
graphtraversals.pdf
 

Mehr von lalithambiga kamaraj (20)

Firewall in Network Security
Firewall in Network SecurityFirewall in Network Security
Firewall in Network Security
 
Data Compression in Multimedia
Data Compression in MultimediaData Compression in Multimedia
Data Compression in Multimedia
 
Data CompressionMultimedia
Data CompressionMultimediaData CompressionMultimedia
Data CompressionMultimedia
 
Digital Audio in Multimedia
Digital Audio in MultimediaDigital Audio in Multimedia
Digital Audio in Multimedia
 
Network Security: Physical security
Network Security: Physical security Network Security: Physical security
Network Security: Physical security
 
Graphs in Data Structure
Graphs in Data StructureGraphs in Data Structure
Graphs in Data Structure
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
 
Estimating Software Maintenance Costs
Estimating Software Maintenance CostsEstimating Software Maintenance Costs
Estimating Software Maintenance Costs
 
Datamining
DataminingDatamining
Datamining
 
Digital Components
Digital ComponentsDigital Components
Digital Components
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
Io management disk scheduling algorithm
Io management disk scheduling algorithmIo management disk scheduling algorithm
Io management disk scheduling algorithm
 
Recovery system
Recovery systemRecovery system
Recovery system
 
File management
File managementFile management
File management
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Inheritance
InheritanceInheritance
Inheritance
 
Managing console of I/o operations & working with files
Managing console of I/o operations & working with filesManaging console of I/o operations & working with files
Managing console of I/o operations & working with files
 

Kürzlich hochgeladen

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 

Kürzlich hochgeladen (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

BFS & DFS Search Techniques

  • 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);