SlideShare uma empresa Scribd logo
1 de 15
Breadth First Search
&
Depth First Search
Ritesh Thakur
Vishal Thoke
Utsav Patel
Sushant Vaidkar
Vedant Valsangkar
Stack
► Stack is a linear data structure which follows a particular order in which the
operations are performed. The order may be LIFO(Last In First Out) or
FILO(First In Last Out).
► Mainly the following three basic operations are performed in the stack:
Push: Adds an item in the stack. If the stack is full, then it is said to be an
Overflow condition.
Pop: Removes an item from the stack. The items are popped in the reversed order
in which they are pushed. If the stack is empty, then it is said to be an Underflow
condition.
Peek or Top: Returns top element of stack.
isEmpty: Returns true if stack is empty, else false.
Queue
► Like Stack, Queue is a linear structure which follows a particular order in which
the operations are performed. The order is First In First Out (FIFO). A good
example of queue is any queue of consumers for a resource where the
consumer that came first is served first.
► Operations on Queue:
Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an
Overflow condition.
Dequeue: Removes an item from the queue. The items are popped in the same
order in which they are pushed. If the queue is empty, then it is said to be an
Underflow condition.
Front: Get the front item from queue.
Rear: Get the last item from queue.
Graph Traversal
Breadth First Search
● Start several paths at a time, and advance in each one
step at a time.
● The Breadth first search uses the FIFO Queue.
Depth First Search
● Once a possible path is found, continue the search
until the end of the path.
● The Depth first search uses the LIFO stack.
Breadth First Search
BFS is a traversing algorithm where you
should start traversing from a selected node
(source or starting node) and traverse the
graph layerwise thus exploring the neighbour
nodes (nodes which are directly connected to
source node). You must then move towards
the next-level neighbour nodes.
BFS Algorithm Complexity
The time complexity of the BFS algorithm is
represented in the form of O(V + E), where V is
the number of nodes and E is the number of
edges.
The space complexity of the algorithm is O(V)
Algorithm steps
► Step 1:
Take an Empty Queue.
► Step 2:
Select a starting node (visiting a node) and insert it into the Queue.
► Step 3:
Provided that the Queue is not empty, extract the node from the Queue and
insert its child nodes (exploring a node) into the Queue.
► Step 4:
Print the extracted node.
Application Of BFS
1. To build index by search index
2. For GPS navigation
3. Path finding algorithms
4. Cycle detection in an undirected graph
5. In minimum spanning tree
6. In Ford-Fulkerson algorithm to find maximum flow in a network
Depth First Search
► Depth first search (DFS) algorithm starts with the initial node of the graph G,
and then goes to deeper and deeper until we find the goal node or the node
which has no children. The algorithm, then backtracks from the dead end
towards the most recent node that is yet to be completely unexplored.
► The data structure which is being used in DFS is stack. The process is similar
to BFS algorithm. In DFS, the edges that leads to an unvisited node are called
discovery edges while the edges that leads to an already visited node are
called block edges.
DFS Algorithm Complexity
1)For a Graph G=(V, E) and n = |V| and m=|E.
2)When Adjacency List is used Complexity is O(m + n).
3)When Adjacency Matrix is used Scanning each row for
checking the connectivity of a Vertex is in order O(n).
So, Complexity is O(n2 )
4)DFS uses space O(|V|) in the worst case to store the
stack of vertices on the current search path as well as
the set of already-visited vertices
Algorithm steps
► Step 1
Push the root node in stack.
► Step 2
Loop until stack is empty.
► Step 3
Peek the node of the stack.
► Step 4
If the node has unvisited child nodes get the unvisited
child node mark it has travers and push it on stack.
Application Of DFS
1.For finding the path
2.To test if the graph is bipartite
3.For finding the strongly connected components of a
graph
4.For detecting cycles in a graph
Bfs and dfs

Mais conteúdo relacionado

Mais procurados

Modern Compiler Design
Modern Compiler DesignModern Compiler Design
Modern Compiler Design
nextlib
 
1212 regular meeting
1212 regular meeting1212 regular meeting
1212 regular meeting
marxliouville
 
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
 
Workshop 05 Review
Workshop 05 ReviewWorkshop 05 Review
Workshop 05 Review
migiwara
 

Mais procurados (20)

Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Modern Compiler Design
Modern Compiler DesignModern Compiler Design
Modern Compiler Design
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
 
1212 regular meeting
1212 regular meeting1212 regular meeting
1212 regular meeting
 
Lect9
Lect9Lect9
Lect9
 
Data structure
Data structureData structure
Data structure
 
Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searching
 
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,
 
Workshop 05 Review
Workshop 05 ReviewWorkshop 05 Review
Workshop 05 Review
 
Python programming l2
Python programming l2Python programming l2
Python programming l2
 
Gnu octave
Gnu octave Gnu octave
Gnu octave
 
Aes
AesAes
Aes
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...
 
