SlideShare a Scribd company logo
© 2015 Goodrich and Tamassia Depth-First Search 1
Depth-First Search
D
B
A
C
E
Presentation for use with the textbook, Algorithm Design and
Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015
© 2015 Goodrich and Tamassia Depth-First Search 2
Subgraphs
q A subgraph S of a graph
G is a graph such that
n The vertices of S are a
subset of the vertices of G
n The edges of S are a
subset of the edges of G
q A spanning subgraph of G
is a subgraph that
contains all the vertices
of G
Subgraph
Spanning subgraph
© 2015 Goodrich and Tamassia
Application: Web Crawlers
q A fundamental kind of algorithmic operation that we
might wish to perform on a graph is traversing the
edges and the vertices of that graph.
q A traversal is a systematic procedure for exploring a
graph by examining all of its vertices and edges.
q For example, a web crawler, which is the data
collecting part of a search engine, must explore a
graph of hypertext documents by examining its
vertices, which are the documents, and its edges,
which are the hyperlinks between documents.
q A traversal is efficient if it visits all the vertices and
edges in linear time.
Depth-First Search 3
© 2015 Goodrich and Tamassia Depth-First Search 4
Connectivity
q A graph is
connected if there is
a path between
every pair of
vertices
q A connected
component of a
graph G is a
maximal connected
subgraph of G
Connected graph
Non connected graph with two
connected components
© 2015 Goodrich and Tamassia Depth-First Search 5
Trees and Forests
q A (free) tree is an
undirected graph T such
that
n T is connected
n T has no cycles
This definition of tree is
different from the one of
a rooted tree
q A forest is an undirected
graph without cycles
q The connected
components of a forest
are trees
Tree
Forest
© 2015 Goodrich and Tamassia Depth-First Search 6
Spanning Trees and Forests
q A spanning tree of a
connected graph is a
spanning subgraph that is
a tree
q A spanning tree is not
unique unless the graph is
a tree
q Spanning trees have
applications to the design
of communication
networks
q A spanning forest of a
graph is a spanning
subgraph that is a forest
Graph
Spanning tree
© 2015 Goodrich and Tamassia Depth-First Search 7
Depth-First Search
q Depth-first search (DFS)
is a general technique
for traversing a graph
q A DFS traversal of a
graph G
n Visits all the vertices and
edges of G
n Determines whether G is
connected
n Computes the connected
components of G
n Computes a spanning
forest of G
q DFS on a graph with n
vertices and m edges
takes O(n + m ) time
q DFS can be further
extended to solve other
graph problems
n Find and report a path
between two given
vertices
n Find a cycle in the graph
q Depth-first search is to
graphs what Euler tour
is to binary trees
© 2015 Goodrich and Tamassia Depth-First Search 8
DFS Algorithm from a Vertex
© 2015 Goodrich and Tamassia Depth-First Search 9
Example
D
B
A
C
E
D
B
A
C
E
D
B
A
C
E
discovery edge
back edge
A visited vertex
A unexplored vertex
unexplored edge
© 2015 Goodrich and Tamassia Depth-First Search 10
Example (cont.)
D
B
A
C
E
D
B
A
C
E
D
B
A
C
E
D
B
A
C
E
© 2015 Goodrich and Tamassia Depth-First Search 11
DFS and Maze Traversal
q The DFS algorithm is
similar to a classic
strategy for exploring
a maze
n We mark each
intersection, corner
and dead end (vertex)
visited
n We mark each corridor
(edge ) traversed
n We keep track of the
path back to the
entrance (start vertex)
by means of a rope
(recursion stack)
© 2015 Goodrich and Tamassia Depth-First Search 12
Properties of DFS
Property 1
DFS(G, v) visits all the
vertices and edges in
the connected
component of v
Property 2
The discovery edges
labeled by DFS(G, v)
form a spanning tree of
the connected
component of v
D
B
A
C
E
© 2015 Goodrich and Tamassia
The General DFS Algorithm
q Perform a DFS from each unexplored
vertex:
Depth-First Search 13
© 2015 Goodrich and Tamassia Depth-First Search 14
Analysis of DFS
q Setting/getting a vertex/edge label takes O(1) time
q Each vertex is labeled twice
n once as UNEXPLORED
n once as VISITED
q Each edge is labeled twice
n once as UNEXPLORED
n once as DISCOVERY or BACK
q Method incidentEdges is called once for each vertex
q DFS runs in O(n + m) time provided the graph is
represented by the adjacency list structure
n Recall that Σv deg(v) = 2m
© 2015 Goodrich and Tamassia Depth-First Search 15
Path Finding (not in book)
q We can specialize the DFS
algorithm to find a path
between two given
vertices u and z using the
template method pattern
q We call DFS(G, u) with u
as the start vertex
q We use a stack S to keep
track of the path between
the start vertex and the
current vertex
q As soon as destination
vertex z is encountered,
we return the path as the
contents of the stack
Algorithm pathDFS(G, v, z)
setLabel(v, VISITED)
S.push(v)
if v = z
return S.elements()
for all e ∈ G.incidentEdges(v)
if getLabel(e) = UNEXPLORED
w ← opposite(v,e)
if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
S.push(e)
pathDFS(G, w, z)
S.pop(e)
else
setLabel(e, BACK)
S.pop(v)
© 2015 Goodrich and Tamassia Depth-First Search 16
Cycle Finding (not in book)
q We can specialize the
DFS algorithm to find a
simple cycle using the
template method pattern
q We use a stack S to
keep track of the path
between the start vertex
and the current vertex
q As soon as a back edge
(v, w) is encountered,
we return the cycle as
the portion of the stack
from the top to vertex w
Algorithm cycleDFS(G, v, z)
setLabel(v, VISITED)
S.push(v)
for all e ∈ G.incidentEdges(v)
if getLabel(e) = UNEXPLORED
w ← opposite(v,e)
S.push(e)
if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
pathDFS(G, w, z)
S.pop(e)
else
T ← new empty stack
repeat
o ← S.pop()
T.push(o)
until o = w
return T.elements()
S.pop(v)

