COP 3503 – Computer Science II
–
CLASS NOTES

DAY #23
Graph Traversals
As with trees, traversing a graph consists of visiting each vertex only one time.
The simple traversal algorithms used for trees (preorder, inorder, postorder) cannot
be applied here because graphs may include cycles which would cause the tree
traversal algorithms to enter an infinite loop.
To prevent this from happening, each
visited vertex is typically marked in some fashion to avoid revisiting it (a common
technique is to renumber the vertices as they are visited).
However, graphs can
have isolated vertices (unconnected vertices), which means that some parts of the
graph are left unvisited if unmodified tree traversal algorithms are applied.
DepthFirst Traversal
The depthfirst search algorithm for graphs was developed by Hopcroft and Tarjan.
In this algorithm, each vertex
V
is visited and then each unvisited vertex adjacent
to
V
is visited.
If a vertex
V
has no adjacent vertices or all of its adjacent vertices
have been visited, the traversal backtracks to the predecessor of
V
.
The traversal is
complete when this process of visiting and backtracking leads to the first vertex
where the traversal started.
If there are still unvisited vertices in the graph, the
traversal continues by restarting on one of the unvisited vertices.
This algorithm
renumbers each vertex as it is visited.
Algorithm:
DFS(v)
num
(v) = i++;
for
all vertices
u
adjacent to
v
if
num
(u)
is
0
attach edge
(uv)
to
edges;
DFS(u);
depthFirstSearch( )
for
all vertices
v
num
(v) = 0;
edges =
null;
i =
1;
while
there is a vertex
v
such that num
(v)
is
0
DFS(v);
output
edges;
Example:
Day 23 
1
Graphs Continued
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThe numbers assigned to each vertex are shown in parentheses in the figure
below.
Once the initializations have been made, depthFirstSearch( ) calles
DFS(a).
DFS( ) is invoked for vertex
a
;
num(a)
is assigned number 1.
Vertex
a
has four adjacent vertices, and vertex
e
is chosen for the next
invocation, DFS(e), which assigns number 2 to this vertex (num(e) = 2) and
puts the
edge(ae)
in the set
edges
.
e
has two unvisited adjacent
vertices, and DFS( ) is called for the first of them, the vertex
f
The call
DFS(f) will lead to the assignment num(f) = 3 and will put
edge(ef)
in
edges
.
Vertex
f
has only one unvisited adjacent vertex,
i
, thus the fourth call DFS(i)
will lead to the assignment num(i) = 4 and to the attaching of
edge(fi)
to
edges
Vertex
i
has only visited adjacent vertices, thus a return to the call
DFS(f) occurs and then to DFS(e) in which vertex
i
is accessed only to
discover that
num(i)
is not 0, thus
edge(ei)
is not included in the set of edges.
The rest of the execution is shown in the figure below where the solid lines
indicate edges that are included in the set
edges
Part (a) is the original
graph and part (b) illustrates the algorithm’s technique.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Summer '09
 Computer Science, Algorithms, Graph Theory, Vertex, currDist

Click to edit the document details