SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Modern Compiler Design
Modern Compiler DesignModern Compiler Design
Modern Compiler Designnextlib
 
1212 regular meeting
1212 regular meeting1212 regular meeting
1212 regular meetingmarxliouville
 
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 searchingKawsar Hamid Sumon
 
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 Reviewmigiwara
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFSRajandeep Gill
 
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...Edureka!
 
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 tensorflowXuhong Zhang
 

Was ist angesagt? (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
 

Ähnlich wie Bfs and dfs

Ähnlich wie 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
 

Kürzlich hochgeladen

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Kürzlich hochgeladen (20)

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

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