More Related Content

Similar to DFS.pdf

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
Amrinder Arora
 
breadth first search
breadth first searchbreadth first search
breadth first search
DeepikaT13
 
Strong (Weak) Triple Connected Domination Number of a Fuzzy Graph
Strong (Weak) Triple Connected Domination Number of a Fuzzy GraphStrong (Weak) Triple Connected Domination Number of a Fuzzy Graph
Strong (Weak) Triple Connected Domination Number of a Fuzzy Graph
ijceronline
 
Chapter 1
Chapter   1Chapter   1
Chapter 1
MeeraMeghpara
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
Traian Rebedea
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
Jyoshna Cec Cse Staf bejjam
 
THE RESULT FOR THE GRUNDY NUMBER ON P4- CLASSES
THE RESULT FOR THE GRUNDY NUMBER ON P4- CLASSESTHE RESULT FOR THE GRUNDY NUMBER ON P4- CLASSES
THE RESULT FOR THE GRUNDY NUMBER ON P4- CLASSES
graphhoc
 
Hamilton Path & Dijkstra's Algorithm
Hamilton Path & Dijkstra's AlgorithmHamilton Path & Dijkstra's Algorithm
Hamilton Path & Dijkstra's Algorithm
Mahesh Singh Madai
 
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
sumitbardhan
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
asimshahzad8611
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Mohanlal Sukhadia University (MLSU)
 
Link Analysis in Networks - or - Finding the Terrorists
Link Analysis in Networks - or - Finding the TerroristsLink Analysis in Networks - or - Finding the Terrorists
Link Analysis in Networks - or - Finding the Terrorists
James McGivern
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
Dabbal Singh Mahara
 
NON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptxNON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptx
Rajitha Reddy Alugati
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
Tribhuvan University
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
showslidedump
 
Clique Relaxation Models in Networks: Theory, Algorithms, and Applications
Clique Relaxation Models in Networks: Theory, Algorithms, and ApplicationsClique Relaxation Models in Networks: Theory, Algorithms, and Applications
Clique Relaxation Models in Networks: Theory, Algorithms, and Applications
SSA KPI
 
Graph theory
Graph theoryGraph theory
Graph theory
Gaurav Yadav
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
DrkhanchanaR
 
Computer Science Assignment Help
Computer Science Assignment Help Computer Science Assignment Help
Computer Science Assignment Help
Programming Homework Help
 

Similar to DFS.pdf (20)

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
 
breadth first search
breadth first searchbreadth first search
breadth first search
 
Strong (Weak) Triple Connected Domination Number of a Fuzzy Graph
Strong (Weak) Triple Connected Domination Number of a Fuzzy GraphStrong (Weak) Triple Connected Domination Number of a Fuzzy Graph
Strong (Weak) Triple Connected Domination Number of a Fuzzy Graph
 
Chapter 1
Chapter   1Chapter   1
Chapter 1
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
 
THE RESULT FOR THE GRUNDY NUMBER ON P4- CLASSES
THE RESULT FOR THE GRUNDY NUMBER ON P4- CLASSESTHE RESULT FOR THE GRUNDY NUMBER ON P4- CLASSES
THE RESULT FOR THE GRUNDY NUMBER ON P4- CLASSES
 
