SlideShare a Scribd company logo
1 of 35
Graph Traversal
Depth-First Search
• Using Stack
Breadth-First Search
• Using Queue
Overview
• Goal
– To systematically visit the nodes of a graph
• A tree is a directed, acyclic, graph (DAG)
• If the graph is a tree,
– DFS is exhibited by preorder, postorder, and
(for binary trees) inorder traversals
– BFS is exhibited by level-order traversal
Depth-First Search
// recursive, preorder, depth-first search
void dfs (Node v) {
if (v == null)
return;
if (v not yet visited)
visit&mark(v); // visit node before adjacent nodes
for (each w adjacent to v)
if (w has not yet been visited)
dfs(w);
} // dfs
Example
0
7
1
5
4
3
2
6
Policy: Visit adjacent nodes in increasing index order
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6 4
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5
Push 5
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5
Pop/Visit/Mark 5
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5
1
2
Push 2, Push 1
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1
2
Pop/Visit/Mark 1
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1
0
4
2
Push 4, Push 0
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0
4
2
Pop/Visit/Mark 0
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0
3
7
4
2
Push 7, Push 3
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3
7
4
2
Pop/Visit/Mark 3
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3
7
4
2
2 is already in stack
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7
7
4
2
Pop/Mark/Visit 7
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7
6
4
2
Push 6
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7 6
4
2
Pop/Mark/Visit 6
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7 6 4
2
Pop/Mark/Visit 4
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7 6 4 2
Pop/Mark/Visit 2
DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7 6 4 2
Done
Breadth-first Search
• Ripples in a pond
• Visit designated node
• Then visited unvisited nodes a distance i
away, where i = 1, 2, 3, etc.
• For nodes the same distance away, visit
nodes in systematic manner (eg. increasing
index order)
Breadth-First Search
// non-recursive, preorder, breadth-first search
void bfs (Node v) {
if (v == null)
return;
enqueue(v);
while (queue is not empty) {
dequeue(v);
if (v has not yet been visited)
mark&visit(v);
for (each w adjacent to v)
if (w has not yet been visited && has not been queued)
enqueue(w);
} // while
} // bfs
BFS: Start with Node 5
7
1
5
4
3
2
6
0
BFS: Start with Node 5
7
1
5
4
3
2
6
0
5
BFS: Node one-away
7
1
5
4
3
2
6
5
0
5 1 2
BFS: Visit 1 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1
0
5 1 2 0 4
BFS: Visit 2 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1 2
0
5 1 2 0 4
BFS: Visit 0 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1 2 0
0
5 1 2 0 4 3 7
BFS: Visit 4 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1 2 0 4
0
5 1 2 0 4 3 7
BFS: Visit 3 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1 2 0 4 3
0
5 1 2 0 4 3 7
BFS: Visit 7 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1 2 0 4 3 7
0
5 1 2 0 4 3 7 6
BFS: Visit 6 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1 2 0 4 3 7 6
0
5 1 2 0 4 3 7 6
BFS Traversal Result
7
1
5
4
3
2
6
5 1 2 0 4 3 7 6
0
Applications of BFS
• Computing Distances: Given a source vertex x, compute the distance
of all vertices from x.
• Checking for cycles in a graph: Given an undirected graph G, report
whether there exists a cycle in the graph or not. (Note: won’t work for
directed graphs)
• Checking for bipartite graph: Given a graph, check whether it is
bipartite or not? A graph is said to be bipartite if there is a partition of
the vertex set V into two sets V1 and V2 such that if two vertices are
adjacent, either both are in V1 or both are in V2.
• Reachability: Given a graph G and vertices x and y, determine if there
exists a path from x to y.
Applications of DFS
• Computing Strongly Connected Components: A directed graph is
strongly connected if there exists a path from every vertex to every
other vertex. Trivial Algo: Perform DFS n times. Efficient Algo:
Single DFS.
• Checking for Biconnected Graph: A graph is biconnected if removal of
any vertex does not affect connectivity of the other vertices. Used to
test if network is robust to failure of certain nodes. A single DFS
traversal algorithm.
• Topological Ordering: Topological sorting for Directed Acyclic Graph
(DAG) is a linear ordering of vertices such that for every directed edge
(x, y), vertex x comes before y in the ordering

More Related Content

What's hot

Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxAlbin562191
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)Trupti Agrawal
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithmhansa khan
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02Krish_ver2
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )Sazzad Hossain
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure Ankit Kumar Singh
 
