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

Similar to CS8391-Data Structures Unit 4 (20)

PPTX
Breath first Search and Depth first search
Kirti Verma
 
PPTX
Graphs
KomalPaliwal3
 
PPTX
BFS (Breadth First Search) Tree Traversal
ANTONY P SAIJI
 
PDF
U1 L5 DAA.pdf
LakshyaBaliyan2
 
PPTX
Graph Traversal Algorithms - Depth First Search Traversal
Amrinder Arora
 
PPTX
Sec B Graph traversal.pptx
tabusam1
 
PPTX
Data Structure and Algorithms Graph Traversal
ManishPrajapati78
 
PPT
8-Graph.ppt
ssuser55cbdb
 
PPTX
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
sahadevbkbiet2023
 
PPTX
Data structure Graph PPT ( BFS & DFS ) NOTES
sahadevbkbiet2023
 
PPTX
Breadth First Search or BFS for a Graph traversal
jeevanasujitha
 
PPT
Graph traversal-BFS & DFS
Rajandeep Gill
 
PPTX
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
PPTX
bfs tree searching ,sortingUntitled presentation.pptx
saurabhpandey679381
 
PPT
Data Structures-Non Linear DataStructures-Graphs
sailaja156145
 
PPTX
kumattt).pptx
gurukhade1
 
PPT
Unit VI - Graphs.ppt
HODElex
 
PPTX
Lecture 16 graphs traversal
Abirami A
 
PPTX
Graph data structures for ppt for understanding.pptx
ramkumar649780
 
PPTX
Depth first traversal(data structure algorithms)
bhuvaneshwariA5
 
Breath first Search and Depth first search
Kirti Verma
 
BFS (Breadth First Search) Tree Traversal
ANTONY P SAIJI
 
U1 L5 DAA.pdf
LakshyaBaliyan2
 
Graph Traversal Algorithms - Depth First Search Traversal
Amrinder Arora
 
Sec B Graph traversal.pptx
tabusam1
 
Data Structure and Algorithms Graph Traversal
ManishPrajapati78
 
8-Graph.ppt
ssuser55cbdb
 
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
sahadevbkbiet2023
 
Data structure Graph PPT ( BFS & DFS ) NOTES
sahadevbkbiet2023
 
Breadth First Search or BFS for a Graph traversal
jeevanasujitha
 
Graph traversal-BFS & DFS
Rajandeep Gill
 
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
bfs tree searching ,sortingUntitled presentation.pptx
saurabhpandey679381
 
Data Structures-Non Linear DataStructures-Graphs
sailaja156145
 
kumattt).pptx
gurukhade1
 
Unit VI - Graphs.ppt
HODElex
 
Lecture 16 graphs traversal
Abirami A
 
Graph data structures for ppt for understanding.pptx
ramkumar649780
 
Depth first traversal(data structure algorithms)
bhuvaneshwariA5
 

More from SIMONTHOMAS S (20)

PPTX
Cs8092 computer graphics and multimedia unit 5
SIMONTHOMAS S
 
PPTX
Cs8092 computer graphics and multimedia unit 4
SIMONTHOMAS S
 
PPTX
Cs8092 computer graphics and multimedia unit 3
SIMONTHOMAS S
 
PPT
Cs8092 computer graphics and multimedia unit 2
SIMONTHOMAS S
 
PPTX
Cs8092 computer graphics and multimedia unit 1
SIMONTHOMAS S
 
PPTX
Mg6088 spm unit-5
SIMONTHOMAS S
 
PPT
Mg6088 spm unit-4
SIMONTHOMAS S
 
PPTX
Mg6088 spm unit-3
SIMONTHOMAS S
 
PPTX
Mg6088 spm unit-2
SIMONTHOMAS S
 
PPTX
Mg6088 spm unit-1
SIMONTHOMAS S
 
PPTX
IT6701-Information Management Unit 5
SIMONTHOMAS S
 
PPTX
IT6701-Information Management Unit 4
SIMONTHOMAS S
 
PPTX
IT6701-Information Management Unit 3
SIMONTHOMAS S
 
PPTX
IT6701-Information Management Unit 2
SIMONTHOMAS S
 
PPTX
IT6701-Information Management Unit 1
SIMONTHOMAS S
 
PPTX
CS8391-Data Structures Unit 5
SIMONTHOMAS S
 
PPTX
CS8391-Data Structures Unit 3
SIMONTHOMAS S
 
PPTX
CS8391-Data Structures Unit 2
SIMONTHOMAS S
 
PPTX
CS8391-Data Structures Unit 1
SIMONTHOMAS S
 
PPT
SPC Unit 5
SIMONTHOMAS S
 
Cs8092 computer graphics and multimedia unit 5
SIMONTHOMAS S
 
Cs8092 computer graphics and multimedia unit 4
SIMONTHOMAS S
 
Cs8092 computer graphics and multimedia unit 3
SIMONTHOMAS S
 
Cs8092 computer graphics and multimedia unit 2
SIMONTHOMAS S
 
Cs8092 computer graphics and multimedia unit 1
SIMONTHOMAS S
 
Mg6088 spm unit-5
SIMONTHOMAS S
 
Mg6088 spm unit-4
SIMONTHOMAS S
 
Mg6088 spm unit-3
SIMONTHOMAS S
 
Mg6088 spm unit-2
SIMONTHOMAS S
 
Mg6088 spm unit-1
SIMONTHOMAS S
 
IT6701-Information Management Unit 5
SIMONTHOMAS S
 
IT6701-Information Management Unit 4
SIMONTHOMAS S
 
IT6701-Information Management Unit 3
SIMONTHOMAS S
 
IT6701-Information Management Unit 2
SIMONTHOMAS S
 
IT6701-Information Management Unit 1
SIMONTHOMAS S
 
CS8391-Data Structures Unit 5
SIMONTHOMAS S
 
CS8391-Data Structures Unit 3
SIMONTHOMAS S
 
CS8391-Data Structures Unit 2
SIMONTHOMAS S
 
CS8391-Data Structures Unit 1
SIMONTHOMAS S
 
SPC Unit 5
SIMONTHOMAS S
 
Ad

Recently uploaded (20)

PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Day2 B2 Best.pptx
helenjenefa1
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
Ad

CS8391-Data Structures Unit 4

  • 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); }