Hamilton Path & Dijkstra's Algorithm
Hamilton Path & Dijkstra's AlgorithmHamilton Path & Dijkstra's Algorithm
Hamilton Path & Dijkstra's Algorithm
 
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
 
Link Analysis in Networks - or - Finding the Terrorists
Link Analysis in Networks - or - Finding the TerroristsLink Analysis in Networks - or - Finding the Terrorists
Link Analysis in Networks - or - Finding the Terrorists
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
NON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptxNON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptx
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
 
Clique Relaxation Models in Networks: Theory, Algorithms, and Applications
Clique Relaxation Models in Networks: Theory, Algorithms, and ApplicationsClique Relaxation Models in Networks: Theory, Algorithms, and Applications
Clique Relaxation Models in Networks: Theory, Algorithms, and Applications
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
 
Computer Science Assignment Help
Computer Science Assignment Help Computer Science Assignment Help
Computer Science Assignment Help
 

More from jayarao21

databasemanagementsystempptforbeginners.ppt
databasemanagementsystempptforbeginners.pptdatabasemanagementsystempptforbeginners.ppt
databasemanagementsystempptforbeginners.ppt
jayarao21
 
authentication.ppt
authentication.pptauthentication.ppt
authentication.ppt
jayarao21
 
6.Lab Syllabus.docx
6.Lab Syllabus.docx6.Lab Syllabus.docx
6.Lab Syllabus.docx
jayarao21
 
unit5_part2.pptx
unit5_part2.pptxunit5_part2.pptx
unit5_part2.pptx
jayarao21
 
CCCDjango2010.pdf
CCCDjango2010.pdfCCCDjango2010.pdf
CCCDjango2010.pdf
jayarao21
 
ch2_jayarao.ppt
ch2_jayarao.pptch2_jayarao.ppt
ch2_jayarao.ppt
jayarao21
 
digital_sign_interview.ppt
digital_sign_interview.pptdigital_sign_interview.ppt
digital_sign_interview.ppt
jayarao21
 
introdution-to-html_jayarao27_11_22.pptx
introdution-to-html_jayarao27_11_22.pptxintrodution-to-html_jayarao27_11_22.pptx
introdution-to-html_jayarao27_11_22.pptx
jayarao21
 
MFCS PPT.pdf
MFCS PPT.pdfMFCS PPT.pdf
MFCS PPT.pdf
jayarao21
 

More from jayarao21 (9)

databasemanagementsystempptforbeginners.ppt
databasemanagementsystempptforbeginners.pptdatabasemanagementsystempptforbeginners.ppt
databasemanagementsystempptforbeginners.ppt
 
authentication.ppt
authentication.pptauthentication.ppt
authentication.ppt
 
6.Lab Syllabus.docx
6.Lab Syllabus.docx6.Lab Syllabus.docx
6.Lab Syllabus.docx
 
unit5_part2.pptx
unit5_part2.pptxunit5_part2.pptx
unit5_part2.pptx
 
CCCDjango2010.pdf
CCCDjango2010.pdfCCCDjango2010.pdf
CCCDjango2010.pdf
 
ch2_jayarao.ppt
ch2_jayarao.pptch2_jayarao.ppt
ch2_jayarao.ppt
 
digital_sign_interview.ppt
digital_sign_interview.pptdigital_sign_interview.ppt
digital_sign_interview.ppt
 
introdution-to-html_jayarao27_11_22.pptx
introdution-to-html_jayarao27_11_22.pptxintrodution-to-html_jayarao27_11_22.pptx
introdution-to-html_jayarao27_11_22.pptx
 
MFCS PPT.pdf
MFCS PPT.pdfMFCS PPT.pdf
MFCS PPT.pdf
 

Recently uploaded

morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
ycwu0509
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
harshapolam10
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
PKavitha10
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
Paris Salesforce Developer Group
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
ijaia
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 

Recently uploaded (20)

morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 

