0% found this document useful (0 votes)
182 views

Subgraphs Depth-First Search

The document discusses depth-first search (DFS), a graph traversal algorithm. It defines key graph terms like subgraphs, trees, and forests. It provides pseudocode for the DFS algorithm and explains how DFS can be used to solve problems like determining graph connectivity and finding spanning trees. An example of applying DFS to a graph is also given.

Uploaded by

nautilus_261
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views

Subgraphs Depth-First Search

The document discusses depth-first search (DFS), a graph traversal algorithm. It defines key graph terms like subgraphs, trees, and forests. It provides pseudocode for the DFS algorithm and explains how DFS can be used to solve problems like determining graph connectivity and finding spanning trees. An example of applying DFS to a graph is also given.

Uploaded by

nautilus_261
Copyright
© Attribution Non-Commercial (BY-NC)
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

© 2004 Goodrich, Tamassia Depth-First Search 1 © 2004 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

© 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

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
Input graph G in the connected component of v
as discovery edges and back edges discovery edge C
Output labeling of the edges of G
as discovery edges and setLabel(v, VISITED) back edge
back edges for all e ∈ G.incidentEdges(v)
for all u ∈ G.vertices() if getLabel(e) = UNEXPLORED
setLabel(u, UNEXPLORED) w ← opposite(v,e) A A
for all e ∈ G.edges() if getLabel(w) = UNEXPLORED
setLabel(e, UNEXPLORED) setLabel(e, DISCOVERY)
B D E B D E
for all v ∈ G.vertices() DFS(G, w)
if getLabel(v) = UNEXPLORED else
DFS(G, v) setLabel(e, BACK) C C

© 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

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

© 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

You might also like