You are on page 1of 3

Depth-First Search 5/6/2002 11:46 AM

Outline and Reading


Definitions (§6.1)

Depth-First Search „

„
Subgraph
Connectivity
„ Spanning trees and forests
A Depth-first search (§6.3.1)
„ Algorithm
B D E „ Example
„ Properties
C „ Analysis
Applications of DFS (§6.5)
„ Path finding
„ Cycle finding
Depth-First Search 1 Depth-First Search 2

Subgraphs Connectivity
A subgraph S of a graph
G is a graph such that A graph is
„ The vertices of S are a connected if there is
subset of the vertices of G a path between
„ The edges of S are a Subgraph every pair of Connected graph
subset of the edges of G vertices
A spanning subgraph of G A connected
is a subgraph that component of a
contains all the vertices graph G is a
of G
maximal connected
subgraph of G Non connected graph with two
Spanning subgraph connected components

Depth-First Search 3 Depth-First Search 4

Trees and Forests Spanning Trees and Forests


A (free) tree is an A spanning tree of a
undirected graph T such connected graph is a
that spanning subgraph that is
„ T is connected a tree
T has no cycles A spanning tree is not
„
Tree unique unless the graph is
This definition of tree is a tree Graph
different from the one of
a rooted tree Spanning trees have
applications to the design
A forest is an undirected of communication
graph without cycles networks
The connected A spanning forest of a
graph is a spanning
components of a forest subgraph that is a forest
are trees Forest
Spanning tree
Depth-First Search 5 Depth-First Search 6

1
Depth-First Search 5/6/2002 11:46 AM

Depth-First Search DFS Algorithm


The algorithm uses a mechanism
Depth-first search (DFS) DFS on a graph with n
for setting and getting “labels” of
is a general technique vertices and m edges vertices and edges
Algorithm DFS(G, v)
Input graph G and a start vertex v of G
for traversing a graph takes O(n + m ) time
Algorithm DFS(G) Output labeling of the edges of G
A DFS traversal of a DFS can be further Input graph G in the connected component of v
graph G extended to solve other Output labeling of the edges of G as discovery edges and back edges
„ Visits all the vertices and graph problems as discovery edges and setLabel(v, VISITED)
edges of G „ Find and report a path back edges for all e ∈ G.incidentEdges(v)
„ Determines whether G is between two given for all u ∈ G.vertices() if getLabel(e) = UNEXPLORED
connected vertices setLabel(u, UNEXPLORED) w ← opposite(v,e)
„ Computes the connected „ Find a cycle in the graph for all e ∈ G.edges() if getLabel(w) = UNEXPLORED
components of G setLabel(e, UNEXPLORED) setLabel(e, DISCOVERY)
Depth-first search is to for all v ∈ G.vertices() DFS(G, w)
„ Computes a spanning graphs what Euler tour
forest of G if getLabel(v) = UNEXPLORED else
is to binary trees DFS(G, v) setLabel(e, BACK)

Depth-First Search 7 Depth-First Search 8

Example Example (cont.)


A
A unexplored vertex A A
A visited vertex
B D E B D E B D E
unexplored edge
discovery edge C C C
back edge

A A A A

B D E B D E B D E B D E

C C C C

Depth-First Search 9 Depth-First Search 10

DFS and Maze Traversal Properties of DFS


The DFS algorithm is Property 1
similar to a classic
strategy for exploring DFS(G, v) visits all the
a maze vertices and edges in
the connected A
„ We mark each
intersection, corner component of v
and dead end (vertex) Property 2
visited B D E
„ We mark each corridor The discovery edges
(edge ) traversed labeled by DFS(G, v)
„ We keep track of the form a spanning tree of C
path back to the the connected
entrance (start vertex) component of v
by means of a rope
(recursion stack)
Depth-First Search 11 Depth-First Search 12

2
Depth-First Search 5/6/2002 11:46 AM

Analysis of DFS Path Finding


Setting/getting a vertex/edge label takes O(1) time We can specialize the DFS
Algorithm pathDFS(G, v, z)
algorithm to find a path
Each vertex is labeled twice between two given setLabel(v, VISITED)
„ once as UNEXPLORED vertices u and z using the S.push(v)
„ once as VISITED template method pattern if v = z
return S.elements()
Each edge is labeled twice We call DFS(G, u) with u
for all e ∈ G.incidentEdges(v)
as the start vertex
„ once as UNEXPLORED if getLabel(e) = UNEXPLORED
We use a stack S to keep w ← opposite(v,e)
„ once as DISCOVERY or BACK track of the path between if getLabel(w) = UNEXPLORED
Method incidentEdges is called once for each vertex the start vertex and the setLabel(e, DISCOVERY)
DFS runs in O(n + m) time provided the graph is current vertex S.push(e)
represented by the adjacency list structure As soon as destination pathDFS(G, w, z)
vertex z is encountered, S.pop(e)
„ Recall that Σv deg(v) = 2m we return the path as the else
contents of the stack setLabel(e, BACK)
S.pop(v)
Depth-First Search 13 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)
template method pattern if getLabel(e) = UNEXPLORED
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

You might also like