SlideShare a Scribd company logo
z
DFS Algorithm
IN Bangla
z
2
Depth-First Search: The Code
Data: color[V], time, prev[V],d[V],
f[V]
DFS(G) // where prog starts
{
for each vertex u  V
{
color[u] = WHITE;
prev[u]=NIL;
f[u]=inf; d[u]=inf;
}
time = 0;
for each vertex u  V
if (color[u] == WHITE)
DFS_Visit(u);
}
DFS_Visit(u)
{
color[u] = GREY;
time = time+1;
d[u] = time;
for each v  Adj[u]
{
if(color[v] == WHITE){
prev[v]=u;
DFS_Visit(v);}
}
color[u] = BLACK;
time = time+1;
f[u] = time;
}
Initialize
z
DFS Example
3
source
vertex
S
A
B C
D
E
F
G
z
DFS Example
4
1 | | |
|||
| |
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
5
1 | | |
|||
2 | |
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
6
1 | | |
||3 |
2 | |
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
7
1 | | |
||3 | 4
2 | |
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
8
1 | | |
|5 |3 | 4
2 | |
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
9
1 | | |
|5 | 63 | 4
2 | |
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
10
1 | | |
|5 | 63 | 4
2 | 7 |
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
11
1 | 8 | |
|5 | 63 | 4
2 | 7 |
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
12
1 | 8 | |
|5 | 63 | 4
2 | 7 9 |
source
vertex
d f
What is the structure of the grey vertices?
What do they represent?
S
A
B C
D
E
F
G
z
DFS Example
13
1 | 8 | |
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
14
1 | 8 |11 |
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
15
1 |12 8 |11 |
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
16
1 |12 8 |11 13|
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
17
1 |12 8 |11 13|
14|5 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
18
1 |12 8 |11 13|
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
z
DFS Example
19
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
z
20
DFS: Kinds of edges
 DFS introduces an important distinction among edges in the original graph:
 Tree edge: encounter new (white) vertex
 Back edge: from descendent to ancestor
 Forward edge: from ancestor to descendent
 Cross edge: between a tree or subtrees
 From a grey node to a black node
z
21
DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d f
Tree edges Back edges Forward edges Cross edges
z
22
DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d f
Tree edges Back edges Forward edges Cross edges

More Related Content

PPT
Bfs
PPT
Graphs bfs dfs
PPT
Depth firstsearchalgorithm
PPTX
Data structures and algorithms lab7
PPT
PDF
Depth First Search and Breadth First Search
PDF
PPT
Bfs and dfs in data structure
Bfs
Graphs bfs dfs
Depth firstsearchalgorithm
Data structures and algorithms lab7
Depth First Search and Breadth First Search
Bfs and dfs in data structure

What's hot (20)

PPTX
Bfs & dfs application
PPTX
Breadth first search (Bfs)
PPT
Depth First Search ( DFS )
PPT
Breadth first search and depth first search
PPTX
Depth first search [dfs]
PPT
Breadth first search
PPTX
Breadth First Search (BFS)
PPTX
Bfs and Dfs
PPTX
PPTX
Depth-First Search
PDF
Bfs dfs
PPTX
Depth First Search and Breadth First Search
PPTX
Graph Algorithms: Breadth-First Search (BFS)
PPT
2.5 bfs & dfs 02
PPTX
130210107039 2130702
PPTX
Presentation on Breadth First Search (BFS)
PPT
Graph traversal-BFS & DFS
PPT
chapter22.ppt
DOC
BFS, Breadth first search | Search Traversal Algorithm
PPT
Algorithm to count number of disjoint paths
Bfs & dfs application
Breadth first search (Bfs)
Depth First Search ( DFS )
Breadth first search and depth first search
Depth first search [dfs]
Breadth first search
Breadth First Search (BFS)
Bfs and Dfs
Depth-First Search
Bfs dfs
Depth First Search and Breadth First Search
Graph Algorithms: Breadth-First Search (BFS)
2.5 bfs & dfs 02
130210107039 2130702
Presentation on Breadth First Search (BFS)
Graph traversal-BFS & DFS
chapter22.ppt
BFS, Breadth first search | Search Traversal Algorithm
Algorithm to count number of disjoint paths
Ad

Similar to DFS algorithm (20)

PPT
lecture 19
PPT
lecture 18
PPTX
Lecture 5 - Graph Algorithms BFS and DFS.pptx
PDF
DFS ppt.pdf
PPTX
Graph Traversing and Seaching - Data Structure- AIUB.pptx
PPT
lecture24.ppt
PPT
Elementary Graph Algo.ppt
PPT
Graph 02
PPTX
Introduction of Depth First Search with example
PPT
YCMOU_FYBCA_DS_Unit-7.ppt
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
PPTX
temp.pptxsusueueuune ueieoekeebhe ejene h
PPTX
DFS & BFS Graph
PPTX
Depth-First Search
PPTX
Algorithm Design and Complexity - Course 7
PDF
Analysis and design of algorithms part 3
PPT
COSC 3101A - Design and Analysis of Algorithms 10
PPT
B.tech admission in india
PPTX
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
PPTX
BFS_DFS_Enhanced_Presentation124567.pptx
lecture 19
lecture 18
Lecture 5 - Graph Algorithms BFS and DFS.pptx
DFS ppt.pdf
Graph Traversing and Seaching - Data Structure- AIUB.pptx
lecture24.ppt
Elementary Graph Algo.ppt
Graph 02
Introduction of Depth First Search with example
YCMOU_FYBCA_DS_Unit-7.ppt
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
temp.pptxsusueueuune ueieoekeebhe ejene h
DFS & BFS Graph
Depth-First Search
Algorithm Design and Complexity - Course 7
Analysis and design of algorithms part 3
COSC 3101A - Design and Analysis of Algorithms 10
B.tech admission in india
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
BFS_DFS_Enhanced_Presentation124567.pptx
Ad