Graph representation
Graph representationGraph representation
Graph representationTech_MX
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and GraphsIntro C# Book
 
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
 
Unit 4 external sorting
Unit 4   external sortingUnit 4   external sorting
Unit 4 external sortingDrkhanchanaR
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structurehafsa komal
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentationAlizay Khan
 

What's hot (20)

Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Graph representation
Graph representationGraph representation
Graph representation
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Heap sort
Heap sortHeap sort
Heap sort
 
Dijkstra
DijkstraDijkstra
Dijkstra
 
Unit 4 external sorting
Unit 4   external sortingUnit 4   external sorting
Unit 4 external sorting
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Linked List
Linked ListLinked List
Linked List
 

Similar to Graph traversal-BFS & DFS

BFS & DFS in Data Structure
BFS & DFS in Data StructureBFS & DFS in Data Structure
BFS & DFS in Data StructureMeghaj Mallick
 
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
 
LAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptxLAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptxionutionescuionut
 
Analysis & design of algorithm
Analysis & design of algorithmAnalysis & design of algorithm
Analysis & design of algorithmrahela bham
 
bfs tree searching ,sortingUntitled presentation.pptx
bfs tree searching ,sortingUntitled presentation.pptxbfs tree searching ,sortingUntitled presentation.pptx
bfs tree searching ,sortingUntitled presentation.pptxsaurabhpandey679381
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs applicationUmme habiba
 
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
 
Graph Traversals
Graph TraversalsGraph Traversals
Graph TraversalsMaryJacob24
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsSigSegVSquad
 
Sec B Graph traversal.pptx
Sec B Graph traversal.pptxSec B Graph traversal.pptx
Sec B Graph traversal.pptxtabusam1
 
Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Deepak John
 

Similar to Graph traversal-BFS & DFS (20)

BFS & DFS in Data Structure
BFS & DFS in Data StructureBFS & DFS in Data Structure
BFS & DFS in Data Structure
 
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
 
LAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptxLAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptx
 
kumattt).pptx
kumattt).pptxkumattt).pptx
kumattt).pptx
 
Analysis & design of algorithm
Analysis & design of algorithmAnalysis & design of algorithm
Analysis & design of algorithm
 
Graphs
GraphsGraphs
Graphs
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graph
GraphGraph
Graph
 
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
 
bfs tree searching ,sortingUntitled presentation.pptx
bfs tree searching ,sortingUntitled presentation.pptxbfs tree searching ,sortingUntitled presentation.pptx
bfs tree searching ,sortingUntitled presentation.pptx
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 
Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
 
2.5 dfs & bfs
2.5 dfs & bfs2.5 dfs & bfs
2.5 dfs & bfs
 
Graph Traversals
Graph TraversalsGraph Traversals
Graph Traversals
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
 
Graph
GraphGraph
Graph
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Sec B Graph traversal.pptx
Sec B Graph traversal.pptxSec B Graph traversal.pptx
Sec B Graph traversal.pptx
 
Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3
 

More from Rajandeep Gill

Chronic Kidney Disease Prediction
Chronic Kidney Disease PredictionChronic Kidney Disease Prediction
Chronic Kidney Disease PredictionRajandeep Gill
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and ComplexityRajandeep Gill
 
Software Engineering Methodology
Software Engineering MethodologySoftware Engineering Methodology
Software Engineering MethodologyRajandeep Gill
 
Enterprise and Enterprise Application
Enterprise and Enterprise ApplicationEnterprise and Enterprise Application
Enterprise and Enterprise ApplicationRajandeep Gill
 

More from Rajandeep Gill (7)

Chronic Kidney Disease Prediction
Chronic Kidney Disease PredictionChronic Kidney Disease Prediction
Chronic Kidney Disease Prediction
 
4. avl
4. avl4. avl
4. avl
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Operating system quiz
Operating system quizOperating system quiz
Operating system quiz
 
Deadlock
DeadlockDeadlock
Deadlock
 
Software Engineering Methodology
Software Engineering MethodologySoftware Engineering Methodology
Software Engineering Methodology
 
Enterprise and Enterprise Application
Enterprise and Enterprise ApplicationEnterprise and Enterprise Application
Enterprise and Enterprise Application
 

Recently uploaded

scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...ppkakm
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxMustafa Ahmed
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesRashidFaridChishti
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...josephjonse
 
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 startQuintin Balsdon
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxkalpana413121
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxMustafa Ahmed
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxhublikarsn
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsmeharikiros2
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
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.pptxSCMS School of Architecture
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Ramkumar k
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfsumitt6_25730773
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...ronahami
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessorAshwiniTodkar4
 

Recently uploaded (20)

scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
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
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
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
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor
 

Graph traversal-BFS & DFS

  • 1. Graph Traversal Depth-First Search • Using Stack Breadth-First Search • Using Queue
  • 2. Overview • Goal – To systematically visit the nodes of a graph • A tree is a directed, acyclic, graph (DAG) • If the graph is a tree, – DFS is exhibited by preorder, postorder, and (for binary trees) inorder traversals – BFS is exhibited by level-order traversal
  • 3. Depth-First Search // recursive, preorder, depth-first search void dfs (Node v) { if (v == null) return; if (v not yet visited) visit&mark(v); // visit node before adjacent nodes for (each w adjacent to v) if (w has not yet been visited) dfs(w); } // dfs
  • 4. Example 0 7 1 5 4 3 2 6 Policy: Visit adjacent nodes in increasing index order
  • 5. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4
  • 6. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 Push 5
  • 7. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 Pop/Visit/Mark 5
  • 8. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 2 Push 2, Push 1
  • 9. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 2 Pop/Visit/Mark 1
  • 10. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 4 2 Push 4, Push 0
  • 11. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 4 2 Pop/Visit/Mark 0
  • 12. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 4 2 Push 7, Push 3
  • 13. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 4 2 Pop/Visit/Mark 3
  • 14. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 4 2 2 is already in stack
  • 15. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 7 4 2 Pop/Mark/Visit 7
  • 16. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 6 4 2 Push 6
  • 17. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 6 4 2 Pop/Mark/Visit 6
  • 18. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 6 4 2 Pop/Mark/Visit 4
  • 19. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 6 4 2 Pop/Mark/Visit 2
  • 20. DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 6 4 2 Done
  • 21. Breadth-first Search • Ripples in a pond • Visit designated node • Then visited unvisited nodes a distance i away, where i = 1, 2, 3, etc. • For nodes the same distance away, visit nodes in systematic manner (eg. increasing index order)
  • 22. Breadth-First Search // non-recursive, preorder, breadth-first search void bfs (Node v) { if (v == null) return; enqueue(v); while (queue is not empty) { dequeue(v); if (v has not yet been visited) mark&visit(v); for (each w adjacent to v) if (w has not yet been visited && has not been queued) enqueue(w); } // while } // bfs
  • 23. BFS: Start with Node 5 7 1 5 4 3 2 6 0
  • 24. BFS: Start with Node 5 7 1 5 4 3 2 6 0 5
  • 26. BFS: Visit 1 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 0 5 1 2 0 4
  • 27. BFS: Visit 2 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 2 0 5 1 2 0 4
  • 28. BFS: Visit 0 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 2 0 0 5 1 2 0 4 3 7
  • 29. BFS: Visit 4 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 2 0 4 0 5 1 2 0 4 3 7
  • 30. BFS: Visit 3 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 2 0 4 3 0 5 1 2 0 4 3 7
  • 31. BFS: Visit 7 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 2 0 4 3 7 0 5 1 2 0 4 3 7 6
  • 32. BFS: Visit 6 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 2 0 4 3 7 6 0 5 1 2 0 4 3 7 6
  • 34. Applications of BFS • Computing Distances: Given a source vertex x, compute the distance of all vertices from x. • Checking for cycles in a graph: Given an undirected graph G, report whether there exists a cycle in the graph or not. (Note: won’t work for directed graphs) • Checking for bipartite graph: Given a graph, check whether it is bipartite or not? A graph is said to be bipartite if there is a partition of the vertex set V into two sets V1 and V2 such that if two vertices are adjacent, either both are in V1 or both are in V2. • Reachability: Given a graph G and vertices x and y, determine if there exists a path from x to y.
  • 35. Applications of DFS • Computing Strongly Connected Components: A directed graph is strongly connected if there exists a path from every vertex to every other vertex. Trivial Algo: Perform DFS n times. Efficient Algo: Single DFS. • Checking for Biconnected Graph: A graph is biconnected if removal of any vertex does not affect connectivity of the other vertices. Used to test if network is robust to failure of certain nodes. A single DFS traversal algorithm. • Topological Ordering: Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge (x, y), vertex x comes before y in the ordering