Depth-First Search 1
Depth-First Search 1
Depth-First Search 1
B D E
Depth-First Search 1
Outline and Reading
Definitions (§6.1)
Subgraph
Connectivity
Spanning trees and forests
Depth-first search (§6.3.1)
Algorithm
Example
Properties
Analysis
Applications of DFS (§6.5)
Path finding
Cycle finding
Depth-First Search 2
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) DFS on a graph with n
is a general technique vertices and m edges
for traversing a graph takes O(n + m ) time
A DFS traversal of a DFS can be further
graph G extended to solve other
Visits all the vertices and graph problems
edges of G Find and report a path
Determines whether G is between two given
connected vertices
Computes the connected Find a cycle in the graph
components of G Depth-first search is to
Computes a spanning graphs what Euler tour
forest of G
is to binary trees
Depth-First Search 7
DFS Algorithm
The algorithm uses a mechanism
for setting and getting “labels” of Algorithm DFS(G, v)
vertices and edges Input graph G and a start vertex v of G
Algorithm DFS(G) Output labeling of the edges of G
Input graph G in the connected component of v
Output labeling of the edges of G as discovery edges and back edges
as discovery edges and setLabel(v, VISITED)
back edges for all e G.incidentEdges(v)
for all u G.vertices() if getLabel(e) = UNEXPLORED
setLabel(u, UNEXPLORED) w G.opposite(v,e)
for all e G.edges() if getLabel(w) = UNEXPLORED
setLabel(e, UNEXPLORED) setLabel(e, DISCOVERY)
for all v G.vertices() DFS(G, w)
if getLabel(v) = UNEXPLORED else
DFS(G, v) setLabel(e, BACK)
Depth-First Search 8
Example
A
A unexplored vertex
A visited vertex
B D E
unexplored edge
discovery edge C
back edge
A A
B D E B D E
C C
Depth-First Search 9
Example (cont.)
A A
B D E B D E
C C
A A
B D E B D E
C C
Depth-First Search 10
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 Sv deg(v) = 2m
Depth-First Search 13
Path Finding
We can specialize the DFS
algorithm to find a path Algorithm pathDFS(G, v, z)
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) = UNEXPLORED
the start vertex and the setLabel(e, DISCOVERY)
current vertex S.push(e)
As soon as destination pathDFS(G, w, z)
vertex z is encountered, S.pop() { e gets popped }
we return the path as the else
contents of the stack setLabel(e, BACK)
S.pop() { v gets popped }
Depth-First 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 keep S.push(e)
track of the path if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
between the start vertex pathDFS(G, w, z)
and the current vertex S.pop()
As soon as a back edge else
C new empty stack
(v, w) is encountered, repeat
we return the cycle as o S.pop()
the portion of the stack C.push(o)
until o = w
from the top to vertex w return C.elements()
S.pop()
Depth-First Search 15