More from Amit Kundu (17)

PDF
Covid Guide Line Bangladesh
PPTX
Blockchain
PPTX
Smart Home Automation And security System
PPTX
Cache Memory
PPTX
Automated Tools For System Analysis and Design
PPTX
Android Operating System
PPTX
Database project IDEA
PPTX
Ipv4 & ipv6
PPTX
Unguided Media
PPTX
Innovative Business Idea
PDF
Intel core i7
PPTX
Laplace transform
PPTX
Presentation on Rectifier
PPTX
Presentation on Heap Sort
PPTX
Bfs algorithm & its application
PPTX
Presentation on chikunguny
PPTX
A presentation on social networking sites
Covid Guide Line Bangladesh
Blockchain
Smart Home Automation And security System
Cache Memory
Automated Tools For System Analysis and Design
Android Operating System
Database project IDEA
Ipv4 & ipv6
Unguided Media
Innovative Business Idea
Intel core i7
Laplace transform
Presentation on Rectifier
Presentation on Heap Sort
Bfs algorithm & its application
Presentation on chikunguny
A presentation on social networking sites

Recently uploaded (20)

PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Well-logging-methods_new................
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
PPT on Performance Review to get promotions
DOCX
573137875-Attendance-Management-System-original
PPTX
Welding lecture in detail for understanding
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Lecture Notes Electrical Wiring System Components
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
OOP with Java - Java Introduction (Basics)
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
UNIT 4 Total Quality Management .pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CH1 Production IntroductoryConcepts.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Well-logging-methods_new................
Operating System & Kernel Study Guide-1 - converted.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
CYBER-CRIMES AND SECURITY A guide to understanding
PPT on Performance Review to get promotions
573137875-Attendance-Management-System-original
Welding lecture in detail for understanding
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
additive manufacturing of ss316l using mig welding
Lecture Notes Electrical Wiring System Components

DFS algorithm

  • 2. z 2 Depth-First Search: The Code Data: color[V], time, prev[V],d[V], f[V] DFS(G) // where prog starts { for each vertex u  V { color[u] = WHITE; prev[u]=NIL; f[u]=inf; d[u]=inf; } time = 0; for each vertex u  V if (color[u] == WHITE) DFS_Visit(u); } DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] { if(color[v] == WHITE){ prev[v]=u; DFS_Visit(v);} } color[u] = BLACK; time = time+1; f[u] = time; } Initialize
  • 4. z DFS Example 4 1 | | | ||| | | source vertex d fS A B C D E F G
  • 5. z DFS Example 5 1 | | | ||| 2 | | source vertex d fS A B C D E F G
  • 6. z DFS Example 6 1 | | | ||3 | 2 | | source vertex d fS A B C D E F G
  • 7. z DFS Example 7 1 | | | ||3 | 4 2 | | source vertex d fS A B C D E F G
  • 8. z DFS Example 8 1 | | | |5 |3 | 4 2 | | source vertex d fS A B C D E F G
  • 9. z DFS Example 9 1 | | | |5 | 63 | 4 2 | | source vertex d fS A B C D E F G
  • 10. z DFS Example 10 1 | | | |5 | 63 | 4 2 | 7 | source vertex d fS A B C D E F G
  • 11. z DFS Example 11 1 | 8 | | |5 | 63 | 4 2 | 7 | source vertex d fS A B C D E F G
  • 12. z DFS Example 12 1 | 8 | | |5 | 63 | 4 2 | 7 9 | source vertex d f What is the structure of the grey vertices? What do they represent? S A B C D E F G
  • 13. z DFS Example 13 1 | 8 | | |5 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 14. z DFS Example 14 1 | 8 |11 | |5 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 15. z DFS Example 15 1 |12 8 |11 | |5 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 16. z DFS Example 16 1 |12 8 |11 13| |5 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 17. z DFS Example 17 1 |12 8 |11 13| 14|5 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 18. z DFS Example 18 1 |12 8 |11 13| 14|155 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 19. z DFS Example 19 1 |12 8 |11 13|16 14|155 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 20. z 20 DFS: Kinds of edges  DFS introduces an important distinction among edges in the original graph:  Tree edge: encounter new (white) vertex  Back edge: from descendent to ancestor  Forward edge: from ancestor to descendent  Cross edge: between a tree or subtrees  From a grey node to a black node
  • 21. z 21 DFS Example 1 |12 8 |11 13|16 14|155 | 63 | 4 2 | 7 9 |10 source vertex d f Tree edges Back edges Forward edges Cross edges
  • 22. z 22 DFS Example 1 |12 8 |11 13|16 14|155 | 63 | 4 2 | 7 9 |10 source vertex d f Tree edges Back edges Forward edges Cross edges