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

What's hot

finite automata
 finite automata finite automata
finite automata
sabiya sabiya
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
Krish_ver2
 
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,8neutron8
 
BFS
BFSBFS
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
Anandhasilambarasan D
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentationAlizay Khan
 
2_4 Finite Automata.ppt
2_4 Finite Automata.ppt2_4 Finite Automata.ppt
2_4 Finite Automata.ppt
Ratnakar Mikkili
 
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)
Johnnatan Messias
 
2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
Krish_ver2
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
Dhrumil Panchal
 
AB-RNA-alignments-2011
AB-RNA-alignments-2011AB-RNA-alignments-2011
AB-RNA-alignments-2011Paula Tataru
 
11 x1 t12 07 primitive function
11 x1 t12 07 primitive function11 x1 t12 07 primitive function
11 x1 t12 07 primitive functionNigel Simmons
 
Chapter 2 2 1 2
Chapter 2 2 1 2Chapter 2 2 1 2
Chapter 2 2 1 2bolovv
 
Chirp z algorithm 1
Chirp z algorithm 1Chirp z algorithm 1
Chirp z algorithm 1
College of Engineering pune
 
Bfs and dfs
Bfs and dfsBfs and dfs
Bfs and dfs
utsav patel
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
azmizryk
 
Pertemuan 4
Pertemuan 4Pertemuan 4
Pertemuan 4
Aswar Amiruddin
 
Missilecommand
MissilecommandMissilecommand
MissilecommandSusan Gold
 

What's hot (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
 

Similar to Data structure

Data structure note
Data structure noteData structure note
Data structure note
Muhammad Nawaz
 
Topological Sort and BFS
Topological Sort and BFSTopological Sort and BFS
Topological Sort and BFS
ArchanaMani2
 
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.pptx
RIYABEPARI
 
LAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptxLAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptx
ionutionescuionut
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
SigSegVSquad
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
 
artificial intelligence
artificial intelligence artificial intelligence
artificial intelligence
ilias ahmed
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
A star algorithms
A star algorithmsA star algorithms
A star algorithms
sandeep54552
 
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
Kirti Verma
 
Graphs
GraphsGraphs
Adsa u2 ver 1.0.
Adsa u2 ver 1.0.Adsa u2 ver 1.0.
Adsa u2 ver 1.0.
Dr. C.V. Suresh Babu
 
Depth first Search.pptx
Depth first Search.pptxDepth first Search.pptx
Depth first Search.pptx
AleezaShakeel3
 
Unit 4.2(graphs)
Unit 4.2(graphs)Unit 4.2(graphs)
Unit 4.2(graphs)
DurgaDeviCbit
 
Analysis & design of algorithm
Analysis & design of algorithmAnalysis & design of algorithm
Analysis & design of algorithm
rahela bham
 
graphtraversals.pdf
graphtraversals.pdfgraphtraversals.pdf
graphtraversals.pdf
SeethaDinesh
 

Similar to Data structure (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
 

More from lalithambiga kamaraj

Firewall in Network Security
Firewall in Network SecurityFirewall in Network Security
Firewall in Network Security
lalithambiga kamaraj
 
Data Compression in Multimedia
Data Compression in MultimediaData Compression in Multimedia
Data Compression in Multimedia
lalithambiga kamaraj
 
Data CompressionMultimedia
Data CompressionMultimediaData CompressionMultimedia
Data CompressionMultimedia
lalithambiga kamaraj
 
Digital Audio in Multimedia
Digital Audio in MultimediaDigital Audio in Multimedia
Digital Audio in Multimedia
lalithambiga kamaraj
 
Network Security: Physical security
Network Security: Physical security Network Security: Physical security
Network Security: Physical security
lalithambiga kamaraj
 
Graphs in Data Structure
Graphs in Data StructureGraphs in Data Structure
Graphs in Data Structure
lalithambiga kamaraj
 
Package in Java
Package in JavaPackage in Java
Package in Java
lalithambiga kamaraj
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
lalithambiga kamaraj
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
lalithambiga kamaraj
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
lalithambiga kamaraj
 
Estimating Software Maintenance Costs
Estimating Software Maintenance CostsEstimating Software Maintenance Costs
Estimating Software Maintenance Costs
lalithambiga kamaraj
 
Datamining
DataminingDatamining
Digital Components
Digital ComponentsDigital Components
Digital Components
lalithambiga kamaraj
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
lalithambiga kamaraj
 
Io management disk scheduling algorithm
Io management disk scheduling algorithmIo management disk scheduling algorithm
Io management disk scheduling algorithm
lalithambiga kamaraj
 
Recovery system
Recovery systemRecovery system
Recovery system
lalithambiga kamaraj
 
File management
File managementFile management
File management
lalithambiga kamaraj
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
lalithambiga kamaraj
 
Inheritance
InheritanceInheritance
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
lalithambiga kamaraj
 

More from 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
 

Recently uploaded

TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 

Recently uploaded (20)

TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 

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