Msc 1
Msc 1Msc 1
Msc 1
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Data structures
Data structuresData structures
Data structures
 
Avro2 tf: a data processing engine for tensorflow
Avro2 tf: a data processing engine for tensorflowAvro2 tf: a data processing engine for tensorflow
Avro2 tf: a data processing engine for tensorflow
 
Data structure day3
Data structure day3Data structure day3
Data structure day3
 

Semelhante a Bfs and dfs

bfs tree searching ,sortingUntitled presentation.pptx
bfs tree searching ,sortingUntitled presentation.pptxbfs tree searching ,sortingUntitled presentation.pptx
bfs tree searching ,sortingUntitled presentation.pptx
saurabhpandey679381
 
Review of basic data structures
Review of basic data structuresReview of basic data structures
Review of basic data structures
Deepa Rani
 

Semelhante a Bfs and dfs (20)

bfs tree searching ,sortingUntitled presentation.pptx
bfs tree searching ,sortingUntitled presentation.pptxbfs tree searching ,sortingUntitled presentation.pptx
bfs tree searching ,sortingUntitled presentation.pptx
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
graphtraversals.pdf
graphtraversals.pdfgraphtraversals.pdf
graphtraversals.pdf
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Depth first Search.pptx
Depth first Search.pptxDepth first Search.pptx
Depth first Search.pptx
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Queue and its operations
Queue and its operationsQueue and its operations
Queue and its operations
 
DFS.pptx
DFS.pptxDFS.pptx
DFS.pptx
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
Review of basic data structures
Review of basic data structuresReview of basic data structures
Review of basic data structures
 
Queues
Queues Queues
Queues
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 

Último

Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
chumtiyababu
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 

Bfs and dfs

  • 1. Breadth First Search & Depth First Search Ritesh Thakur Vishal Thoke Utsav Patel Sushant Vaidkar Vedant Valsangkar
  • 2. Stack ► Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). ► Mainly the following three basic operations are performed in the stack: Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. Peek or Top: Returns top element of stack. isEmpty: Returns true if stack is empty, else false.
  • 3. Queue ► Like Stack, Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of queue is any queue of consumers for a resource where the consumer that came first is served first. ► Operations on Queue: Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition. Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition. Front: Get the front item from queue. Rear: Get the last item from queue.
  • 4. Graph Traversal Breadth First Search ● Start several paths at a time, and advance in each one step at a time. ● The Breadth first search uses the FIFO Queue. Depth First Search ● Once a possible path is found, continue the search until the end of the path. ● The Depth first search uses the LIFO stack.
  • 5. Breadth First Search BFS is a traversing algorithm where you should start traversing from a selected node (source or starting node) and traverse the graph layerwise thus exploring the neighbour nodes (nodes which are directly connected to source node). You must then move towards the next-level neighbour nodes.
  • 6. BFS Algorithm Complexity The time complexity of the BFS algorithm is represented in the form of O(V + E), where V is the number of nodes and E is the number of edges. The space complexity of the algorithm is O(V)
  • 7. Algorithm steps ► Step 1: Take an Empty Queue. ► Step 2: Select a starting node (visiting a node) and insert it into the Queue. ► Step 3: Provided that the Queue is not empty, extract the node from the Queue and insert its child nodes (exploring a node) into the Queue. ► Step 4: Print the extracted node.
  • 8.
  • 9. Application Of BFS 1. To build index by search index 2. For GPS navigation 3. Path finding algorithms 4. Cycle detection in an undirected graph 5. In minimum spanning tree 6. In Ford-Fulkerson algorithm to find maximum flow in a network
  • 10. Depth First Search ► Depth first search (DFS) algorithm starts with the initial node of the graph G, and then goes to deeper and deeper until we find the goal node or the node which has no children. The algorithm, then backtracks from the dead end towards the most recent node that is yet to be completely unexplored. ► The data structure which is being used in DFS is stack. The process is similar to BFS algorithm. In DFS, the edges that leads to an unvisited node are called discovery edges while the edges that leads to an already visited node are called block edges.
  • 11. DFS Algorithm Complexity 1)For a Graph G=(V, E) and n = |V| and m=|E. 2)When Adjacency List is used Complexity is O(m + n). 3)When Adjacency Matrix is used Scanning each row for checking the connectivity of a Vertex is in order O(n). So, Complexity is O(n2 ) 4)DFS uses space O(|V|) in the worst case to store the stack of vertices on the current search path as well as the set of already-visited vertices
  • 12. Algorithm steps ► Step 1 Push the root node in stack. ► Step 2 Loop until stack is empty. ► Step 3 Peek the node of the stack. ► Step 4 If the node has unvisited child nodes get the unvisited child node mark it has travers and push it on stack.
  • 13.
  • 14. Application Of DFS 1.For finding the path 2.To test if the graph is bipartite 3.For finding the strongly connected components of a graph 4.For detecting cycles in a graph