Subgraphs Depth-First Search
Subgraphs Depth-First Search
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
© 2004 Goodrich, Tamassia Depth-First Search 1 © 2004 Goodrich, Tamassia Depth-First Search 2
© 2004 Goodrich, Tamassia Depth-First Search 3 © 2004 Goodrich, Tamassia Depth-First Search 4
Spanning Trees and Forests Depth-First Search (§ 12.3.1)
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
unique unless the graph is graph G extended to solve other
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
© 2004 Goodrich, Tamassia Depth-First Search 5 © 2004 Goodrich, Tamassia Depth-First Search 6
© 2004 Goodrich, Tamassia Depth-First Search 7 © 2004 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)
© 2004 Goodrich, Tamassia Depth-First Search 9 © 2004 Goodrich, Tamassia Depth-First Search 10
© 2004 Goodrich, Tamassia Depth-First Search 11 © 2004 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) setLabel(v, VISITED)
setLabel(v, VISITED) DFS algorithm to find a S.push(v)
between two given
vertices u and z using the S.push(v) simple cycle using the for all e ∈ G.incidentEdges(v)
if v = z template method pattern if getLabel(e) = UNEXPLORED
template method pattern w ← opposite(v,e)
return S.elements() We use a stack S to
We call DFS(G, u) with u S.push(e)
for all e ∈ G.incidentEdges(v)
as the start vertex keep track of the path if getLabel(w) = UNEXPLORED
if getLabel(e) = UNEXPLORED setLabel(e, DISCOVERY)
We use a stack S to keep w ← opposite(v,e)
between the start vertex
pathDFS(G, w, z)
track of the path between if getLabel(w) = UNEXPLORED and the current vertex S.pop(e)
the start vertex and the setLabel(e, DISCOVERY) As soon as a back edge else
current vertex S.push(e) T ← new empty stack
(v, w) is encountered, repeat
As soon as destination pathDFS(G, w, z)
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 setLabel(e, BACK) from the top to vertex w return T.elements()
S.pop(v) S.pop(v)
© 2004 Goodrich, Tamassia Depth-First Search 13 © 2004 Goodrich, Tamassia Depth-First Search 14