DFS.pdf

  • 1. © 2015 Goodrich and Tamassia Depth-First Search 1 Depth-First Search D B A C E Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015
  • 2. © 2015 Goodrich and Tamassia Depth-First Search 2 Subgraphs q A subgraph S of a graph G is a graph such that n The vertices of S are a subset of the vertices of G n The edges of S are a subset of the edges of G q A spanning subgraph of G is a subgraph that contains all the vertices of G Subgraph Spanning subgraph
  • 3. © 2015 Goodrich and Tamassia Application: Web Crawlers q A fundamental kind of algorithmic operation that we might wish to perform on a graph is traversing the edges and the vertices of that graph. q A traversal is a systematic procedure for exploring a graph by examining all of its vertices and edges. q For example, a web crawler, which is the data collecting part of a search engine, must explore a graph of hypertext documents by examining its vertices, which are the documents, and its edges, which are the hyperlinks between documents. q A traversal is efficient if it visits all the vertices and edges in linear time. Depth-First Search 3
  • 4. © 2015 Goodrich and Tamassia Depth-First Search 4 Connectivity q A graph is connected if there is a path between every pair of vertices q A connected component of a graph G is a maximal connected subgraph of G Connected graph Non connected graph with two connected components
  • 5. © 2015 Goodrich and Tamassia Depth-First Search 5 Trees and Forests q A (free) tree is an undirected graph T such that n T is connected n T has no cycles This definition of tree is different from the one of a rooted tree q A forest is an undirected graph without cycles q The connected components of a forest are trees Tree Forest
  • 6. © 2015 Goodrich and Tamassia Depth-First Search 6 Spanning Trees and Forests q A spanning tree of a connected graph is a spanning subgraph that is a tree q A spanning tree is not unique unless the graph is a tree q Spanning trees have applications to the design of communication networks q A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree
  • 7. © 2015 Goodrich and Tamassia Depth-First Search 7 Depth-First Search q Depth-first search (DFS) is a general technique for traversing a graph q A DFS traversal of a graph G n Visits all the vertices and edges of G n Determines whether G is connected n Computes the connected components of G n Computes a spanning forest of G q DFS on a graph with n vertices and m edges takes O(n + m ) time q DFS can be further extended to solve other graph problems n Find and report a path between two given vertices n Find a cycle in the graph q Depth-first search is to graphs what Euler tour is to binary trees
  • 8. © 2015 Goodrich and Tamassia Depth-First Search 8 DFS Algorithm from a Vertex
  • 9. © 2015 Goodrich and Tamassia Depth-First Search 9 Example D B A C E D B A C E D B A C E discovery edge back edge A visited vertex A unexplored vertex unexplored edge
  • 10. © 2015 Goodrich and Tamassia Depth-First Search 10 Example (cont.) D B A C E D B A C E D B A C E D B A C E
  • 11. © 2015 Goodrich and Tamassia Depth-First Search 11 DFS and Maze Traversal q The DFS algorithm is similar to a classic strategy for exploring a maze n We mark each intersection, corner and dead end (vertex) visited n We mark each corridor (edge ) traversed n We keep track of the path back to the entrance (start vertex) by means of a rope (recursion stack)
  • 12. © 2015 Goodrich and Tamassia Depth-First Search 12 Properties of DFS Property 1 DFS(G, v) visits all the vertices and edges in the connected component of v Property 2 The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v D B A C E
  • 13. © 2015 Goodrich and Tamassia The General DFS Algorithm q Perform a DFS from each unexplored vertex: Depth-First Search 13
  • 14. © 2015 Goodrich and Tamassia Depth-First Search 14 Analysis of DFS q Setting/getting a vertex/edge label takes O(1) time q Each vertex is labeled twice n once as UNEXPLORED n once as VISITED q Each edge is labeled twice n once as UNEXPLORED n once as DISCOVERY or BACK q Method incidentEdges is called once for each vertex q DFS runs in O(n + m) time provided the graph is represented by the adjacency list structure n Recall that Σv deg(v) = 2m
  • 15. © 2015 Goodrich and Tamassia Depth-First Search 15 Path Finding (not in book) q We can specialize the DFS algorithm to find a path between two given vertices u and z using the template method pattern q We call DFS(G, u) with u as the start vertex q We use a stack S to keep track of the path between the start vertex and the current vertex q As soon as destination vertex z is encountered, we return the path as the contents of the stack Algorithm pathDFS(G, v, z) setLabel(v, VISITED) S.push(v) if v = z return S.elements() for all e ∈ G.incidentEdges(v) if getLabel(e) = UNEXPLORED w ← opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) S.push(e) pathDFS(G, w, z) S.pop(e) else setLabel(e, BACK) S.pop(v)
  • 16. © 2015 Goodrich and Tamassia Depth-First Search 16 Cycle Finding (not in book) q We can specialize the DFS algorithm to find a simple cycle using the template method pattern q We use a stack S to keep track of the path between the start vertex and the current vertex q As soon as a back edge (v, w) is encountered, we return the cycle as the portion of the stack from the top to vertex w Algorithm cycleDFS(G, v, z) setLabel(v, VISITED) S.push(v) for all e ∈ G.incidentEdges(v) if getLabel(e) = UNEXPLORED w ← opposite(v,e) S.push(e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) pathDFS(G, w, z) S.pop(e) else T ← new empty stack repeat o ← S.pop() T.push(o) until o = w return T.elements() S.pop(v)