Subgraphs
A subgraph S of a graph
G is a graph such that
 The vertices of S are a
subset of the vertices of G
 The edges of S are a Subgraph
subset of the edges of G
A spanning subgraph of
G is a subgraph that
contains all the vertices of
G
Spanning subgraph
Depth-First Search 3

Connectivity
A graph is
connected if there is
a path between
every pair of Connected graph
vertices
A connected
component of a
graph G is a
maximal connected
subgraph of G Non connected graph with two
connected components
Depth-First Search 4

Trees and Forests
A (free) tree is an
undirected graph T such
that
 T is connected
 T has no cycles
Tree
This definition of tree is
different from the one of a
rooted tree
A forest is an undirected
graph without cycles
The connected
components of a forest
are trees Forest
Depth-First Search 5

Spanning Trees and Forests
A spanning tree of a
connected graph is a
spanning subgraph that is
a tree
A spanning tree is not
unique unless the graph is
a tree Graph
Spanning trees have
applications to the design
of communication
networks
A spanning forest of a
graph is a spanning
subgraph that is a forest
Spanning tree
Depth-First Search 6

Depth­First Search
Depth­first search DFS on a graph with n
(DFS) is a general vertices and m edges
technique for traversing takes O(n + m ) time
a graph DFS can be further
A DFS traversal of a extended to solve other
graph G graph problems
 Visits all the vertices and  Find and report a path
edges of G between two given
 Determines whether G is vertices
connected  Find a cycle in the graph
 Computes the connected Depth­first search is to
components of G graphs what Euler tour
 Computes a spanning is to binary trees
forest of G
Depth-First Search 7

DFS and Maze Traversal
The DFS algorithm is
similar to a classic
strategy for exploring
a maze
 We mark each
intersection, corner
and dead end (vertex)
visited
 We mark each corridor
(edge ) traversed
 We keep track of the
path back to the
entrance (start vertex)
by means of a rope
(recursion stack)
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
Depth-First Search 12

Analysis of DFS
Setting/getting a vertex/edge label takes O(1) time
Each vertex is labeled twice
 once as UNEXPLORED
 once as VISITED
Each edge is labeled twice
 once as UNEXPLORED
 once as DISCOVERY or BACK
Method incidentEdges is called once for each vertex
DFS runs in O(n + m) time provided the graph is
represented by the adjacency list structure

Recall that Σ v deg(v) = 2m
Depth-First Search 13

Path Finding
We can specialize the
DFS algorithm to find a Algorithm pathDFS(G, v, z)
path between two given setLabel(v, VISITED)
vertices u and z using the S.push(v)
template method pattern if v = z
return S.elements()
We call DFS(G, u) with u for all e ∈ G.incidentEdges(v)
as the start vertex if getLabel(e) = UNEXPLORED
We use a stack S to keep w ← opposite(v,e)
track of the path between if getLabel(w)
the start vertex and the = UNEXPLORED
current vertex setLabel(e, DISCOVERY)
As soon as destination S.push(e)
vertex z is encountered, pathDFS(G, w, z)
we return the path as the S.pop(e)
contents of the stack else
setLabel(e, BACK)
Depth-First S.pop(v)
Search 14

Cycle Finding
Algorithm cycleDFS(G, v, z)
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)
We use a stack S to S.push(e)
keep track of the path if getLabel(w) = UNEXPLORED
between the start vertex setLabel(e, DISCOVERY)
pathDFS(G, w, z)
and the current vertex S.pop(e)
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)
Depth-First Search 15