Depth-First Search Subgraphs
Depth-First Search Subgraphs
A subgraph S of a graph
G is a graph such that
Depth-First Search The vertices of S are a
subset of the vertices of G
The edges of S are a Subgraph
A
subset of the edges of G
A spanning subgraph of G
B D E
is a subgraph that
contains all the vertices
C
of G
Spanning subgraph
© 2010 Goodrich, Tamassia Depth-First Search 1 © 2010 Goodrich, Tamassia Depth-First Search 2
© 2010 Goodrich, Tamassia Depth-First Search 3 © 2010 Goodrich, Tamassia Depth-First Search 4
Spanning Trees and Forests Depth-First Search
A spanning tree of a Depth-first search (DFS) DFS on a graph with n
connected graph is a is a general technique vertices and m edges
spanning subgraph that is for traversing a graph takes O(n + m ) time
a tree
A DFS traversal of a DFS can be further
A spanning tree is not
graph G extended to solve other
unique unless the graph is
a tree Graph Visits all the vertices and graph problems
Spanning trees have edges of G Find and report a path
applications to the design Determines whether G is between two given
of communication connected vertices
networks Computes the connected Find a cycle in the graph
A spanning forest of a components of G Depth-first search is to
graph is a spanning Computes a spanning
subgraph that is a forest
graphs what Euler tour
forest of G
is to binary trees
Spanning tree
© 2010 Goodrich, Tamassia Depth-First Search 5 © 2010 Goodrich, Tamassia Depth-First Search 6
© 2010 Goodrich, Tamassia Depth-First Search 7 © 2010 Goodrich, Tamassia Depth-First Search 8
Example (cont.) DFS and Maze Traversal
A A The DFS algorithm is
similar to a classic
B D E B D E strategy for exploring
a maze
C C We mark each
intersection, corner
and dead end (vertex)
visited
We mark each corridor
A A
(edge ) traversed
We keep track of the
B D E B D E path back to the
entrance (start vertex)
by means of a rope
C C (recursion stack)
© 2010 Goodrich, Tamassia Depth-First Search 9 © 2010 Goodrich, Tamassia Depth-First Search 10
© 2010 Goodrich, Tamassia Depth-First Search 11 © 2010 Goodrich, Tamassia Depth-First Search 12
Path Finding Cycle Finding
We can specialize the DFS We can specialize the
Algorithm cycleDFS(G, v, z)
algorithm to find a path Algorithm pathDFS(G, v, z) v.setLabel(VISITED)
DFS algorithm to find a S.push(v)
between two given v.setLabel(VISITED)
for all e ∈ v.incidentEdges()
vertices u and z using the S.push(v) simple cycle using the
if e.getLabel() = UNEXPLORED
template method pattern if v = z template method pattern w ← e.opposite(v)
return S.elements() We use a stack S to
We call DFS(G, u) with u for all e ∈ v.incidentEdges()
S.push(e)
as the start vertex keep track of the path if w.getLabel() = UNEXPLORED
if e.getLabel() = UNEXPLORED
We use a stack S to keep w ← e.opposite(v) between the start vertex e.setLabel(DISCOVERY)
pathDFS(G, w, z)
track of the path between if w.getLabel() = UNEXPLORED and the current vertex S.pop(e)
the start vertex and the e.setLabel(DISCOVERY) As soon as a back edge else
current vertex T ← new empty stack
S.push(e) (v, w) is encountered,
As soon as destination pathDFS(G, w, z) repeat
we return the cycle as o ← S.pop()
vertex z is encountered, S.pop(e)
we return the path as the else the portion of the stack T.push(o)
until o = w
contents of the stack e.setLabel(BACK) from the top to vertex w return T.elements()
S.pop(v) S.pop(v)
© 2010 Goodrich, Tamassia Depth-First Search 13 © 2010 Goodrich, Tamassia Depth-First Search 14