SlideShare a Scribd company logo
1 of 21
GRAPH TRAVERSAL
SUBMITTED BY
GROUP 6
SUBMITTED TO
MAM JAVERIA NAZ
CONTENT TABLE
• Graph Traversal Definition
• Types of Graph Traversal
GRAPH TRAVERSAL
• In computer science, graph traversal (also known
as graph search) refers to the process of visiting
(checking and/or updating) each vertex in a
graph.
OR
• Graph traversal is the problem of visiting all the
vertices of a graph in some systematic order.
TYPES OF GRAPH
TRAVERSAL
• There are mainly two ways to traverse a graph.
1. Breadth First Search
2. Depth First Search
BREADTH FIRST SEARCH
• Breadth First Search (BFS) starts at starting level-0
vertex X of the graph G
• Then we visit all the vertices that are the neighbors
of X.
• After visiting, we mark the vertices as "visited," and
place them into level-1.
• Then we start from the level-1 vertices and apply
the same method on every level-1 vertex and so on.
• The BFS traversal terminates when every vertex of
the graph has been visited.
BFS ALGORITHM
• The concept is to visit all the neighbor vertices
before visiting other neighbor vertices of neighbor
vertices.
• Initialize status of all nodes as “Ready”.
• Put source vertex in a queue and change its status
to “Waiting”.
• Repeat the following two steps until queue is empty
−
• Remove the first vertex from the queue and mark it as
“Visited”.
• Add to the rear of queue all neighbors of the removed
vertex whose status is “Ready”. Mark their status as
“Waiting”.
PROBLEM
• Let us take a graph (Source vertex is ‘a’) and apply
the BFS algorithm to find out the traversal order.
SOLUTION
• Initialize status of all vertices to “Ready”.
• Put a in queue and change its status to “Waiting”.
• Remove a from queue, mark it as “Visited”.
• Add a’s neighbors in “Ready” state b, d and e to
end of queue and mark them as “Waiting”.
• Remove b from queue, mark it as “Visited”, put its
“Ready” neighbor c at end of queue and mark c as
“Waiting”.
• Remove d from queue and mark it as “Visited”. It
has no neighbor in “Ready” state.
• Remove e from queue and mark it as “Visited”. It
has no neighbor in “Ready” state.
• Remove c from queue and mark it as “Visited”. It
has no neighbor in “Ready” state.
• Queue is empty so stop.
• So the traversal order is −
• $a rightarrow b rightarrow d rightarrow e rightarrow c$
• The alternate orders of traversal are −
• $a rightarrow b rightarrow e rightarrow d rightarrow c$
• Or, $a rightarrow d rightarrow b rightarrow e rightarrow c$
• Or, $a rightarrow e rightarrow b rightarrow d rightarrow c$
• Or, $a rightarrow b rightarrow e rightarrow d rightarrow c$
• Or, $a rightarrow d rightarrow e rightarrow b rightarrow c$
APPLICATION OF BFS
• Finding the shortest path
• Minimum spanning tree for un-weighted graph
• GPS navigation system
• Detecting cycles in an undirected graph
• Finding all nodes within one connected component
COMPLEXITY ANALYSIS
• Let G(V, E)be a graph with |V| number of vertices
and |E| number of edges. If breadth first search
algorithm visits every vertex in the graph and
checks every edge, then its time complexity would
be −
• O( | V | + | E | ). O( | E | )
• It may vary between O(1) and O(|V2|)
DEPTH FIRST SEARCH
• Depth First Search (DFS) algorithm starts from a vertex
v, then it traverses to its adjacent vertex (say x) that has
not been visited before and mark as "visited" and goes
on with the adjacent vertex of x and so on.
• If at any vertex, it encounters that all the adjacent
vertices are visited, then it backtracks until it finds the
first vertex having an adjacent vertex that has not been
traversed before. Then, it traverses that vertex,
continues with its adjacent vertices until it traverses all
visited vertices and has to backtrack again. In this way, it
will traverse all the vertices reachable from the initial
vertex v.
DFS ALGORITHM
• The concept is to visit all the neighbor vertices of a
neighbor vertex before visiting the other neighbor
vertices.
• Initialize status of all nodes as “Ready”
• Put source vertex in a stack and change its status to
“Waiting”
• Repeat the following two steps until stack is empty −
• Pop the top vertex from the stack and mark it as “Visited”
• Push onto the top of the stack all neighbors of the removed
vertex whose status is “Ready”. Mark their status as
“Waiting”.
PROBLEM
• Let us take a graph (Source vertex is ‘a’) and apply
the DFS algorithm to find out the traversal order.
SOLUTION
• Initialize status of all vertices to “Ready”.
• Push a in stack and change its status to “Waiting”.
• Pop a and mark it as “Visited”.
• Push a’s neighbors in “Ready” state e, d and b to top of stack and
mark them as “Waiting”.
• Pop b from stack, mark it as “Visited”, push its “Ready”
neighbor c onto stack.
• Pop c from stack and mark it as “Visited”. It has no “Ready”
neighbor.
• Pop d from stack and mark it as “Visited”. It has no “Ready”
neighbor.
• Pop e from stack and mark it as “Visited”. It has no “Ready”
neighbor.
• Stack is empty. So stop.
• So the traversal order is −
• $a rightarrow b rightarrow c rightarrow d rightarrow e$
• The alternate orders of traversal are −
• $a rightarrow e rightarrow b rightarrow c rightarrow d$
• Or, $a rightarrow b rightarrow e rightarrow c rightarrow d$
• Or, $a rightarrow d rightarrow e rightarrow b rightarrow c$
• Or, $a rightarrow d rightarrow c rightarrow e rightarrow b$
• Or, $a rightarrow d rightarrow c rightarrow b rightarrow e$
COMPLEXITY ANALYSIS
• Let G(V, E) be a graph with |V| number of vertices
and |E| number of edges. If DFS algorithm visits
every vertex in the graph and checks every edge,
then the time complexity is −
• circleddash ( | V | + | E | )
APPLICATIONS
• Detecting cycle in a graph
• To find topological sorting
• To test if a graph is bipartite
• Finding connected components
• Finding the bridges of a graph
• Finding bi-connectivity in graphs
• Solving the Knight’s Tour problem
• Solving puzzles with only one solution
Sec B Graph traversal.pptx

