SlideShare a Scribd company logo
1 of 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

More Related Content

What's hot

What's hot (20)

DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)
 
Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searching
 
BFS
BFSBFS
BFS
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Minimum spanning Tree
Minimum spanning TreeMinimum spanning Tree
Minimum spanning Tree
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS 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 Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Heap
HeapHeap
Heap
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 

Similar to Depth First Search ( DFS )

lecture 18
lecture 18lecture 18
lecture 18sajinsc
 
lecture 19
lecture 19lecture 19
lecture 19sajinsc
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.pptSazidHossain9
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in indiaEdhole.com
 
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 3Deepak John
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02Krish_ver2
 
DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdfRajkk5
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithmjyothimonc
 
topological_sort_strongly Connected Components
topological_sort_strongly Connected Componentstopological_sort_strongly Connected Components
topological_sort_strongly Connected ComponentsJahidulIslam47153
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxwhittemorelucilla
 

Similar to 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
 
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
 
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
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 

Recently uploaded

Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptxmohitesoham12
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxRomil Mishra
 
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmComputer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmDeepika Walanjkar
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Coursebim.edu.pl
 
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfalene1
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
GSK & SEAMANSHIP-IV LIFE SAVING APPLIANCES .pptx
GSK & SEAMANSHIP-IV LIFE SAVING APPLIANCES .pptxGSK & SEAMANSHIP-IV LIFE SAVING APPLIANCES .pptx
GSK & SEAMANSHIP-IV LIFE SAVING APPLIANCES .pptxshuklamittt0077
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...Erbil Polytechnic University
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Cooling Tower SERD pH drop issue (11 April 2024) .pptx
Cooling Tower SERD pH drop issue (11 April 2024) .pptxCooling Tower SERD pH drop issue (11 April 2024) .pptx
Cooling Tower SERD pH drop issue (11 April 2024) .pptxmamansuratman0253
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxsiddharthjain2303
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solidnamansinghjarodiya
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewsandhya757531
 
Turn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxTurn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxStephen Sitton
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfManish Kumar
 

Recently uploaded (20)

Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptx
 
Designing pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptxDesigning pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptx
 
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmComputer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Course
 
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
GSK & SEAMANSHIP-IV LIFE SAVING APPLIANCES .pptx
GSK & SEAMANSHIP-IV LIFE SAVING APPLIANCES .pptxGSK & SEAMANSHIP-IV LIFE SAVING APPLIANCES .pptx
GSK & SEAMANSHIP-IV LIFE SAVING APPLIANCES .pptx
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Cooling Tower SERD pH drop issue (11 April 2024) .pptx
Cooling Tower SERD pH drop issue (11 April 2024) .pptxCooling Tower SERD pH drop issue (11 April 2024) .pptx
Cooling Tower SERD pH drop issue (11 April 2024) .pptx
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptx
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solid
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overview
 
Turn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxTurn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptx
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). 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