0% found this document useful (0 votes)
13 views4 pages

Depth-First Search Subgraphs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Depth-First Search Subgraphs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

Connectivity Trees and Forests


 A (free) tree is an
 A graph is undirected graph T such
connected if there is that
a path between  T is connected
every pair of Connected graph  T has no cycles
Tree
This definition of tree is
vertices different from the one of
 A connected a rooted tree
component of a  A forest is an undirected
graph G is a graph without cycles
maximal connected  The connected
subgraph of G Non connected graph with two components of a forest
connected components are trees Forest

© 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

DFS Algorithm Example


The algorithm uses a mechanism A

A unexplored vertex
for setting and getting “labels” of Algorithm DFS(G, v)
vertices and edges Input graph G and a start vertex v of G
A visited vertex
B D E
Algorithm DFS(G) Output labeling of the edges of G unexplored edge
in the connected component of v
Input graph G
as discovery edges and back edges discovery edge C
Output labeling of the edges of G
as discovery edges and v.setLabel(VISITED) back edge
back edges for all e ∈ G.incidentEdges(v)
for all u ∈ G.vertices() if e.getLabel() = UNEXPLORED
u.setLabel(UNEXPLORED) w ← e.opposite(v) A A
for all e ∈ G.edges() if w.getLabel() = UNEXPLORED
e.setLabel(UNEXPLORED) e.setLabel(DISCOVERY)
B D E B D E
for all v ∈ G.vertices() DFS(G, w)
if v.getLabel() = UNEXPLORED else
DFS(G, v) e.setLabel(BACK) C C

© 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

Properties of DFS Analysis of DFS


Property 1  Setting/getting a vertex/edge label takes O(1) time
DFS(G, v) visits all the  Each vertex is labeled twice
vertices and edges in  once as UNEXPLORED
the connected A  once as VISITED
component of v  Each edge is labeled twice
once as UNEXPLORED
Property 2 B D E

 once as DISCOVERY or BACK


The discovery edges
 Method incidentEdges is called once for each vertex
labeled by DFS(G, v)
form a spanning tree of C  DFS runs in O(n + m) time provided the graph is
the connected represented by the adjacency list structure
component of v  Recall that Σv deg(v) = 2m

© 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

You might also like