More Related Content

Similar to Sec B Graph traversal.pptx (6)

130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
 
Graphs
GraphsGraphs
Graphs
 
Skiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivitySkiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivity
 
BFS (Breadth First Search) Tree Traversal
BFS (Breadth First Search)  Tree TraversalBFS (Breadth First Search)  Tree Traversal
BFS (Breadth First Search) Tree Traversal
 
Unit 4 dsuc
Unit 4 dsucUnit 4 dsuc
Unit 4 dsuc
 
Lec216
Lec216Lec216
Lec216
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

Sec B Graph traversal.pptx

  • 1. GRAPH TRAVERSAL SUBMITTED BY GROUP 6 SUBMITTED TO MAM JAVERIA NAZ
  • 2. CONTENT TABLE • Graph Traversal Definition • Types of Graph Traversal
  • 3. GRAPH TRAVERSAL • In computer science, graph traversal (also known as graph search) refers to the process of visiting (checking and/or updating) each vertex in a graph. OR • Graph traversal is the problem of visiting all the vertices of a graph in some systematic order.
  • 4. TYPES OF GRAPH TRAVERSAL • There are mainly two ways to traverse a graph. 1. Breadth First Search 2. Depth First Search
  • 5. BREADTH FIRST SEARCH • Breadth First Search (BFS) starts at starting level-0 vertex X of the graph G • Then we visit all the vertices that are the neighbors of X. • After visiting, we mark the vertices as "visited," and place them into level-1. • Then we start from the level-1 vertices and apply the same method on every level-1 vertex and so on. • The BFS traversal terminates when every vertex of the graph has been visited.
  • 6. BFS ALGORITHM • The concept is to visit all the neighbor vertices before visiting other neighbor vertices of neighbor vertices. • Initialize status of all nodes as “Ready”. • Put source vertex in a queue and change its status to “Waiting”.
  • 7. • Repeat the following two steps until queue is empty − • Remove the first vertex from the queue and mark it as “Visited”. • Add to the rear of queue all neighbors of the removed vertex whose status is “Ready”. Mark their status as “Waiting”.
  • 8. PROBLEM • Let us take a graph (Source vertex is ‘a’) and apply the BFS algorithm to find out the traversal order.
  • 9. SOLUTION • Initialize status of all vertices to “Ready”. • Put a in queue and change its status to “Waiting”. • Remove a from queue, mark it as “Visited”. • Add a’s neighbors in “Ready” state b, d and e to end of queue and mark them as “Waiting”. • Remove b from queue, mark it as “Visited”, put its “Ready” neighbor c at end of queue and mark c as “Waiting”.
  • 10. • Remove d from queue and mark it as “Visited”. It has no neighbor in “Ready” state. • Remove e from queue and mark it as “Visited”. It has no neighbor in “Ready” state. • Remove c from queue and mark it as “Visited”. It has no neighbor in “Ready” state. • Queue is empty so stop.
  • 11. • So the traversal order is − • $a rightarrow b rightarrow d rightarrow e rightarrow c$ • The alternate orders of traversal are − • $a rightarrow b rightarrow e rightarrow d rightarrow c$ • Or, $a rightarrow d rightarrow b rightarrow e rightarrow c$ • Or, $a rightarrow e rightarrow b rightarrow d rightarrow c$ • Or, $a rightarrow b rightarrow e rightarrow d rightarrow c$ • Or, $a rightarrow d rightarrow e rightarrow b rightarrow c$
  • 12. APPLICATION OF BFS • Finding the shortest path • Minimum spanning tree for un-weighted graph • GPS navigation system • Detecting cycles in an undirected graph • Finding all nodes within one connected component
  • 13. COMPLEXITY ANALYSIS • Let G(V, E)be a graph with |V| number of vertices and |E| number of edges. If breadth first search algorithm visits every vertex in the graph and checks every edge, then its time complexity would be − • O( | V | + | E | ). O( | E | ) • It may vary between O(1) and O(|V2|)
  • 14. DEPTH FIRST SEARCH • Depth First Search (DFS) algorithm starts from a vertex v, then it traverses to its adjacent vertex (say x) that has not been visited before and mark as "visited" and goes on with the adjacent vertex of x and so on. • If at any vertex, it encounters that all the adjacent vertices are visited, then it backtracks until it finds the first vertex having an adjacent vertex that has not been traversed before. Then, it traverses that vertex, continues with its adjacent vertices until it traverses all visited vertices and has to backtrack again. In this way, it will traverse all the vertices reachable from the initial vertex v.
  • 15. DFS ALGORITHM • The concept is to visit all the neighbor vertices of a neighbor vertex before visiting the other neighbor vertices. • Initialize status of all nodes as “Ready” • Put source vertex in a stack and change its status to “Waiting” • Repeat the following two steps until stack is empty − • Pop the top vertex from the stack and mark it as “Visited” • Push onto the top of the stack all neighbors of the removed vertex whose status is “Ready”. Mark their status as “Waiting”.
  • 16. PROBLEM • Let us take a graph (Source vertex is ‘a’) and apply the DFS algorithm to find out the traversal order.
  • 17. SOLUTION • Initialize status of all vertices to “Ready”. • Push a in stack and change its status to “Waiting”. • Pop a and mark it as “Visited”. • Push a’s neighbors in “Ready” state e, d and b to top of stack and mark them as “Waiting”. • Pop b from stack, mark it as “Visited”, push its “Ready” neighbor c onto stack. • Pop c from stack and mark it as “Visited”. It has no “Ready” neighbor. • Pop d from stack and mark it as “Visited”. It has no “Ready” neighbor. • Pop e from stack and mark it as “Visited”. It has no “Ready” neighbor. • Stack is empty. So stop.
  • 18. • So the traversal order is − • $a rightarrow b rightarrow c rightarrow d rightarrow e$ • The alternate orders of traversal are − • $a rightarrow e rightarrow b rightarrow c rightarrow d$ • Or, $a rightarrow b rightarrow e rightarrow c rightarrow d$ • Or, $a rightarrow d rightarrow e rightarrow b rightarrow c$ • Or, $a rightarrow d rightarrow c rightarrow e rightarrow b$ • Or, $a rightarrow d rightarrow c rightarrow b rightarrow e$
  • 19. COMPLEXITY ANALYSIS • Let G(V, E) be a graph with |V| number of vertices and |E| number of edges. If DFS algorithm visits every vertex in the graph and checks every edge, then the time complexity is − • circleddash ( | V | + | E | )
  • 20. APPLICATIONS • Detecting cycle in a graph • To find topological sorting • To test if a graph is bipartite • Finding connected components • Finding the bridges of a graph • Finding bi-connectivity in graphs • Solving the Knight’s Tour problem • Solving puzzles with only one solution