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

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
Ad

Recently uploaded (20)

PPTX
web development for engineering and engineering
PDF
composite construction of structures.pdf
PPTX
AgentX UiPath Community Webinar series - Delhi
PPT
Chapter 6 Design in software Engineeing.ppt
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Road Safety tips for School Kids by a k maurya.pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
Internship_Presentation_Final engineering.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Geodesy 1.pptx...............................................
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Queuing formulas to evaluate throughputs and servers
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PDF
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
web development for engineering and engineering
composite construction of structures.pdf
AgentX UiPath Community Webinar series - Delhi
Chapter 6 Design in software Engineeing.ppt
OOP with Java - Java Introduction (Basics)
Road Safety tips for School Kids by a k maurya.pptx
Structs to JSON How Go Powers REST APIs.pdf
Internship_Presentation_Final engineering.pptx
bas. eng. economics group 4 presentation 1.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Geodesy 1.pptx...............................................
Lesson 3_Tessellation.pptx finite Mathematics
Model Code of Practice - Construction Work - 21102022 .pdf
Queuing formulas to evaluate throughputs and servers
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf

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