SlideShare a Scribd company logo
Graph traversal
•A graph traversal is a systematic way of
visiting the nodes in a specific order
•Types
•Depth first traversal
•Breadth first traversal
Breadth first traversal(BFS)
• Graph G starts from an unvisited vertex u
• Then all unvisited vertices vi adjacent to u are
visited and then all unvisited vertices wj
adjacent to vj are visited and so on
• Choose any node in the graph designate it as
the search node & mark it as visited
• Using the adjacency matrix of the graph, find
all the unvisited adjacent nodes to the search
node and enqueue them into the queue Q
• Then the search node is dequeued from the
queue. Mark that node as visited and select
the new search node
• Repeat step 2 & 3 using the new search node
Void BFS(vertex u)
{
initialize queue Q;
visited[u]=1;
enqueue(u,Q);
while(!isempty)
{
u= Dequeue(Q)
print u
for all vertices v adjacent to u do
if (visited[v]==0) then
{
Enqueue(v,Q)
visited[v]=1;
}
}
}
applications
• To check whether the graph is connected or
not
Depth first search
• Depth first search works by selecting one
vertex V of G as a start vertex
• V is marked visited
• Then each unvisited vertex adjacent to V is
searched using depth first search recursively
• This process continues until a dead end / no
adjacent unvisited vertices is encountered
1. Choose any node in the graph. Designate it as
the search node and mark it as visited
2. Using the adjacency matrix of the graph, find
a node adjacent to the search node that has
not been visited yet. Designate this as the
new search node and mark it as visited.
3. Repeat step 1 & 2 using the new search node
Routine for DFS
Void DFS(Vertex V)
{
visited[V]=True;
for each W adjacent to V
if (! Visited[W])
DFS(W);
}

More Related Content

Recently uploaded (20)

PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
monopile foundation seminar topic for civil engineering students
Ahina5
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PPTX
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPTX
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
PPTX
Snet+Pro+Service+Software_SNET+Pro+2+Instructions.pptx
jenilsatikuvar1
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
monopile foundation seminar topic for civil engineering students
Ahina5
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
Snet+Pro+Service+Software_SNET+Pro+2+Instructions.pptx
jenilsatikuvar1
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 

Featured (20)

PDF
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
 
PDF
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
 
PDF
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
 
PDF
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
 
PDF
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
PDF
Everything You Need To Know About ChatGPT
Expeed Software
 
PDF
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
PDF
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
PDF
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
PDF
Skeleton Culture Code
Skeleton Technologies
 
PDF
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
PDF
Content Methodology: A Best Practices Report (Webinar)
contently
 
PPTX
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
PDF
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
PDF
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
PDF
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
PDF
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
PDF
Getting into the tech field. what next
Tessa Mero
 
PDF
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
PDF
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
 
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
 
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
 
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
 
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Ad

DS - Graph Traversal

  • 1. Graph traversal •A graph traversal is a systematic way of visiting the nodes in a specific order •Types •Depth first traversal •Breadth first traversal
  • 2. Breadth first traversal(BFS) • Graph G starts from an unvisited vertex u • Then all unvisited vertices vi adjacent to u are visited and then all unvisited vertices wj adjacent to vj are visited and so on
  • 3. • Choose any node in the graph designate it as the search node & mark it as visited • Using the adjacency matrix of the graph, find all the unvisited adjacent nodes to the search node and enqueue them into the queue Q • Then the search node is dequeued from the queue. Mark that node as visited and select the new search node • Repeat step 2 & 3 using the new search node
  • 4. Void BFS(vertex u) { initialize queue Q; visited[u]=1; enqueue(u,Q); while(!isempty) { u= Dequeue(Q) print u for all vertices v adjacent to u do if (visited[v]==0) then { Enqueue(v,Q) visited[v]=1; } } }
  • 5. applications • To check whether the graph is connected or not
  • 6. Depth first search • Depth first search works by selecting one vertex V of G as a start vertex • V is marked visited • Then each unvisited vertex adjacent to V is searched using depth first search recursively • This process continues until a dead end / no adjacent unvisited vertices is encountered
  • 7. 1. Choose any node in the graph. Designate it as the search node and mark it as visited 2. Using the adjacency matrix of the graph, find a node adjacent to the search node that has not been visited yet. Designate this as the new search node and mark it as visited. 3. Repeat step 1 & 2 using the new search node
  • 8. Routine for DFS Void DFS(Vertex V) { visited[V]=True; for each W adjacent to V if (! Visited[W]) DFS(W); }