SlideShare une entreprise Scribd logo
1  sur  39
1
Depth First Search
Depth-First Search
● Input:
■ G = (V, E) (No source vertex given!)
● Goal:
■ Explore the edges of G to “discover” every vertex in V starting at the most
current visited node
■ Search may be repeated from multiple sources
● Output:
■ 2 timestamps on each vertex:
○ d[v] = discovery time
○ f[v] = finishing time (done with examining v’s adjacency list)
■ Depth-first forest
1 2
5 4
3
Depth-First Search
● Search “deeper” in the graph whenever possible
● Edges are explored out of the most recently
discovered vertex v that still has unexplored edges
• After all edges of v have been explored, the search
“backtracks” from the parent of v
• The process continues until all vertices reachable from the
original source have been discovered
• If undiscovered vertices remain, choose one of them as a
new source and repeat the search from that vertex
• DFS creates a “depth-first forest”
1 2
5 4
3
DFS Additional Data Structures
● Global variable: time-stamp
■ Incremented when nodes are discovered or finished
● color[u] – similar to BFS
■ White before discovery, gray while processing and black
when finished processing
● prev[u] – predecessor of u
● d[u], f[u] – discovery and finish times
5
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
6
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;
}
What does u[d] represent?
7
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;
}
What does f[d] represent?
8
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;
}Will all vertices eventually be colored black?
9
DFS Example
source
vertex
S
A
B C
D
E
F
G
10
DFS Example
1 | | |
|||
| |
source
vertex
d fS
A
B C
D
E
F
G
11
DFS Example
1 | | |
|||
2 | |
source
vertex
d fS
A
B C
D
E
F
G
12
DFS Example
1 | | |
||3 |
2 | |
source
vertex
d fS
A
B C
D
E
F
G
13
DFS Example
1 | | |
||3 | 4
2 | |
source
vertex
d fS
A
B C
D
E
F
G
14
DFS Example
1 | | |
|5 |3 | 4
2 | |
source
vertex
d fS
A
B C
D
E
F
G
15
DFS Example
1 | | |
|5 | 63 | 4
2 | |
source
vertex
d fS
A
B C
D
E
F
G
16
DFS Example
1 | | |
|5 | 63 | 4
2 | 7 |
source
vertex
d fS
A
B C
D
E
F
G
17
DFS Example
1 | 8 | |
|5 | 63 | 4
2 | 7 |
source
vertex
d fS
A
B C
D
E
F
G
18
DFS Example
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
19
DFS Example
1 | 8 | |
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
20
DFS Example
1 | 8 |11 |
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
21
DFS Example
1 |12 8 |11 |
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
22
DFS Example
1 |12 8 |11 13|
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
23
DFS Example
1 |12 8 |11 13|
14|5 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
24
DFS Example
1 |12 8 |11 13|
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d fS
A
B C
D
E
F
G
25
DFS Example
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
26
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;
}What will be the running time?
27
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;
}Running time: O(V2
) because call DFS_Visit on each vertex,
and the loop over Adj[] can run as many as |V| times
O(V)
O(V)
O(V)
28
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;
}
BUT, there is actually a tighter bound.
How many times will DFS_Visit() actually be called?
29
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;
}
So, running time of DFS = O(V+E)
30
DFS: Kinds of edges
● DFS introduces an important distinction
among edges in the original graph:
■ Tree edge: encounter new (white) vertex
○ The tree edges form a spanning forest
○ Can tree edges form cycles? Why or why not?
 No
31
DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d f
Tree edges
32
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
○ Encounter a grey vertex (grey to grey)
○ Self loops are considered as to be back edge.
33
DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d f
Tree edges Back edges
34
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
○ Not a tree edge, though
○ From grey node to black node
35
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
36
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
37
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
38
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
● Note: tree & back edges are important; most
algorithms don’t distinguish forward & cross
Reference
● Cormen –
■ Chapter 22 (Elementary Graph Algorithms)
● Exercise –
■ 22.3-4 –Detect edge using d[u], d[v], f[u], f[v]
■ 22.3-11 – Connected Component
■ 22.3-12 – Singly connected
39

Contenu connexe

Tendances

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
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First SearchKevin Jadiya
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithmhansa khan
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra Sahil Kumar
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFSRajandeep Gill
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithmmansab MIRZA
 
Bubble sort
Bubble sortBubble sort
Bubble sortManek Ar
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structureAbrish06
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search treeKrish_ver2
 
Lecture 16 memory bounded search
Lecture 16 memory bounded searchLecture 16 memory bounded search
Lecture 16 memory bounded searchHema Kashyap
 

Tendances (20)

Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
 
Dfs
DfsDfs
Dfs
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
stack & queue
stack & queuestack & queue
stack & queue
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Lecture 16 memory bounded search
Lecture 16 memory bounded searchLecture 16 memory bounded search
Lecture 16 memory bounded search
 

Similaire à Depth First Search ( DFS )

Similaire à Depth First Search ( DFS ) (20)

DFS algorithm
DFS algorithmDFS algorithm
DFS algorithm
 
lecture 18
lecture 18lecture 18
lecture 18
 
lecture 19
lecture 19lecture 19
lecture 19
 
lecture24.ppt
lecture24.pptlecture24.ppt
lecture24.ppt
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.ppt
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3
 
