SlideShare a Scribd company logo
1 of 15
WelcomeTo Our
Presentation
NAME :- Kumar Ayush Singh Deo
BRANCH :- CSE
SUBJECT :- Analysis and digital of algorithm
ENROLLMENT No.:- 0301cs211034
Submitted to:- Miss Soumya Pandey
Depth First Search (DFS)
Breadth First Search (BFS)
T
opics
What is a graph?
1.Directed/Undirected
2.Weighted/Unweighted
3.Cyclic/Acyclic
A set of vertices and edges
 Breadth First Search (BFS)
Start several paths at a time, and advance in each one step at a time
The breadth-first search uses a FIFO queue.
 Depth First Search (DFS)
Once a possible path is found, continue the search untilthe end of the path
The Depth-first search uses a LIFO Stack.
Graph Traversal
How ItWorks?
1.Pick a source vertex S to start.
2.Discover the vertices that are adjacent to S.
Depth-first:
visit all neighbors of
a neighbor before
visiting your other
neighbors
Breadth First:
Pick each child of
S in turn and
discover their
vertices adjacent
to that child.
Done when all
children have been
discovered and
examined.
For a Graph G=(V, E) and n = |V| and
m=|E|
When Adjacency List is used Complexity
is O(m + n)
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 )
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
Analysis of DFS
Example:
Depth-first search: Directed
graphs
The algorithm is essentially the same as for undirected
graphs, the difference residing in the interpretation of the
word "adjacent".
In a directed graph, node w is adjacent to node v if the
directed edge (v, w) exists.
If (v, w) exists but (w, v) does not, then w is adjacent to v
but v is not adjacent to w.
With this change of interpretation the procedures dfs and
search apply equally well in the case of a directed graph.
DFS Example
8 |11 13|16
14|15
5 | 6
3 | 4
2 | 7 9 |10
source vertex
d f
1 |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.
DFS: Algorithm
• DFS(G)
 for each vertex u inV,
 color[u]=white; [u]=NIL
 time=0;
 for each vertex u inV
 if (color[u]=white)
 DFS-VISIT(u)
DFS-VISIT(u)
 color[u]=gray;
 time = time +
1;
 d[u] = time;






for each v inAdj(u) do if (color[v] = white)
[v] = u;
DFS-VISIT(v);
color[u] = black;
time = time + 1; f[u]= time;
DFS: Algorithm (Cont.)
source vertex
BFS:
*Testing a graph for bipartitions
*To find the shortest path from a
vertex s to a vertex v in an un
weighted graph
*To find the length of such a path
*To find out if a graph contains cycles
*To construct a BSF tree/forest from a
graph
*Copying garbage collection
Applications
Applications
DFS:
* Finding connected components.
*Topological sorting.
*Finding the bridges of a graph.
*cycle Detecting
*Finding strongly connected components.
*Finding biconnectivity in graphs.
DFS and BFS Algorithms Explained

More Related Content

Similar to DFS and BFS Algorithms Explained

lecture 18
lecture 18lecture 18
lecture 18sajinsc
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)Ishucs
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )Sazzad Hossain
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.pptSazidHossain9
 
LAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptxLAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptxionutionescuionut
 
DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdfRajkk5
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]DEEPIKA T
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsSigSegVSquad
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.pptKarunaBiswas3
 
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm EData Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm Ejeniihykdevara
 
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
 
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gpchandrashekarr799
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxrandyburney60861
 
BFS & DFS in Data Structure
BFS & DFS in Data StructureBFS & DFS in Data Structure
BFS & DFS in Data StructureMeghaj Mallick
 

Similar to DFS and BFS Algorithms Explained (20)

Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
lecture 18
lecture 18lecture 18
lecture 18
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.ppt
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
LAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptxLAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptx
 
DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdf
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt
 
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm EData Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docx
 
Graph
GraphGraph
Graph
 
5 searching
5 searching5 searching
5 searching
 
BFS & DFS in Data Structure
BFS & DFS in Data StructureBFS & DFS in Data Structure
BFS & DFS in Data Structure
 

Recently uploaded

PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 

Recently uploaded (20)

PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 

DFS and BFS Algorithms Explained

  • 1. WelcomeTo Our Presentation NAME :- Kumar Ayush Singh Deo BRANCH :- CSE SUBJECT :- Analysis and digital of algorithm ENROLLMENT No.:- 0301cs211034 Submitted to:- Miss Soumya Pandey
  • 2. Depth First Search (DFS) Breadth First Search (BFS) T opics
  • 3. What is a graph? 1.Directed/Undirected 2.Weighted/Unweighted 3.Cyclic/Acyclic A set of vertices and edges
  • 4.  Breadth First Search (BFS) Start several paths at a time, and advance in each one step at a time The breadth-first search uses a FIFO queue.  Depth First Search (DFS) Once a possible path is found, continue the search untilthe end of the path The Depth-first search uses a LIFO Stack. Graph Traversal
  • 5. How ItWorks? 1.Pick a source vertex S to start. 2.Discover the vertices that are adjacent to S. Depth-first: visit all neighbors of a neighbor before visiting your other neighbors Breadth First: Pick each child of S in turn and discover their vertices adjacent to that child. Done when all children have been discovered and examined.
  • 6. For a Graph G=(V, E) and n = |V| and m=|E| When Adjacency List is used Complexity is O(m + n) 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 ) 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 Analysis of DFS
  • 8. Depth-first search: Directed graphs The algorithm is essentially the same as for undirected graphs, the difference residing in the interpretation of the word "adjacent". In a directed graph, node w is adjacent to node v if the directed edge (v, w) exists. If (v, w) exists but (w, v) does not, then w is adjacent to v but v is not adjacent to w. With this change of interpretation the procedures dfs and search apply equally well in the case of a directed graph.
  • 9. DFS Example 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 source vertex d f 1 |12
  • 10. 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.
  • 11. DFS: Algorithm • DFS(G)  for each vertex u inV,  color[u]=white; [u]=NIL  time=0;  for each vertex u inV  if (color[u]=white)  DFS-VISIT(u)
  • 12. DFS-VISIT(u)  color[u]=gray;  time = time + 1;  d[u] = time;       for each v inAdj(u) do if (color[v] = white) [v] = u; DFS-VISIT(v); color[u] = black; time = time + 1; f[u]= time; DFS: Algorithm (Cont.) source vertex
  • 13. BFS: *Testing a graph for bipartitions *To find the shortest path from a vertex s to a vertex v in an un weighted graph *To find the length of such a path *To find out if a graph contains cycles *To construct a BSF tree/forest from a graph *Copying garbage collection Applications
  • 14. Applications DFS: * Finding connected components. *Topological sorting. *Finding the bridges of a graph. *cycle Detecting *Finding strongly connected components. *Finding biconnectivity in graphs.