Presentation for use with the textbook, Algorithm Design and
Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015
Depth-First Search
A
B D E
© 2015 Goodrich and Tamassia Depth-First Search 1
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 Subgraph
subset of the edges of G
q A spanning subgraph of G
is a subgraph that
contains all the vertices
of G
Spanning subgraph
© 2015 Goodrich and Tamassia Depth-First Search 2
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.
© 2015 Goodrich and Tamassia Depth-First Search 3
Connectivity
q A graph is
connected if there is
a path between
every pair of Connected graph
vertices
q A connected
component of a
graph G is a
maximal connected
subgraph of G Non connected graph with two
connected components
© 2015 Goodrich and Tamassia Depth-First Search 4
Trees and Forests
q A (free) tree is an
undirected graph T such
that
n T is connected
n T has no cycles
Tree
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 Forest
© 2015 Goodrich and Tamassia Depth-First Search 5
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 Graph
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
Spanning tree
© 2015 Goodrich and Tamassia Depth-First Search 6
Depth-First Search
q Depth-first search (DFS) q DFS on a graph with n
is a general technique vertices and m edges
for traversing a graph takes O(n + m ) time
q A DFS traversal of a q DFS can be further
graph G extended to solve other
n Visits all the vertices and graph problems
edges of G n Find and report a path
n Determines whether G is between two given
connected vertices
n Computes the connected n Find a cycle in the graph
components of G q Depth-first search is to
n Computes a spanning graphs what Euler tour
forest of G
is to binary trees
© 2015 Goodrich and Tamassia Depth-First Search 7
DFS Algorithm from a Vertex
© 2015 Goodrich and Tamassia Depth-First Search 8
Example
A
A unexplored vertex
A visited vertex
B D E
unexplored edge
discovery edge C
back edge
A A
B D E B D E
C C
© 2015 Goodrich and Tamassia Depth-First Search 9
Example (cont.)
A A
B D E B D E
C C
A A
B D E B D E
C C
© 2015 Goodrich and Tamassia Depth-First Search 10
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 11
Properties of DFS
Property 1
DFS(G, v) visits all the
vertices and edges in
the connected A
component of v
Property 2 B D E
The discovery edges
labeled by DFS(G, v)
form a spanning tree of C
the connected
component of v
© 2015 Goodrich and Tamassia Depth-First Search 12
The General DFS Algorithm
q Perform a DFS from each unexplored
vertex:
© 2015 Goodrich and Tamassia Depth-First Search 13
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 14
Path Finding (not in book)
q We can specialize the DFS
algorithm to find a path Algorithm pathDFS(G, v, z)
between two given setLabel(v, VISITED)
vertices u and z using the S.push(v)
template method pattern if v = z
return S.elements()
q We call DFS(G, u) with u for all e ∈ G.incidentEdges(v)
as the start vertex if getLabel(e) = UNEXPLORED
q We use a stack S to keep w ← opposite(v,e)
track of the path between if getLabel(w) = UNEXPLORED
the start vertex and the setLabel(e, DISCOVERY)
current vertex S.push(e)
q As soon as destination pathDFS(G, w, z)
vertex z is encountered, S.pop(e)
we return the path as the else
contents of the stack setLabel(e, BACK)
S.pop(v)
© 2015 Goodrich and Tamassia Depth-First Search 15
Cycle Finding (not in book)
Algorithm cycleDFS(G, v, z)
q We can specialize the setLabel(v, VISITED)
DFS algorithm to find a S.push(v)
simple cycle using the for all e ∈ G.incidentEdges(v)
if getLabel(e) = UNEXPLORED
template method pattern
w ← opposite(v,e)
q We use a stack S to S.push(e)
keep track of the path if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
between the start vertex
pathDFS(G, w, z)
and the current vertex S.pop(e)
q As soon as a back edge else
T ← new empty stack
(v, w) is encountered, repeat
we return the cycle as o ← S.pop()
the portion of the stack T.push(o)
until o = w
from the top to vertex w return T.elements()
S.pop(v)
© 2015 Goodrich and Tamassia Depth-First Search 16