Outline and Reading
Outline and Reading
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
1
Depth-First Search 5/6/2002 11:46 AM
A A A A
B D E B D E B D E B D E
C C C C
2
Depth-First Search 5/6/2002 11:46 AM
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