kumattt).pptx
kumattt).pptxkumattt).pptx
kumattt).pptx
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdf
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
BFS
BFSBFS
BFS
 
19-graph1 (1).ppt
19-graph1 (1).ppt19-graph1 (1).ppt
19-graph1 (1).ppt
 
Topological sort
Topological sortTopological sort
Topological sort
 
topological_sort_strongly Connected Components
topological_sort_strongly Connected Componentstopological_sort_strongly Connected Components
topological_sort_strongly Connected Components
 
Graph
GraphGraph
Graph
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Graps 2
Graps 2Graps 2
Graps 2
 

Dernier

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Piping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringPiping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringJuanCarlosMorales19600
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptJasonTagapanGulla
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 

Dernier (20)

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Piping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringPiping Basic stress analysis by engineering
Piping Basic stress analysis by engineering
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.ppt
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 

Depth First Search ( DFS )

  • 2. Depth-First Search ● Input: ■ G = (V, E) (No source vertex given!) ● Goal: ■ Explore the edges of G to “discover” every vertex in V starting at the most current visited node ■ Search may be repeated from multiple sources ● Output: ■ 2 timestamps on each vertex: ○ d[v] = discovery time ○ f[v] = finishing time (done with examining v’s adjacency list) ■ Depth-first forest 1 2 5 4 3
  • 3. Depth-First Search ● Search “deeper” in the graph whenever possible ● Edges are explored out of the most recently discovered vertex v that still has unexplored edges • After all edges of v have been explored, the search “backtracks” from the parent of v • The process continues until all vertices reachable from the original source have been discovered • If undiscovered vertices remain, choose one of them as a new source and repeat the search from that vertex • DFS creates a “depth-first forest” 1 2 5 4 3
  • 4. DFS Additional Data Structures ● Global variable: time-stamp ■ Incremented when nodes are discovered or finished ● color[u] – similar to BFS ■ White before discovery, gray while processing and black when finished processing ● prev[u] – predecessor of u ● d[u], f[u] – discovery and finish times
  • 5. 5 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
  • 6. 6 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; } What does u[d] represent?
  • 7. 7 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; } What does f[d] represent?
  • 8. 8 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; }Will all vertices eventually be colored black?
  • 10. 10 DFS Example 1 | | | ||| | | source vertex d fS A B C D E F G
  • 11. 11 DFS Example 1 | | | ||| 2 | | source vertex d fS A B C D E F G
  • 12. 12 DFS Example 1 | | | ||3 | 2 | | source vertex d fS A B C D E F G
  • 13. 13 DFS Example 1 | | | ||3 | 4 2 | | source vertex d fS A B C D E F G
  • 14. 14 DFS Example 1 | | | |5 |3 | 4 2 | | source vertex d fS A B C D E F G
  • 15. 15 DFS Example 1 | | | |5 | 63 | 4 2 | | source vertex d fS A B C D E F G
  • 16. 16 DFS Example 1 | | | |5 | 63 | 4 2 | 7 | source vertex d fS A B C D E F G
  • 17. 17 DFS Example 1 | 8 | | |5 | 63 | 4 2 | 7 | source vertex d fS A B C D E F G
  • 18. 18 DFS Example 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
  • 19. 19 DFS Example 1 | 8 | | |5 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 20. 20 DFS Example 1 | 8 |11 | |5 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 21. 21 DFS Example 1 |12 8 |11 | |5 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 22. 22 DFS Example 1 |12 8 |11 13| |5 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 23. 23 DFS Example 1 |12 8 |11 13| 14|5 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 24. 24 DFS Example 1 |12 8 |11 13| 14|155 | 63 | 4 2 | 7 9 |10 source vertex d fS A B C D E F G
  • 25. 25 DFS Example 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
  • 26. 26 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; }What will be the running time?
  • 27. 27 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; }Running time: O(V2 ) because call DFS_Visit on each vertex, and the loop over Adj[] can run as many as |V| times O(V) O(V) O(V)
  • 28. 28 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; } BUT, there is actually a tighter bound. How many times will DFS_Visit() actually be called?
  • 29. 29 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; } So, running time of DFS = O(V+E)
  • 30. 30 DFS: Kinds of edges ● DFS introduces an important distinction among edges in the original graph: ■ Tree edge: encounter new (white) vertex ○ The tree edges form a spanning forest ○ Can tree edges form cycles? Why or why not?  No
  • 31. 31 DFS Example 1 |12 8 |11 13|16 14|155 | 63 | 4 2 | 7 9 |10 source vertex d f Tree edges
  • 32. 32 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 ○ Encounter a grey vertex (grey to grey) ○ Self loops are considered as to be back edge.
  • 33. 33 DFS Example 1 |12 8 |11 13|16 14|155 | 63 | 4 2 | 7 9 |10 source vertex d f Tree edges Back edges
  • 34. 34 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 ○ Not a tree edge, though ○ From grey node to black node
  • 35. 35 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
  • 36. 36 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
  • 37. 37 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
  • 38. 38 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 ● Note: tree & back edges are important; most algorithms don’t distinguish forward & cross
  • 39. Reference ● Cormen – ■ Chapter 22 (Elementary Graph Algorithms) ● Exercise – ■ 22.3-4 –Detect edge using d[u], d[v], f[u], f[v] ■ 22.3-11 – Connected Component ■ 22.3-12 – Singly connected 39